Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #30

Merged
merged 12 commits into from
Nov 20, 2019
20 changes: 14 additions & 6 deletions plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2386,18 +2386,26 @@ namespace eosio {

if( peer_lib <= lib_num && peer_lib > 0) {
try {
block_id_type peer_lib_id = cc.get_block_id_for_num( peer_lib);
on_fork =( msg.last_irreversible_block_id != peer_lib_id);
}
catch( const unknown_block_exception &ex) {
fc_wlog( logger, "peer last irreversible block ${pl} is unknown", ("pl", peer_lib) );
on_fork = true;
auto peer_lib_blk = cc.fetch_block_by_number(peer_lib);
if (peer_lib_blk) {
on_fork = (msg.last_irreversible_block_id != peer_lib_blk->id());
}
else {
auto peer_lib_next_blk = cc.fetch_block_by_number(peer_lib + 1);
if (peer_lib_next_blk) {
on_fork = (msg.last_irreversible_block_id != peer_lib_next_blk->previous);
}
else {
on_fork = true;
}
}
}
catch( ...) {
fc_wlog( logger, "caught an exception getting block id for ${pl}",("pl",peer_lib) );
on_fork = true;
}
if( on_fork) {
fc_wlog( logger, "peer last irreversible block ${pl} is unknown", ("pl", peer_lib) );
fc_elog( logger, "Peer chain is forked" );
c->enqueue( go_away_message( forked ));
return;
Expand Down
71 changes: 40 additions & 31 deletions plugins/randpa_plugin/include/eosio/randpa_plugin/randpa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ class randpa {
}
#endif

template <typename T>
bool check_is_valid_msg(const T& msg) const {
try {
msg.public_key();
} catch (const fc::exception&) {
return false;
}
return true;
}

// need handle all messages
void process_msg(randpa_message_ptr msg_ptr) {
auto msg = *msg_ptr;
Expand All @@ -338,43 +348,33 @@ class randpa {
}
}

class net_msg_handler: public fc::visitor<void> {
public:
net_msg_handler(randpa& randpa_ref, uint32_t ses_id):
_randpa(randpa_ref),
_ses_id(ses_id)
{ }

template <typename T>
void operator() (const T& msg) {
if (_randpa.check_is_valid_msg(msg)) {
_randpa.on(_ses_id, msg);
}
}

private:
randpa& _randpa;
uint32_t _ses_id;
};

void process_net_msg(const randpa_net_msg& msg) {
if (fc::time_point::now() - msg.receive_time > fc::milliseconds(msg_expiration_ms)) {
wlog("Network message dropped, msg age: ${age}", ("age", fc::time_point::now() - msg.receive_time));
return;
}

auto ses_id = msg.ses_id;
const auto& data = msg.data;

switch (data.which()) {
case randpa_net_msg_data::tag<prevote_msg>::value:
process_round_msg(ses_id, data.get<prevote_msg>());
break;
case randpa_net_msg_data::tag<precommit_msg>::value:
process_round_msg(ses_id, data.get<precommit_msg>());
break;
case randpa_net_msg_data::tag<proof_msg>::value:
on(ses_id, data.get<proof_msg>());
break;
case randpa_net_msg_data::tag<handshake_msg>::value:
on(ses_id, data.get<handshake_msg>());
break;
case randpa_net_msg_data::tag<handshake_ans_msg>::value:
on(ses_id, data.get<handshake_ans_msg>());
break;
case randpa_net_msg_data::tag<finality_notice_msg>::value:
on(ses_id, data.get<finality_notice_msg>());
break;
case randpa_net_msg_data::tag<finality_req_proof_msg>::value:
on(ses_id, data.get<finality_req_proof_msg>());
break;
default:
wlog("Randpa message received, but handler not found, type: ${type}",
("type", data.which())
);
break;
}
auto visitor = net_msg_handler(*this, msg.ses_id);
msg.data.visit(visitor);
}

void process_event(const randpa_event& event){
Expand Down Expand Up @@ -467,6 +467,14 @@ class randpa {
return precommited_keys.size() > node->active_bp_keys.size() * 2 / 3;
}

void on(uint32_t ses_id, const prevote_msg& msg) {
process_round_msg(ses_id, msg);
}

void on(uint32_t ses_id, const precommit_msg& msg) {
process_round_msg(ses_id, msg);
}

void on(uint32_t ses_id, const finality_notice_msg& msg) {
const auto& data = msg.data;
dlog("Randpa finality_notice_msg received for block ${bid}", ("bid", data.best_block));
Expand Down Expand Up @@ -702,6 +710,7 @@ class randpa {

if (get_block_num(_lib) < get_block_num(proof.best_block)) {
on_proof_gained(proof);
update_lib(proof.best_block);
}
dlog("round ${r} finished", ("r", _round->get_num()));
}
Expand Down
Loading