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

Commit

Permalink
Implement EIP-2046 in LegacyVM
Browse files Browse the repository at this point in the history
Reduced gas cost for static calls made to precompiles.
  • Loading branch information
gumb0 committed Jul 25, 2019
1 parent 5bf79e5 commit f2d070e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions libethcore/EVMSchedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct EVMSchedule
unsigned logTopicGas = 375;
unsigned createGas = 32000;
unsigned callGas = 40;
unsigned precompileStaticCallGas = 700;
unsigned callStipend = 2300;
unsigned callValueTransferGas = 9000;
unsigned callNewAccountGas = 25000;
Expand Down Expand Up @@ -151,6 +152,7 @@ static const EVMSchedule IstanbulSchedule = [] {
EVMSchedule schedule = ConstantinopleFixSchedule;
schedule.accountVersion = 1;
schedule.txDataNonZeroGas = 16;
schedule.precompileStaticCallGas = 40;
return schedule;
}();

Expand Down
9 changes: 6 additions & 3 deletions libevm/LegacyVMCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,16 @@ bool LegacyVM::caseCallSetup(CallParameters *callParams, bytesRef& o_output)
assert(callParams->valueTransfer == 0);
assert(callParams->apparentValue == 0);

m_runGas = toInt63(m_schedule->callGas);

callParams->staticCall = (m_OP == Instruction::STATICCALL || m_ext->staticCall);
Address const destinationAddr = asAddress(m_SP[1]);

if (callParams->staticCall && isPrecompiledContract(destinationAddr))
m_runGas = toInt63(m_schedule->precompileStaticCallGas);
else
m_runGas = toInt63(m_schedule->callGas);

bool const haveValueArg = m_OP == Instruction::CALL || m_OP == Instruction::CALLCODE;

Address destinationAddr = asAddress(m_SP[1]);
if (m_OP == Instruction::CALL &&
(m_SP[2] > 0 || m_schedule->zeroValueTransferChargesNewAccountGas()) &&
!m_ext->exists(destinationAddr))
Expand Down
6 changes: 6 additions & 0 deletions libevm/VMFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,11 @@ inline u256 fromAddress(Address _a)
return (u160)_a;
}

// Checks whether address is in the address range for precompiles according to EIP-1352
inline bool isPrecompiledContract(Address const& _addr) noexcept
{
static Address const c_maxPrecompiledAddress{0xffff};
return _addr <= c_maxPrecompiledAddress;
}
}
}

0 comments on commit f2d070e

Please sign in to comment.