From 1ec9d55454118c1410266ba689b60b779451c573 Mon Sep 17 00:00:00 2001 From: yarkin Date: Thu, 25 Apr 2024 14:44:31 +0800 Subject: [PATCH 1/4] Handle corner case where lib happen to be on the edge of a eos block gap. --- src/engine_plugin.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/engine_plugin.cpp b/src/engine_plugin.cpp index c6e5f4f..e6f3e13 100644 --- a/src/engine_plugin.cpp +++ b/src/engine_plugin.cpp @@ -135,6 +135,28 @@ class engine_plugin_impl : std::enable_shared_from_this { return silkworm::db::read_canonical_header(txn, head_num); } + std::optional find_block_with_valid_eos_id(uint64_t start_height) { + // Search for the last block with a valid eos id in it. + // The search is not that optimized. However, this function will only be called during initialization process + // in rara cases and the gap in eos will not be too large in most cases. Therefore, it should be fine to leave + // the search process simple. + SILK_INFO << "Search for block containing a valid eos id. Start from:" << "#" << start_height; + silkworm::db::ROTxn txn(db_env); + do { + auto res = silkworm::db::read_canonical_header(txn, start_height); + if(!res) return {}; + + for (int i = 0; i < 32; ++i) { + if (res->prev_randao.bytes[i]) { + SILK_INFO << "Found block at: " << "#" << start_height; + return res; + } + } + } + while(--start_height > 0); + return {}; + } + std::optional get_canonical_block_at_height(std::optional height) { uint64_t target = 0; SILK_INFO << "Determining effective canonical header."; @@ -164,10 +186,19 @@ class engine_plugin_impl : std::enable_shared_from_this { SILK_INFO << "Canonical header at: " << "#" << target; } } + + // Make sure block returned from this function has a proper eos_id in it. + auto new_header = find_block_with_valid_eos_id(target); + if (!new_header) { + SILK_INFO << "Failed to find proper canonical header"; + return {}; + } + target = new_header->number; } else { // Do not check canonical header or lib. // If there's anything wrong, overriding here has some chance to fix it. + // Note that we even skip valid eos id check just in case we are trying to force recovery into that state. target = *height; SILK_INFO << "Command line options set the canonical height as " << "#" << target; } From ef3593b8e8ff5d2dac10132f27b0eba3520a7bcc Mon Sep 17 00:00:00 2001 From: yarkin Date: Sun, 28 Apr 2024 15:41:17 +0800 Subject: [PATCH 2/4] Check if block has valid eos id in it even when user input the target block via command line. --- src/engine_plugin.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/engine_plugin.cpp b/src/engine_plugin.cpp index e6f3e13..9d26035 100644 --- a/src/engine_plugin.cpp +++ b/src/engine_plugin.cpp @@ -186,23 +186,22 @@ class engine_plugin_impl : std::enable_shared_from_this { SILK_INFO << "Canonical header at: " << "#" << target; } } - - // Make sure block returned from this function has a proper eos_id in it. - auto new_header = find_block_with_valid_eos_id(target); - if (!new_header) { - SILK_INFO << "Failed to find proper canonical header"; - return {}; - } - target = new_header->number; } else { // Do not check canonical header or lib. // If there's anything wrong, overriding here has some chance to fix it. - // Note that we even skip valid eos id check just in case we are trying to force recovery into that state. target = *height; SILK_INFO << "Command line options set the canonical height as " << "#" << target; } + // Make sure block returned from this function has a proper eos_id in it. + auto new_header = find_block_with_valid_eos_id(target); + if (!new_header) { + SILK_INFO << "Failed to find proper canonical header"; + return {}; + } + target = new_header->number; + silkworm::db::ROTxn txn(db_env); silkworm::Block block; auto res = read_block_by_number(txn, target, false, block); From 1108b05a909089b1f8c85b8d18ab4c80ad796aeb Mon Sep 17 00:00:00 2001 From: yarkin Date: Tue, 7 May 2024 08:25:46 +0800 Subject: [PATCH 3/4] Refector to make code clear. --- src/engine_plugin.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/engine_plugin.cpp b/src/engine_plugin.cpp index 9d26035..7b875cf 100644 --- a/src/engine_plugin.cpp +++ b/src/engine_plugin.cpp @@ -142,19 +142,15 @@ class engine_plugin_impl : std::enable_shared_from_this { // the search process simple. SILK_INFO << "Search for block containing a valid eos id. Start from:" << "#" << start_height; silkworm::db::ROTxn txn(db_env); - do { - auto res = silkworm::db::read_canonical_header(txn, start_height); - if(!res) return {}; - - for (int i = 0; i < 32; ++i) { - if (res->prev_randao.bytes[i]) { - SILK_INFO << "Found block at: " << "#" << start_height; - return res; - } + std::optional res; + while(start_height>0) { + res = silkworm::db::read_canonical_header(txn, start_height); + if(res && !is_zero(res->prev_randao)) { + break; } + --start_height; } - while(--start_height > 0); - return {}; + return res; } std::optional get_canonical_block_at_height(std::optional height) { From 74a52396592f89f4a1131c1973c857879f921536 Mon Sep 17 00:00:00 2001 From: yarkin Date: Tue, 7 May 2024 18:28:07 +0800 Subject: [PATCH 4/4] Handle genesis case properly. --- src/engine_plugin.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/engine_plugin.cpp b/src/engine_plugin.cpp index 7b875cf..dc5bc29 100644 --- a/src/engine_plugin.cpp +++ b/src/engine_plugin.cpp @@ -143,13 +143,12 @@ class engine_plugin_impl : std::enable_shared_from_this { SILK_INFO << "Search for block containing a valid eos id. Start from:" << "#" << start_height; silkworm::db::ROTxn txn(db_env); std::optional res; - while(start_height>0) { + do { res = silkworm::db::read_canonical_header(txn, start_height); if(res && !is_zero(res->prev_randao)) { break; } - --start_height; - } + } while(start_height-- > 0); return res; }