Skip to content

Commit

Permalink
rpcdaemon: remove db::chain read header hash (#2600)
Browse files Browse the repository at this point in the history
  • Loading branch information
lupin012 authored Dec 14, 2024
1 parent eb63c0e commit bb62df8
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 88 deletions.
44 changes: 0 additions & 44 deletions silkworm/db/chain/chain.cpp

This file was deleted.

36 changes: 0 additions & 36 deletions silkworm/db/chain/chain.hpp

This file was deleted.

2 changes: 2 additions & 0 deletions silkworm/db/chain/chain_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class ChainStorage {

virtual Task<std::optional<BlockNum>> read_block_num_by_transaction_hash(const evmc::bytes32& transaction_hash) const = 0;
virtual Task<std::optional<Transaction>> read_transaction_by_idx_in_block(BlockNum block_num, uint64_t txn_id) const = 0;

virtual Task<std::pair<std::optional<BlockHeader>, std::optional<Hash>>> read_head_header_and_hash() const = 0;
};

} // namespace silkworm::db::chain
4 changes: 4 additions & 0 deletions silkworm/db/chain/local_chain_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,8 @@ Task<std::optional<Transaction>> LocalChainStorage::read_transaction_by_idx_in_b
throw std::runtime_error{"not yet implemented"};
}

Task<std::pair<std::optional<BlockHeader>, std::optional<Hash>>> LocalChainStorage::read_head_header_and_hash() const {
co_return data_model_.read_head_header_and_hash();
}

} // namespace silkworm::db::chain
2 changes: 2 additions & 0 deletions silkworm/db/chain/local_chain_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class LocalChainStorage : public ChainStorage {
Task<std::optional<BlockNum>> read_block_num_by_transaction_hash(const evmc::bytes32& transaction_hash) const override;
Task<std::optional<Transaction>> read_transaction_by_idx_in_block(BlockNum block_num, uint64_t txn_id) const override;

Task<std::pair<std::optional<BlockHeader>, std::optional<Hash>>> read_head_header_and_hash() const override;

private:
db::DataModel data_model_;
};
Expand Down
19 changes: 17 additions & 2 deletions silkworm/db/chain/remote_chain_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
#include <utility>

#include <silkworm/core/common/base.hpp>
#include <silkworm/core/common/bytes_to_string.hpp>
#include <silkworm/core/common/util.hpp>
#include <silkworm/core/types/evmc_bytes32.hpp>
#include <silkworm/db/tables.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/infra/grpc/common/conversion.hpp>

#include "chain.hpp"

namespace silkworm::db::chain {

RemoteChainStorage::RemoteChainStorage(kv::api::Transaction& tx, Providers providers)
Expand Down Expand Up @@ -239,4 +239,19 @@ Task<std::optional<Transaction>> RemoteChainStorage::read_transaction_by_idx_in_
co_return body.transactions[txn_id];
}

Task<std::pair<std::optional<BlockHeader>, std::optional<Hash>>> RemoteChainStorage::read_head_header_and_hash() const {
const auto value = co_await tx_.get_one(table::kHeadHeaderName, string_to_bytes(table::kHeadHeaderName));
if (value.empty()) {
throw std::runtime_error{"empty head header hash value in read_head_header_hash"};
}
const auto head_header_hash{to_bytes32(value)};
SILK_DEBUG << "head header hash: " << to_hex(head_header_hash);

auto header = co_await read_header(head_header_hash);

Hash header_hash{head_header_hash};

co_return std::pair{std::move(header), std::move(header_hash)};
}

} // namespace silkworm::db::chain
4 changes: 4 additions & 0 deletions silkworm/db/chain/remote_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/db/chain/providers.hpp>
#include <silkworm/db/kv/api/transaction.hpp>

Expand Down Expand Up @@ -69,6 +71,8 @@ class RemoteChainStorage : public ChainStorage {
Task<std::optional<BlockNum>> read_block_num_by_transaction_hash(const evmc::bytes32& transaction_hash) const override;
Task<std::optional<Transaction>> read_transaction_by_idx_in_block(BlockNum block_num, uint64_t txn_id) const override;

Task<std::pair<std::optional<BlockHeader>, std::optional<Hash>>> read_head_header_and_hash() const override;

protected:
Providers& providers() { return providers_; }

Expand Down
2 changes: 2 additions & 0 deletions silkworm/db/test_util/mock_chain_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class MockChainStorage : public chain::ChainStorage {

MOCK_METHOD((Task<std::optional<BlockNum>>), read_block_num_by_transaction_hash, (const evmc::bytes32&), (const, override));
MOCK_METHOD((Task<std::optional<Transaction>>), read_transaction_by_idx_in_block, (BlockNum, uint64_t), (const, override));

MOCK_METHOD((Task<std::pair<std::optional<BlockHeader>, std::optional<Hash>>>), read_head_header_and_hash, (), (const, override));
};

} // namespace silkworm::db::test_util
6 changes: 1 addition & 5 deletions silkworm/rpc/commands/erigon_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include <silkworm/core/common/base.hpp>
#include <silkworm/core/types/evmc_bytes32.hpp>
#include <silkworm/db/chain/chain.hpp>
#include <silkworm/infra/common/async_binary_search.hpp>
#include <silkworm/infra/common/ensure.hpp>
#include <silkworm/infra/common/log.hpp>
Expand Down Expand Up @@ -123,10 +122,7 @@ Task<void> ErigonRpcApi::handle_erigon_get_block_by_timestamp(const nlohmann::js
// Lookup the first and last block headers
const auto first_header = co_await chain_storage->read_canonical_header(kEarliestBlockNum);
ensure(first_header.has_value(), "cannot find earliest header");
const auto head_header_hash = co_await db::chain::read_head_header_hash(*tx);
const auto head_header_block_num = co_await chain_storage->read_block_num(head_header_hash);
ensure(head_header_block_num.has_value(), "cannot find head header hash");
const auto current_header = co_await chain_storage->read_header(*head_header_block_num, head_header_hash);
const auto [current_header, head_header_hash] = co_await chain_storage->read_head_header_and_hash();
ensure(current_header.has_value(), "cannot find head header");
const BlockNum current_block_num = current_header->number;

Expand Down
1 change: 0 additions & 1 deletion silkworm/rpc/core/logs_walker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include <silkworm/core/types/address.hpp>
#include <silkworm/core/types/evmc_bytes32.hpp>
#include <silkworm/db/chain/chain.hpp>
#include <silkworm/db/kv/txn_num.hpp>
#include <silkworm/db/tables.hpp>
#include <silkworm/db/util.hpp>
Expand Down

0 comments on commit bb62df8

Please sign in to comment.