Skip to content

Commit

Permalink
rpcdaemon: use ETHBACKEND.canonical_block_hash_from_number (#2426)
Browse files Browse the repository at this point in the history
  • Loading branch information
lupin012 authored Oct 24, 2024
1 parent e6778cd commit ab7ea14
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 197 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rpc-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Checkout RPC Tests Repository & Install Requirements
run: |
rm -rf ${{runner.workspace}}/rpc-tests
git -c advice.detachedHead=false clone --depth 1 --branch v1.0.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests
git -c advice.detachedHead=false clone --depth 1 --branch v1.1.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests
cd ${{runner.workspace}}/rpc-tests
pip3 install -r requirements.txt
Expand Down
24 changes: 23 additions & 1 deletion .github/workflows/run_integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,28 @@ cd "$1" || exit 1
rm -rf ./mainnet/results/

python3 ./run_tests.py --continue --blockchain mainnet --jwt "$2" --display-only-fail --port 51515 -x engine_,\
debug_traceCall/test_02.json,\
erigon_getHeaderByHash/test_05.json,\
debug_accountAt,\
debug_traceBlockByHash,\
erigon_getBlockReceiptsByBlockHash,\
erigon_getHeaderByHash,\
erigon_getLogsByHash,\
eth_getBlockByHash,\
eth_getBlockTransactionCountByHash,\
eth_getRawTransactionByBlockHashAndIndex,\
eth_getTransactionByBlockHashAndIndex,\
eth_getUncleCountByBlockHash,\
eth_getBalance,\
debug_traceCall/test_02.json,\
eth_getCode,\
ots_hasCode,\
eth_estimateGas,\
eth_getStorageAt/test_01.json,\
eth_getStorageAt/test_02.json,\
eth_getStorageAt/test_03.json,\
eth_getTransactionCount/test_01.json,\
eth_getTransactionCount/test_06.json,\
eth_createAccessList/test_16.json,\
debug_accountRange,\
debug_getModifiedAccounts,\
debug_storageRangeAt,\
Expand All @@ -39,3 +59,5 @@ else
fi

exit $failed_test


12 changes: 0 additions & 12 deletions silkworm/db/chain/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ Task<uint64_t> read_header_number(kv::api::Transaction& tx, const evmc::bytes32&
co_return endian::load_big_u64(value.data());
}

Task<evmc::bytes32> read_canonical_block_hash(kv::api::Transaction& tx, uint64_t block_number) {
const auto block_key = db::block_key(block_number);
SILK_TRACE << "read_canonical_block_hash block_key: " << to_hex(block_key);
const auto value{co_await tx.get_one(table::kCanonicalHashesName, block_key)};
if (value.empty()) {
throw std::invalid_argument{"empty block hash value in read_canonical_block_hash"};
}
const auto canonical_block_hash{to_bytes32(value)};
SILK_DEBUG << "read_canonical_block_hash canonical block hash: " << to_hex(canonical_block_hash);
co_return canonical_block_hash;
}

Task<intx::uint256> read_total_difficulty(kv::api::Transaction& tx, const evmc::bytes32& block_hash, uint64_t block_number) {
const auto block_key = db::block_key(block_number, block_hash.bytes);
SILK_TRACE << "read_total_difficulty block_key: " << to_hex(block_key);
Expand Down
2 changes: 0 additions & 2 deletions silkworm/db/chain/chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ using Transactions = std::vector<silkworm::Transaction>;

Task<uint64_t> read_header_number(kv::api::Transaction& tx, const evmc::bytes32& block_hash);

Task<evmc::bytes32> read_canonical_block_hash(kv::api::Transaction& tx, BlockNum block_number);

Task<intx::uint256> read_total_difficulty(kv::api::Transaction& tx, const evmc::bytes32& block_hash, BlockNum block_number);

Task<evmc::bytes32> read_head_header_hash(kv::api::Transaction& tx);
Expand Down
37 changes: 0 additions & 37 deletions silkworm/db/chain/chain_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,43 +79,6 @@ TEST_CASE_METHOD(ChainTest, "read_header_number") {
}
}

TEST_CASE_METHOD(ChainTest, "read_canonical_block_hash") {
SECTION("empty hash bytes") {
EXPECT_CALL(transaction, get_one(table::kCanonicalHashesName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return Bytes{};
}));
uint64_t block_number{4'000'000};
CHECK_THROWS_AS(spawn_and_wait(read_canonical_block_hash(transaction, block_number)), std::invalid_argument);
}

SECTION("shorter hash bytes") {
EXPECT_CALL(transaction, get_one(table::kCanonicalHashesName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return *from_hex("9816753229fc0736bf86a5048de4bc9fcdede8c91dadf88c828c76b2281dff");
}));
uint64_t block_number{4'000'000};
const auto block_hash = spawn_and_wait(read_canonical_block_hash(transaction, block_number));
CHECK(block_hash == 0x009816753229fc0736bf86a5048de4bc9fcdede8c91dadf88c828c76b2281dff_bytes32);
}

SECTION("longer hash bytes") {
EXPECT_CALL(transaction, get_one(table::kCanonicalHashesName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return *from_hex("439816753229fc0736bf86a5048de4bc9fcdede8c91dadf88c828c76b2281dffabcdef");
}));
uint64_t block_number{4'000'000};
const auto block_hash = spawn_and_wait(read_canonical_block_hash(transaction, block_number));
CHECK(block_hash == 0x439816753229fc0736bf86a5048de4bc9fcdede8c91dadf88c828c76b2281dff_bytes32);
}

SECTION("valid canonical hash") {
EXPECT_CALL(transaction, get_one(table::kCanonicalHashesName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return kBlockHash;
}));
uint64_t block_number{4'000'000};
const auto block_hash = spawn_and_wait(read_canonical_block_hash(transaction, block_number));
CHECK(block_hash == 0x439816753229fc0736bf86a5048de4bc9fcdede8c91dadf88c828c76b2281dff_bytes32);
}
}

TEST_CASE_METHOD(ChainTest, "read_total_difficulty") {
SECTION("empty RLP buffer") {
EXPECT_CALL(transaction, get_one(table::kDifficultyName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
Expand Down
12 changes: 6 additions & 6 deletions silkworm/db/chain/remote_chain_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ RemoteChainStorage::RemoteChainStorage(kv::api::Transaction& tx, Providers provi
: tx_{tx}, providers_{std::move(providers)} {}

Task<ChainConfig> RemoteChainStorage::read_chain_config() const {
const auto genesis_block_hash{co_await read_canonical_block_hash(tx_, kEarliestBlockNumber)};
const auto genesis_block_hash{co_await providers_.canonical_block_hash_from_number(kEarliestBlockNumber)};
SILK_DEBUG << "rawdb::read_chain_config genesis_block_hash: " << to_hex(genesis_block_hash.bytes);
const ByteView genesis_block_hash_bytes{genesis_block_hash.bytes, kHashLength};
const auto data{co_await tx_.get_one(db::table::kConfigName, genesis_block_hash_bytes)};
Expand Down Expand Up @@ -72,7 +72,7 @@ Task<bool> RemoteChainStorage::read_block(const Hash& hash, Block& block) const
}

Task<bool> RemoteChainStorage::read_block(BlockNum number, bool read_senders, Block& block) const {
const auto hash = co_await read_canonical_block_hash(tx_, number);
const auto hash = co_await providers_.canonical_block_hash_from_number(number);
co_return co_await providers_.block(number, hash.bytes, read_senders, block);
}

Expand Down Expand Up @@ -121,17 +121,17 @@ Task<bool> RemoteChainStorage::read_body(const Hash& hash, BlockBody& body) cons
}

Task<std::optional<Hash>> RemoteChainStorage::read_canonical_header_hash(BlockNum number) const {
co_return co_await read_canonical_block_hash(tx_, number);
co_return co_await providers_.canonical_block_hash_from_number(number);
}

Task<std::optional<BlockHeader>> RemoteChainStorage::read_canonical_header(BlockNum number) const {
const auto hash = co_await read_canonical_block_hash(tx_, number);
const auto hash = co_await providers_.canonical_block_hash_from_number(number);
co_return co_await read_header(number, hash);
}

Task<bool> RemoteChainStorage::read_canonical_body(BlockNum number, BlockBody& body) const {
Block block;
const auto hash = co_await read_canonical_block_hash(tx_, number);
const auto hash = co_await providers_.canonical_block_hash_from_number(number);
const bool success = co_await providers_.block(number, hash.bytes, /*.read_senders=*/false, block);
if (!success) {
co_return false;
Expand All @@ -143,7 +143,7 @@ Task<bool> RemoteChainStorage::read_canonical_body(BlockNum number, BlockBody& b
}

Task<bool> RemoteChainStorage::read_canonical_block(BlockNum number, Block& block) const {
const auto hash = co_await read_canonical_block_hash(tx_, number);
const auto hash = co_await providers_.canonical_block_hash_from_number(number);
const bool success = co_await providers_.block(number, hash.bytes, /*.read_senders=*/false, block);
if (!success) {
co_return false;
Expand Down
25 changes: 14 additions & 11 deletions silkworm/db/chain/remote_chain_storage_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <string>

#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/thread_pool.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_exception.hpp>
#include <gmock/gmock.h>
Expand All @@ -35,7 +37,8 @@ using testing::_;
using testing::InvokeWithoutArgs;
using testing::Unused;

static Bytes kBlockHash{*from_hex("439816753229fc0736bf86a5048de4bc9fcdede8c91dadf88c828c76b2281dff")};
static const evmc::bytes32 kBlockHash{0x439816753229fc0736bf86a5048de4bc9fcdede8c91dadf88c828c76b2281dff_bytes32};

static Bytes kInvalidJsonChainConfig{*from_hex("000102")};
static Bytes kChainConfig{*from_hex(
"7b226265726c696e426c6f636b223a31323234343030302c2262797a6"
Expand All @@ -45,16 +48,22 @@ static Bytes kChainConfig{*from_hex(
"30302c22697374616e62756c426c6f636b223a393036393030302c226c6f6e646f6e426c6f636b223a31323936353030302c226d75697"
"2476c6163696572426c6f636b223a393230303030302c2270657465727362757267426c6f636b223a373238303030307d")};

chain::BlockProvider block_provider{
[](BlockNum, HashAsSpan, bool, Block&) -> Task<bool> { co_return false; }};
chain::BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider{
[](HashAsSpan) -> Task<BlockNum> { co_return 0; }};
chain::BlockNumberFromBlockHashProvider block_number_from_block_hash_provider{
[](HashAsSpan) -> Task<BlockNum> { co_return 0; }};
chain::CanonicalBlockHashFromNumberProvider canonical_block_hash_from_number_provider{
[](BlockNum) -> Task<evmc::bytes32> { co_return kBlockHash; }};

struct RemoteChainStorageTest : public silkworm::test_util::ContextTestBase {
test_util::MockTransaction transaction;
RemoteChainStorage storage{transaction, Providers{}};
RemoteChainStorage storage{transaction, {block_provider, block_number_from_txn_hash_provider, block_number_from_block_hash_provider, canonical_block_hash_from_number_provider}};
};

TEST_CASE_METHOD(RemoteChainStorageTest, "read_chain_config") {
SECTION("empty chain data") {
EXPECT_CALL(transaction, get_one(table::kCanonicalHashesName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return kBlockHash;
}));
EXPECT_CALL(transaction, get_one(table::kConfigName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return Bytes{};
}));
Expand All @@ -66,19 +75,13 @@ TEST_CASE_METHOD(RemoteChainStorageTest, "read_chain_config") {
}

SECTION("invalid JSON chain data") {
EXPECT_CALL(transaction, get_one(table::kCanonicalHashesName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return kBlockHash;
}));
EXPECT_CALL(transaction, get_one(table::kConfigName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return kInvalidJsonChainConfig;
}));
CHECK_THROWS_AS(spawn_and_wait(storage.read_chain_config()), nlohmann::json::parse_error);
}

SECTION("valid JSON chain data") {
EXPECT_CALL(transaction, get_one(table::kCanonicalHashesName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return kBlockHash;
}));
EXPECT_CALL(transaction, get_one(table::kConfigName, _)).WillOnce(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return kChainConfig;
}));
Expand Down
17 changes: 5 additions & 12 deletions silkworm/db/kv/txn_num.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,11 @@ static Task<TxNum> last_tx_num_for_block(Transaction& tx, BlockNum block_number,
const auto block_number_key = block_key(block_number);
auto key_value = co_await max_tx_num_cursor->seek_exact(block_number_key);
if (key_value.value.empty()) {
/* START temporary for E3 alpha2 */
if (canonical_body_for_storage_provider) {
Bytes block_body_data = co_await canonical_body_for_storage_provider(block_number);
ByteView block_body_data_view{block_body_data};
const auto stored_body{unwrap_or_throw(decode_stored_block_body(block_body_data_view))};
co_return stored_body.base_txn_id + stored_body.txn_count - 1;
}
/* END temporary for E3 alpha2 */
key_value = co_await max_tx_num_cursor->last();
if (key_value.value.empty()) {
co_return 0;
}
SILKWORM_ASSERT(canonical_body_for_storage_provider);
Bytes block_body_data = co_await canonical_body_for_storage_provider(block_number);
ByteView block_body_data_view{block_body_data};
const auto stored_body{unwrap_or_throw(decode_stored_block_body(block_body_data_view))};
co_return stored_body.base_txn_id + stored_body.txn_count - 1;
}
if (key_value.value.size() != sizeof(TxNum)) {
throw std::length_error("Bad TxNum value size " + std::to_string(key_value.value.size()) + " in db");
Expand Down
Loading

0 comments on commit ab7ea14

Please sign in to comment.