From bfb2659d2fa023ef36c297144e555fd5c9b124ac Mon Sep 17 00:00:00 2001 From: battlmonstr Date: Wed, 13 Mar 2024 09:36:57 +0100 Subject: [PATCH] db: replace HeadInfo with ChainHead --- cmd/dev/backend_kv_server.cpp | 8 ++-- cmd/silkworm.cpp | 8 ++-- silkworm/core/types/block.cpp | 2 - silkworm/core/types/block.hpp | 34 ------------- .../types/{head_info.hpp => block_id.hpp} | 13 +++-- silkworm/core/types/chain_head.hpp | 48 +++++++++++++++++++ silkworm/db/{head_info.cpp => chain_head.cpp} | 24 +++++----- silkworm/db/{head_info.hpp => chain_head.hpp} | 6 +-- silkworm/node/stagedsync/client.hpp | 1 + .../node/stagedsync/forks/canonical_chain.hpp | 1 + silkworm/sentry/eth/status_data_provider.cpp | 22 ++++----- silkworm/sentry/eth/status_data_provider.hpp | 10 ++-- silkworm/sync/internals/chain_fork_view.hpp | 1 + .../internals/header_chain_plus_exec_test.cpp | 2 +- silkworm/sync/sync_pow.cpp | 4 +- 15 files changed, 99 insertions(+), 85 deletions(-) rename silkworm/core/types/{head_info.hpp => block_id.hpp} (78%) create mode 100644 silkworm/core/types/chain_head.hpp rename silkworm/db/{head_info.cpp => chain_head.cpp} (66%) rename silkworm/db/{head_info.hpp => chain_head.hpp} (83%) diff --git a/cmd/dev/backend_kv_server.cpp b/cmd/dev/backend_kv_server.cpp index 77f2ebeb83..c0c36088a6 100644 --- a/cmd/dev/backend_kv_server.cpp +++ b/cmd/dev/backend_kv_server.cpp @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include #include @@ -116,10 +116,10 @@ std::shared_ptr make_sentry_client( db::ROAccess db_access) { std::shared_ptr sentry_client; - auto head_info_provider = [db_access = std::move(db_access)]() { - return db::read_head_info(db_access); + auto chain_head_provider = [db_access = std::move(db_access)]() { + return db::read_chain_head(db_access); }; - silkworm::sentry::eth::StatusDataProvider eth_status_data_provider{std::move(head_info_provider), node_settings.chain_config.value()}; + silkworm::sentry::eth::StatusDataProvider eth_status_data_provider{std::move(chain_head_provider), node_settings.chain_config.value()}; if (node_settings.remote_sentry_addresses.empty()) { assert(false); diff --git a/cmd/silkworm.cpp b/cmd/silkworm.cpp index 36a10b3299..17ef54f957 100644 --- a/cmd/silkworm.cpp +++ b/cmd/silkworm.cpp @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include #include @@ -278,10 +278,10 @@ int main(int argc, char* argv[]) { settings.sentry_settings.data_dir_path = node_settings.data_directory->path(); settings.sentry_settings.network_id = node_settings.network_id; - auto head_info_provider = [db_access = db::ROAccess{chaindata_db}] { - return db::read_head_info(db_access); + auto chain_head_provider = [db_access = db::ROAccess{chaindata_db}] { + return db::read_chain_head(db_access); }; - sentry::eth::StatusDataProvider eth_status_data_provider{std::move(head_info_provider), node_settings.chain_config.value()}; + sentry::eth::StatusDataProvider eth_status_data_provider{std::move(chain_head_provider), node_settings.chain_config.value()}; auto [sentry_client, sentry_server] = sentry::SentryClientFactory::make_sentry( std::move(settings.sentry_settings), diff --git a/silkworm/core/types/block.cpp b/silkworm/core/types/block.cpp index e39093c9b4..b6c0525649 100644 --- a/silkworm/core/types/block.cpp +++ b/silkworm/core/types/block.cpp @@ -26,8 +26,6 @@ namespace silkworm { -BlockNum height(const BlockId& b) { return b.number; } - evmc::bytes32 BlockHeader::hash(bool for_sealing, bool exclude_extra_data_sig) const { Bytes rlp; rlp::encode(rlp, *this, for_sealing, exclude_extra_data_sig); diff --git a/silkworm/core/types/block.hpp b/silkworm/core/types/block.hpp index d15585f78a..255e18e49e 100644 --- a/silkworm/core/types/block.hpp +++ b/silkworm/core/types/block.hpp @@ -36,19 +36,6 @@ namespace silkworm { using TotalDifficulty = intx::uint256; -struct BlockId { // TODO(canepat) rename BlockNumberAndHash - BlockNum number{}; - Hash hash; -}; - -BlockNum height(const BlockId& b); - -struct ChainHead { - BlockNum height{}; - Hash hash; - TotalDifficulty total_difficulty{}; -}; - struct BlockHeader { using NonceType = std::array; @@ -124,25 +111,4 @@ namespace rlp { DecodingResult decode(ByteView& from, Block& to, Leftover mode = Leftover::kProhibit) noexcept; } // namespace rlp -// Comparison operator == -inline bool operator==(const BlockId& a, const BlockId& b) { - return a.number == b.number && a.hash == b.hash; -} - -inline bool operator==(const ChainHead& a, const BlockId& b) { - return a.height == b.number && a.hash == b.hash; -} - -inline bool operator==(const BlockId& a, const ChainHead& b) { - return a.number == b.height && a.hash == b.hash; -} - -inline bool operator==(const ChainHead& a, const ChainHead& b) { - return a.height == b.height && a.hash == b.hash && a.total_difficulty == b.total_difficulty; -} - -inline BlockId to_BlockId(const ChainHead& head) { - return {.number = head.height, .hash = head.hash}; -} - } // namespace silkworm diff --git a/silkworm/core/types/head_info.hpp b/silkworm/core/types/block_id.hpp similarity index 78% rename from silkworm/core/types/head_info.hpp rename to silkworm/core/types/block_id.hpp index bba7a62516..830e326d2f 100644 --- a/silkworm/core/types/head_info.hpp +++ b/silkworm/core/types/block_id.hpp @@ -16,18 +16,17 @@ #pragma once -#include - #include - -#include "hash.hpp" +#include namespace silkworm { -struct HeadInfo { - BlockNum block_num{0}; +// TODO(canepat) rename BlockNumberAndHash +struct BlockId { + BlockNum number{}; Hash hash; - intx::uint256 total_difficulty; + + friend bool operator==(const BlockId&, const BlockId&) = default; }; } // namespace silkworm diff --git a/silkworm/core/types/chain_head.hpp b/silkworm/core/types/chain_head.hpp new file mode 100644 index 0000000000..93da75f50a --- /dev/null +++ b/silkworm/core/types/chain_head.hpp @@ -0,0 +1,48 @@ +/* + Copyright 2024 The Silkworm Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#pragma once + +#include + +#include + +#include "block_id.hpp" +#include "hash.hpp" + +namespace silkworm { + +struct ChainHead { + BlockNum height{0}; + Hash hash; + intx::uint256 total_difficulty; + + friend bool operator==(const ChainHead&, const ChainHead&) = default; +}; + +inline bool operator==(const ChainHead& a, const BlockId& b) { + return a.height == b.number && a.hash == b.hash; +} + +inline bool operator==(const BlockId& a, const ChainHead& b) { + return a.number == b.height && a.hash == b.hash; +} + +inline BlockId to_BlockId(const ChainHead& head) { + return {.number = head.height, .hash = head.hash}; +} + +} // namespace silkworm diff --git a/silkworm/db/head_info.cpp b/silkworm/db/chain_head.cpp similarity index 66% rename from silkworm/db/head_info.cpp rename to silkworm/db/chain_head.cpp index b372e15f73..aa7e24c32f 100644 --- a/silkworm/db/head_info.cpp +++ b/silkworm/db/chain_head.cpp @@ -14,7 +14,7 @@ limitations under the License. */ -#include "head_info.hpp" +#include "chain_head.hpp" #include @@ -24,35 +24,35 @@ namespace silkworm::db { -HeadInfo read_head_info(ROTxn& txn) { - HeadInfo head_info; +ChainHead read_chain_head(ROTxn& txn) { + ChainHead chain_head; BlockNum head_height = db::stages::read_stage_progress(txn, db::stages::kBlockBodiesKey); - head_info.block_num = head_height; + chain_head.height = head_height; auto head_hash = db::read_canonical_hash(txn, head_height); if (head_hash) { - head_info.hash = head_hash.value(); + chain_head.hash = head_hash.value(); } else { - log::Warning("db::HeadInfo") << "canonical hash at height " << std::to_string(head_height) << " not found in db"; - return head_info; + log::Warning("db::ChainHead") << "canonical hash at height " << std::to_string(head_height) << " not found in db"; + return chain_head; } auto head_total_difficulty = db::read_total_difficulty(txn, head_height, *head_hash); if (head_total_difficulty) { - head_info.total_difficulty = head_total_difficulty.value(); + chain_head.total_difficulty = head_total_difficulty.value(); } else { - log::Warning("db::HeadInfo") << "total difficulty of canonical hash at height " << std::to_string(head_height) << " not found in db"; + log::Warning("db::ChainHead") << "total difficulty of canonical hash at height " << std::to_string(head_height) << " not found in db"; } - return head_info; + return chain_head; } -HeadInfo read_head_info(db::ROAccess db_access) { +ChainHead read_chain_head(db::ROAccess db_access) { auto txn = db_access.start_ro_tx(); [[maybe_unused]] auto _ = gsl::finally([&txn] { txn.abort(); }); - return read_head_info(txn); + return read_chain_head(txn); } } // namespace silkworm::db diff --git a/silkworm/db/head_info.hpp b/silkworm/db/chain_head.hpp similarity index 83% rename from silkworm/db/head_info.hpp rename to silkworm/db/chain_head.hpp index 3faf80aae5..fe17cd292a 100644 --- a/silkworm/db/head_info.hpp +++ b/silkworm/db/chain_head.hpp @@ -16,12 +16,12 @@ #pragma once -#include +#include #include namespace silkworm::db { -HeadInfo read_head_info(ROTxn& txn); -HeadInfo read_head_info(db::ROAccess db_access); +ChainHead read_chain_head(ROTxn& txn); +ChainHead read_chain_head(db::ROAccess db_access); } // namespace silkworm::db diff --git a/silkworm/node/stagedsync/client.hpp b/silkworm/node/stagedsync/client.hpp index bdfe0c243d..86af89ff3e 100644 --- a/silkworm/node/stagedsync/client.hpp +++ b/silkworm/node/stagedsync/client.hpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace silkworm::execution { diff --git a/silkworm/node/stagedsync/forks/canonical_chain.hpp b/silkworm/node/stagedsync/forks/canonical_chain.hpp index f81fd73f45..acd1648956 100644 --- a/silkworm/node/stagedsync/forks/canonical_chain.hpp +++ b/silkworm/node/stagedsync/forks/canonical_chain.hpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include diff --git a/silkworm/sentry/eth/status_data_provider.cpp b/silkworm/sentry/eth/status_data_provider.cpp index a21f8706e8..3866697b84 100644 --- a/silkworm/sentry/eth/status_data_provider.cpp +++ b/silkworm/sentry/eth/status_data_provider.cpp @@ -24,9 +24,9 @@ namespace silkworm::sentry::eth { -static void log_head_info(const HeadInfo& info) { +static void log_chain_head(const ChainHead& info) { log::Debug( - "StatusDataProvider::HeadInfo", + "StatusDataProvider::ChainHead", { "head hash", info.hash.to_hex(), @@ -35,32 +35,32 @@ static void log_head_info(const HeadInfo& info) { intx::to_string(info.total_difficulty), "head block num", - std::to_string(info.block_num), + std::to_string(info.height), }); } StatusDataProvider::StatusData StatusDataProvider::make_status_data( - HeadInfo head_info, + ChainHead chain_head, uint8_t eth_version, const ChainConfig& chain_config) { auto fork_numbers = chain_config.distinct_fork_numbers(); auto fork_times = chain_config.distinct_fork_times(); - auto best_block_hash = Bytes{ByteView{head_info.hash}}; + auto best_block_hash = Bytes{ByteView{chain_head.hash}}; auto genesis_hash = ByteView{chain_config.genesis_hash.value()}; silkworm::sentry::eth::StatusMessage status_message = { eth_version, chain_config.chain_id, - head_info.total_difficulty, + chain_head.total_difficulty, best_block_hash, Bytes{genesis_hash}, - silkworm::sentry::eth::ForkId(genesis_hash, fork_numbers, fork_times, head_info.block_num), + silkworm::sentry::eth::ForkId(genesis_hash, fork_numbers, fork_times, chain_head.height), }; silkworm::sentry::eth::StatusData status_data = { std::move(fork_numbers), std::move(fork_times), - head_info.block_num, + chain_head.height, std::move(status_message), }; @@ -72,10 +72,10 @@ StatusDataProvider::StatusData StatusDataProvider::get_status_data(uint8_t eth_v throw std::runtime_error("StatusDataProvider::get_status_data: unsupported eth version " + std::to_string(eth_version)); } - auto head_info = head_info_provider_(); - log_head_info(head_info); + auto chain_head = chain_head_provider_(); + log_chain_head(chain_head); - return make_status_data(head_info, eth_version, chain_config_); + return make_status_data(chain_head, eth_version, chain_config_); } StatusDataProvider::StatusDataProviderFactory StatusDataProvider::to_factory_function() { diff --git a/silkworm/sentry/eth/status_data_provider.hpp b/silkworm/sentry/eth/status_data_provider.hpp index 3ee85c8abb..8e6035af49 100644 --- a/silkworm/sentry/eth/status_data_provider.hpp +++ b/silkworm/sentry/eth/status_data_provider.hpp @@ -21,7 +21,7 @@ #include #include -#include +#include #include "status_data.hpp" @@ -30,9 +30,9 @@ namespace silkworm::sentry::eth { class StatusDataProvider { public: StatusDataProvider( - std::function head_info_provider, + std::function chain_head_provider, const ChainConfig& chain_config) - : head_info_provider_(std::move(head_info_provider)), + : chain_head_provider_(std::move(chain_head_provider)), chain_config_(chain_config) {} using StatusData = silkworm::sentry::eth::StatusData; @@ -43,11 +43,11 @@ class StatusDataProvider { private: static StatusData make_status_data( - HeadInfo head_info, + ChainHead chain_head, uint8_t eth_version, const ChainConfig& chain_config); - std::function head_info_provider_; + std::function chain_head_provider_; const ChainConfig& chain_config_; }; diff --git a/silkworm/sync/internals/chain_fork_view.hpp b/silkworm/sync/internals/chain_fork_view.hpp index bc622da4bc..1eb033f170 100644 --- a/silkworm/sync/internals/chain_fork_view.hpp +++ b/silkworm/sync/internals/chain_fork_view.hpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace silkworm::chainsync { diff --git a/silkworm/sync/internals/header_chain_plus_exec_test.cpp b/silkworm/sync/internals/header_chain_plus_exec_test.cpp index da857347e0..a8216c0657 100644 --- a/silkworm/sync/internals/header_chain_plus_exec_test.cpp +++ b/silkworm/sync/internals/header_chain_plus_exec_test.cpp @@ -92,7 +92,7 @@ TEST_CASE("Headers receiving and saving") { auto& tx = exec_engine.main_chain_.tx(); // mdbx refuses to open a ROTxn when there is a RWTxn in the same thread auto head = exec_engine.last_fork_choice(); - REQUIRE(height(head) == 0); + REQUIRE(head.number == 0); std::vector last_headers = exec_engine.get_last_headers(1); REQUIRE(last_headers.size() == 1); diff --git a/silkworm/sync/sync_pow.cpp b/silkworm/sync/sync_pow.cpp index 9d078ab5af..54fc82a101 100644 --- a/silkworm/sync/sync_pow.cpp +++ b/silkworm/sync/sync_pow.cpp @@ -51,9 +51,9 @@ PoWSync::NewHeight PoWSync::resume() { // find the point (head) where we left o auto last_fcu = sync_wait(in(exec_engine_), exec_engine_.last_fork_choice()); // previously was get_canonical_head() auto block_progress = sync_wait(in(exec_engine_), exec_engine_.block_progress()); - ensure_invariant(height(last_fcu) <= block_progress, "canonical head beyond block progress"); + ensure_invariant(last_fcu.number <= block_progress, "canonical head beyond block progress"); - if (block_progress == height(last_fcu)) { + if (block_progress == last_fcu.number) { // if fcu and header progress match than we have the actual canonical, we only need to do a forward sync... ensure_invariant(last_fcu == chain_fork_view_.head(), "last fcu misaligned with canonical head"); chain_fork_view_.reset_head({last_fcu.number, last_fcu.hash,