integrate evmc 'getCodeHash'

This commit is contained in:
andri lim 2020-01-16 22:48:22 +07:00 committed by zah
parent 5507e80b63
commit 8a9d9114e7
4 changed files with 25 additions and 14 deletions

View File

@ -96,7 +96,17 @@ template getCodeSize*(c: Computation, address: EthAddress): uint =
when evmc_enabled:
c.host.getCodeSize(address)
else:
uint(c.vmState.readOnlyStateDB.getCode(account).len)
uint(c.vmState.readOnlyStateDB.getCode(address).len)
template getCodeHash*(c: Computation, address: EthAddress): Hash256 =
when evmc_enabled:
c.host.getCodeHash(address)
else:
let db = c.vmState.readOnlyStateDB
if not db.accountExists(address) or db.isEmptyAccount(address):
default(Hash256)
else:
db.getCodeHash(address)
proc newComputation*(vmState: BaseVMState, message: Message): Computation =
new result

View File

@ -71,7 +71,8 @@ proc getCodeSize*(ctx: HostContext, address: EthAddress): uint =
proc getCodeHash*(ctx: HostContext, address: EthAddress): Hash256 =
var address = toEvmc(address)
Hash256.fromEvmc ctx.host.get_code_hash(ctx.context, address.addr)
{.gcsafe.}:
Hash256.fromEvmc ctx.host.get_code_hash(ctx.context, address.addr)
proc copyCode*(ctx: HostContext, address: EthAddress, codeOffset: int = 0): seq[byte] =
let size = ctx.getCodeSize(address).int

View File

@ -80,7 +80,17 @@ proc hostGetCodeSizeImpl(ctx: Computation, address: var evmc_address): uint {.cd
ctx.vmState.readOnlyStateDB.getCode(fromEvmc(address)).len.uint
proc hostGetCodeHashImpl(ctx: Computation, address: var evmc_address): evmc_bytes32 {.cdecl.} =
ctx.vmstate.readOnlyStateDB.getCodeHash(fromEvmc(address)).toEvmc()
let
db = ctx.vmstate.readOnlyStateDB
address = fromEvmc(address)
if not db.accountExists(address):
return
if db.isEmptyAccount(address):
return
db.getCodeHash(address).toEvmc()
proc hostCopyCodeImpl(ctx: Computation, address: var evmc_address,
codeOffset: uint, bufferData: ptr byte,

View File

@ -904,17 +904,7 @@ op sarOp, inline = true:
op extCodeHash, inline = true:
let address = c.stack.popAddress()
# this is very inefficient, it calls underlying
# database too much, we can reduce it by implementing accounts
# cache
if not c.accountExists(address):
push: 0
return
if c.vmState.readOnlyStateDB.isEmptyAccount(address):
push: 0
else:
push: c.vmState.readOnlyStateDB.getCodeHash(address)
push: c.getCodeHash(address)
op sstoreEIP2200, inline = false, slot, value:
checkInStaticContext(c)