mirror of https://github.com/oxen-io/oxen-core.git
Check if SN has a ping from lokinet and the storage server before registration (#1457)
* Make preparing registration fail if lokinet or the storage server have not received a ping yet * Add prepare_registration boolean default * Add signature override for get_human_time_ago to accept an optional uint64. Add stricter comparisons to ping checks * Remove duplicate logic * Remove method override and assign/cast last_lokinet_ping/last_storage_server_ping * Use auto definition and static_cast res.last_lokinet_ping
This commit is contained in:
parent
3ddcf4879b
commit
e32cbcbccd
|
@ -305,10 +305,16 @@ bool command_parser_executor::print_sr(const std::vector<std::string>& args)
|
|||
return result;
|
||||
}
|
||||
|
||||
bool command_parser_executor::prepare_registration()
|
||||
bool command_parser_executor::prepare_registration(const std::vector<std::string>& args)
|
||||
{
|
||||
bool result = m_executor.prepare_registration();
|
||||
return result;
|
||||
bool force_registration = false;
|
||||
for (auto& arg : args)
|
||||
{
|
||||
if (arg == "+force")
|
||||
force_registration = true;
|
||||
}
|
||||
|
||||
return m_executor.prepare_registration(force_registration);
|
||||
}
|
||||
|
||||
bool command_parser_executor::print_sn(const std::vector<std::string>& args)
|
||||
|
|
|
@ -74,7 +74,7 @@ public:
|
|||
|
||||
bool print_sr(const std::vector<std::string>& args);
|
||||
|
||||
bool prepare_registration();
|
||||
bool prepare_registration(const std::vector<std::string>& args);
|
||||
|
||||
bool print_sn(const std::vector<std::string>& args);
|
||||
|
||||
|
|
|
@ -128,8 +128,8 @@ void command_server::init_commands(cryptonote::rpc::core_rpc_server* rpc_server)
|
|||
);
|
||||
m_command_lookup.set_handler(
|
||||
"prepare_registration"
|
||||
, [this](const auto &) { return m_parser.prepare_registration(); }
|
||||
, "prepare_registration"
|
||||
, [this](const auto &x) { return m_parser.prepare_registration(x); }
|
||||
, "prepare_registration [+force]"
|
||||
, "Interactive prompt to prepare a service node registration command. The resulting registration command can be run in the command-line wallet to send the registration to the blockchain."
|
||||
);
|
||||
m_command_lookup.set_handler(
|
||||
|
|
|
@ -186,6 +186,7 @@ namespace {
|
|||
}
|
||||
return s + " " + (ago < 0s ? "in the future" : "ago");
|
||||
}
|
||||
|
||||
std::string get_human_time_ago(std::time_t t, std::time_t now, bool abbreviate = false) {
|
||||
return get_human_time_ago(std::chrono::seconds{now - t}, abbreviate);
|
||||
}
|
||||
|
@ -1967,7 +1968,7 @@ static uint64_t get_actual_amount(uint64_t amount, uint64_t portions)
|
|||
return resultlo;
|
||||
}
|
||||
|
||||
bool rpc_command_executor::prepare_registration()
|
||||
bool rpc_command_executor::prepare_registration(bool force_registration)
|
||||
{
|
||||
// RAII-style class to temporarily clear categories and restore upon destruction (i.e. upon returning).
|
||||
struct clear_log_categories {
|
||||
|
@ -1991,6 +1992,20 @@ bool rpc_command_executor::prepare_registration()
|
|||
tools::fail_msg_writer() << "Unable to prepare registration: this daemon is not running in --service-node mode";
|
||||
return false;
|
||||
}
|
||||
else if (auto last_lokinet_ping = static_cast<std::time_t>(res.last_lokinet_ping.value_or(0));
|
||||
last_lokinet_ping < (time(nullptr) - 60) && !force_registration)
|
||||
{
|
||||
tools::fail_msg_writer() << "Unable to prepare registration: this daemon has not received a ping from lokinet "
|
||||
<< (res.last_lokinet_ping == 0 ? "yet" : "since " + get_human_time_ago(last_lokinet_ping, std::time(nullptr)));
|
||||
return false;
|
||||
}
|
||||
else if (auto last_storage_server_ping = static_cast<std::time_t>(res.last_storage_server_ping.value_or(0));
|
||||
last_storage_server_ping < (time(nullptr) - 60) && !force_registration)
|
||||
{
|
||||
tools::fail_msg_writer() << "Unable to prepare registration: this daemon has not received a ping from the storage server "
|
||||
<< (res.last_storage_server_ping == 0 ? "yet" : "since " + get_human_time_ago(last_storage_server_ping, std::time(nullptr)));
|
||||
return false;
|
||||
}
|
||||
|
||||
uint64_t block_height = std::max(res.height, res.target_height);
|
||||
uint8_t hf_version = hf_res.version;
|
||||
|
|
|
@ -187,7 +187,7 @@ public:
|
|||
|
||||
bool print_sr(uint64_t height);
|
||||
|
||||
bool prepare_registration();
|
||||
bool prepare_registration(bool force_registration=false);
|
||||
|
||||
bool print_sn(const std::vector<std::string> &args);
|
||||
|
||||
|
|
Loading…
Reference in New Issue