integrate evmc 'selfDestruct' and 'copyCode'

This commit is contained in:
andri lim 2020-01-16 23:12:46 +07:00 committed by zah
parent 8a9d9114e7
commit 295d2c180b
3 changed files with 21 additions and 6 deletions

View File

@ -108,6 +108,18 @@ template getCodeHash*(c: Computation, address: EthAddress): Hash256 =
else:
db.getCodeHash(address)
template selfDestruct*(c: Computation, address: EthAddress) =
when evmc_enabled:
c.host.selfDestruct(c.msg.contractAddress, address)
else:
c.registerAccountForDeletion(address)
template getCode*(c: Computation, address: EthAddress): ByteRange =
when evmc_enabled:
c.host.copyCode(address).toRange
else:
c.vmState.readOnlyStateDB.getCode(address)
proc newComputation*(vmState: BaseVMState, message: Message): Computation =
new result
result.vmState = vmState

View File

@ -79,14 +79,17 @@ proc copyCode*(ctx: HostContext, address: EthAddress, codeOffset: int = 0): seq[
var address = toEvmc(address)
if size - codeOffset > 0:
result = newSeq[byte](size - codeOffset)
let read = ctx.host.copy_code(ctx.context, address.addr, code_offset.uint, result[0].addr, result.len.uint).int
{.gcsafe.}:
let read = ctx.host.copy_code(ctx.context, address.addr,
code_offset.uint, result[0].addr, result.len.uint).int
doAssert(read == result.len)
proc selfdestruct*(ctx: HostContext, address, beneficiary: EthAddress) =
var
address = toEvmc(address)
beneficiary = toEvmc(beneficiary)
ctx.host.selfdestruct(ctx.context, address.addr, beneficiary.addr)
{.gcsafe.}:
ctx.host.selfdestruct(ctx.context, address.addr, beneficiary.addr)
proc emitLog*(ctx: HostContext, address: EthAddress, data: openArray[byte], topics: openArray[evmc_bytes32]) =
var address = toEvmc(address)

View File

@ -310,7 +310,7 @@ op extCodeSize, inline = true:
op extCodeCopy, inline = true:
## 0x3c, Copy an account's code to memory.
let account = c.stack.popAddress()
let address = c.stack.popAddress()
let (memStartPos, codeStartPos, size) = c.stack.popInt(3)
let (memPos, codePos, len) = (memStartPos.cleanMemRef, codeStartPos.cleanMemRef, size.cleanMemRef)
@ -318,7 +318,7 @@ op extCodeCopy, inline = true:
c.gasCosts[ExtCodeCopy].m_handler(c.memory.len, memPos, len),
reason="ExtCodeCopy fee")
let codeBytes = c.vmState.readOnlyStateDB.getCode(account)
let codeBytes = c.getCode(address)
c.memory.writePaddedResult(codeBytes.toOpenArray, memPos, codePos, len)
op returnDataSize, inline = true:
@ -734,7 +734,7 @@ template genCall(callName: untyped, opCode: Op): untyped =
let
callData = c.memory.read(memInPos, memInLen)
code = c.vmState.readOnlyStateDb.getCode(codeAddress)
code = c.getCode(codeAddress)
var childMsg = Message(
kind: callKind,
@ -834,7 +834,7 @@ proc selfDestructImpl(c: Computation, beneficiary: EthAddress) =
db.setBalance(c.msg.contractAddress, 0.u256)
# Register the account to be deleted
c.registerAccountForDeletion(beneficiary)
c.selfDestruct(beneficiary)
trace "SELFDESTRUCT",
contractAddress = c.msg.contractAddress.toHex,