Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/dpos-pbft' into feature/…
Browse files Browse the repository at this point in the history
…bos-dpos-pbft
  • Loading branch information
qianxiaofeng committed Jan 11, 2019
2 parents ac46164 + a6e5fad commit db7466b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 104 deletions.
67 changes: 2 additions & 65 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ struct controller_impl {
void maybe_switch_forks( controller::block_status s = controller::block_status::complete ) {

if (pbft_prepared) fork_db.mark_pbft_prepared_fork(*pbft_prepared);
if (my_prepare) fork_db.mark_pbft_prepared_fork(*my_prepare);
if (my_prepare) fork_db.mark_pbft_my_prepare_fork(*my_prepare);

auto new_head = fork_db.head();

Expand Down Expand Up @@ -1468,66 +1468,8 @@ struct controller_impl {
} /// end for each block in branch
ilog("successfully switched fork to new head ${new_head_id}", ("new_head_id", new_head->id));
}
// } else if( new_head->id != head->id && !pbft_prepared) {
// switch_forks(new_head);
// } else if (pbft_prepared && !fork_db.is_in_current_chain(*pbft_prepared)) {
// auto prepared_head = fork_db.get_fork_head(*pbft_prepared);
// EOS_ASSERT (prepared_head, fork_database_exception, "prepared fork head invalid");
// switch_forks(prepared_head);
// fork_db.mark_pbft_supported_fork(prepared_head->id);
// }
} /// push_block

void switch_forks(const block_state_ptr &new_head) {
ilog("switching forks from ${current_head_id} (block number ${current_head_num}) to ${new_head_id} (block number ${new_head_num})",
("current_head_id", head->id)("current_head_num", head->block_num)("new_head_id", new_head->id)("new_head_num", new_head->block_num) );
auto branches = fork_db.fetch_branch_from( new_head->id, head->id );

for( auto itr = branches.second.begin(); itr != branches.second.end(); ++itr ) {
fork_db.mark_in_current_chain( *itr , false );
pop_block();
}
EOS_ASSERT( self.head_block_id() == branches.second.back()->header.previous, fork_database_exception,
"loss of sync between fork_db and chainbase during fork switch" ); // _should_ never fail

for( auto ritr = branches.first.rbegin(); ritr != branches.first.rend(); ++ritr) {
optional<fc::exception> except;
try {
apply_block( (*ritr)->block, (*ritr)->validated ? controller::block_status::validated : controller::block_status::complete );
head = *ritr;
fork_db.mark_in_current_chain( *ritr, true );
(*ritr)->validated = true;
}
catch (const fc::exception& e) { except = e; }
if (except) {
elog("exception thrown while switching forks ${e}", ("e",except->to_detail_string()));

// ritr currently points to the block that threw
// if we mark it invalid it will automatically remove all forks built off it.
fork_db.set_validity( *ritr, false );

// pop all blocks from the bad fork
// ritr base is a forward itr to the last block successfully applied
auto applied_itr = ritr.base();
for( auto itr = applied_itr; itr != branches.first.end(); ++itr ) {
fork_db.mark_in_current_chain( *itr , false );
pop_block();
}
EOS_ASSERT( self.head_block_id() == branches.second.back()->header.previous, fork_database_exception,
"loss of sync between fork_db and chainbase during fork switch reversal" ); // _should_ never fail

// re-apply good blocks
for( auto ritr = branches.second.rbegin(); ritr != branches.second.rend(); ++ritr ) {
apply_block( (*ritr)->block, controller::block_status::validated /* we previously validated these blocks*/ );
head = *ritr;
fork_db.mark_in_current_chain( *ritr, true );
}
throw *except;
} // end if exception
} /// end for each block in branch
ilog("successfully switched fork to new head ${new_head_id}", ("new_head_id", new_head->id));
}

void abort_block() {
if( pending ) {
if ( read_mode == db_read_mode::SPECULATIVE ) {
Expand Down Expand Up @@ -1824,12 +1766,6 @@ chainbase::database& controller::mutable_db()const { return my->db; }

const fork_database& controller::fork_db()const { return my->fork_db; }

//pbft_controller& controller::pbft()const { return my->pbft_ctrl; }

set<chain::account_name> controller::my_producers()const{
return my->conf.my_producers;
}

std::map<chain::public_key_type, signature_provider_type> controller::my_signature_providers()const{
return my->conf.my_signature_providers;
}
Expand Down Expand Up @@ -2256,6 +2192,7 @@ block_id_type controller::get_pbft_my_prepare() const {
}

void controller::reset_pbft_my_prepare() const {
if (my->my_prepare) my->fork_db.remove_pbft_my_prepare_fork(*my->my_prepare);
my->my_prepare.reset();
}

Expand Down
43 changes: 29 additions & 14 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,6 @@ namespace eosio { namespace chain {
});
}

bool fork_database::is_in_current_chain( const block_id_type& id) const {
auto& by_id_idx = my->index.get<by_block_id>();
auto itr = by_id_idx.find( id );
if (itr == by_id_idx.end()) return false;
return (*itr)->in_current_chain;
}

void fork_database::prune( const block_state_ptr& h ) {
auto num = h->block_num;

Expand Down Expand Up @@ -346,13 +339,35 @@ namespace eosio { namespace chain {
my->head = *my->index.get<by_lib_block_num>().begin();
}

void fork_database::remove_pbft_supported_mark() const {
// auto& by_num_idx = my->index.get<by_lib_block_num>();
// auto itr = by_num_idx.begin();
// while (itr != by_num_idx.end() && (*itr)->pbft_supported) {
// by_num_idx.modify( itr, [&]( auto& bsp ) { bsp->pbft_supported = false; });
// ++itr;
// }
void fork_database::remove_pbft_my_prepare_fork(const block_id_type &id) const {
auto& by_id_idx = my->index.get<by_block_id>();
auto itr = by_id_idx.find( 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_my_prepare = false; });

auto update = [&]( const vector<block_id_type>& in ) {
vector<block_id_type> updated;

for( const auto& i : in ) {
auto& pidx = my->index.get<by_prev>();
auto pitr = pidx.lower_bound( i );
auto epitr = pidx.upper_bound( i );
while( pitr != epitr ) {
pidx.modify( pitr, [&]( auto& bsp ) {
bsp->pbft_my_prepare = false;
updated.push_back( bsp->id );
});
++pitr;
}
}
return updated;
};

vector<block_id_type> queue{id};
while(!queue.empty()) {
queue = update( queue );
}
my->head = *my->index.get<by_lib_block_num>().begin();
}

block_state_ptr fork_database::get_block_in_current_chain_by_num( uint32_t n )const {
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ namespace eosio { namespace chain {


const fork_database& fork_db()const;
set<chain::account_name> my_producers()const;

std::map<chain::public_key_type, signature_provider_type> my_signature_providers()const;
void set_my_signature_providers(std::map<chain::public_key_type, signature_provider_type> msp);

Expand Down
4 changes: 1 addition & 3 deletions libraries/chain/include/eosio/chain/fork_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ namespace eosio { namespace chain {

void set_latest_checkpoint( block_id_type id);

bool is_in_current_chain( const block_id_type& id ) const;

void mark_pbft_prepared_fork(const block_id_type &id) const;

void mark_pbft_my_prepare_fork(const block_id_type &id) const;

void remove_pbft_supported_mark() const;
void remove_pbft_my_prepare_fork(const block_id_type &id) const;

private:
unique_ptr<fork_database_impl> my;
Expand Down
15 changes: 6 additions & 9 deletions libraries/chain/pbft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace eosio {
namespace chain {

pbft_controller::pbft_controller(controller &ctrl) :
pbft_db(ctrl),
state_machine(pbft_db) {
pbft_controller::pbft_controller(controller &ctrl) : pbft_db(ctrl), state_machine(pbft_db) {
config.view_change_timeout = 6;
config.bp_candidate = true;
datadir = ctrl.state_dir();
Expand Down Expand Up @@ -98,8 +96,7 @@ namespace eosio {
psm_state::~psm_state() = default;


psm_machine::psm_machine(pbft_database &pbft_db) :
pbft_db(pbft_db) {
psm_machine::psm_machine(pbft_database &pbft_db) : pbft_db(pbft_db) {
this->set_current(new psm_committed_state);

this->set_prepares_cache(vector<pbft_prepare>{});
Expand Down Expand Up @@ -321,19 +318,19 @@ namespace eosio {
* psm_view_change_state
*/
void psm_view_change_state::on_prepare(psm_machine *m, pbft_prepare &e, pbft_database &pbft_db) {

//ignore;
}

void psm_view_change_state::send_prepare(psm_machine *m, pbft_database &pbft_db) {

//ignore;
}

void psm_view_change_state::on_commit(psm_machine *m, pbft_commit &e, pbft_database &pbft_db) {

//ignore;
}

void psm_view_change_state::send_commit(psm_machine *m, pbft_database &pbft_db) {

//ignore;
}

void psm_view_change_state::on_view_change(psm_machine *m, pbft_view_change &e, pbft_database &pbft_db) {
Expand Down
24 changes: 12 additions & 12 deletions plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2988,7 +2988,7 @@ namespace eosio {
if (!pcc.pbft_db.is_valid_prepare(msg)) return;

bcast_pbft_msg(msg);
fc_dlog(logger, "sending prepare at height: ${h}, view: ${v}", ("h", msg.block_num)("v", msg.view));
dlog("sending prepare at height: ${h}, view: ${v}", ("h", msg.block_num)("v", msg.view));
}

void net_plugin_impl::pbft_outgoing_commit(const pbft_commit &msg) {
Expand All @@ -2999,7 +2999,7 @@ namespace eosio {
if (!pcc.pbft_db.is_valid_commit(msg)) return;

bcast_pbft_msg(msg);
fc_dlog(logger, "sending commit at height: ${h}, view: ${v}", ("h", msg.block_num)("v", msg.view));
dlog("sending commit at height: ${h}, view: ${v}", ("h", msg.block_num)("v", msg.view));
}

void net_plugin_impl::pbft_outgoing_view_change(const pbft_view_change &msg) {
Expand All @@ -3010,7 +3010,7 @@ namespace eosio {
if (!pcc.pbft_db.is_valid_view_change(msg)) return;

bcast_pbft_msg(msg);
fc_dlog(logger, "sending view change {cv: ${cv}, tv: ${tv}}", ("cv", msg.current_view)("tv", msg.target_view));
dlog("sending view change {cv: ${cv}, tv: ${tv}}", ("cv", msg.current_view)("tv", msg.target_view));
}

void net_plugin_impl::pbft_outgoing_new_view(const pbft_new_view &msg) {
Expand All @@ -3021,7 +3021,7 @@ namespace eosio {
if (!pcc.pbft_db.is_valid_new_view(msg)) return;

bcast_pbft_msg(msg);
fc_dlog(logger, "sending new view at ${n}", ("n", msg));
dlog("sending new view at ${n}", ("n", msg));
}

void net_plugin_impl::pbft_outgoing_checkpoint(const pbft_checkpoint &msg) {
Expand Down Expand Up @@ -3066,7 +3066,7 @@ namespace eosio {
if (!pcc.pbft_db.is_valid_prepare(msg)) return;

forward_pbft_msg(c, msg);
fc_dlog(logger, "received prepare at height: ${n}, view: ${v}, from ${k}, ", ("n", msg.block_num)("v", msg.view)("k", msg.public_key));
dlog("received prepare at height: ${n}, view: ${v}, from ${k}, ", ("n", msg.block_num)("v", msg.view)("k", msg.public_key));

pbft_incoming_prepare_channel.publish(msg);

Expand All @@ -3083,7 +3083,7 @@ namespace eosio {
if (!pcc.pbft_db.is_valid_commit(msg)) return;

forward_pbft_msg(c, msg);
fc_dlog(logger, "received commit at height: ${n}, view: ${v}, from ${k}, ", ("n", msg.block_num)("v", msg.view)("k", msg.public_key));
dlog("received commit at height: ${n}, view: ${v}, from ${k}, ", ("n", msg.block_num)("v", msg.view)("k", msg.public_key));

pbft_incoming_commit_channel.publish(msg);
}
Expand All @@ -3099,7 +3099,7 @@ namespace eosio {
if (!pcc.pbft_db.is_valid_view_change(msg)) return;

forward_pbft_msg(c, msg);
fc_dlog(logger, "received view change {cv: ${cv}, tv: ${tv}} from ${v}", ("cv", msg.current_view)("tv", msg.target_view)("v", msg.public_key));
dlog("received view change {cv: ${cv}, tv: ${tv}} from ${v}", ("cv", msg.current_view)("tv", msg.target_view)("v", msg.public_key));

pbft_incoming_view_change_channel.publish(msg);
}
Expand All @@ -3115,7 +3115,7 @@ namespace eosio {
if (!pcc.pbft_db.is_valid_new_view(msg)) return;

forward_pbft_msg(c, msg);
fc_dlog(logger, "received new view at ${n}, from ${v}", ("n", msg)("v", msg.public_key));
dlog("received new view at ${n}, from ${v}", ("n", msg)("v", msg.public_key));

pbft_incoming_new_view_channel.publish(msg);
}
Expand All @@ -3131,7 +3131,7 @@ namespace eosio {
if (!pcc.pbft_db.is_valid_checkpoint(msg)) return;

forward_pbft_msg(c, msg);
fc_dlog(logger, "received checkpoint at ${n}, from ${v}", ("n", msg.block_num)("v", msg.public_key));
dlog("received checkpoint at ${n}, from ${v}", ("n", msg.block_num)("v", msg.public_key));

pbft_incoming_checkpoint_channel.publish(msg);
}
Expand Down Expand Up @@ -3243,10 +3243,10 @@ namespace eosio {
}
}

fc_dlog(logger, "connection: ${conn} \tstatus(socket|connecting|syncing|current): ${status}\t|\twrite_queue: ${write}\t|\tout_queue: ${out}\t|\tpbft_queue: ${pbft}", ("status",status)("conn",conn_str)("write",write_queue)("out",out_queue)("pbft",pbft_queue));
dlog("connection: ${conn} \tstatus(socket|connecting|syncing|current): ${status}\t|\twrite_queue: ${write}\t|\tout_queue: ${out}\t|\tpbft_queue: ${pbft}", ("status",status)("conn",conn_str)("write",write_queue)("out",out_queue)("pbft",pbft_queue));
}
fc_dlog(logger, "connections stats: current : ${current}\t total : ${total} ",("current",current)("total",total));
fc_dlog(logger, "================================================================================================");
dlog("connections stats: current : ${current}\t total : ${total} ",("current",current)("total",total));
dlog("================================================================================================");
auto local_trx_pool_size = local_txns.size();
fc_dlog(logger, "local trx pool size: ${local_trx_pool_size}",("local_trx_pool_size",local_trx_pool_size));
fc_dlog(logger, "================================================================================================");
Expand Down

0 comments on commit db7466b

Please sign in to comment.