removed kludge and simplified sources oph_call.nim and oph_create.nim

why:
  kludge not needed anymore for oph_handlers.nim sub-sources and sources
  that rely on oph_handlers.nim (but not state_transactions.nim which
  relies on computation.nim.)
This commit is contained in:
Jordan Hrycaj 2021-04-23 18:43:44 +01:00 committed by zah
parent 77518446d9
commit caabc9c292
5 changed files with 137 additions and 249 deletions

View File

@ -14,10 +14,8 @@
const const
noisy {.intdefine.}: int = 0 noisy {.intdefine.}: int = 0
isNoisy {.used.} = noisy > 0 # isNoisy {.used.} = noisy > 0
isChatty {.used.} = noisy > 1
kludge {.intdefine.}: int = 0
breakCircularDependency {.used.} = kludge > 0
import import
strformat, strformat,
@ -28,31 +26,6 @@ import
oph_memory, oph_push, oph_dup, oph_swap, oph_log, oph_memory, oph_push, oph_dup, oph_swap, oph_log,
oph_create, oph_call, oph_sysops] oph_create, oph_call, oph_sysops]
# ------------------------------------------------------------------------------
# Kludge BEGIN
# ------------------------------------------------------------------------------
when not breakCircularDependency:
const
useExecCreate = vm2OpExecCreate
useExecCall = vm2OpExecCall
else:
# note: oph_create/call are always imported to check for syntactic corretness,
# at the moment, it would not match the other handler lists due to the
# fake Computation object definition.
const
useExecCreate: seq[Vm2OpExec] = @[]
useExecCall: seq[Vm2OpExec] = @[]
ignoreVm2OpExecCreate {.used.} = vm2OpExecCreate
ignoreVm2OpExecCall {.used.} = vm2OpExecCall
{.warning: "*** Ignoring tables from <oph_create> and <oph_call>".}
# ------------------------------------------------------------------------------
# Kludge END
# ------------------------------------------------------------------------------
const const
allHandlersList = @[ allHandlersList = @[
(vm2OpExecArithmetic, "Arithmetic"), (vm2OpExecArithmetic, "Arithmetic"),
@ -64,8 +37,8 @@ const
(vm2OpExecDup, "Dup"), (vm2OpExecDup, "Dup"),
(vm2OpExecSwap, "Swap"), (vm2OpExecSwap, "Swap"),
(vm2OpExecLog, "Log"), (vm2OpExecLog, "Log"),
(useExecCreate, "Create"), (vm2OpExecCreate, "Create"),
(useExecCall, "Call"), (vm2OpExecCall, "Call"),
(vm2OpExecSysOp, "SysOp")] (vm2OpExecSysOp, "SysOp")]
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -146,7 +119,7 @@ const
# Debugging ... # Debugging ...
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
when isMainModule and isNoisy: when isMainModule and isChatty:
proc opHandlersRun(fork: Fork; op: Op; d: var Vm2Ctx) {.used.} = proc opHandlersRun(fork: Fork; op: Op; d: var Vm2Ctx) {.used.} =
## Given a particular `fork` and an `op`-code, run the associated handler ## Given a particular `fork` and an `op`-code, run the associated handler

View File

@ -12,46 +12,26 @@
## ==================================== ## ====================================
## ##
const
kludge {.intdefine.}: int = 0
breakCircularDependency {.used.} = kludge > 0
import import
../../../constants,
../../../db/accounts_cache,
../../../errors, ../../../errors,
../../stack, ../../compu_helper,
../../memory, ../../memory,
../../stack,
../../state,
../../types,
../forks_list, ../forks_list,
../gas_costs,
../gas_meter,
../op_codes, ../op_codes,
../utils/utils_numeric, ../utils/utils_numeric,
./oph_defs,
chronicles, chronicles,
eth/common,
eth/common/eth_types, eth/common/eth_types,
stint stint
# ------------------------------------------------------------------------------
# Kludge BEGIN
# ------------------------------------------------------------------------------
when not breakCircularDependency:
import
./oph_defs,
../../../constants,
../../../db/accounts_cache,
../../compu_helper,
../../computation,
../../state,
../../types,
../gas_costs,
../gas_meter,
eth/common
else:
import
./oph_kludge
# ------------------------------------------------------------------------------
# Kludge END
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Private # Private
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -158,6 +138,29 @@ proc staticCallParams(c: Computation): LocalParams =
result.updateStackAndParams(c) result.updateStackAndParams(c)
proc execSubCall(k: var Vm2Ctx; childMsg: Message; memPos, memLen: int) =
## Call new VM -- helper for `Call`-like operations
# need to provide explicit <c> and <child> for capturing in chainTo proc()
# <memPos> and <memLen> are provided by value and need not be captured
var
c = k.cpt
child = newComputation(k.cpt.vmState, childMsg)
k.cpt.chainTo(child):
if not child.shouldBurnGas:
c.gasMeter.returnGas(child.gasMeter.gasRemaining)
if child.isSuccess:
c.merge(child)
c.stack.top(1)
c.returnData = child.output
let actualOutputSize = min(memLen, child.output.len)
if actualOutputSize > 0:
c.memory.write(memPos, child.output.toOpenArray(0, actualOutputSize - 1))
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Private, op handlers implementation # Private, op handlers implementation
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -215,34 +218,19 @@ const
k.cpt.gasMeter.returnGas(childGasLimit) k.cpt.gasMeter.returnGas(childGasLimit)
return return
let msg = Message( k.execSubCall(
kind: evmcCall, memPos = p.memOutPos,
depth: k.cpt.msg.depth + 1, memLen = p.memOutLen,
gas: childGasLimit, childMsg = Message(
sender: p.sender, kind: evmcCall,
contractAddress: p.contractAddress, depth: k.cpt.msg.depth + 1,
codeAddress: p.destination, gas: childGasLimit,
value: p.value, sender: p.sender,
data: k.cpt.memory.read(p.memInPos, p.memInLen), contractAddress: p.contractAddress,
flags: p.flags) codeAddress: p.destination,
value: p.value,
# call -- need to un-capture k data: k.cpt.memory.read(p.memInPos, p.memInLen),
var flags: p.flags))
c = k.cpt
child = newComputation(c.vmState, msg)
c.chainTo(child):
if not child.shouldBurnGas:
c.gasMeter.returnGas(child.gasMeter.gasRemaining)
if child.isSuccess:
c.merge(child)
c.stack.top(1)
c.returnData = child.output
let actualOutputSize = min(p.memOutLen, child.output.len)
if actualOutputSize > 0:
c.memory.write(p.memOutPos,
child.output.toOpenArray(0, actualOutputSize - 1))
# --------------------- # ---------------------
@ -296,34 +284,19 @@ const
k.cpt.gasMeter.returnGas(childGasLimit) k.cpt.gasMeter.returnGas(childGasLimit)
return return
let msg = Message( k.execSubCall(
kind: evmcCallCode, memPos = p.memOutPos,
depth: k.cpt.msg.depth + 1, memLen = p.memOutLen,
gas: childGasLimit, childMsg = Message(
sender: p.sender, kind: evmcCallCode,
contractAddress: p.contractAddress, depth: k.cpt.msg.depth + 1,
codeAddress: p.destination, gas: childGasLimit,
value: p.value, sender: p.sender,
data: k.cpt.memory.read(p.memInPos, p.memInLen), contractAddress: p.contractAddress,
flags: p.flags) codeAddress: p.destination,
value: p.value,
# call -- need to un-capture k data: k.cpt.memory.read(p.memInPos, p.memInLen),
var flags: p.flags))
c = k.cpt
child = newComputation(c.vmState, msg)
c.chainTo(child):
if not child.shouldBurnGas:
c.gasMeter.returnGas(child.gasMeter.gasRemaining)
if child.isSuccess:
c.merge(child)
c.stack.top(1)
c.returnData = child.output
let actualOutputSize = min(p.memOutLen, child.output.len)
if actualOutputSize > 0:
c.memory.write(p.memOutPos,
child.output.toOpenArray(0, actualOutputSize - 1))
# --------------------- # ---------------------
@ -366,34 +339,19 @@ const
k.cpt.memory.extend(p.memInPos, p.memInLen) k.cpt.memory.extend(p.memInPos, p.memInLen)
k.cpt.memory.extend(p.memOutPos, p.memOutLen) k.cpt.memory.extend(p.memOutPos, p.memOutLen)
let msg = Message( k.execSubCall(
kind: evmcDelegateCall, memPos = p.memOutPos,
depth: k.cpt.msg.depth + 1, memLen = p.memOutLen,
gas: childGasLimit, childMsg = Message(
sender: p.sender, kind: evmcDelegateCall,
contractAddress: p.contractAddress, depth: k.cpt.msg.depth + 1,
codeAddress: p.destination, gas: childGasLimit,
value: p.value, sender: p.sender,
data: k.cpt.memory.read(p.memInPos, p.memInLen), contractAddress: p.contractAddress,
flags: p.flags) codeAddress: p.destination,
value: p.value,
# call -- need to un-capture k data: k.cpt.memory.read(p.memInPos, p.memInLen),
var flags: p.flags))
c = k.cpt
child = newComputation(c.vmState, msg)
c.chainTo(child):
if not child.shouldBurnGas:
c.gasMeter.returnGas(child.gasMeter.gasRemaining)
if child.isSuccess:
c.merge(child)
c.stack.top(1)
c.returnData = child.output
let actualOutputSize = min(p.memOutLen, child.output.len)
if actualOutputSize > 0:
c.memory.write(p.memOutPos,
child.output.toOpenArray(0, actualOutputSize - 1))
# --------------------- # ---------------------
@ -441,34 +399,19 @@ const
k.cpt.memory.extend(p.memInPos, p.memInLen) k.cpt.memory.extend(p.memInPos, p.memInLen)
k.cpt.memory.extend(p.memOutPos, p.memOutLen) k.cpt.memory.extend(p.memOutPos, p.memOutLen)
let msg = Message( k.execSubCall(
kind: evmcCall, memPos = p.memOutPos,
depth: k.cpt.msg.depth + 1, memLen = p.memOutLen,
gas: childGasLimit, childMsg = Message(
sender: p.sender, kind: evmcCall,
contractAddress: p.contractAddress, depth: k.cpt.msg.depth + 1,
codeAddress: p.destination, gas: childGasLimit,
value: p.value, sender: p.sender,
data: k.cpt.memory.read(p.memInPos, p.memInLen), contractAddress: p.contractAddress,
flags: p.flags) codeAddress: p.destination,
value: p.value,
# call -- need to un-capture k data: k.cpt.memory.read(p.memInPos, p.memInLen),
var flags: p.flags))
c = k.cpt
child = newComputation(c.vmState, msg)
c.chainTo(child):
if not child.shouldBurnGas:
c.gasMeter.returnGas(child.gasMeter.gasRemaining)
if child.isSuccess:
c.merge(child)
c.stack.top(1)
c.returnData = child.output
let actualOutputSize = min(p.memOutLen, child.output.len)
if actualOutputSize > 0:
c.memory.write(p.memOutPos,
child.output.toOpenArray(0, actualOutputSize - 1))
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Public, op exec table entries # Public, op exec table entries

View File

@ -12,46 +12,48 @@
## ====================================== ## ======================================
## ##
const
kludge {.intdefine.}: int = 0
breakCircularDependency {.used.} = kludge > 0
import import
../../../constants,
../../../errors, ../../../errors,
../../stack, ../../compu_helper,
../../memory, ../../memory,
../../stack,
../../state,
../../types,
../forks_list, ../forks_list,
../gas_costs,
../gas_meter,
../op_codes, ../op_codes,
../utils/utils_numeric, ../utils/utils_numeric,
./oph_defs,
./oph_helpers,
chronicles, chronicles,
eth/common,
eth/common/eth_types, eth/common/eth_types,
stint, stint,
strformat strformat
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Kludge BEGIN # Private helpers
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
when not breakCircularDependency: proc execSubCreate(k: var Vm2Ctx; childMsg: Message; salt = 0.u256) =
import ## Create new VM -- helper for `Create`-like operations
../../../constants,
../../compu_helper,
../../computation,
../../state,
../../types,
../gas_costs,
../gas_meter,
./oph_defs,
./oph_helpers,
eth/common
else: # need to provide explicit <c> and <child> for capturing in chainTo proc()
import var
./oph_kludge c = k.cpt
child = newComputation(k.cpt.vmState, childMsg, salt)
# ------------------------------------------------------------------------------ k.cpt.chainTo(child):
# Kludge END if not child.shouldBurnGas:
# ------------------------------------------------------------------------------ c.gasMeter.returnGas(child.gasMeter.gasRemaining)
if child.isSuccess:
c.merge(child)
c.stack.top child.msg.contractAddress
else:
c.returnData = child.output
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Private, op handlers implementation # Private, op handlers implementation
@ -66,7 +68,6 @@ const
endowment = k.cpt.stack.popInt() endowment = k.cpt.stack.popInt()
memPos = k.cpt.stack.popInt().safeInt memPos = k.cpt.stack.popInt().safeInt
memLen = k.cpt.stack.peekInt().safeInt memLen = k.cpt.stack.peekInt().safeInt
salt = 0.u256
k.cpt.stack.top(0) k.cpt.stack.top(0)
@ -103,27 +104,14 @@ const
createMsgGas -= createMsgGas div 64 createMsgGas -= createMsgGas div 64
k.cpt.gasMeter.consumeGas(createMsgGas, reason = "CREATE") k.cpt.gasMeter.consumeGas(createMsgGas, reason = "CREATE")
let childMsg = Message( k.execSubCreate(
kind: evmcCreate, childMsg = Message(
depth: k.cpt.msg.depth + 1, kind: evmcCreate,
gas: createMsgGas, depth: k.cpt.msg.depth + 1,
sender: k.cpt.msg.contractAddress, gas: createMsgGas,
value: endowment, sender: k.cpt.msg.contractAddress,
data: k.cpt.memory.read(memPos, memLen)) value: endowment,
data: k.cpt.memory.read(memPos, memLen)))
# call -- need to un-capture k
var
c = k.cpt
child = newComputation(c.vmState, childMsg, salt)
c.chainTo(child):
if not child.shouldBurnGas:
c.gasMeter.returnGas(child.gasMeter.gasRemaining)
if child.isSuccess:
c.merge(child)
c.stack.top child.msg.contractAddress
else:
c.returnData = child.output
# --------------------- # ---------------------
@ -174,27 +162,15 @@ const
createMsgGas -= createMsgGas div 64 createMsgGas -= createMsgGas div 64
k.cpt.gasMeter.consumeGas(createMsgGas, reason = "CREATE") k.cpt.gasMeter.consumeGas(createMsgGas, reason = "CREATE")
let childMsg = Message( k.execSubCreate(
kind: evmcCreate2, salt = salt,
depth: k.cpt.msg.depth + 1, childMsg = Message(
gas: createMsgGas, kind: evmcCreate2,
sender: k.cpt.msg.contractAddress, depth: k.cpt.msg.depth + 1,
value: endowment, gas: createMsgGas,
data: k.cpt.memory.read(memPos, memLen)) sender: k.cpt.msg.contractAddress,
value: endowment,
# call -- need to un-capture k data: k.cpt.memory.read(memPos, memLen)))
var
c = k.cpt
child = newComputation(c.vmState, childMsg, salt)
c.chainTo(child):
if not child.shouldBurnGas:
c.gasMeter.returnGas(child.gasMeter.gasRemaining)
if child.isSuccess:
c.merge(child)
c.stack.top child.msg.contractAddress
else:
c.returnData = child.output
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Public, op exec table entries # Public, op exec table entries

View File

@ -20,7 +20,6 @@ import
../../stack, ../../stack,
../../state, ../../state,
../../types, ../../types,
../forks_list,
../gas_costs, ../gas_costs,
../gas_meter, ../gas_meter,
../op_codes, ../op_codes,

View File

@ -13,13 +13,10 @@ const
lowmem {.intdefine.}: int = 0 lowmem {.intdefine.}: int = 0
lowMemoryCompileTime {.used.} = lowmem > 0 lowMemoryCompileTime {.used.} = lowmem > 0
# debugging flag # debugging flag, dump macro info when asked for
noisy {.intdefine.}: int = 0 noisy {.intdefine.}: int = 0
isNoisy {.used.} = noisy > 0 # isNoisy {.used.} = noisy > 0
isChatty {.used.} = noisy > 1
# needed for compiling locally
kludge {.intdefine.}: int = 0
breakCircularDependency {.used.} = kludge > 0
import import
./compu_helper, ./compu_helper,
@ -121,7 +118,7 @@ proc toCaseStmt(forkArg, opArg, k: NimNode): NimNode =
newIdentNode(op.toSymbolName), newIdentNode(op.toSymbolName),
branchStmt) branchStmt)
when breakCircularDependency and isNoisy: when isChatty:
echo ">>> ", result.repr echo ">>> ", result.repr