less explicit 'copyMem'

This commit is contained in:
andri lim 2020-02-06 11:37:44 +07:00 committed by zah
parent 7c9f6b48d6
commit deb09f40f0
3 changed files with 15 additions and 31 deletions

View File

@ -116,25 +116,20 @@ proc hostEmitLogImpl(ctx: Computation, address: EthAddress,
for i in 0 ..< topicsCount: for i in 0 ..< topicsCount:
log.topics[i] = topics[i].bytes log.topics[i] = topics[i].bytes
if dataSize > 0: log.data = @makeOpenArray(data, dataSize)
log.data = newSeq[byte](dataSize)
copyMem(log.data[0].addr, data, dataSize)
log.address = address log.address = address
ctx.addLogEntry(log) ctx.addLogEntry(log)
template createImpl(c: Computation, m: nimbus_message, res: nimbus_result) = template createImpl(c: Computation, m: nimbus_message, res: nimbus_result) =
# TODO: use evmc_message to evoid copy # TODO: use evmc_message to evoid copy
var childMsg = Message( let childMsg = Message(
kind: CallKind(m.kind), kind: CallKind(m.kind),
depth: m.depth, depth: m.depth,
gas: m.gas, gas: m.gas,
sender: m.sender, sender: m.sender,
value: Uint256.fromEvmc(m.value) value: Uint256.fromEvmc(m.value),
data: @makeOpenArray(m.inputData, m.inputSize.int)
) )
if m.input_size.int > 0:
childMsg.data = newSeq[byte](m.input_size.int)
copyMem(childMsg.data[0].addr, m.input_data, m.input_size.int)
let child = newComputation(c.vmState, childMsg, Uint256.fromEvmc(m.create2_salt)) let child = newComputation(c.vmState, childMsg, Uint256.fromEvmc(m.create2_salt))
child.execCreate() child.execCreate()
@ -149,13 +144,14 @@ template createImpl(c: Computation, m: nimbus_message, res: nimbus_result) =
else: else:
res.status_code = if child.shouldBurnGas: EVMC_FAILURE else: EVMC_REVERT res.status_code = if child.shouldBurnGas: EVMC_FAILURE else: EVMC_REVERT
if child.output.len > 0: if child.output.len > 0:
# TODO: can we move the ownership of seq to raw pointer?
res.output_size = child.output.len.uint res.output_size = child.output.len.uint
res.output_data = cast[ptr byte](alloc(child.output.len)) res.output_data = cast[ptr byte](alloc(child.output.len))
copyMem(res.output_data, child.output[0].addr, child.output.len) copyMem(res.output_data, child.output[0].addr, child.output.len)
res.release = hostReleaseResultImpl res.release = hostReleaseResultImpl
template callImpl(c: Computation, m: nimbus_message, res: nimbus_result) = template callImpl(c: Computation, m: nimbus_message, res: nimbus_result) =
var childMsg = Message( let childMsg = Message(
kind: CallKind(m.kind), kind: CallKind(m.kind),
depth: m.depth, depth: m.depth,
gas: m.gas, gas: m.gas,
@ -163,13 +159,10 @@ template callImpl(c: Computation, m: nimbus_message, res: nimbus_result) =
codeAddress: m.destination, codeAddress: m.destination,
contractAddress: if m.kind == EVMC_CALL: m.destination else: c.msg.contractAddress, contractAddress: if m.kind == EVMC_CALL: m.destination else: c.msg.contractAddress,
value: Uint256.fromEvmc(m.value), value: Uint256.fromEvmc(m.value),
data: @makeOpenArray(m.inputData, m.inputSize.int)
flags: MsgFlags(m.flags) flags: MsgFlags(m.flags)
) )
if m.input_size.int > 0:
childMsg.data = newSeq[byte](m.input_size.int)
copyMem(childMsg.data[0].addr, m.input_data, m.input_size.int)
let child = newComputation(c.vmState, childMsg) let child = newComputation(c.vmState, childMsg)
child.execCall() child.execCall()
@ -183,6 +176,7 @@ template callImpl(c: Computation, m: nimbus_message, res: nimbus_result) =
res.status_code = if child.shouldBurnGas: EVMC_FAILURE else: EVMC_REVERT res.status_code = if child.shouldBurnGas: EVMC_FAILURE else: EVMC_REVERT
if child.output.len > 0: if child.output.len > 0:
# TODO: can we move the ownership of seq to raw pointer?
res.output_size = child.output.len.uint res.output_size = child.output.len.uint
res.output_data = cast[ptr byte](alloc(child.output.len)) res.output_data = cast[ptr byte](alloc(child.output.len))
copyMem(res.output_data, child.output[0].addr, child.output.len) copyMem(res.output_data, child.output[0].addr, child.output.len)

View File

@ -7,7 +7,7 @@
import import
strformat, times, sets, stew/ranges, sequtils, options, strformat, times, sets, stew/ranges, sequtils, options,
chronicles, stint, nimcrypto, stew/ranges/typedranges, eth/common, chronicles, stint, nimcrypto, stew/ranges/[typedranges, ptr_arith], eth/common,
./utils/[macros_procs_opcodes, utils_numeric], ./utils/[macros_procs_opcodes, utils_numeric],
./gas_meter, ./gas_costs, ./opcode_values, ./vm_forks, ./gas_meter, ./gas_costs, ./opcode_values, ./vm_forks,
../memory, ../message, ../stack, ../code_stream, ../computation, ../memory, ../message, ../stack, ../code_stream, ../computation,
@ -606,10 +606,7 @@ template genCreate(callName: untyped, opCode: Op): untyped =
) )
var res = c.host.call(msg) var res = c.host.call(msg)
if res.output_size.int > 0: c.returnData = @makeOpenArray(res.outputData, res.outputSize.int)
c.returnData = newSeq[byte](res.output_size.int)
copyMem(c.returnData[0].addr, res.output_data, c.returnData.len)
c.gasMeter.returnGas(res.gas_left) c.gasMeter.returnGas(res.gas_left)
if res.status_code == EVMC_SUCCESS: if res.status_code == EVMC_SUCCESS:
@ -775,9 +772,7 @@ template genCall(callName: untyped, opCode: Op): untyped =
) )
var res = c.host.call(msg) var res = c.host.call(msg)
if res.output_size.int > 0: c.returnData = @makeOpenArray(res.outputData, res.outputSize.int)
c.returnData = newSeq[byte](res.output_size.int)
copyMem(c.returnData[0].addr, res.output_data, c.returnData.len)
let actualOutputSize = min(memOutLen, c.returnData.len) let actualOutputSize = min(memOutLen, c.returnData.len)
if actualOutputSize > 0: if actualOutputSize > 0:

View File

@ -245,10 +245,8 @@ proc bn256ecAdd*(computation: Computation, fork: Fork = FkByzantium) =
input: array[128, byte] input: array[128, byte]
output: array[64, byte] output: array[64, byte]
# Padding data # Padding data
let msglen = len(computation.msg.data) let len = min(computation.msg.data.len, 128) - 1
let tocopy = if msglen < 128: msglen else: 128 input[0..len] = computation.msg.data[0..len]
if tocopy > 0:
copyMem(addr input[0], addr computation.msg.data[0], tocopy)
var p1 = G1.getPoint(input.toOpenArray(0, 63)) var p1 = G1.getPoint(input.toOpenArray(0, 63))
var p2 = G1.getPoint(input.toOpenArray(64, 127)) var p2 = G1.getPoint(input.toOpenArray(64, 127))
var apo = (p1 + p2).toAffine() var apo = (p1 + p2).toAffine()
@ -267,11 +265,8 @@ proc bn256ecMul*(computation: Computation, fork: Fork = FkByzantium) =
output: array[64, byte] output: array[64, byte]
# Padding data # Padding data
let msglen = len(computation.msg.data) let len = min(computation.msg.data.len, 96) - 1
let tocopy = if msglen < 96: msglen else: 96 input[0..len] = computation.msg.data[0..len]
if tocopy > 0:
copyMem(addr input[0], addr computation.msg.data[0], tocopy)
var p1 = G1.getPoint(input.toOpenArray(0, 63)) var p1 = G1.getPoint(input.toOpenArray(0, 63))
var fr = getFR(input.toOpenArray(64, 95)) var fr = getFR(input.toOpenArray(64, 95))
var apo = (p1 * fr).toAffine() var apo = (p1 * fr).toAffine()