Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Charge for hashing init code in CREATE2 implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Oct 4, 2018
1 parent 0add2b3 commit da1cf1f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libaleth-interpreter/VMCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ void VM::caseCreate()

u256 salt;
if (m_OP == Instruction::CREATE2)
{
salt = m_SP[3];
// charge for hashing initCode = GSHA3WORD * ceil(len(init_code) / 32)
m_runGas += toInt63((u512{initSize} + 31) / 32 * uint64_t{VMSchedule::sha3WordGas});
}

updateMem(memNeed(initOff, initSize));
updateIOGas();
Expand Down
4 changes: 4 additions & 0 deletions libevm/LegacyVMCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ void LegacyVM::caseCreate()

u256 salt;
if (m_OP == Instruction::CREATE2)
{
salt = m_SP[3];
// charge for hashing initCode = GSHA3WORD * ceil(len(init_code) / 32)
m_runGas += toInt63((u512{initSize} +31) / 32 * m_schedule->sha3WordGas);
}

updateMem(memNeed(initOff, initSize));
updateIOGas();
Expand Down
39 changes: 39 additions & 0 deletions test/unittests/libevm/VMTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,40 @@ class Create2TestFixture: public TestOutputHelperFixture
BOOST_REQUIRE_EQUAL(state.getNonce(expectedAddress), 1);
}

void testCreate2costIncludesInitCodeHashing()
{
ExtVM extVm(state, envInfo, *se, address, address, address, value, gasPrice, ref(inputData),
ref(code), sha3(code), depth, isCreate, staticCall);

uint64_t gasBefore = 0;
uint64_t gasAfter = 0;
auto onOp = [&gasBefore, &gasAfter](uint64_t /*steps*/, uint64_t /* PC */,
Instruction _instr, bigint /* newMemSize */, bigint /* gasCost */,
bigint _gas, VMFace const*, ExtVMFace const*) {
if (_instr == Instruction::CREATE2)
{
// before CREATE2 instruction
gasBefore = static_cast<uint64_t>(_gas);
}
else if (gasBefore != 0 && gasAfter == 0)
{
// first instruction of the init code
gasAfter = static_cast<uint64_t>(_gas);
}
};

vm->exec(gas, extVm, onOp);

// create cost
uint64_t expectedGasAfter = gasBefore - 32000;
// hashing cost, assuming no memory expansion needed
expectedGasAfter -=
static_cast<uint64_t>(std::ceil(static_cast<double>(inputData.size()) / 32)) * 6;
// EIP-150 adjustion of subcall gas
expectedGasAfter -= expectedGasAfter / 64;
BOOST_REQUIRE_EQUAL(gasAfter, expectedGasAfter);
}


BlockHeader blockHeader{initBlockHeader()};
LastBlockHashes lastBlockHashes;
Expand Down Expand Up @@ -494,6 +528,11 @@ BOOST_AUTO_TEST_CASE(LegacyVMCreate2collisionWithNonEmptyStorageEmptyInitCode)
testCreate2collisionWithNonEmptyStorageEmptyInitCode();
}

BOOST_AUTO_TEST_CASE(LegacyVMCreate2costIncludesInitCodeHashing)
{
testCreate2costIncludesInitCodeHashing();
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_FIXTURE_TEST_SUITE(LegacyVMExtcodehashSuite, LegacyVMExtcodehashTestFixture)
Expand Down

0 comments on commit da1cf1f

Please sign in to comment.