From dae98b38ac512deb2f521cc4c1fd13a73a6e6dee Mon Sep 17 00:00:00 2001 From: canepat <16927169+canepat@users.noreply.github.com> Date: Fri, 7 Jul 2023 13:50:00 +0100 Subject: [PATCH] node, rpcdaemon: add read total difficulty to chain storage (#1317) --- silkworm/node/db/access_layer.cpp | 12 ++++++++++++ silkworm/node/db/access_layer.hpp | 5 +++++ silkworm/node/db/access_layer_test.cpp | 2 ++ silkworm/node/storage/chain_storage.hpp | 4 +++- silkworm/node/storage/local_chain_storage.cpp | 4 ++-- silkworm/node/storage/local_chain_storage.hpp | 2 +- silkworm/silkrpc/storage/remote_chain_storage.cpp | 2 +- silkworm/silkrpc/storage/remote_chain_storage.hpp | 2 +- 8 files changed, 27 insertions(+), 6 deletions(-) diff --git a/silkworm/node/db/access_layer.cpp b/silkworm/node/db/access_layer.cpp index 79156a2245..f2fa399f6a 100644 --- a/silkworm/node/db/access_layer.cpp +++ b/silkworm/node/db/access_layer.cpp @@ -1206,4 +1206,16 @@ bool DataModel::read_rlp_transactions(BlockNum height, const evmc::bytes32& hash return read_rlp_transactions_from_snapshot(height, transactions); } +std::optional DataModel::read_total_difficulty(BlockNum height, const evmc::bytes32& hash) const { + return db::read_total_difficulty(txn_, height, hash); +} + +std::optional DataModel::read_total_difficulty(BlockNum height, HashAsArray hash) const { + return db::read_total_difficulty(txn_, height, hash); +} + +std::optional DataModel::read_total_difficulty(ByteView key) const { + return db::read_total_difficulty(txn_, key); +} + } // namespace silkworm::db diff --git a/silkworm/node/db/access_layer.hpp b/silkworm/node/db/access_layer.hpp index a88482f659..26ed1fc2e4 100644 --- a/silkworm/node/db/access_layer.hpp +++ b/silkworm/node/db/access_layer.hpp @@ -303,6 +303,11 @@ class DataModel { //! Read the RLP encoded block transactions at specified height [[nodiscard]] bool read_rlp_transactions(BlockNum height, const evmc::bytes32& hash, std::vector& rlp_txs) const; + //! Read total difficulty at specified height + [[nodiscard]] std::optional read_total_difficulty(BlockNum height, const evmc::bytes32& hash) const; + [[nodiscard]] std::optional read_total_difficulty(BlockNum, HashAsArray hash) const; + [[nodiscard]] std::optional read_total_difficulty(ByteView key) const; + private: static bool read_block_from_snapshot(BlockNum height, bool read_senders, Block& block); static std::optional read_header_from_snapshot(BlockNum height); diff --git a/silkworm/node/db/access_layer_test.cpp b/silkworm/node/db/access_layer_test.cpp index 3016aa0224..a64d03fd17 100644 --- a/silkworm/node/db/access_layer_test.cpp +++ b/silkworm/node/db/access_layer_test.cpp @@ -445,6 +445,8 @@ TEST_CASE("Difficulty") { write_total_difficulty(txn, block_num, hash, difficulty); CHECK(read_total_difficulty(txn, block_num, hash) == difficulty); + DataModel model{txn}; + CHECK(model.read_total_difficulty(block_num, hash) == difficulty); } TEST_CASE("Headers and bodies") { diff --git a/silkworm/node/storage/chain_storage.hpp b/silkworm/node/storage/chain_storage.hpp index 55a4cda3b7..a021cb4e0a 100644 --- a/silkworm/node/storage/chain_storage.hpp +++ b/silkworm/node/storage/chain_storage.hpp @@ -16,6 +16,8 @@ #pragma once +#include + #include #include @@ -83,7 +85,7 @@ class ChainStorage { virtual Task read_rlp_transactions(BlockNum number, const evmc::bytes32& hash, std::vector& rlp_txs) const = 0; //! Read total difficulty for block specified by hash and number - [[nodiscard]] virtual Task read_total_difficulty(const Hash& block_hash, BlockNum block_number) const = 0; + [[nodiscard]] virtual Task> read_total_difficulty(const Hash& block_hash, BlockNum block_number) const = 0; // Task read_block_number_by_transaction_hash(const evmc::bytes32& transaction_hash); diff --git a/silkworm/node/storage/local_chain_storage.cpp b/silkworm/node/storage/local_chain_storage.cpp index 612af45527..1e8722dbcd 100644 --- a/silkworm/node/storage/local_chain_storage.cpp +++ b/silkworm/node/storage/local_chain_storage.cpp @@ -112,8 +112,8 @@ Task LocalChainStorage::read_rlp_transactions(BlockNum number, const evmc: co_return data_model_.read_rlp_transactions(number, hash, rlp_txs); } -Task LocalChainStorage::read_total_difficulty(const Hash& /*block_hash*/, BlockNum /*block_number*/) const { - throw std::logic_error{"LocalChainStorage::read_total_difficulty"}; +Task> LocalChainStorage::read_total_difficulty(const Hash& hash, BlockNum number) const { + co_return data_model_.read_total_difficulty(number, hash); } } // namespace silkworm::node diff --git a/silkworm/node/storage/local_chain_storage.hpp b/silkworm/node/storage/local_chain_storage.hpp index c766f7e290..ff550c44f5 100644 --- a/silkworm/node/storage/local_chain_storage.hpp +++ b/silkworm/node/storage/local_chain_storage.hpp @@ -65,7 +65,7 @@ class LocalChainStorage : public ChainStorage { Task read_rlp_transactions(BlockNum number, const evmc::bytes32& hash, std::vector& rlp_txs) const override; - [[nodiscard]] Task read_total_difficulty(const Hash& block_hash, BlockNum block_number) const override; + [[nodiscard]] Task> read_total_difficulty(const Hash& block_hash, BlockNum block_number) const override; private: db::DataModel data_model_; diff --git a/silkworm/silkrpc/storage/remote_chain_storage.cpp b/silkworm/silkrpc/storage/remote_chain_storage.cpp index 5d97d7ff2e..2c0681c925 100644 --- a/silkworm/silkrpc/storage/remote_chain_storage.cpp +++ b/silkworm/silkrpc/storage/remote_chain_storage.cpp @@ -181,7 +181,7 @@ Task RemoteChainStorage::read_rlp_transactions(BlockNum number, const evmc co_return true; } -Task RemoteChainStorage::read_total_difficulty(const Hash& /*block_hash*/, BlockNum /*block_number*/) const { +Task> RemoteChainStorage::read_total_difficulty(const Hash& /*block_hash*/, BlockNum /*block_number*/) const { throw std::logic_error{"RemoteChainStorage::read_total_difficulty"}; } diff --git a/silkworm/silkrpc/storage/remote_chain_storage.hpp b/silkworm/silkrpc/storage/remote_chain_storage.hpp index 26e65d2a7e..4de07603c4 100644 --- a/silkworm/silkrpc/storage/remote_chain_storage.hpp +++ b/silkworm/silkrpc/storage/remote_chain_storage.hpp @@ -68,7 +68,7 @@ class RemoteChainStorage : public node::ChainStorage { Task read_rlp_transactions(BlockNum number, const evmc::bytes32& hash, std::vector& rlp_txs) const override; - [[nodiscard]] Task read_total_difficulty(const Hash& block_hash, BlockNum block_number) const override; + [[nodiscard]] Task> read_total_difficulty(const Hash& block_hash, BlockNum block_number) const override; private: const DatabaseReader& reader_;