Skip to content

Commit

Permalink
fix slow operations in evm
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Aug 1, 2023
1 parent 9be5df9 commit dc5907c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
12 changes: 4 additions & 8 deletions nimbus/evm/interpreter/op_handlers/oph_memory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import
./oph_defs,
./oph_helpers,
eth/common,
stint,
strformat
stint

{.push raises: [CatchableError].} # basically the annotation type of a `Vm2OpFn`

Expand All @@ -50,8 +49,7 @@ when evmc_enabled:
gasCost = c.gasCosts[Sstore].c_handler(newValue, gasParam)[0]

c.gasMeter.consumeGas(
gasCost, &"SSTORE: {c.msg.contractAddress}[{slot}] " &
&"-> {newValue} ({currentValue})")
gasCost, "SSTORE")

else:
proc sstoreImpl(c: Computation, slot, newValue: UInt256) =
Expand All @@ -65,8 +63,7 @@ else:
c.gasCosts[Sstore].c_handler(newValue, gasParam)

c.gasMeter.consumeGas(
gasCost, &"SSTORE: {c.msg.contractAddress}[{slot}] " &
&"-> {newValue} ({currentValue})")
gasCost, "SSTORE")
if gasRefund > 0:
c.gasMeter.refundGas(gasRefund)

Expand All @@ -87,8 +84,7 @@ else:
(gasCost, gasRefund) = c.gasCosts[Sstore].c_handler(newValue, gasParam)

c.gasMeter.consumeGas(
gasCost, &"SSTORE EIP2200: {c.msg.contractAddress}[{slot}]" &
&" -> {newValue} ({currentValue})")
gasCost, "SSTORE EIP2200")

if gasRefund != 0:
c.gasMeter.refundGas(gasRefund)
Expand Down
5 changes: 3 additions & 2 deletions nimbus/evm/memory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ proc newMemory*(size: Natural): Memory =
result.extend(0, size)

proc read*(memory: var Memory, startPos: Natural, size: Natural): seq[byte] =
# TODO: use an openArray[byte]
result = memory.bytes[startPos ..< (startPos + size)]
result = newSeq[byte](size)
if size > 0:
copyMem(result[0].addr, memory.bytes[startPos].addr, size)

when defined(evmc_enabled):
proc readPtr*(memory: var Memory, startPos: Natural): ptr byte =
Expand Down
10 changes: 8 additions & 2 deletions nimbus/evm/modexp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ proc modExp*(b, e, m: openArray[byte]): seq[byte] =
var
base, exp, modulo, res: mp_int

if m.len == 0:
return @[0.byte]

if e.len == 0:
return @[1.byte]

if mp_init_multi(base, exp.addr, modulo.addr, nil) != MP_OKAY:
return

Expand All @@ -192,7 +198,7 @@ proc modExp*(b, e, m: openArray[byte]): seq[byte] =
# EVM special case 1
# If m == 0: EVM returns 0.
# If m == 1: we can shortcut that to 0 as well
mp_clear(modulo)
mp_clear_multi(base, exp.addr, modulo.addr, nil)
return @[0.byte]

if e.len > 0:
Expand All @@ -201,7 +207,7 @@ proc modExp*(b, e, m: openArray[byte]): seq[byte] =
# EVM special case 2
# If 0^0: EVM returns 1
# For all x != 0, x^0 == 1 as well
mp_clear_multi(exp, modulo.addr, nil)
mp_clear_multi(base, exp.addr, modulo.addr, nil)
return @[1.byte]

if b.len > 0:
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/TracerTests/block46402.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
"0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000060"
],
"error": "Opcode Dispatch Error msg=Out of gas: Needed 20000 - Remaining 412 - Reason: SSTORE: 9a049f5d18c239efaa258af9f3e7002949a977a0[0] -> 924236965777326770894530693462975209021625492404 (0), depth=0",
"error": "Opcode Dispatch Error msg=Out of gas: Needed 20000 - Remaining 412 - Reason: SSTORE, depth=0",
"gasCost": 0
}
],
Expand Down Expand Up @@ -705,7 +705,7 @@
"0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000060"
],
"error": "Opcode Dispatch Error msg=Out of gas: Needed 20000 - Remaining 412 - Reason: SSTORE: 9a049f5d18c239efaa258af9f3e7002949a977a0[0] -> 924236965777326770894530693462975209021625492404 (0), depth=0",
"error": "Opcode Dispatch Error msg=Out of gas: Needed 20000 - Remaining 412 - Reason: SSTORE, depth=0",
"gasCost": 0
}
]
Expand Down

0 comments on commit dc5907c

Please sign in to comment.