updated circular dependency klugde

why:
  removing <when> clauses and replacing gas calculation by stubs
  makes up for better reading of the code
This commit is contained in:
Jordan Hrycaj 2021-04-14 17:51:54 +01:00 committed by zah
parent d373ab6460
commit f4bc9c4561
3 changed files with 52 additions and 26 deletions

View File

@ -41,6 +41,8 @@ when not breakCircularDependency:
else:
import macros
var blindGasCosts: array[Op,int]
# copied from stack.nim
macro genTupleType(len: static[int], elemType: untyped): untyped =
result = nnkTupleConstr.newNimNode()
@ -53,11 +55,20 @@ else:
var rc: genTupleType(n, UInt256)
return rc
# function stubs from v2computation.nim (to satisfy compiler logic)
proc gasCosts(c: Computation): array[Op,int] = blindGasCosts
# function stubs from v2utils_numeric.nim
proc extractSign(v: var UInt256, sign: var bool) = discard
proc setSign(v: var UInt256, sign: bool) = discard
func safeInt(x: Uint256): int = discard
# function stubs from gas_meter.nim
proc consumeGas(gasMeter: var GasMeter; amount: int; reason: string) = discard
# stubs from v2gas_costs.nim
proc d_handler(x: int; value: Uint256): int = 0
# ------------------------------------------------------------------------------
# Kludge END
# ------------------------------------------------------------------------------
@ -166,10 +177,9 @@ const
## 0x0A, Exponentiation
let (base, exponent) = k.cpt.stack.popInt(2)
when not breakCircularDependency:
k.cpt.gasMeter.consumeGas(
k.cpt.gasCosts[Exp].d_handler(exponent),
reason = "EXP: exponent bytes")
k.cpt.gasMeter.consumeGas(
k.cpt.gasCosts[Exp].d_handler(exponent),
reason = "EXP: exponent bytes")
k.cpt.stack.push:
if not base.isZero:

View File

@ -41,6 +41,8 @@ when not breakCircularDependency:
else:
import macros
var blindGasCosts: array[Op,int]
# copied from stack.nim
macro genTupleType(len: static[int], elemType: untyped): untyped =
result = nnkTupleConstr.newNimNode()
@ -54,11 +56,12 @@ else:
return rc
# function stubs from v2computation.nim (to satisfy compiler logic)
proc gasCosts(c: Computation): array[Op,int] = blindGasCosts
proc getBalance[T](c: Computation, address: T): Uint256 = 0.u256
proc getOrigin(c: Computation): Uint256 = 0.u256
proc getGasPrice(c: Computation): Uint256 = 0.u256
proc getCodeSize[T](c: Computation, address: T): uint = 0
proc getCode[T](c: Computation, address: T): seq[byte] = @[]
proc getGasPrice(c: Computation): Uint256 = 0.u256
proc getOrigin(c: Computation): Uint256 = 0.u256
# function stubs from v2utils_numeric.nim
func cleanMemRef(x: UInt256): int = 0
@ -71,6 +74,12 @@ else:
# function stubs from code_stream.nim
proc len(c: CodeStream): int = len(c.bytes)
# function stubs from gas_meter.nim
proc consumeGas(gasMeter: var GasMeter; amount: int; reason: string) = discard
# stubs from v2gas_costs.nim
proc m_handler(x: int; curMemSize, memOffset, memLen: int64): int = 0
# ------------------------------------------------------------------------------
# Kludge END
# ------------------------------------------------------------------------------
@ -164,10 +173,9 @@ const
let (memPos, copyPos, len) =
(memStartPos.cleanMemRef, copyStartPos.cleanMemRef, size.cleanMemRef)
when not breakCircularDependency:
k.cpt.gasMeter.consumeGas(
k.cpt.gasCosts[CallDataCopy].m_handler(k.cpt.memory.len, memPos, len),
reason = "CallDataCopy fee")
k.cpt.gasMeter.consumeGas(
k.cpt.gasCosts[CallDataCopy].m_handler(k.cpt.memory.len, memPos, len),
reason = "CallDataCopy fee")
k.cpt.memory.writePaddedResult(k.cpt.msg.data, memPos, copyPos, len)
@ -186,10 +194,9 @@ const
let (memPos, copyPos, len) =
(memStartPos.cleanMemRef, copyStartPos.cleanMemRef, size.cleanMemRef)
when not breakCircularDependency:
k.cpt.gasMeter.consumeGas(
k.cpt.gasCosts[CodeCopy].m_handler(k.cpt.memory.len, memPos, len),
reason = "CodeCopy fee")
k.cpt.gasMeter.consumeGas(
k.cpt.gasCosts[CodeCopy].m_handler(k.cpt.memory.len, memPos, len),
reason = "CodeCopy fee")
k.cpt.memory.writePaddedResult(k.cpt.code.bytes, memPos, copyPos, len)
@ -214,10 +221,9 @@ const
let (memPos, codePos, len) =
(memStartPos.cleanMemRef, codeStartPos.cleanMemRef, size.cleanMemRef)
when not breakCircularDependency:
k.cpt.gasMeter.consumeGas(
k.cpt.gasCosts[ExtCodeCopy].m_handler(k.cpt.memory.len, memPos, len),
reason = "ExtCodeCopy fee")
k.cpt.gasMeter.consumeGas(
k.cpt.gasCosts[ExtCodeCopy].m_handler(k.cpt.memory.len, memPos, len),
reason = "ExtCodeCopy fee")
let codeBytes = k.cpt.getCode(address)
k.cpt.memory.writePaddedResult(codeBytes, memPos, codePos, len)
@ -237,10 +243,9 @@ const
let (memPos, copyPos, len) =
(memStartPos.cleanMemRef, copyStartPos.cleanMemRef, size.cleanMemRef)
when not breakCircularDependency:
let gasCost = k.cpt.gasCosts[ReturnDataCopy].m_handler(
k.cpt.memory.len, memPos, len)
k.cpt.gasMeter.consumeGas(gasCost, reason = "returnDataCopy fee")
let gasCost = k.cpt.gasCosts[ReturnDataCopy].m_handler(
k.cpt.memory.len, memPos, len)
k.cpt.gasMeter.consumeGas(gasCost, reason = "returnDataCopy fee")
if copyPos + len > k.cpt.returnData.len:
raise newException(

View File

@ -40,6 +40,8 @@ when not breakCircularDependency:
else:
import macros
var blindGasCosts: array[Op,int]
# copied from stack.nim
macro genTupleType(len: static[int], elemType: untyped): untyped =
result = nnkTupleConstr.newNimNode()
@ -58,6 +60,12 @@ else:
proc len(mem: Memory): int = 0
proc extend(mem: var Memory; startPos: Natural; size: Natural) = discard
# function stubs from v2computation.nim (to satisfy compiler logic)
proc gasCosts(c: Computation): array[Op,int] = blindGasCosts
# function stubs from gas_meter.nim
proc consumeGas(gasMeter: var GasMeter; amount: int; reason: string) = discard
# dummy stubs from constants
const EMPTY_SHA3 = 0xdeadbeef.u256
@ -65,6 +73,9 @@ else:
const keccak256 = 0xfeedbeef
proc digest(dummy: int64, data: openarray[byte]): UInt256 = EMPTY_SHA3
# stubs from v2gas_costs.nim
proc m_handler(x: int; curMemSize, memOffset, memLen: int64): int = 0
# ------------------------------------------------------------------------------
# Kludge END
# ------------------------------------------------------------------------------
@ -82,12 +93,12 @@ const
if pos < 0 or len < 0 or pos > 2147483648'i64:
raise newException(OutOfBoundsRead, "Out of bounds memory access")
when not breakCircularDependency:
k.cpt.gasMeter.consumeGas(
k.cpt.gasCosts[Op.Sha3].m_handler(k.cpt.memory.len, pos, len),
reason = "SHA3: word gas cost")
k.cpt.gasMeter.consumeGas(
k.cpt.gasCosts[Op.Sha3].m_handler(k.cpt.memory.len, pos, len),
reason = "SHA3: word gas cost")
k.cpt.memory.extend(pos, len)
let endRange = min(pos + len, k.cpt.memory.len) - 1
if endRange == -1 or pos >= k.cpt.memory.len:
k.cpt.stack.push(EMPTY_SHA3)