diff --git a/httpserver/service_node.cpp b/httpserver/service_node.cpp index 284aaab6..552723e9 100644 --- a/httpserver/service_node.cpp +++ b/httpserver/service_node.cpp @@ -209,8 +209,12 @@ ServiceNode::ServiceNode(boost::asio::io_context& ioc, // TODO: fail hard if we can't encode our public key LOKI_LOG(info, "Read our snode address: {}", our_address_); our_address_.set_port(port); + swarm_ = std::make_unique(our_address_); LOKI_LOG(info, "Requesting initial swarm state"); + #ifndef INTEGRATION_TEST + bootstrap_data(); + #endif swarm_timer_tick(); lokid_ping_timer_tick(); cleanup_timer_tick(); @@ -260,8 +264,6 @@ parse_swarm_update(const std::shared_ptr& response_body) { } bu.height = body.at("result").at("height").get(); - bu.target_height = - body.at("result").at("target_height").get(); bu.block_hash = body.at("result").at("block_hash").get(); bu.hardfork = body.at("result").at("hardfork").get(); @@ -279,7 +281,7 @@ parse_swarm_update(const std::shared_ptr& response_body) { } void ServiceNode::bootstrap_data() { - LOKI_LOG(trace, "Bootstrapping peer ips"); + LOKI_LOG(trace, "Bootstrapping peer data"); json params; json fields; @@ -289,7 +291,6 @@ void ServiceNode::bootstrap_data() { fields["storage_port"] = true; fields["public_ip"] = true; fields["height"] = true; - fields["target_height"] = true; fields["block_hash"] = true; fields["hardfork"] = true; @@ -529,32 +530,23 @@ void ServiceNode::save_bulk(const std::vector& items) { reset_listeners(); } -void ServiceNode::on_sync_complete() { - -#ifndef INTEGRATION_TEST - bootstrap_data(); -#endif -} - void ServiceNode::on_bootstrap_update(const block_update_t& bu) { - swarm_->bootstrap_state(bu.swarms); + swarm_->apply_swarm_changes(bu.swarms); + target_height_ = std::max(target_height_, bu.height); } void ServiceNode::on_swarm_update(const block_update_t& bu) { hardfork_ = bu.hardfork; - bool sync_complete = false; - - if (syncing_ && bu.target_height != 0) { - syncing_ = bu.height < bu.target_height - 1; - sync_complete = !syncing_; + if (syncing_ && target_height_ != 0) { + syncing_ = bu.height < target_height_; } /// We don't have anything to do until we have synced if (syncing_) { - LOKI_LOG(debug, "Still syncing: {}/{}", bu.height, bu.target_height); + LOKI_LOG(debug, "Still syncing: {}/{}", bu.height, target_height_); return; } @@ -584,11 +576,6 @@ void ServiceNode::on_swarm_update(const block_update_t& bu) { return; } - if (!swarm_) { - LOKI_LOG(info, "Initialized our swarm"); - swarm_ = std::make_unique(our_address_); - } - const SwarmEvents events = swarm_->derive_swarm_events(bu.swarms); swarm_->set_swarm_id(events.our_swarm_id); @@ -600,10 +587,6 @@ void ServiceNode::on_swarm_update(const block_update_t& bu) { swarm_->update_state(bu.swarms, events); - if (sync_complete) { - on_sync_complete(); - } - if (!events.new_snodes.empty()) { bootstrap_peers(events.new_snodes); } @@ -642,7 +625,6 @@ void ServiceNode::swarm_timer_tick() { fields["storage_port"] = true; fields["public_ip"] = true; fields["height"] = true; - fields["target_height"] = true; fields["block_hash"] = true; fields["hardfork"] = true; diff --git a/httpserver/service_node.h b/httpserver/service_node.h index 0f218a55..802e1e55 100644 --- a/httpserver/service_node.h +++ b/httpserver/service_node.h @@ -104,6 +104,7 @@ class ServiceNode { bool syncing_ = true; int hardfork_ = 0; uint64_t block_height_ = 0; + uint64_t target_height_ = 0; const LokidClient& lokid_client_; std::string block_hash_; std::unique_ptr swarm_; @@ -138,8 +139,6 @@ class ServiceNode { /// request swarm info from the blockchain void update_swarms(); - void on_sync_complete(); - void on_bootstrap_update(const block_update_t& bu); void on_swarm_update(const block_update_t& bu); diff --git a/httpserver/swarm.cpp b/httpserver/swarm.cpp index db0dff01..d5a1edcd 100644 --- a/httpserver/swarm.cpp +++ b/httpserver/swarm.cpp @@ -142,11 +142,6 @@ static all_swarms_t apply_ips(const all_swarms_t& swarms_to_keep, return result_swarms; } -void Swarm::bootstrap_state(const all_swarms_t& bootstrap_swarms) { - - all_cur_swarms_ = apply_ips(all_cur_swarms_, bootstrap_swarms); -} - void Swarm::apply_swarm_changes(const all_swarms_t& new_swarms) { all_cur_swarms_ = apply_ips(new_swarms, all_cur_swarms_); diff --git a/httpserver/swarm.h b/httpserver/swarm.h index aa3ab7b3..11b3963e 100644 --- a/httpserver/swarm.h +++ b/httpserver/swarm.h @@ -26,7 +26,6 @@ using all_swarms_t = std::vector; struct block_update_t { all_swarms_t swarms; uint64_t height; - uint64_t target_height; std::string block_hash; int hardfork; }; @@ -67,8 +66,6 @@ class Swarm { /// Update swarm state according to `events` void update_state(const all_swarms_t& swarms, const SwarmEvents& events); - void bootstrap_state(const all_swarms_t& bootstrap_swarms); - void apply_swarm_changes(const all_swarms_t& new_swarms); bool is_pubkey_for_us(const std::string& pk) const;