Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

state: Fix failing code deployment in Frontier #824

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions test/state/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,9 @@ evmc::Result Host::create(const evmc_message& msg) noexcept
gas_left -= cost;
if (gas_left < 0)
{
return (m_rev == EVMC_FRONTIER) ? evmc::Result{EVMC_SUCCESS, result.gas_left} :
evmc::Result{EVMC_FAILURE};
return (m_rev == EVMC_FRONTIER) ?
evmc::Result{EVMC_SUCCESS, result.gas_left, result.gas_refund, msg.recipient} :
evmc::Result{EVMC_FAILURE};
}

if (m_rev >= EVMC_PRAGUE && (is_eof_container(initcode) || is_eof_container(code)))
Expand Down
106 changes: 106 additions & 0 deletions test/unittests/state_transition_create_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,109 @@ TEST_F(state_transition, create2_max_nonce)

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

TEST_F(state_transition, code_deployment_out_of_gas_tw)
{
rev = EVMC_TANGERINE_WHISTLE; // 63/64 gas rule enabled
block.base_fee = 0;
const auto initcode = ret(0, 5000); // create contract with a lot of zeros, deploy cost 1M

tx.to = To;
tx.gas_limit = 1000000;
pre.insert(To, {.code = mstore(0, push(initcode)) +
sstore(0, create().input(32 - initcode.size(), initcode.size()))});

expect.post[To].storage[0x00_bytes32] = 0x00_bytes32;
}

TEST_F(state_transition, code_deployment_out_of_gas_f)
{
rev = EVMC_FRONTIER;
block.base_fee = 0;
const auto initcode = ret(0, 1000); // create contract with a lot of zeros

tx.to = To;
tx.gas_limit = 100000;
pre.insert(To, {.code = mstore(0, push(initcode)) +
sstore(0, create().input(32 - initcode.size(), initcode.size()))});

const auto created = compute_create_address(To, pre.get(To).nonce);
expect.post[created].code = bytes{}; // code deployment failure creates empty account
expect.post[created].nonce = 0;
expect.post[To].storage[0x00_bytes32] = to_bytes32(created); // address of created empty
}

TEST_F(state_transition, code_deployment_out_of_gas_storage_tw)
{
rev = EVMC_TANGERINE_WHISTLE; // 63/64 gas rule enabled
block.base_fee = 0;
const auto initcode = sstore(0, 1) // set storage
+ ret(0, 5000); // create contract with a lot of zeros

tx.to = To;
tx.gas_limit = 1000000;
pre.insert(To, {.code = mstore(0, push(initcode)) +
sstore(0, create().input(32 - initcode.size(), initcode.size()))});

expect.post[To].storage[0x00_bytes32] = 0x00_bytes32;
}

TEST_F(state_transition, code_deployment_out_of_gas_storage_f)
{
rev = EVMC_FRONTIER;
block.base_fee = 0;
const auto initcode = sstore(0, 1) // set storage
+ ret(0, 1000); // create contract with a lot of zeros

tx.to = To;
tx.gas_limit = 100000;
pre.insert(To, {.code = mstore(0, push(initcode)) +
sstore(0, create().input(32 - initcode.size(), initcode.size()))});

expect.post[To].exists = true;
const auto created = compute_create_address(To, pre.get(To).nonce);
expect.post[created].code = bytes{}; // code deployment failure creates empty account
expect.post[created].nonce = 0;
expect.post[created].storage[0x00_bytes32] = 0x01_bytes32; // storage stays
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frontier was crazy time

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect.post[To].storage[0x00_bytes32] = to_bytes32(created);
expect.gas_used = 93134;
}

TEST_F(state_transition, code_deployment_out_of_gas_refund_tw)
{
rev = EVMC_TANGERINE_WHISTLE; // 63/64 gas rule enabled
block.base_fee = 0;
const auto initcode = sstore(0, 1) // set storage
+ sstore(0, 0) // gas refund
+ ret(0, 5000); // create contract with a lot of zeros

tx.to = To;
tx.gas_limit = 1000000;
pre.insert(To, {.code = mstore(0, push(initcode)) +
sstore(0, create().input(32 - initcode.size(), initcode.size()))});

expect.post[To].storage[0x00_bytes32] = 0x00_bytes32;
expect.gas_used = 990207;
}

TEST_F(state_transition, code_deployment_out_of_gas_refund_f)
{
rev = EVMC_FRONTIER;
block.base_fee = 0;
const auto initcode = sstore(0, 1) // set storage
+ sstore(0, 0) // gas refund
+ ret(0, 1000); // create contract with a lot of zeros

tx.to = To;
tx.gas_limit = 100000;
pre.insert(To, {.code = mstore(0, push(initcode)) +
sstore(0, create().input(32 - initcode.size(), initcode.size()))});

expect.post[To].exists = true;
const auto created = compute_create_address(To, pre.get(To).nonce);
expect.post[created].code = bytes{}; // code deployment failure creates empty account
expect.post[created].nonce = 0;
expect.post[created].storage[0x00_bytes32] = 0x00_bytes32;
expect.post[To].storage[0x00_bytes32] = to_bytes32(created);
expect.gas_used = 83140;
}