Skip to content

Commit

Permalink
test: Use common fork names in JSON export
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Feb 28, 2024
1 parent 17c3be1 commit 3632935
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
20 changes: 19 additions & 1 deletion test/unittests/state_transition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void state_transition::TearDown()

if (rev < EVMC_LONDON)
{
ASSERT_EQ(block.base_fee, 0);
ASSERT_EQ(tx.type, state::Transaction::Type::legacy);
}
if (tx.type == state::Transaction::Type::legacy)
Expand Down Expand Up @@ -148,6 +149,23 @@ void state_transition::TearDown()
export_state_test(receipt, state);
}

namespace
{
/// Converts EVM revision to the fork name commonly used in tests.
std::string_view to_test_fork_name(evmc_revision rev) noexcept
{
switch (rev)
{
case EVMC_TANGERINE_WHISTLE:
return "EIP150";
case EVMC_SPURIOUS_DRAGON:
return "EIP158";
default:
return evmc::to_string(rev);
}
}
} // namespace

void state_transition::export_state_test(const TransactionReceipt& receipt, const State& post)
{
json::json j;
Expand Down Expand Up @@ -183,7 +201,7 @@ void state_transition::export_state_test(const TransactionReceipt& receipt, cons
jtx["gasLimit"][0] = hex0x(tx.gas_limit);
jtx["value"][0] = hex0x(tx.value);

auto& jpost = jt["post"][evmc::to_string(rev)][0];
auto& jpost = jt["post"][to_test_fork_name(rev)][0];
jpost["indexes"] = {{"data", 0}, {"gas", 0}, {"value", 0}};
jpost["hash"] = hex0x(mpt_hash(post.get_accounts()));
jpost["logs"] = hex0x(logs_hash(receipt.logs));
Expand Down
42 changes: 33 additions & 9 deletions test/unittests/state_transition_tx_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,44 @@ using namespace evmone::test;
TEST_F(state_transition, tx_legacy)
{
rev = EVMC_ISTANBUL;
block.base_fee = 0; // should be 0 before London
tx.to = To;

expect.post.at(Sender).nonce = pre.get(Sender).nonce + 1;
}

TEST_F(state_transition, tx_non_existing_sender)
{
rev = EVMC_BERLIN;
block.base_fee = 0; // should be 0 before London
tx.to = To;
tx.max_gas_price = 0;
tx.max_priority_gas_price = 0;
tx.nonce = 0;
block.base_fee = 0;
pre.get_accounts().erase(Sender);

rev = EVMC_BERLIN;

expect.status = EVMC_SUCCESS;
expect.post.at(Sender).nonce = 1;
expect.post.at(Coinbase).exists = false;
}

TEST_F(state_transition, invalid_tx_non_existing_sender)
{
rev = EVMC_BERLIN;
block.base_fee = 0; // should be 0 before London
tx.to = To;
tx.max_gas_price = 1;
tx.max_priority_gas_price = 1;
tx.nonce = 0;
block.base_fee = 1;
pre.get_accounts().erase(Sender);

rev = EVMC_BERLIN;

expect.tx_error = INSUFFICIENT_FUNDS;
}

TEST_F(state_transition, blob_tx_insuficient_funds)
{
rev = EVMC_CANCUN;
block.base_fee = 1;
tx.to = To;
tx.gas_limit = 25000;
tx.max_gas_price = 1;
Expand All @@ -56,12 +57,35 @@ TEST_F(state_transition, blob_tx_insuficient_funds)
tx.blob_hashes.emplace_back(
0x0100000000000000000000000000000000000000000000000000000000000000_bytes32);
tx.max_blob_gas_price = 1;
block.base_fee = 1;

pre.get_accounts()[tx.sender].balance = 0x20000 + 25000;

rev = EVMC_CANCUN;

expect.post.at(Coinbase).exists = false;
expect.status = EVMC_SUCCESS;
}

TEST_F(state_transition, empty_coinbase_fee_0_sd)
{
rev = EVMC_SPURIOUS_DRAGON;
block_reward = 0;
block.base_fee = 0; // should be 0 before London
tx.max_gas_price = 0;
tx.max_priority_gas_price = 0;
tx.to = To;
pre.insert(Coinbase, {});
expect.post[To].exists = false;
expect.post[Coinbase].exists = false;
}

TEST_F(state_transition, empty_coinbase_fee_0_tw)
{
rev = EVMC_TANGERINE_WHISTLE;
block_reward = 0;
block.base_fee = 0; // should be 0 before London
tx.max_gas_price = 0;
tx.max_priority_gas_price = 0;
tx.to = To;
pre.insert(Coinbase, {});
expect.post[To].exists = true;
expect.post[Coinbase].balance = 0;
}
4 changes: 2 additions & 2 deletions test/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ evmc_revision to_rev(std::string_view s)
return EVMC_FRONTIER;
if (s == "Homestead")
return EVMC_HOMESTEAD;
if (s == "EIP150")
if (s == "Tangerine Whistle" || s == "EIP150")
return EVMC_TANGERINE_WHISTLE;
if (s == "EIP158")
if (s == "Spurious Dragon" || s == "EIP158")
return EVMC_SPURIOUS_DRAGON;
if (s == "Byzantium")
return EVMC_BYZANTIUM;
Expand Down

0 comments on commit 3632935

Please sign in to comment.