From f1d44d0f5bc0da64c3c44cf9bb801c94589b9736 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Fri, 5 Jan 2024 09:54:53 -0500 Subject: [PATCH 1/4] use the same way to determine if instant finality is active --- libraries/chain/controller.cpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index 5b44b322d4..e7a2214523 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -2796,16 +2796,8 @@ struct controller_impl { // thread safe, expected to be called from thread other than the main thread block_state_legacy_ptr create_block_state_i( const block_id_type& id, const signed_block_ptr& b, const block_header_state_legacy& prev ) { - bool hs_active = false; - if (!b->header_extensions.empty()) { - std::optional instant_finality_ext = b->extract_header_extension(instant_finality_extension::extension_id()); -#warning change to use instant_finality_ext https://github.com/AntelopeIO/leap/issues/1508 - if (instant_finality_ext) { - const auto& ext = std::get(*instant_finality_ext); - hs_active = !!ext.new_proposer_policy; - } - } - + uint32_t hs_lib = hs_irreversible_block_num.load(); + const bool hs_active = hs_lib > 0; auto trx_mroot = calculate_trx_merkle( b->transactions, hs_active ); EOS_ASSERT( b->transaction_mroot == trx_mroot, block_validate_exception, "invalid block transaction merkle root ${b} != ${c}", ("b", b->transaction_mroot)("c", trx_mroot) ); From 7d6b8afa64dc223532f68b4f5bfea99b5d146c5e Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Fri, 5 Jan 2024 15:30:26 -0500 Subject: [PATCH 2/4] fall back validation if the first try fails --- libraries/chain/controller.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index e7a2214523..e427db0ad8 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -2799,8 +2799,13 @@ struct controller_impl { uint32_t hs_lib = hs_irreversible_block_num.load(); const bool hs_active = hs_lib > 0; auto trx_mroot = calculate_trx_merkle( b->transactions, hs_active ); - EOS_ASSERT( b->transaction_mroot == trx_mroot, block_validate_exception, - "invalid block transaction merkle root ${b} != ${c}", ("b", b->transaction_mroot)("c", trx_mroot) ); + if( b->transaction_mroot != trx_mroot ) { + // Call of create_block_state_i can happen right before hs_irreversible_block_num + // is set. Fall back to verify in the other way. + trx_mroot = calculate_trx_merkle( b->transactions, !hs_active ); + EOS_ASSERT( b->transaction_mroot == trx_mroot, block_validate_exception, + "invalid block transaction merkle root ${b} != ${c}", ("b", b->transaction_mroot)("c", trx_mroot) ); + } const bool skip_validate_signee = false; auto bsp = std::make_shared( From 75acf8ab5acd8ac03ca05386bd7faef990a372b3 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Sat, 6 Jan 2024 14:40:03 -0500 Subject: [PATCH 3/4] revert the bad retry when mroot validation fails --- libraries/chain/controller.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index e427db0ad8..26cde3fd65 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -2796,16 +2796,15 @@ struct controller_impl { // thread safe, expected to be called from thread other than the main thread block_state_legacy_ptr create_block_state_i( const block_id_type& id, const signed_block_ptr& b, const block_header_state_legacy& prev ) { - uint32_t hs_lib = hs_irreversible_block_num.load(); - const bool hs_active = hs_lib > 0; - auto trx_mroot = calculate_trx_merkle( b->transactions, hs_active ); - if( b->transaction_mroot != trx_mroot ) { - // Call of create_block_state_i can happen right before hs_irreversible_block_num - // is set. Fall back to verify in the other way. - trx_mroot = calculate_trx_merkle( b->transactions, !hs_active ); - EOS_ASSERT( b->transaction_mroot == trx_mroot, block_validate_exception, + // There is a small race condition at time of activation where create_block_state_i + // is called right before hs_irreversible_block_num is set. If that happens, + // the block is considered invalid, and the node will attempt to sync the block + // in the future and succeed + uint32_t instant_finality_lib = hs_irreversible_block_num.load(); + const bool instant_finality_active = instant_finality_lib > 0; + auto trx_mroot = calculate_trx_merkle( b->transactions, instant_finality_active ); + EOS_ASSERT( b->transaction_mroot == trx_mroot, block_validate_exception, "invalid block transaction merkle root ${b} != ${c}", ("b", b->transaction_mroot)("c", trx_mroot) ); - } const bool skip_validate_signee = false; auto bsp = std::make_shared( From 9f24fd3ca42749c6572eb009ca5fb1b7e916a6d1 Mon Sep 17 00:00:00 2001 From: Lin Huang Date: Sat, 6 Jan 2024 14:42:49 -0500 Subject: [PATCH 4/4] remove extra spaces for alignment --- libraries/chain/controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index 26cde3fd65..938c2d6438 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -2804,7 +2804,7 @@ struct controller_impl { const bool instant_finality_active = instant_finality_lib > 0; auto trx_mroot = calculate_trx_merkle( b->transactions, instant_finality_active ); EOS_ASSERT( b->transaction_mroot == trx_mroot, block_validate_exception, - "invalid block transaction merkle root ${b} != ${c}", ("b", b->transaction_mroot)("c", trx_mroot) ); + "invalid block transaction merkle root ${b} != ${c}", ("b", b->transaction_mroot)("c", trx_mroot) ); const bool skip_validate_signee = false; auto bsp = std::make_shared(