fix slow operations in evm

This commit is contained in:
jangko 2023-07-31 17:42:07 +07:00
parent 9be5df90cf
commit dc5907c30c
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
4 changed files with 17 additions and 14 deletions

View File

@ -27,8 +27,7 @@ import
./oph_defs,
./oph_helpers,
eth/common,
stint,
strformat
stint
{.push raises: [CatchableError].} # basically the annotation type of a `Vm2OpFn`
@ -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) =
@ -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)
@ -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)

View File

@ -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 =

View File

@ -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
@ -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:
@ -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:

View File

@ -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
}
],
@ -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
}
]