Skip to content

Commit

Permalink
node, rpcdaemon: add read total difficulty to chain storage (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat committed Jul 7, 2023
1 parent 6abe931 commit dae98b3
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 6 deletions.
12 changes: 12 additions & 0 deletions silkworm/node/db/access_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<intx::uint256> DataModel::read_total_difficulty(BlockNum height, const evmc::bytes32& hash) const {
return db::read_total_difficulty(txn_, height, hash);
}

std::optional<intx::uint256> DataModel::read_total_difficulty(BlockNum height, HashAsArray hash) const {
return db::read_total_difficulty(txn_, height, hash);
}

std::optional<intx::uint256> DataModel::read_total_difficulty(ByteView key) const {
return db::read_total_difficulty(txn_, key);
}

} // namespace silkworm::db
5 changes: 5 additions & 0 deletions silkworm/node/db/access_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bytes>& rlp_txs) const;

//! Read total difficulty at specified height
[[nodiscard]] std::optional<intx::uint256> read_total_difficulty(BlockNum height, const evmc::bytes32& hash) const;
[[nodiscard]] std::optional<intx::uint256> read_total_difficulty(BlockNum, HashAsArray hash) const;
[[nodiscard]] std::optional<intx::uint256> read_total_difficulty(ByteView key) const;

private:
static bool read_block_from_snapshot(BlockNum height, bool read_senders, Block& block);
static std::optional<BlockHeader> read_header_from_snapshot(BlockNum height);
Expand Down
2 changes: 2 additions & 0 deletions silkworm/node/db/access_layer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
4 changes: 3 additions & 1 deletion silkworm/node/storage/chain_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#pragma once

#include <optional>

#include <silkworm/infra/concurrency/task.hpp>

#include <silkworm/core/common/base.hpp>
Expand Down Expand Up @@ -83,7 +85,7 @@ class ChainStorage {
virtual Task<bool> read_rlp_transactions(BlockNum number, const evmc::bytes32& hash, std::vector<Bytes>& rlp_txs) const = 0;

//! Read total difficulty for block specified by hash and number
[[nodiscard]] virtual Task<intx::uint256> read_total_difficulty(const Hash& block_hash, BlockNum block_number) const = 0;
[[nodiscard]] virtual Task<std::optional<intx::uint256>> read_total_difficulty(const Hash& block_hash, BlockNum block_number) const = 0;

// Task<uint64_t> read_block_number_by_transaction_hash(const evmc::bytes32& transaction_hash);

Expand Down
4 changes: 2 additions & 2 deletions silkworm/node/storage/local_chain_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ Task<bool> LocalChainStorage::read_rlp_transactions(BlockNum number, const evmc:
co_return data_model_.read_rlp_transactions(number, hash, rlp_txs);
}

Task<intx::uint256> LocalChainStorage::read_total_difficulty(const Hash& /*block_hash*/, BlockNum /*block_number*/) const {
throw std::logic_error{"LocalChainStorage::read_total_difficulty"};
Task<std::optional<intx::uint256>> LocalChainStorage::read_total_difficulty(const Hash& hash, BlockNum number) const {
co_return data_model_.read_total_difficulty(number, hash);
}

} // namespace silkworm::node
2 changes: 1 addition & 1 deletion silkworm/node/storage/local_chain_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class LocalChainStorage : public ChainStorage {

Task<bool> read_rlp_transactions(BlockNum number, const evmc::bytes32& hash, std::vector<Bytes>& rlp_txs) const override;

[[nodiscard]] Task<intx::uint256> read_total_difficulty(const Hash& block_hash, BlockNum block_number) const override;
[[nodiscard]] Task<std::optional<intx::uint256>> read_total_difficulty(const Hash& block_hash, BlockNum block_number) const override;

private:
db::DataModel data_model_;
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/storage/remote_chain_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Task<bool> RemoteChainStorage::read_rlp_transactions(BlockNum number, const evmc
co_return true;
}

Task<intx::uint256> RemoteChainStorage::read_total_difficulty(const Hash& /*block_hash*/, BlockNum /*block_number*/) const {
Task<std::optional<intx::uint256>> RemoteChainStorage::read_total_difficulty(const Hash& /*block_hash*/, BlockNum /*block_number*/) const {
throw std::logic_error{"RemoteChainStorage::read_total_difficulty"};
}

Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/storage/remote_chain_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class RemoteChainStorage : public node::ChainStorage {

Task<bool> read_rlp_transactions(BlockNum number, const evmc::bytes32& hash, std::vector<Bytes>& rlp_txs) const override;

[[nodiscard]] Task<intx::uint256> read_total_difficulty(const Hash& block_hash, BlockNum block_number) const override;
[[nodiscard]] Task<std::optional<intx::uint256>> read_total_difficulty(const Hash& block_hash, BlockNum block_number) const override;

private:
const DatabaseReader& reader_;
Expand Down

0 comments on commit dae98b3

Please sign in to comment.