Skip to content

Commit

Permalink
test: New State transition tests
Browse files Browse the repository at this point in the history
New tests for:
- contract creation,
- account touches,
- empty coinbase,
- selfdestruct.
  • Loading branch information
chfast committed Feb 2, 2024
1 parent e849042 commit f3f09d9
Show file tree
Hide file tree
Showing 5 changed files with 379 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ target_sources(
state_transition.hpp
state_transition.cpp
state_transition_block_test.cpp
state_transition_call_test.cpp
state_transition_create_test.cpp
state_transition_eof_test.cpp
state_transition_selfdestruct_test.cpp
state_transition_touch_test.cpp
state_transition_trace_test.cpp
state_transition_transient_storage_test.cpp
state_transition_tx_test.cpp
Expand Down
21 changes: 21 additions & 0 deletions test/unittests/state_transition_call_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2024 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include "../utils/bytecode.hpp"
#include "state_transition.hpp"

using namespace evmc::literals;
using namespace evmone::test;

TEST_F(state_transition, call_value_to_empty)
{
rev = EVMC_LONDON;
static constexpr auto BENEFICIARY = 0xbe_address;
tx.to = To;
pre.insert(*tx.to, {.balance = 1, .code = call(BENEFICIARY).value(1)});
pre.insert(BENEFICIARY, {});

expect.post[To].balance = 0;
expect.post[BENEFICIARY].balance = 1;
}
152 changes: 152 additions & 0 deletions test/unittests/state_transition_create_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,162 @@ TEST_F(state_transition, create_tx)
expect.post[create_address].code = bytes{0xFE};
}

TEST_F(state_transition, create_tx_failure)
{
static constexpr auto create_address = 0x3442a1dec1e72f337007125aa67221498cdd759d_address;

tx.data = bytecode{} + OP_INVALID;

expect.status = EVMC_INVALID_INSTRUCTION;
expect.post[create_address].exists = false;
}

TEST_F(state_transition, create2_max_nonce)
{
tx.to = To;
pre.insert(*tx.to, {.nonce = ~uint64_t{0}, .code = create2()});

expect.post[*tx.to].nonce = pre.get(*tx.to).nonce; // Nonce is unchanged.
}

TEST_F(state_transition, create_tx_collision)
{
static constexpr auto CREATED = 0x3442a1dec1e72f337007125aa67221498cdd759d_address;

pre.insert(CREATED, {.nonce = 2});

expect.status = EVMC_FAILURE;
expect.post[CREATED].nonce = 2;
}

TEST_F(state_transition, create_collision)
{
static constexpr auto CREATED = 0x8bbc3514477d75ec797bbe4e19d7961660bb849c_address;

tx.to = To;
pre.insert(*tx.to, {.code = create()});
pre.insert(CREATED, {.nonce = 2});

expect.post[*tx.to].nonce = pre.get(*tx.to).nonce + 1;
expect.post[CREATED].nonce = pre.get(CREATED).nonce;
}

TEST_F(state_transition, create_collision_revert)
{
static constexpr auto CREATED = 0x8bbc3514477d75ec797bbe4e19d7961660bb849c_address;

tx.to = To;
pre.insert(*tx.to, {.code = create() + OP_INVALID});
pre.insert(CREATED, {.nonce = 2});

expect.status = EVMC_INVALID_INSTRUCTION;
expect.post[*tx.to].nonce = pre.get(*tx.to).nonce;
expect.post[CREATED].nonce = pre.get(CREATED).nonce;
}

TEST_F(state_transition, create_prefunded_revert)
{
static constexpr auto CREATED = 0x8bbc3514477d75ec797bbe4e19d7961660bb849c_address;

tx.to = To;
pre.insert(*tx.to, {.code = create() + OP_INVALID});
pre.insert(CREATED, {.balance = 2});

expect.status = EVMC_INVALID_INSTRUCTION;
expect.post[*tx.to].nonce = pre.get(*tx.to).nonce;
expect.post[CREATED].nonce = pre.get(CREATED).nonce;
}

TEST_F(state_transition, create_revert)
{
static constexpr auto CREATED = 0x8bbc3514477d75ec797bbe4e19d7961660bb849c_address;

tx.to = To;
pre.insert(*tx.to, {.code = create() + OP_INVALID});

expect.status = EVMC_INVALID_INSTRUCTION;
expect.post[*tx.to].nonce = pre.get(*tx.to).nonce;
expect.post[CREATED].exists = false;
}

TEST_F(state_transition, create_revert_sd)
{
rev = EVMC_SPURIOUS_DRAGON;
static constexpr auto CREATED = 0x8bbc3514477d75ec797bbe4e19d7961660bb849c_address;

tx.to = To;
pre.insert(*tx.to, {.code = create() + OP_INVALID});

expect.status = EVMC_INVALID_INSTRUCTION;
expect.post[*tx.to].nonce = pre.get(*tx.to).nonce;
expect.post[CREATED].exists = false;
}

TEST_F(state_transition, create_revert_tw)
{
rev = EVMC_TANGERINE_WHISTLE;
static constexpr auto CREATED = 0x8bbc3514477d75ec797bbe4e19d7961660bb849c_address;

tx.to = To;
pre.insert(*tx.to, {.code = create() + OP_INVALID});

expect.status = EVMC_INVALID_INSTRUCTION;
expect.post[*tx.to].nonce = pre.get(*tx.to).nonce;
expect.post[CREATED].exists = false;
}

TEST_F(state_transition, create_collision_empty_revert)
{
static constexpr auto CREATED = 0x8bbc3514477d75ec797bbe4e19d7961660bb849c_address;

tx.to = To;
pre.insert(*tx.to, {.code = create() + OP_INVALID});
pre.insert(CREATED, {});

expect.status = EVMC_INVALID_INSTRUCTION;
expect.post[*tx.to].nonce = pre.get(*tx.to).nonce;
expect.post[CREATED].exists = true;
}

TEST_F(state_transition, create_collision_empty_revert_tw)
{
rev = EVMC_TANGERINE_WHISTLE;
static constexpr auto CREATED = 0x8bbc3514477d75ec797bbe4e19d7961660bb849c_address;

tx.to = To;
pre.insert(*tx.to, {.code = create() + OP_INVALID});
pre.insert(CREATED, {});

expect.status = EVMC_INVALID_INSTRUCTION;
expect.post[*tx.to].nonce = pre.get(*tx.to).nonce;
expect.post[CREATED].exists = true;
}

TEST_F(state_transition, touch_create_collision_empty_revert)
{
static constexpr auto CREATED = 0x11f72042f0f1c9d8a1aeffc3680d0b41dd7769a7_address;
static constexpr auto REVERT_PROXY = 0x94_address;

tx.to = To;
pre.insert(*tx.to, {.code = call(CREATED) + call(REVERT_PROXY).gas(0xffff)});
pre.insert(REVERT_PROXY, {.code = create() + OP_INVALID});

expect.post[*tx.to].nonce = pre.get(*tx.to).nonce;
expect.post[CREATED].exists = false;
expect.post[REVERT_PROXY].exists = true;
}

TEST_F(state_transition, touch_create_collision_empty_revert_tw)
{
rev = EVMC_TANGERINE_WHISTLE;
static constexpr auto CREATED = 0x11f72042f0f1c9d8a1aeffc3680d0b41dd7769a7_address;
static constexpr auto REVERT_PROXY = 0x94_address;

tx.to = To;
pre.insert(*tx.to, {.code = call(CREATED) + call(REVERT_PROXY).gas(0xffff)});
pre.insert(REVERT_PROXY, {.code = create() + OP_INVALID});

expect.post[*tx.to].nonce = pre.get(*tx.to).nonce;
expect.post[CREATED].exists = true;
expect.post[REVERT_PROXY].exists = true;
}
5 changes: 5 additions & 0 deletions test/unittests/state_transition_selfdestruct_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ TEST_F(state_transition, selfdestruct_double_revert)
expect.post[BENEFICIARY].balance = 1;
}

TEST_F(state_transition, selfdestruct_initcode)
{
tx.data = selfdestruct(0xbe_address);
}

TEST_F(state_transition, massdestruct_shanghai)
{
rev = EVMC_SHANGHAI;
Expand Down
Loading

0 comments on commit f3f09d9

Please sign in to comment.