Skip to content

Commit

Permalink
Merge pull request #219 from BeaudanBrown/bootstrap-height
Browse files Browse the repository at this point in the history
Bootstrap on startup and use the target height from bootstrap nodes to determine when we have finished syncing
  • Loading branch information
msgmaxim committed Jul 11, 2019
2 parents 1eca662 + a6a146e commit edbf13f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 38 deletions.
38 changes: 10 additions & 28 deletions httpserver/service_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Swarm>(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();
Expand Down Expand Up @@ -260,8 +264,6 @@ parse_swarm_update(const std::shared_ptr<std::string>& response_body) {
}

bu.height = body.at("result").at("height").get<uint64_t>();
bu.target_height =
body.at("result").at("target_height").get<uint64_t>();
bu.block_hash = body.at("result").at("block_hash").get<std::string>();
bu.hardfork = body.at("result").at("hardfork").get<int>();

Expand All @@ -279,7 +281,7 @@ parse_swarm_update(const std::shared_ptr<std::string>& response_body) {
}

void ServiceNode::bootstrap_data() {
LOKI_LOG(trace, "Bootstrapping peer ips");
LOKI_LOG(trace, "Bootstrapping peer data");

json params;
json fields;
Expand All @@ -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;

Expand Down Expand Up @@ -529,32 +530,23 @@ void ServiceNode::save_bulk(const std::vector<Item>& 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;
}

Expand Down Expand Up @@ -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<Swarm>(our_address_);
}

const SwarmEvents events = swarm_->derive_swarm_events(bu.swarms);

swarm_->set_swarm_id(events.our_swarm_id);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions httpserver/service_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -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> swarm_;
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions httpserver/swarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down
3 changes: 0 additions & 3 deletions httpserver/swarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ using all_swarms_t = std::vector<SwarmInfo>;
struct block_update_t {
all_swarms_t swarms;
uint64_t height;
uint64_t target_height;
std::string block_hash;
int hardfork;
};
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit edbf13f

Please sign in to comment.