Skip to content

Commit

Permalink
add head_catched_up check in all handle_message functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vonhenry committed Sep 25, 2019
1 parent 0036ca4 commit 0f10014
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions plugins/ibc_plugin/ibc_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ namespace eosio { namespace ibc {
void handle_message(const connection_ptr& c, const ibc_block_merkle_path_request_message &msg);
void handle_message(const connection_ptr& c, const ibc_block_merkle_path_data_message &msg);

bool head_catched_up = false;

lwc_section_type sum_received_lwcls_info( );
bool is_head_catchup( );
bool should_send_ibc_heartbeat( );
Expand Down Expand Up @@ -2452,6 +2454,8 @@ namespace eosio { namespace ibc {
("cf",msg.cashtrxs_table_seq_num_range.first)("ct",msg.cashtrxs_table_seq_num_range.second)
("n",msg.new_producers_block_num)("lsf",msg.lwcls.first_num)("lst",msg.lwcls.last_num));

if ( ! head_catched_up ) { fc_ilog(logger,"head does not catchup"); return; }

// step one: check ibc_chain_state and lwcls
if ( msg.ibc_chain_state == deployed ) { // send lwc_init_message
controller &cc = chain_plug->chain();
Expand Down Expand Up @@ -2588,6 +2592,8 @@ namespace eosio { namespace ibc {
void ibc_plugin_impl::handle_message(const connection_ptr& c, const lwc_init_message &msg) {
peer_dlog(c, "received lwc_init_message");

if ( ! head_catched_up ) { fc_ilog(logger,"head does not catchup"); return; }

chain_contract->get_contract_state();
if ( chain_contract->state == deployed && chain_contract->lwc_config_valid() ){
chain_contract->chain_init( msg );
Expand Down Expand Up @@ -2651,6 +2657,8 @@ namespace eosio { namespace ibc {
void ibc_plugin_impl::handle_message(const connection_ptr& c, const lwc_section_request_message &msg) {
peer_dlog(c, "received lwc_section_request_message [${from},${to}]",("from",msg.start_block_num)("to",msg.end_block_num));

if ( ! head_catched_up ) { fc_ilog(logger,"head does not catchup"); return; }

uint32_t rq_length = msg.end_block_num - msg.start_block_num + 1;
uint32_t lib_block_num = chain_plug->chain().last_irreversible_block_num();

Expand Down Expand Up @@ -2707,6 +2715,8 @@ namespace eosio { namespace ibc {
void ibc_plugin_impl::handle_message(const connection_ptr& c, const lwc_section_data_message &msg) {
peer_dlog(c, "received lwc_section_data_message [${from},${to}]",("from",msg.headers.front().block_num())("to",msg.headers.back().block_num()));

if ( ! head_catched_up ) { fc_ilog(logger,"head does not catchup"); return; }

auto p = chain_contract->get_sections_tb_reverse_nth_section();
if ( !p.valid() ){
fc_elog(logger,"can not get section info from ibc.chain contract");
Expand Down Expand Up @@ -2783,6 +2793,8 @@ namespace eosio { namespace ibc {
void ibc_plugin_impl::handle_message(const connection_ptr& c, const lwc_block_commits_request_message &msg){
peer_dlog(c, "received lwc_block_commits_request_message [${num}]",("num",msg.block_num));

if ( ! head_catched_up ) { fc_ilog(logger,"head does not catchup"); return; }

#ifdef BOSCORE
uint32_t lib_block_num = chain_plug->chain().last_irreversible_block_num();
if ( msg.block_num > lib_block_num ){ return; }
Expand Down Expand Up @@ -2870,6 +2882,8 @@ namespace eosio { namespace ibc {
void ibc_plugin_impl::handle_message(const connection_ptr& c, const lwc_block_commits_data_message &msg){
peer_dlog(c, "received lwc_block_commits_data_message [${from},${to}]",("from",msg.headers.front().block_num())("to",msg.headers.back().block_num()));

if ( ! head_catched_up ) { fc_ilog(logger,"head does not catchup"); return; }

auto p = chain_contract->get_sections_tb_reverse_nth_section();
if ( !p.valid() ){
fc_elog(logger,"can not get section info from ibc.chain contract");
Expand All @@ -2885,6 +2899,8 @@ namespace eosio { namespace ibc {
void ibc_plugin_impl::handle_message(const connection_ptr& c, const ibc_trxs_request_message &msg ) {
peer_dlog(c, "received ibc_trxs_request_message, table ${tb}, id range [${f},${t}]",("tb",msg.table)("f",msg.range.first)("t",msg.range.second));

if ( ! head_catched_up ) { fc_ilog(logger,"head does not catchup"); return; }

uint32_t safe_tslot = my_impl->get_lib_tslot();
ibc_trxs_data_message ret_msg;
ret_msg.table = msg.table;
Expand Down Expand Up @@ -2933,6 +2949,8 @@ namespace eosio { namespace ibc {
void ibc_plugin_impl::handle_message(const connection_ptr& c, const ibc_trxs_data_message &msg ) {
peer_dlog(c, "received ibc_trxs_data_message, table ${tb}, id range [${f},${t}]", ("tb",msg.table)("f",msg.trxs_rich_info.front().table_id)("t",msg.trxs_rich_info.back().table_id));

if ( ! head_catched_up ) { fc_ilog(logger,"head does not catchup"); return; }

if ( msg.table == N(origtrxs) ) {
for( const auto& trx_info : msg.trxs_rich_info ){
if ( local_origtrxs.size() == 0 ){
Expand Down Expand Up @@ -3005,6 +3023,8 @@ namespace eosio { namespace ibc {
void ibc_plugin_impl::handle_message(const connection_ptr& c, const ibc_block_merkle_path_request_message &msg){
peer_dlog(c, "received ibc_block_merkle_path_request_message, anchor_block_num [${num}]",("num",msg.anchor_block_num));

if ( ! head_catched_up ) { fc_ilog(logger,"head does not catchup"); return; }

ibc_block_merkle_path_data_message ret_msg;
ret_msg.table = msg.table;
ret_msg.anchor_block_num = msg.anchor_block_num;
Expand All @@ -3029,6 +3049,8 @@ namespace eosio { namespace ibc {
void ibc_plugin_impl::handle_message(const connection_ptr& c, const ibc_block_merkle_path_data_message &msg){
peer_dlog(c, "received ibc_block_merkle_path_data_message, anchor_block_num [${num}]",("num",msg.anchor_block_num));

if ( ! head_catched_up ) { fc_ilog(logger,"head does not catchup"); return; }

uint32_t first_block_num = msg.block_merkle_paths.front().first;

if ( msg.table == N(origtrxs) ) {
Expand Down Expand Up @@ -3118,7 +3140,7 @@ namespace eosio { namespace ibc {
bool ibc_plugin_impl::is_head_catchup() {
auto head_block_time_point = fc::time_point( chain_plug->chain().fork_db_head_block_time() );
return head_block_time_point < fc::time_point::now() + fc::seconds(3) &&
head_block_time_point > fc::time_point::now() - fc::seconds(5);
head_block_time_point > fc::time_point::now() - fc::seconds(3);
}

bool ibc_plugin_impl::should_send_ibc_heartbeat(){
Expand All @@ -3129,7 +3151,8 @@ namespace eosio { namespace ibc {
// }

// check if local head catch up
if ( !is_head_catchup() ){
head_catched_up = is_head_catchup();
if ( ! head_catched_up ){
fc_ilog(logger,"local chain head doesn't catch up current chain head, waiting...");
return false;
}
Expand Down

0 comments on commit 0f10014

Please sign in to comment.