nimbus-eth1/nimbus/vm/evmc_host.nim

253 lines
9.0 KiB
Nim
Raw Normal View History

2020-01-16 04:20:13 +00:00
# Nimbus
# Copyright (c) 2019 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except according to those terms.
2020-01-31 01:08:44 +00:00
proc hostReleaseResultImpl(res: var nimbus_result) {.cdecl, gcsafe.} =
dealloc(res.output_data)
2020-01-16 04:20:13 +00:00
proc hostGetTxContextImpl(ctx: Computation): nimbus_tx_context {.cdecl.} =
2020-01-16 04:20:13 +00:00
let vmstate = ctx.vmState
result.tx_gas_price = toEvmc(vmstate.txGasPrice.u256)
result.tx_origin = vmstate.txOrigin
result.block_coinbase = vmstate.coinbase
2020-01-16 04:20:13 +00:00
result.block_number = vmstate.blockNumber.truncate(int64)
result.block_timestamp = vmstate.timestamp.toUnix()
result.block_gas_limit = int64(vmstate.blockHeader.gasLimit)
result.block_difficulty = toEvmc(vmstate.difficulty)
result.chain_id = toEvmc(vmstate.chaindb.config.chainId.u256)
proc hostGetBlockHashImpl(ctx: Computation, number: int64): Hash256 {.cdecl.} =
ctx.vmState.getAncestorHash(number.u256)
2020-01-16 04:20:13 +00:00
proc hostAccountExistsImpl(ctx: Computation, address: EthAddress): bool {.cdecl.} =
2020-01-17 11:48:14 +00:00
let db = ctx.vmState.readOnlyStateDB
if ctx.fork >= FkSpurious:
not db.isDeadAccount(address)
2020-01-17 11:48:14 +00:00
else:
db.accountExists(address)
2020-01-16 04:20:13 +00:00
proc hostGetStorageImpl(ctx: Computation, address: EthAddress, key: var evmc_bytes32): evmc_bytes32 {.cdecl.} =
2020-07-21 06:15:06 +00:00
ctx.vmState.accountDB.getStorage(address, Uint256.fromEvmc(key)).toEvmc()
2020-01-16 04:20:13 +00:00
2020-07-21 12:58:17 +00:00
proc sstoreNetGasMetering(ctx: Computation): bool {.inline.} =
ctx.fork in {FkConstantinople, FkIstanbul}
proc hostSetStorageImpl(ctx: Computation, address: EthAddress,
2020-01-16 04:20:13 +00:00
key, value: var evmc_bytes32): evmc_storage_status {.cdecl.} =
let
slot = Uint256.fromEvmc(key)
newValue = Uint256.fromEvmc(value)
statedb = ctx.vmState.readOnlyStateDb
2020-07-21 06:15:06 +00:00
currValue = statedb.getStorage(address, slot)
2020-01-16 04:20:13 +00:00
assert address == ctx.msg.contractAddress
2020-01-16 04:20:13 +00:00
var
status = EVMC_STORAGE_MODIFIED
2020-01-18 00:32:39 +00:00
gasRefund = 0.GasInt
origValue = 0.u256
2020-01-16 04:20:13 +00:00
if newValue == currValue:
status = EVMC_STORAGE_UNCHANGED
else:
origValue = statedb.getCommittedStorage(address, slot)
2020-07-21 12:58:17 +00:00
if origValue == currValue or not ctx.sstoreNetGasMetering():
2020-01-17 14:49:22 +00:00
if currValue == 0:
2020-01-18 00:32:39 +00:00
status = EVMC_STORAGE_ADDED
elif newValue == 0:
status = EVMC_STORAGE_DELETED
else:
status = EVMC_STORAGE_MODIFIED_AGAIN
ctx.vmState.mutateStateDB:
db.setStorage(address, slot, newValue)
2020-01-18 00:32:39 +00:00
let gasParam = GasParams(kind: Op.Sstore,
s_status: status,
s_currentValue: currValue,
s_originalValue: origValue
)
gasRefund = ctx.gasCosts[Sstore].c_handler(newValue, gasParam)[1]
if gasRefund != 0:
2020-01-17 14:49:22 +00:00
ctx.gasMeter.refundGas(gasRefund)
2020-01-16 04:20:13 +00:00
result = status
proc hostGetBalanceImpl(ctx: Computation, address: EthAddress): evmc_bytes32 {.cdecl.} =
ctx.vmState.readOnlyStateDB.getBalance(address).toEvmc()
2020-01-16 04:20:13 +00:00
proc hostGetCodeSizeImpl(ctx: Computation, address: EthAddress): uint {.cdecl.} =
ctx.vmState.readOnlyStateDB.getCode(address).len.uint
2020-01-16 15:48:22 +00:00
proc hostGetCodeHashImpl(ctx: Computation, address: EthAddress): Hash256 {.cdecl.} =
let db = ctx.vmstate.readOnlyStateDB
2020-01-16 15:48:22 +00:00
if not db.accountExists(address):
return
if db.isEmptyAccount(address):
return
db.getCodeHash(address)
2020-01-16 15:48:22 +00:00
proc hostCopyCodeImpl(ctx: Computation, address: EthAddress,
codeOffset: int, bufferData: ptr byte,
bufferSize: int): int {.cdecl.} =
2020-01-16 04:20:13 +00:00
var code = ctx.vmState.readOnlyStateDB.getCode(address)
2020-01-16 04:20:13 +00:00
# Handle "big offset" edge case.
if codeOffset > code.len:
2020-01-16 04:20:13 +00:00
return 0
let maxToCopy = code.len - codeOffset
let numToCopy = min(maxToCopy, bufferSize)
2020-01-16 04:20:13 +00:00
if numToCopy > 0:
2020-07-21 06:15:06 +00:00
copyMem(bufferData, code[codeOffset].addr, numToCopy)
result = numToCopy
2020-01-16 04:20:13 +00:00
proc hostSelfdestructImpl(ctx: Computation, address, beneficiary: EthAddress) {.cdecl.} =
assert address == ctx.msg.contractAddress
ctx.execSelfDestruct(beneficiary)
2020-01-16 04:20:13 +00:00
proc hostEmitLogImpl(ctx: Computation, address: EthAddress,
2020-01-17 13:11:02 +00:00
data: ptr byte, dataSize: int,
topics: UncheckedArray[evmc_bytes32], topicsCount: int) {.cdecl.} =
2020-01-16 04:20:13 +00:00
var log: Log
2020-01-17 13:11:02 +00:00
if topicsCount > 0:
log.topics = newSeq[Topic](topicsCount)
for i in 0 ..< topicsCount:
log.topics[i] = topics[i].bytes
2020-03-09 10:20:08 +00:00
log.data = @(makeOpenArray(data, dataSize))
log.address = address
2020-01-16 04:20:13 +00:00
ctx.addLogEntry(log)
2020-01-31 01:08:44 +00:00
template createImpl(c: Computation, m: nimbus_message, res: nimbus_result) =
# TODO: use evmc_message to evoid copy
2020-02-06 04:37:44 +00:00
let childMsg = Message(
2020-02-03 05:37:33 +00:00
kind: CallKind(m.kind),
2020-01-31 01:08:44 +00:00
depth: m.depth,
gas: m.gas,
sender: m.sender,
2020-02-06 04:37:44 +00:00
value: Uint256.fromEvmc(m.value),
2020-03-09 10:20:08 +00:00
data: @(makeOpenArray(m.inputData, m.inputSize.int))
2020-01-31 01:08:44 +00:00
)
let child = newComputation(c.vmState, childMsg, Uint256.fromEvmc(m.create2_salt))
child.execCreate()
if not child.shouldBurnGas:
res.gas_left = child.gasMeter.gasRemaining
if child.isSuccess:
c.merge(child)
res.status_code = EVMC_SUCCESS
res.create_address = child.msg.contractAddress
else:
res.status_code = if child.shouldBurnGas: EVMC_FAILURE else: EVMC_REVERT
if child.output.len > 0:
2020-02-06 04:37:44 +00:00
# TODO: can we move the ownership of seq to raw pointer?
2020-01-31 01:08:44 +00:00
res.output_size = child.output.len.uint
res.output_data = cast[ptr byte](alloc(child.output.len))
copyMem(res.output_data, child.output[0].addr, child.output.len)
res.release = hostReleaseResultImpl
2020-02-03 05:37:33 +00:00
template callImpl(c: Computation, m: nimbus_message, res: nimbus_result) =
2020-02-06 04:37:44 +00:00
let childMsg = Message(
2020-02-03 05:37:33 +00:00
kind: CallKind(m.kind),
depth: m.depth,
gas: m.gas,
sender: m.sender,
codeAddress: m.destination,
contractAddress: if m.kind == EVMC_CALL: m.destination else: c.msg.contractAddress,
value: Uint256.fromEvmc(m.value),
2020-03-09 10:20:08 +00:00
data: @(makeOpenArray(m.inputData, m.inputSize.int)),
2020-02-03 05:37:33 +00:00
flags: MsgFlags(m.flags)
)
let child = newComputation(c.vmState, childMsg)
child.execCall()
if not child.shouldBurnGas:
res.gas_left = child.gasMeter.gasRemaining
if child.isSuccess:
c.merge(child)
res.status_code = EVMC_SUCCESS
else:
res.status_code = if child.shouldBurnGas: EVMC_FAILURE else: EVMC_REVERT
if child.output.len > 0:
2020-02-06 04:37:44 +00:00
# TODO: can we move the ownership of seq to raw pointer?
2020-02-03 05:37:33 +00:00
res.output_size = child.output.len.uint
res.output_data = cast[ptr byte](alloc(child.output.len))
copyMem(res.output_data, child.output[0].addr, child.output.len)
res.release = hostReleaseResultImpl
2020-01-16 04:20:13 +00:00
2020-01-31 01:08:44 +00:00
proc hostCallImpl(ctx: Computation, msg: var nimbus_message): nimbus_result {.cdecl.} =
if msg.kind == EVMC_CREATE or msg.kind == EVMC_CREATE2:
createImpl(ctx, msg, result)
else:
callImpl(ctx, msg, result)
2020-01-16 04:20:13 +00:00
proc initHostInterface(): evmc_host_interface =
result.account_exists = cast[evmc_account_exists_fn](hostAccountExistsImpl)
result.get_storage = cast[evmc_get_storage_fn](hostGetStorageImpl)
result.set_storage = cast[evmc_set_storage_fn](hostSetStorageImpl)
result.get_balance = cast[evmc_get_balance_fn](hostGetBalanceImpl)
result.get_code_size = cast[evmc_get_code_size_fn](hostGetCodeSizeImpl)
result.get_code_hash = cast[evmc_get_code_hash_fn](hostGetCodeHashImpl)
result.copy_code = cast[evmc_copy_code_fn](hostCopyCodeImpl)
result.selfdestruct = cast[evmc_selfdestruct_fn](hostSelfdestructImpl)
result.call = cast[evmc_call_fn](hostCallImpl)
result.get_tx_context = cast[evmc_get_tx_context_fn](hostGetTxContextImpl)
result.get_block_hash = cast[evmc_get_block_hash_fn](hostGetBlockHashImpl)
result.emit_log = cast[evmc_emit_log_fn](hostEmitLogImpl)
proc vmSetOptionImpl(vm: ptr evmc_vm, name, value: cstring): evmc_set_option_result {.cdecl.} =
return EVMC_SET_OPTION_INVALID_NAME
proc vmExecuteImpl(vm: ptr evmc_vm, host: ptr evmc_host_interface,
ctx: Computation, rev: evmc_revision,
msg: evmc_message, code: ptr byte, code_size: uint): evmc_result {.cdecl.} =
discard
proc vmGetCapabilitiesImpl(vm: ptr evmc_vm): evmc_capabilities {.cdecl.} =
result.incl(EVMC_CAPABILITY_EVM1)
proc vmDestroyImpl(vm: ptr evmc_vm) {.cdecl.} =
dealloc(vm)
const
EVMC_HOST_NAME = "nimbus_vm"
EVMC_VM_VERSION = "0.0.1"
proc init(vm: var evmc_vm) =
vm.abi_version = EVMC_ABI_VERSION
vm.name = EVMC_HOST_NAME
vm.version = EVMC_VM_VERSION
vm.destroy = vmDestroyImpl
vm.execute = cast[evmc_execute_fn](vmExecuteImpl)
vm.get_capabilities = vmGetCapabilitiesImpl
vm.set_option = vmSetOptionImpl
let gHost = initHostInterface()
proc nim_host_get_interface(): ptr nimbus_host_interface {.exportc, cdecl.} =
result = cast[ptr nimbus_host_interface](gHost.unsafeAddr)
2020-01-16 04:20:13 +00:00
proc nim_host_create_context(vmstate: BaseVmState, msg: ptr evmc_message): Computation {.exportc, cdecl.} =
#result = HostContext(
# vmState: vmstate,
# gasPrice: GasInt(gasPrice),
# origin: fromEvmc(origin)
#)
GC_ref(result)
proc nim_host_destroy_context(ctx: Computation) {.exportc, cdecl.} =
GC_unref(ctx)
proc nim_create_nimbus_vm(): ptr evmc_vm {.exportc, cdecl.} =
result = create(evmc_vm)
init(result[])