Fix 80 arithmetic gas cost tests, improve exp gas cost
This commit is contained in:
parent
f179695341
commit
8be1a51c64
162
VMTests.md
162
VMTests.md
|
@ -32,89 +32,89 @@ VMTests
|
|||
+ divByNonZero3.json OK
|
||||
+ divByZero.json OK
|
||||
+ divByZero_2.json OK
|
||||
- exp0.json Fail
|
||||
- exp1.json Fail
|
||||
- exp2.json Fail
|
||||
- exp3.json Fail
|
||||
+ exp0.json OK
|
||||
+ exp1.json OK
|
||||
+ exp2.json OK
|
||||
+ exp3.json OK
|
||||
+ exp4.json OK
|
||||
- exp5.json Fail
|
||||
- exp6.json Fail
|
||||
- exp7.json Fail
|
||||
- expPowerOf256Of256_0.json Fail
|
||||
- expPowerOf256Of256_1.json Fail
|
||||
- expPowerOf256Of256_10.json Fail
|
||||
- expPowerOf256Of256_11.json Fail
|
||||
- expPowerOf256Of256_12.json Fail
|
||||
- expPowerOf256Of256_13.json Fail
|
||||
- expPowerOf256Of256_14.json Fail
|
||||
- expPowerOf256Of256_15.json Fail
|
||||
- expPowerOf256Of256_16.json Fail
|
||||
- expPowerOf256Of256_17.json Fail
|
||||
- expPowerOf256Of256_18.json Fail
|
||||
- expPowerOf256Of256_19.json Fail
|
||||
- expPowerOf256Of256_2.json Fail
|
||||
- expPowerOf256Of256_20.json Fail
|
||||
- expPowerOf256Of256_21.json Fail
|
||||
- expPowerOf256Of256_22.json Fail
|
||||
- expPowerOf256Of256_23.json Fail
|
||||
- expPowerOf256Of256_24.json Fail
|
||||
- expPowerOf256Of256_25.json Fail
|
||||
- expPowerOf256Of256_26.json Fail
|
||||
- expPowerOf256Of256_27.json Fail
|
||||
- expPowerOf256Of256_28.json Fail
|
||||
- expPowerOf256Of256_29.json Fail
|
||||
- expPowerOf256Of256_3.json Fail
|
||||
- expPowerOf256Of256_30.json Fail
|
||||
- expPowerOf256Of256_31.json Fail
|
||||
+ exp5.json OK
|
||||
+ exp6.json OK
|
||||
+ exp7.json OK
|
||||
+ expPowerOf256Of256_0.json OK
|
||||
+ expPowerOf256Of256_1.json OK
|
||||
+ expPowerOf256Of256_10.json OK
|
||||
+ expPowerOf256Of256_11.json OK
|
||||
+ expPowerOf256Of256_12.json OK
|
||||
+ expPowerOf256Of256_13.json OK
|
||||
+ expPowerOf256Of256_14.json OK
|
||||
+ expPowerOf256Of256_15.json OK
|
||||
+ expPowerOf256Of256_16.json OK
|
||||
+ expPowerOf256Of256_17.json OK
|
||||
+ expPowerOf256Of256_18.json OK
|
||||
+ expPowerOf256Of256_19.json OK
|
||||
+ expPowerOf256Of256_2.json OK
|
||||
+ expPowerOf256Of256_20.json OK
|
||||
+ expPowerOf256Of256_21.json OK
|
||||
+ expPowerOf256Of256_22.json OK
|
||||
+ expPowerOf256Of256_23.json OK
|
||||
+ expPowerOf256Of256_24.json OK
|
||||
+ expPowerOf256Of256_25.json OK
|
||||
+ expPowerOf256Of256_26.json OK
|
||||
+ expPowerOf256Of256_27.json OK
|
||||
+ expPowerOf256Of256_28.json OK
|
||||
+ expPowerOf256Of256_29.json OK
|
||||
+ expPowerOf256Of256_3.json OK
|
||||
+ expPowerOf256Of256_30.json OK
|
||||
+ expPowerOf256Of256_31.json OK
|
||||
- expPowerOf256Of256_32.json Fail
|
||||
- expPowerOf256Of256_33.json Fail
|
||||
- expPowerOf256Of256_4.json Fail
|
||||
- expPowerOf256Of256_5.json Fail
|
||||
- expPowerOf256Of256_6.json Fail
|
||||
- expPowerOf256Of256_7.json Fail
|
||||
- expPowerOf256Of256_8.json Fail
|
||||
- expPowerOf256Of256_9.json Fail
|
||||
- expPowerOf256_1.json Fail
|
||||
- expPowerOf256_10.json Fail
|
||||
- expPowerOf256_11.json Fail
|
||||
- expPowerOf256_12.json Fail
|
||||
- expPowerOf256_13.json Fail
|
||||
- expPowerOf256_14.json Fail
|
||||
- expPowerOf256_15.json Fail
|
||||
- expPowerOf256_16.json Fail
|
||||
- expPowerOf256_17.json Fail
|
||||
- expPowerOf256_18.json Fail
|
||||
- expPowerOf256_19.json Fail
|
||||
- expPowerOf256_2.json Fail
|
||||
- expPowerOf256_20.json Fail
|
||||
- expPowerOf256_21.json Fail
|
||||
- expPowerOf256_22.json Fail
|
||||
- expPowerOf256_23.json Fail
|
||||
- expPowerOf256_24.json Fail
|
||||
- expPowerOf256_25.json Fail
|
||||
- expPowerOf256_26.json Fail
|
||||
- expPowerOf256_27.json Fail
|
||||
- expPowerOf256_28.json Fail
|
||||
- expPowerOf256_29.json Fail
|
||||
- expPowerOf256_3.json Fail
|
||||
- expPowerOf256_30.json Fail
|
||||
- expPowerOf256_31.json Fail
|
||||
- expPowerOf256_32.json Fail
|
||||
- expPowerOf256_33.json Fail
|
||||
- expPowerOf256_4.json Fail
|
||||
- expPowerOf256_5.json Fail
|
||||
- expPowerOf256_6.json Fail
|
||||
- expPowerOf256_7.json Fail
|
||||
- expPowerOf256_8.json Fail
|
||||
- expPowerOf256_9.json Fail
|
||||
- expPowerOf2_128.json Fail
|
||||
- expPowerOf2_16.json Fail
|
||||
- expPowerOf2_2.json Fail
|
||||
- expPowerOf2_256.json Fail
|
||||
- expPowerOf2_32.json Fail
|
||||
- expPowerOf2_4.json Fail
|
||||
- expPowerOf2_64.json Fail
|
||||
- expPowerOf2_8.json Fail
|
||||
+ expPowerOf256Of256_4.json OK
|
||||
+ expPowerOf256Of256_5.json OK
|
||||
+ expPowerOf256Of256_6.json OK
|
||||
+ expPowerOf256Of256_7.json OK
|
||||
+ expPowerOf256Of256_8.json OK
|
||||
+ expPowerOf256Of256_9.json OK
|
||||
+ expPowerOf256_1.json OK
|
||||
+ expPowerOf256_10.json OK
|
||||
+ expPowerOf256_11.json OK
|
||||
+ expPowerOf256_12.json OK
|
||||
+ expPowerOf256_13.json OK
|
||||
+ expPowerOf256_14.json OK
|
||||
+ expPowerOf256_15.json OK
|
||||
+ expPowerOf256_16.json OK
|
||||
+ expPowerOf256_17.json OK
|
||||
+ expPowerOf256_18.json OK
|
||||
+ expPowerOf256_19.json OK
|
||||
+ expPowerOf256_2.json OK
|
||||
+ expPowerOf256_20.json OK
|
||||
+ expPowerOf256_21.json OK
|
||||
+ expPowerOf256_22.json OK
|
||||
+ expPowerOf256_23.json OK
|
||||
+ expPowerOf256_24.json OK
|
||||
+ expPowerOf256_25.json OK
|
||||
+ expPowerOf256_26.json OK
|
||||
+ expPowerOf256_27.json OK
|
||||
+ expPowerOf256_28.json OK
|
||||
+ expPowerOf256_29.json OK
|
||||
+ expPowerOf256_3.json OK
|
||||
+ expPowerOf256_30.json OK
|
||||
+ expPowerOf256_31.json OK
|
||||
+ expPowerOf256_32.json OK
|
||||
+ expPowerOf256_33.json OK
|
||||
+ expPowerOf256_4.json OK
|
||||
+ expPowerOf256_5.json OK
|
||||
+ expPowerOf256_6.json OK
|
||||
+ expPowerOf256_7.json OK
|
||||
+ expPowerOf256_8.json OK
|
||||
+ expPowerOf256_9.json OK
|
||||
+ expPowerOf2_128.json OK
|
||||
+ expPowerOf2_16.json OK
|
||||
+ expPowerOf2_2.json OK
|
||||
+ expPowerOf2_256.json OK
|
||||
+ expPowerOf2_32.json OK
|
||||
+ expPowerOf2_4.json OK
|
||||
+ expPowerOf2_64.json OK
|
||||
+ expPowerOf2_8.json OK
|
||||
- expXY.json Fail
|
||||
- expXY_success.json Fail
|
||||
+ fibbonacci_unrolled.json OK
|
||||
|
@ -198,7 +198,7 @@ VMTests
|
|||
+ sub3.json OK
|
||||
+ sub4.json OK
|
||||
```
|
||||
OK: 105/195 Fail: 89/195 Skip: 1/195
|
||||
OK: 185/195 Fail: 9/195 Skip: 1/195
|
||||
## vmBitwiseLogicOperation
|
||||
```diff
|
||||
+ and0.json OK
|
||||
|
|
|
@ -11,7 +11,7 @@ proc memoryGasCost*(sizeInBytes: UInt256): UInt256 =
|
|||
totalCost = linearCost + quadraticCost
|
||||
result = totalCost
|
||||
|
||||
const VARIABLE_GAS_COST_OPS* = {Op.Exp}
|
||||
#const VARIABLE_GAS_COST_OPS* = {Op.Exp}
|
||||
|
||||
type
|
||||
BaseComputation* = ref object of RootObj
|
||||
|
@ -38,10 +38,10 @@ type
|
|||
erasesReturnData*: bool
|
||||
|
||||
Opcode* = ref object of RootObj
|
||||
case kind*: Op
|
||||
of VARIABLE_GAS_COST_OPS:
|
||||
gasCostHandler*: proc(computation: var BaseComputation): UInt256
|
||||
else:
|
||||
kind*: Op
|
||||
#of VARIABLE_GAS_COST_OPS:
|
||||
# gasCostHandler*: proc(computation: var BaseComputation): UInt256
|
||||
#else:
|
||||
gasCostConstant*: UInt256
|
||||
runLogic*: proc(computation: var BaseComputation)
|
||||
|
||||
|
|
|
@ -154,7 +154,7 @@ let
|
|||
GAS_HIGH* = 10.u256
|
||||
GAS_EXT_CODE* = 20.u256
|
||||
GAS_BALANCE* = 20.u256
|
||||
GAS_SLOAD* = 50.u256
|
||||
GAS_SLOAD* = 200.u256 # TODO: pre eip150
|
||||
GAS_JUMP_DEST* = 1.u256
|
||||
GAS_SSET* = 20_000.u256
|
||||
GAS_SRESET* = 5_000.u256
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import
|
||||
../constants, ../utils_numeric, ../computation,
|
||||
.. / vm / [gas_meter, stack], ../opcode, ../opcode_values,
|
||||
helpers, ttmath
|
||||
helpers, ttmath, strutils
|
||||
|
||||
proc add*(computation: var BaseComputation) =
|
||||
# Addition
|
||||
|
@ -78,15 +78,15 @@ proc sdiv*(computation: var BaseComputation) =
|
|||
# no curry
|
||||
proc exp*(computation: var BaseComputation) =
|
||||
# Exponentiation
|
||||
var (base, exponent) = computation.stack.popInt(2)
|
||||
let (base, exponent) = computation.stack.popInt(2)
|
||||
|
||||
var bitSize = 0.u256 # TODO exponent.bitLength()
|
||||
var byteSize = ceil8(bitSize) div 8
|
||||
var res = if base == 0: 0.u256 else: (base.pow(exponent.getUInt)) mod UINT_256_CEILING
|
||||
# computation.gasMeter.consumeGas(
|
||||
# gasPerByte * byteSize,
|
||||
# reason="EXP: exponent bytes"
|
||||
# )
|
||||
var gasCost = GAS_EXP_BYTE.u256
|
||||
#if exponent != 0:
|
||||
# gasCost += GAS_EXP_BYTE * (1 + log256(exponent))
|
||||
gasCost += (ceil8(exponent.bitLength()) div 8).u256 * GAS_EXP_BYTE # TODO
|
||||
computation.gasMeter.consumeGas(gasCost, reason="EXP: exponent bytes")
|
||||
#echo "exp", base, " ", exponent, " ", res
|
||||
var res = if base == 0: 0.u256 else: base.pow(exponent)
|
||||
pushRes()
|
||||
|
||||
proc signextend*(computation: var BaseComputation) =
|
||||
|
|
|
@ -31,6 +31,7 @@ proc sstore*(computation) =
|
|||
|
||||
proc sload*(computation) =
|
||||
let slot = stack.popInt()
|
||||
var value = 2.u256
|
||||
|
||||
# TODO: with
|
||||
# with computation.vm_state.state_db(read_only=True) as state_db:
|
||||
|
@ -38,4 +39,4 @@ proc sload*(computation) =
|
|||
# address=computation.msg.storage_address,
|
||||
# slot=slot,
|
||||
# )
|
||||
# computation.stack.push(value)
|
||||
computation.stack.push(value)
|
||||
|
|
|
@ -11,9 +11,9 @@ method logger*(opcode: Opcode): Logger =
|
|||
logging.getLogger(&"vm.opcode.{opcode.kind}")
|
||||
|
||||
method gasCost*(opcode: Opcode, computation: var BaseComputation): UInt256 =
|
||||
if opcode.kind in VARIABLE_GAS_COST_OPS:
|
||||
opcode.gasCostHandler(computation)
|
||||
else:
|
||||
#if opcode.kind in VARIABLE_GAS_COST_OPS:
|
||||
# opcode.gasCostHandler(computation)
|
||||
#else:
|
||||
opcode.gasCostConstant
|
||||
|
||||
template newOpcode*(kind: Op, gasCost: UInt256, logic: proc(computation: var BaseComputation)): Opcode =
|
||||
|
@ -23,10 +23,11 @@ template newOpcode*(kind: Op, gasHandler: proc(computation: var BaseComputation)
|
|||
Opcode(kind: kind, gasCostHandler: gasHandler, runLogic: logic)
|
||||
|
||||
method `$`*(opcode: Opcode): string =
|
||||
let gasCost = if opcode.kind in VARIABLE_GAS_COST_OPS:
|
||||
"variable"
|
||||
else:
|
||||
$opcode.gasCostConstant
|
||||
let gasCost = $opcode.gasCostConstant
|
||||
# if opcode.kind in VARIABLE_GAS_COST_OPS:
|
||||
# "variable"
|
||||
# else:
|
||||
# $opcode.gasCostConstant
|
||||
&"{opcode.kind}(0x{opcode.kind.int.toHex(2)}: {gasCost})"
|
||||
|
||||
macro initOpcodes*(spec: untyped): untyped =
|
||||
|
|
|
@ -15,7 +15,7 @@ var OPCODE_TABLE* = initOpcodes:
|
|||
Op.SMod: GAS_LOW smod
|
||||
Op.AddMod: GAS_MID addmod
|
||||
Op.MulMod: GAS_MID mulmod
|
||||
Op.Exp: expGasCost arithmetic.exp
|
||||
Op.Exp: GAS_ZERO arithmetic.exp
|
||||
Op.SignExtend: GAS_LOW signextend
|
||||
|
||||
|
||||
|
@ -75,7 +75,7 @@ var OPCODE_TABLE* = initOpcodes:
|
|||
Op.MSize: GAS_BASE msize
|
||||
|
||||
# storage
|
||||
Op.SLoad: GAS_SLOAD_COST sload
|
||||
Op.SLoad: GAS_SLOAD sload
|
||||
Op.SStore: GAS_ZERO sstore
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import ttmath, constants, strformat, sequtils, endians, macros, utils / padding, rlp
|
||||
import ttmath, constants, strformat, strutils, sequtils, endians, macros, utils / padding, rlp
|
||||
|
||||
# TODO improve
|
||||
|
||||
|
@ -18,6 +18,22 @@ proc bigEndianToInt*(value: Bytes): UInt256 =
|
|||
|
||||
#echo intToBigEndian("32482610168005790164680892356840817100452003984372336767666156211029086934369".u256)
|
||||
|
||||
proc bitLength*(value: UInt256): int =
|
||||
var b = ""
|
||||
for z in 0 ..< 4:
|
||||
b.add(value.table[3 - z].int64.toBin(64))
|
||||
result = b.strip(chars={'0'}, trailing=false).len
|
||||
|
||||
#proc log256*(value: UInt256): UInt256 =
|
||||
# log2(value) div 8
|
||||
|
||||
proc ceil8*(value: int): int =
|
||||
let remainder = value mod 8
|
||||
if remainder == 0:
|
||||
value
|
||||
else:
|
||||
value + 8 - remainder
|
||||
|
||||
proc unsignedToSigned*(value: UInt256): Int256 =
|
||||
0.i256
|
||||
# TODO
|
||||
|
|
|
@ -12,6 +12,7 @@ proc validTest*(folder: string, name: string): bool =
|
|||
"callstatelessToReturn1" notin name and
|
||||
"arith" notin name and
|
||||
folder notin @["vmRandomTest", "vmSystemOperations", "vmPerformance", "vmEnvironmentalInfo", "vmLogTest", "vmSha3Test", "vmIOandFlowOperations"]
|
||||
#result = name == "exp2.json"
|
||||
|
||||
macro jsonTest*(s: static[string], handler: untyped): untyped =
|
||||
let testStatusIMPL = ident("testStatusIMPL")
|
||||
|
|
|
@ -72,6 +72,7 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
|
|||
|
||||
let expectedGasRemaining = fixture{"gas"}.getHexadecimalInt.u256
|
||||
let actualGasRemaining = gasMeter.gasRemaining
|
||||
checkpoint(&"{actualGasRemaining} {expectedGasRemaining}")
|
||||
check(actualGasRemaining == expectedGasRemaining or
|
||||
computation.code.hasSStore() and
|
||||
(actualGasRemaining > expectedGasRemaining and (actualGasRemaining - expectedGasRemaining) mod 15_000 == 0 or
|
||||
|
|
Loading…
Reference in New Issue