Skip to content

Commit

Permalink
add unit tests for batch size
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat committed Jan 16, 2024
1 parent f3e96f8 commit fe93c80
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions silkworm/node/db/buffer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ TEST_CASE("Account update") {
Buffer buffer{txn, 0};
buffer.begin_block(1);
buffer.update_account(address, /*initial=*/std::nullopt, current_account);
REQUIRE(buffer.account_changes().empty() == false);
REQUIRE(!buffer.account_changes().empty());
// Current state batch: current account address + current account encoding
CHECK(buffer.current_batch_state_size() == kAddressLength + current_account.encoding_length_for_storage());
// State history batch: current block + initial account address + initial account encoding (empty)
CHECK(buffer.current_batch_history_size() == sizeof(uint64_t) + kAddressLength);
REQUIRE_NOTHROW(buffer.write_to_db());

auto account_changeset{db::open_cursor(txn, table::kAccountChangeSet)};
Expand All @@ -122,7 +126,7 @@ TEST_CASE("Account update") {
auto changeset_address{bytes_to_address(data_value_view)};
REQUIRE(changeset_address == address);
data_value_view.remove_prefix(kAddressLength);
REQUIRE(data_value_view.length() == 0);
REQUIRE(data_value_view.empty());
}

SECTION("Changed EOA account") {
Expand All @@ -138,7 +142,11 @@ TEST_CASE("Account update") {
Buffer buffer{txn, 0};
buffer.begin_block(1);
buffer.update_account(address, /*initial=*/initial_account, current_account);
REQUIRE(buffer.account_changes().empty() == false);
REQUIRE(!buffer.account_changes().empty());
// Current state batch: current account address + current account encoding
CHECK(buffer.current_batch_state_size() == kAddressLength + current_account.encoding_length_for_storage());
// State history batch: current block + initial account address + initial account encoding
CHECK(buffer.current_batch_history_size() == sizeof(uint64_t) + kAddressLength + initial_account.encoding_length_for_storage());
REQUIRE_NOTHROW(buffer.write_to_db());

auto account_changeset{db::open_cursor(txn, table::kAccountChangeSet)};
Expand All @@ -153,22 +161,26 @@ TEST_CASE("Account update") {
auto changeset_address{bytes_to_address(data_value_view)};
REQUIRE(changeset_address == address);
data_value_view.remove_prefix(kAddressLength);
REQUIRE(data_value_view.length() != 0);
REQUIRE(!data_value_view.empty());

auto previous_account{Account::from_encoded_storage(data_value_view)};
CHECK(previous_account == initial_account);
}

SECTION("Delete Contract account") {
SECTION("Delete contract account") {
const auto address{0xbe00000000000000000000000000000000000000_address};
Account account;
account.incarnation = kDefaultIncarnation;
account.code_hash = to_bytes32(keccak256(address.bytes).bytes); // Just a fake hash

Buffer buffer{txn, 0};
buffer.begin_block(1);
buffer.update_account(address, /*initial=*/account, std::nullopt);
REQUIRE(buffer.account_changes().empty() == false);
buffer.update_account(address, /*initial=*/account, /*current=*/std::nullopt);
REQUIRE(!buffer.account_changes().empty());
// Current state batch: initial account for delete + (initial account + incarnation) for incarnation
CHECK(buffer.current_batch_state_size() == kAddressLength + (kAddressLength + kIncarnationLength));
// State history batch: current block + initial account address + initial account encoding
CHECK(buffer.current_batch_history_size() == sizeof(uint64_t) + kAddressLength + account.encoding_length_for_storage());
REQUIRE_NOTHROW(buffer.write_to_db());

auto incarnations{db::open_cursor(txn, table::kIncarnationMap)};
Expand All @@ -192,6 +204,8 @@ TEST_CASE("Account update") {
buffer.begin_block(1);
buffer.update_account(address, /*initial=*/initial_account, current_account);
REQUIRE(buffer.account_changes().empty());
CHECK(buffer.current_batch_state_size() == 0); // No change in current state batch
CHECK(buffer.current_batch_history_size() == 0); // No change in state history batch
REQUIRE_NOTHROW(buffer.write_to_db());

auto account_changeset{db::open_cursor(txn, table::kAccountChangeSet)};
Expand Down

0 comments on commit fe93c80

Please sign in to comment.