implement EIP 2046

This commit is contained in:
jangko 2020-11-19 13:56:22 +07:00
parent cd98ddbbd3
commit 97f73fd03d
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
5 changed files with 45 additions and 3 deletions

View File

@ -65,4 +65,5 @@ const
# and: https://github.com/ethereum/EIPs/issues/170 # and: https://github.com/ethereum/EIPs/issues/170
EIP170_CODE_SIZE_LIMIT* = 24577 EIP170_CODE_SIZE_LIMIT* = 24577
# EIP
MaxPrecompilesAddr* = 0xFFFF

View File

@ -731,7 +731,7 @@ template genCall(callName: untyped, opCode: Op): untyped =
(memOutPos, memOutLen) (memOutPos, memOutLen)
let contractAddress = when opCode in {Call, StaticCall}: destination else: c.msg.contractAddress let contractAddress = when opCode in {Call, StaticCall}: destination else: c.msg.contractAddress
let (childGasFee, childGasLimit) = c.gasCosts[opCode].c_handler( var (childGasFee, childGasLimit) = c.gasCosts[opCode].c_handler(
value, value,
GasParams(kind: opCode, GasParams(kind: opCode,
c_isNewAccount: not c.accountExists(contractAddress), c_isNewAccount: not c.accountExists(contractAddress),
@ -742,6 +742,13 @@ template genCall(callName: untyped, opCode: Op): untyped =
c_memLength: memLength c_memLength: memLength
)) ))
# EIP 2046
# reduce gas fee for precompiles
# from 700 to 40
when opCode == StaticCall:
if c.fork >= FkBerlin and destination.toInt <= MaxPrecompilesAddr:
childGasFee = childGasFee - 660.GasInt
if childGasFee >= 0: if childGasFee >= 0:
c.gasMeter.consumeGas(childGasFee, reason = $opCode) c.gasMeter.consumeGas(childGasFee, reason = $opCode)

View File

@ -7,6 +7,7 @@
import import
macros, macros,
stew/endians2, stew/ranges/ptr_arith,
eth/common/eth_types, eth/common/eth_types,
../../../constants ../../../constants
@ -112,3 +113,9 @@ func safeInt*(x: Uint256): int {.inline.} =
result = x.truncate(int) result = x.truncate(int)
if x > high(int32).u256 or result < 0: if x > high(int32).u256 or result < 0:
result = high(int32) result = high(int32)
func toInt*(x: EthAddress): int =
type T = uint32
const len = sizeof(T)
fromBytesBE(T, makeOpenArray(x[x.len-len].unsafeAddr, len)).int

View File

@ -113,4 +113,5 @@ cliBuilder:
./test_forkid, ./test_forkid,
../stateless/test_witness_keys, ../stateless/test_witness_keys,
../stateless/test_block_witness, ../stateless/test_block_witness,
../stateless/test_witness_json ../stateless/test_witness_json,
./test_misc

26
tests/test_misc.nim Normal file
View File

@ -0,0 +1,26 @@
import
unittest2, stew/byteutils,
eth/common/eth_types,
../nimbus/vm/interpreter/utils/utils_numeric
func toAddress(n: int): EthAddress =
result[19] = n.byte
func toAddress(a, b: int): EthAddress =
result[18] = a.byte
result[19] = b.byte
func toAddress(a, b, c: int): EthAddress =
result[17] = a.byte
result[18] = b.byte
result[19] = c.byte
proc miscMain*() =
suite "Misc test suite":
test "EthAddress to int":
check toAddress(0xff).toInt == 0xFF
check toAddress(0x10, 0x0).toInt == 0x1000
check toAddress(0x10, 0x0, 0x0).toInt == 0x100000
when isMainModule:
miscMain()