Skip to content

Commit

Permalink
generate pbft watermark from forkdb.
Browse files Browse the repository at this point in the history
  • Loading branch information
oldcold committed May 22, 2019
1 parent cf75956 commit 303c12d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 104 deletions.
35 changes: 9 additions & 26 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ struct controller_impl {
bool pbft_upgrading = false;
optional<block_id_type> pending_pbft_lib;
optional<block_id_type> pending_pbft_checkpoint;
vector<block_num_type> proposed_schedule_blocks;
vector<block_num_type> promoted_schedule_blocks;
block_state_ptr pbft_prepared;
block_state_ptr my_prepare;
block_state_ptr head;
Expand Down Expand Up @@ -944,6 +942,10 @@ struct controller_impl {
});
}

if (pbft_enabled && pending->_pending_block_state->pbft_watermark) {
if (auto bs = fork_db.get_block(pending->_pending_block_state->id)) fork_db.mark_as_pbft_watermark(bs);
}

emit( self.accepted_block, pending->_pending_block_state );
} catch (...) {
// dont bother resetting pending, instead abort the block
Expand Down Expand Up @@ -1387,17 +1389,10 @@ struct controller_impl {
const auto& gpo = db.get<global_property_object>();

auto lib_num = std::max(pending->_pending_block_state->dpos_irreversible_blocknum, pending->_pending_block_state->bft_irreversible_blocknum);
auto lscb_num = pending->_pending_block_state->pbft_stable_checkpoint_blocknum;

if (pbft_enabled && gpo.proposed_schedule_block_num) {
proposed_schedule_blocks.emplace_back(*gpo.proposed_schedule_block_num);
for ( auto itr = proposed_schedule_blocks.begin(); itr != proposed_schedule_blocks.end();) {
if ((*itr) < lscb_num) {
itr = proposed_schedule_blocks.erase(itr);
} else {
++itr;
}
}
auto bs = fork_db.get_block_in_current_chain_by_num(*gpo.proposed_schedule_block_num);
if (bs) fork_db.mark_as_pbft_watermark(bs);
}

bool should_promote_pending_schedule = false;
Expand Down Expand Up @@ -1429,14 +1424,7 @@ struct controller_impl {
pending->_pending_block_state->set_new_producers(gpo.proposed_schedule);

if (pbft_enabled) {
promoted_schedule_blocks.emplace_back(pending->_pending_block_state->block_num);
for ( auto itr = promoted_schedule_blocks.begin(); itr != promoted_schedule_blocks.end();) {
if ((*itr) < lscb_num) {
itr = promoted_schedule_blocks.erase(itr);
} else {
++itr;
}
}
pending->_pending_block_state->pbft_watermark = true;
}
}
db.modify( gpo, [&]( auto& gp ) {
Expand Down Expand Up @@ -2343,13 +2331,8 @@ block_id_type controller::last_stable_checkpoint_block_id() const {
return block_id_type{};
}


vector<uint32_t> controller::proposed_schedule_block_nums() const {
return my->proposed_schedule_blocks;
}

vector<uint32_t> controller::promoted_schedule_block_nums() const {
return my->promoted_schedule_blocks;
vector<uint32_t> controller::get_watermarks() const {
return my->fork_db.get_watermarks_in_forkdb();
}

bool controller::is_replaying() const {
Expand Down
40 changes: 33 additions & 7 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace eosio { namespace chain {
struct by_block_id;
struct by_block_num;
struct by_lib_block_num;
struct by_watermark;
struct by_prev;
typedef multi_index_container<
block_state_ptr,
Expand All @@ -32,13 +33,20 @@ namespace eosio { namespace chain {
>,
ordered_non_unique< tag<by_lib_block_num>,
composite_key< block_state,
member<block_header_state,uint32_t,&block_header_state::dpos_irreversible_blocknum>,
member<block_header_state,uint32_t,&block_header_state::bft_irreversible_blocknum>,
member<block_state,bool,&block_state::pbft_prepared>,
member<block_state,bool,&block_state::pbft_my_prepare>,
member<block_header_state,uint32_t,&block_header_state::block_num>
member<block_header_state,uint32_t,&block_header_state::dpos_irreversible_blocknum>,
member<block_header_state,uint32_t,&block_header_state::bft_irreversible_blocknum>,
member<block_state,bool,&block_state::pbft_prepared>,
member<block_state,bool,&block_state::pbft_my_prepare>,
member<block_header_state,uint32_t,&block_header_state::block_num>
>,
composite_key_compare< std::greater<uint32_t>, std::greater<uint32_t>, std::greater<bool>, std::greater<bool>, std::greater<uint32_t> >
>,
ordered_non_unique< tag<by_watermark>,
composite_key< block_state,
member<block_state,bool,&block_state::pbft_watermark>,
member<block_header_state,uint32_t,&block_header_state::block_num>
>,
composite_key_compare< std::greater<>, std::less<> >
>
>
> fork_multi_index_type;
Expand Down Expand Up @@ -202,7 +210,6 @@ namespace eosio { namespace chain {

auto prior = my->index.find( n->block->previous );

//TODO: to be optimised.
if (prior != my->index.end()) {
if ((*prior)->pbft_prepared) mark_pbft_prepared_fork(*prior);
if ((*prior)->pbft_my_prepare) mark_pbft_my_prepare_fork(*prior);
Expand Down Expand Up @@ -601,5 +608,24 @@ namespace eosio { namespace chain {
}
}

vector<block_num_type> fork_database::get_watermarks_in_forkdb() {
vector<block_num_type> watermarks;
auto& pidx = my->index.get<by_watermark>();
auto pitr = pidx.begin();
while (pitr != pidx.end() && (*pitr)->pbft_watermark) {
watermarks.emplace_back((*pitr)->block_num);
++pitr;
}
return watermarks;
}

void fork_database::mark_as_pbft_watermark( const block_state_ptr& h) {
auto& by_id_idx = my->index.get<by_block_id>();
auto itr = by_id_idx.find( h->id );
EOS_ASSERT( itr != by_id_idx.end(), fork_db_block_not_found, "could not find block in fork database" );
by_id_idx.modify( itr, [&]( auto& bsp ) { bsp->pbft_watermark = true; });
}



} } /// eosio::chain
} } /// eosio::chain
3 changes: 2 additions & 1 deletion libraries/chain/include/eosio/chain/block_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace eosio { namespace chain {
bool in_current_chain = false;
bool pbft_prepared = false;
bool pbft_my_prepare = false;
bool pbft_watermark = false;

/// this data is redundant with the data stored in block, but facilitates
/// recapturing transactions when we pop a block
Expand All @@ -33,4 +34,4 @@ namespace eosio { namespace chain {

} } /// namespace eosio::chain

FC_REFLECT_DERIVED( eosio::chain::block_state, (eosio::chain::block_header_state), (block)(validated)(in_current_chain)(pbft_prepared)(pbft_my_prepare) )
FC_REFLECT_DERIVED( eosio::chain::block_state, (eosio::chain::block_header_state), (block)(validated)(in_current_chain)(pbft_prepared)(pbft_my_prepare)(pbft_watermark) )
3 changes: 1 addition & 2 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ namespace eosio { namespace chain {

bool pending_pbft_lib();

vector<uint32_t> proposed_schedule_block_nums()const;
vector<uint32_t> promoted_schedule_block_nums()const;
vector<uint32_t> get_watermarks() const;

void set_pbft_latest_checkpoint( const block_id_type& id );
uint32_t last_stable_checkpoint_block_num()const;
Expand Down
4 changes: 4 additions & 0 deletions libraries/chain/include/eosio/chain/fork_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ namespace eosio { namespace chain {

void remove_pbft_prepared_fork();

vector<block_num_type> get_watermarks_in_forkdb();

void mark_as_pbft_watermark( const block_state_ptr& h);

private:
unique_ptr<fork_database_impl> my;
};
Expand Down
3 changes: 3 additions & 0 deletions libraries/chain/include/eosio/chain/pbft_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,8 @@ namespace eosio {

block_num_type get_current_pbft_watermark();

void update_fork_schedules();

private:
controller &ctrl;
pbft_state_multi_index_type pbft_state_index;
Expand All @@ -663,6 +665,7 @@ namespace eosio {
fc::path checkpoints_dir;
boost::uuids::random_generator uuid_generator;
vector<block_num_type> prepare_watermarks;
flat_map<public_key_type, uint32_t> fork_schedules;

bool is_valid_prepared_certificate(const pbft_prepared_certificate &certificate);

Expand Down
Loading

0 comments on commit 303c12d

Please sign in to comment.