mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-12 21:34:33 +00:00
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:
parent
d373ab6460
commit
f4bc9c4561
@ -41,6 +41,8 @@ when not breakCircularDependency:
|
|||||||
else:
|
else:
|
||||||
import macros
|
import macros
|
||||||
|
|
||||||
|
var blindGasCosts: array[Op,int]
|
||||||
|
|
||||||
# copied from stack.nim
|
# copied from stack.nim
|
||||||
macro genTupleType(len: static[int], elemType: untyped): untyped =
|
macro genTupleType(len: static[int], elemType: untyped): untyped =
|
||||||
result = nnkTupleConstr.newNimNode()
|
result = nnkTupleConstr.newNimNode()
|
||||||
@ -53,11 +55,20 @@ else:
|
|||||||
var rc: genTupleType(n, UInt256)
|
var rc: genTupleType(n, UInt256)
|
||||||
return rc
|
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
|
# function stubs from v2utils_numeric.nim
|
||||||
proc extractSign(v: var UInt256, sign: var bool) = discard
|
proc extractSign(v: var UInt256, sign: var bool) = discard
|
||||||
proc setSign(v: var UInt256, sign: bool) = discard
|
proc setSign(v: var UInt256, sign: bool) = discard
|
||||||
func safeInt(x: Uint256): int = 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
|
# Kludge END
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
@ -166,10 +177,9 @@ const
|
|||||||
## 0x0A, Exponentiation
|
## 0x0A, Exponentiation
|
||||||
let (base, exponent) = k.cpt.stack.popInt(2)
|
let (base, exponent) = k.cpt.stack.popInt(2)
|
||||||
|
|
||||||
when not breakCircularDependency:
|
k.cpt.gasMeter.consumeGas(
|
||||||
k.cpt.gasMeter.consumeGas(
|
k.cpt.gasCosts[Exp].d_handler(exponent),
|
||||||
k.cpt.gasCosts[Exp].d_handler(exponent),
|
reason = "EXP: exponent bytes")
|
||||||
reason = "EXP: exponent bytes")
|
|
||||||
|
|
||||||
k.cpt.stack.push:
|
k.cpt.stack.push:
|
||||||
if not base.isZero:
|
if not base.isZero:
|
||||||
|
@ -41,6 +41,8 @@ when not breakCircularDependency:
|
|||||||
else:
|
else:
|
||||||
import macros
|
import macros
|
||||||
|
|
||||||
|
var blindGasCosts: array[Op,int]
|
||||||
|
|
||||||
# copied from stack.nim
|
# copied from stack.nim
|
||||||
macro genTupleType(len: static[int], elemType: untyped): untyped =
|
macro genTupleType(len: static[int], elemType: untyped): untyped =
|
||||||
result = nnkTupleConstr.newNimNode()
|
result = nnkTupleConstr.newNimNode()
|
||||||
@ -54,11 +56,12 @@ else:
|
|||||||
return rc
|
return rc
|
||||||
|
|
||||||
# function stubs from v2computation.nim (to satisfy compiler logic)
|
# 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 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 getCodeSize[T](c: Computation, address: T): uint = 0
|
||||||
proc getCode[T](c: Computation, address: T): seq[byte] = @[]
|
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
|
# function stubs from v2utils_numeric.nim
|
||||||
func cleanMemRef(x: UInt256): int = 0
|
func cleanMemRef(x: UInt256): int = 0
|
||||||
@ -71,6 +74,12 @@ else:
|
|||||||
# function stubs from code_stream.nim
|
# function stubs from code_stream.nim
|
||||||
proc len(c: CodeStream): int = len(c.bytes)
|
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
|
# Kludge END
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
@ -164,10 +173,9 @@ const
|
|||||||
let (memPos, copyPos, len) =
|
let (memPos, copyPos, len) =
|
||||||
(memStartPos.cleanMemRef, copyStartPos.cleanMemRef, size.cleanMemRef)
|
(memStartPos.cleanMemRef, copyStartPos.cleanMemRef, size.cleanMemRef)
|
||||||
|
|
||||||
when not breakCircularDependency:
|
k.cpt.gasMeter.consumeGas(
|
||||||
k.cpt.gasMeter.consumeGas(
|
k.cpt.gasCosts[CallDataCopy].m_handler(k.cpt.memory.len, memPos, len),
|
||||||
k.cpt.gasCosts[CallDataCopy].m_handler(k.cpt.memory.len, memPos, len),
|
reason = "CallDataCopy fee")
|
||||||
reason = "CallDataCopy fee")
|
|
||||||
|
|
||||||
k.cpt.memory.writePaddedResult(k.cpt.msg.data, memPos, copyPos, len)
|
k.cpt.memory.writePaddedResult(k.cpt.msg.data, memPos, copyPos, len)
|
||||||
|
|
||||||
@ -186,10 +194,9 @@ const
|
|||||||
let (memPos, copyPos, len) =
|
let (memPos, copyPos, len) =
|
||||||
(memStartPos.cleanMemRef, copyStartPos.cleanMemRef, size.cleanMemRef)
|
(memStartPos.cleanMemRef, copyStartPos.cleanMemRef, size.cleanMemRef)
|
||||||
|
|
||||||
when not breakCircularDependency:
|
k.cpt.gasMeter.consumeGas(
|
||||||
k.cpt.gasMeter.consumeGas(
|
k.cpt.gasCosts[CodeCopy].m_handler(k.cpt.memory.len, memPos, len),
|
||||||
k.cpt.gasCosts[CodeCopy].m_handler(k.cpt.memory.len, memPos, len),
|
reason = "CodeCopy fee")
|
||||||
reason = "CodeCopy fee")
|
|
||||||
|
|
||||||
k.cpt.memory.writePaddedResult(k.cpt.code.bytes, memPos, copyPos, len)
|
k.cpt.memory.writePaddedResult(k.cpt.code.bytes, memPos, copyPos, len)
|
||||||
|
|
||||||
@ -214,10 +221,9 @@ const
|
|||||||
let (memPos, codePos, len) =
|
let (memPos, codePos, len) =
|
||||||
(memStartPos.cleanMemRef, codeStartPos.cleanMemRef, size.cleanMemRef)
|
(memStartPos.cleanMemRef, codeStartPos.cleanMemRef, size.cleanMemRef)
|
||||||
|
|
||||||
when not breakCircularDependency:
|
k.cpt.gasMeter.consumeGas(
|
||||||
k.cpt.gasMeter.consumeGas(
|
k.cpt.gasCosts[ExtCodeCopy].m_handler(k.cpt.memory.len, memPos, len),
|
||||||
k.cpt.gasCosts[ExtCodeCopy].m_handler(k.cpt.memory.len, memPos, len),
|
reason = "ExtCodeCopy fee")
|
||||||
reason = "ExtCodeCopy fee")
|
|
||||||
|
|
||||||
let codeBytes = k.cpt.getCode(address)
|
let codeBytes = k.cpt.getCode(address)
|
||||||
k.cpt.memory.writePaddedResult(codeBytes, memPos, codePos, len)
|
k.cpt.memory.writePaddedResult(codeBytes, memPos, codePos, len)
|
||||||
@ -237,10 +243,9 @@ const
|
|||||||
let (memPos, copyPos, len) =
|
let (memPos, copyPos, len) =
|
||||||
(memStartPos.cleanMemRef, copyStartPos.cleanMemRef, size.cleanMemRef)
|
(memStartPos.cleanMemRef, copyStartPos.cleanMemRef, size.cleanMemRef)
|
||||||
|
|
||||||
when not breakCircularDependency:
|
let gasCost = k.cpt.gasCosts[ReturnDataCopy].m_handler(
|
||||||
let gasCost = k.cpt.gasCosts[ReturnDataCopy].m_handler(
|
k.cpt.memory.len, memPos, len)
|
||||||
k.cpt.memory.len, memPos, len)
|
k.cpt.gasMeter.consumeGas(gasCost, reason = "returnDataCopy fee")
|
||||||
k.cpt.gasMeter.consumeGas(gasCost, reason = "returnDataCopy fee")
|
|
||||||
|
|
||||||
if copyPos + len > k.cpt.returnData.len:
|
if copyPos + len > k.cpt.returnData.len:
|
||||||
raise newException(
|
raise newException(
|
||||||
|
@ -40,6 +40,8 @@ when not breakCircularDependency:
|
|||||||
else:
|
else:
|
||||||
import macros
|
import macros
|
||||||
|
|
||||||
|
var blindGasCosts: array[Op,int]
|
||||||
|
|
||||||
# copied from stack.nim
|
# copied from stack.nim
|
||||||
macro genTupleType(len: static[int], elemType: untyped): untyped =
|
macro genTupleType(len: static[int], elemType: untyped): untyped =
|
||||||
result = nnkTupleConstr.newNimNode()
|
result = nnkTupleConstr.newNimNode()
|
||||||
@ -58,6 +60,12 @@ else:
|
|||||||
proc len(mem: Memory): int = 0
|
proc len(mem: Memory): int = 0
|
||||||
proc extend(mem: var Memory; startPos: Natural; size: Natural) = discard
|
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
|
# dummy stubs from constants
|
||||||
const EMPTY_SHA3 = 0xdeadbeef.u256
|
const EMPTY_SHA3 = 0xdeadbeef.u256
|
||||||
|
|
||||||
@ -65,6 +73,9 @@ else:
|
|||||||
const keccak256 = 0xfeedbeef
|
const keccak256 = 0xfeedbeef
|
||||||
proc digest(dummy: int64, data: openarray[byte]): UInt256 = EMPTY_SHA3
|
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
|
# Kludge END
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
@ -82,12 +93,12 @@ const
|
|||||||
if pos < 0 or len < 0 or pos > 2147483648'i64:
|
if pos < 0 or len < 0 or pos > 2147483648'i64:
|
||||||
raise newException(OutOfBoundsRead, "Out of bounds memory access")
|
raise newException(OutOfBoundsRead, "Out of bounds memory access")
|
||||||
|
|
||||||
when not breakCircularDependency:
|
k.cpt.gasMeter.consumeGas(
|
||||||
k.cpt.gasMeter.consumeGas(
|
k.cpt.gasCosts[Op.Sha3].m_handler(k.cpt.memory.len, pos, len),
|
||||||
k.cpt.gasCosts[Op.Sha3].m_handler(k.cpt.memory.len, pos, len),
|
reason = "SHA3: word gas cost")
|
||||||
reason = "SHA3: word gas cost")
|
|
||||||
|
|
||||||
k.cpt.memory.extend(pos, len)
|
k.cpt.memory.extend(pos, len)
|
||||||
|
|
||||||
let endRange = min(pos + len, k.cpt.memory.len) - 1
|
let endRange = min(pos + len, k.cpt.memory.len) - 1
|
||||||
if endRange == -1 or pos >= k.cpt.memory.len:
|
if endRange == -1 or pos >= k.cpt.memory.len:
|
||||||
k.cpt.stack.push(EMPTY_SHA3)
|
k.cpt.stack.push(EMPTY_SHA3)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user