Skip to content

Commit

Permalink
Merge pull request EOSIO#101 from eosiosg/feature/dpos-pbft-bos-upgrade
Browse files Browse the repository at this point in the history
Feature/dpos pbft bos upgrade: bug fix
  • Loading branch information
Thaipanda authored Jun 14, 2019
2 parents 3dd5850 + e18d397 commit 5c2cf58
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
2 changes: 2 additions & 0 deletions libraries/chain/include/eosio/chain/pbft_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ namespace eosio {

bool is_valid_pbft_message(const pbft_message_common &common);

bool is_less_than_high_watermark(const block_num_type &bnum);

bool is_valid_prepared_certificate(const pbft_prepared_certificate &certificate);

bool is_valid_committed_certificate(const pbft_committed_certificate &certificate);
Expand Down
6 changes: 2 additions & 4 deletions libraries/chain/pbft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,8 @@ namespace eosio {
}
}
pbft_db.mark_as_prepared(new_view.prepared_cert.block_info.block_id);
if (pbft_db.should_prepared()) {
transit_to_prepared_state(s);
return;
}
transit_to_prepared_state(s);
return;
}

transit_to_committed_state(s, true);
Expand Down
35 changes: 22 additions & 13 deletions libraries/chain/pbft_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace eosio {
} else {
checkpoint_index = pbft_checkpoint_state_multi_index_type{};
}

fc::remove(checkpoints_db);
}

void pbft_database::close() {
Expand Down Expand Up @@ -131,13 +133,12 @@ namespace eosio {
auto prepares = cpsp->prepares;
auto as = current->active_schedule.producers;
auto threshold = as.size()* 2 / 3 + 1;
if (prepares.size() >= threshold && !cpsp->is_prepared) {
if (prepares.size() >= threshold && !cpsp->is_prepared && is_less_than_high_watermark(cpsp->block_num)) {
flat_map<pbft_view_type, uint32_t> prepare_count;
for (auto const &pre: prepares) {
if (prepare_count.find(pre.view) == prepare_count.end()) prepare_count[pre.view] = 0;
}


for (auto const &sp: as) {
for (auto const &pp: prepares) {
if (sp.block_signing_key == pp.common.sender) prepare_count[pp.view] += 1;
Expand All @@ -156,8 +157,10 @@ namespace eosio {
void pbft_database::mark_as_prepared(const block_id_type &bid) {
auto &by_block_id_index = pbft_state_index.get<by_block_id>();
auto itr = by_block_id_index.find(bid);
auto bnum = block_info_type{bid}.block_num();

if (itr == by_block_id_index.end()) {
auto ps = pbft_state{bid, block_info_type{bid}.block_num(), .is_prepared = true};
auto ps = pbft_state{bid, bnum, .is_prepared = true};
auto psp = make_shared<pbft_state>(ps);
pbft_state_index.insert(psp);
return;
Expand Down Expand Up @@ -238,9 +241,6 @@ namespace eosio {
if (itr == by_prepare_and_num_index.end()) return false;

pbft_state_ptr psp = *itr;
auto current_watermark = get_current_pbft_watermark();

if (psp->block_num > current_watermark && current_watermark > 0) return false;

if (psp->is_prepared && (psp->block_num > ctrl.last_irreversible_block_num())) {
ctrl.set_pbft_prepared((*itr)->block_id);
Expand Down Expand Up @@ -294,13 +294,12 @@ namespace eosio {
curr_itr = by_block_id_index.find(current->id);
if (curr_itr == by_block_id_index.end()) return;


auto cpsp = *curr_itr;

auto as = current->active_schedule.producers;
auto threshold = as.size()* 2 / 3 + 1;
auto commits = cpsp->commits;
if (commits.size() >= threshold && !cpsp->is_committed) {
if (commits.size() >= threshold && !cpsp->is_committed && is_less_than_high_watermark(cpsp->block_num)) {
flat_map<pbft_view_type, uint32_t> commit_count;
for (auto const &com: commits) {
if (commit_count.find(com.view) == commit_count.end()) commit_count[com.view] = 0;
Expand Down Expand Up @@ -374,10 +373,6 @@ namespace eosio {
if (itr == by_commit_and_num_index.end()) return false;
pbft_state_ptr psp = *itr;

auto current_watermark = get_current_pbft_watermark();

if (psp->block_num > current_watermark && current_watermark > 0) return false;

return (psp->is_committed && (psp->block_num > ctrl.last_irreversible_block_num()));
}

Expand Down Expand Up @@ -1179,7 +1174,8 @@ namespace eosio {
pbft_state_ptr psp = (*itr);

flat_map<block_num_type, bool> pending_checkpoint_block_num; // block_height and retry_flag
for (auto i = psp->block_num; i > ctrl.last_stable_checkpoint_block_num() && i > 1; --i) {
auto lscb_num = ctrl.last_stable_checkpoint_block_num();
for (auto i = psp->block_num; i > lscb_num && i > 1; --i) {
if (checkpoint(i)) {
auto &by_block = checkpoint_index.get<by_block_id>();

Expand Down Expand Up @@ -1219,6 +1215,14 @@ namespace eosio {
}
}
}
} else if (lscb_num > 0) { //retry sending my lscb
for (auto const &my_sp : ctrl.my_signature_providers()) {
auto uuid = boost::uuids::to_string(uuid_generator());
pbft_checkpoint cp;
cp.common.uuid=uuid; cp.block_info={ctrl.last_stable_checkpoint_block_id()}; cp.common.sender=my_sp.first; cp.common.chain_id=chain_id;
cp.sender_signature = my_sp.second(cp.digest());
new_pc.emplace_back(cp);
}
}
return new_pc;
}
Expand Down Expand Up @@ -1488,6 +1492,11 @@ namespace eosio {
return fork_schedules;
}

bool pbft_database::is_less_than_high_watermark(const block_num_type &bnum) {
auto current_watermark = get_current_pbft_watermark();
return current_watermark == 0 || bnum <= current_watermark;
}

pbft_state_ptr pbft_database::get_pbft_state_by_id(const block_id_type& id) const {

auto &by_block_id_index = pbft_state_index.get<by_block_id>();
Expand Down
8 changes: 7 additions & 1 deletion plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2797,6 +2797,7 @@ namespace eosio {
case normal :
peer_ilog(c, "received request_message:normal");
if( !msg.req_blocks.ids.empty() ) {
fc_dlog( logger, "received request_message, sending ${num} blocks from my node", ("num", msg.req_blocks.ids.size()));
for (auto const &bid: msg.req_blocks.ids) {
c->blk_send(bid);
}
Expand Down Expand Up @@ -2986,7 +2987,11 @@ namespace eosio {

template<typename M>
bool net_plugin_impl::is_pbft_msg_outdated(M const & msg) {
return (time_point_sec(time_point::now()) > time_point_sec(msg.common.timestamp) + pbft_message_TTL);
if (time_point_sec(time_point::now()) > time_point_sec(msg.common.timestamp) + pbft_message_TTL) {
fc_dlog( logger, "received an outdated pbft message ${m}", ("m", msg));
return true;
}
return false;
}

template<typename M>
Expand Down Expand Up @@ -3144,6 +3149,7 @@ namespace eosio {
}

if (!missing_blocks.empty()) {
fc_dlog( logger, "requesting ${num} missing blocks from view change", ("num", missing_blocks.size()));
request_message req;
for (auto const &b: missing_blocks) {
req.req_blocks.ids.push_back(b);
Expand Down

0 comments on commit 5c2cf58

Please sign in to comment.