Fix 80 arithmetic gas cost tests, improve exp gas cost

This commit is contained in:
Alexander Ivanov 2018-02-28 17:06:05 +02:00
parent f179695341
commit 8be1a51c64
10 changed files with 129 additions and 109 deletions

View File

@ -32,89 +32,89 @@ VMTests
+ divByNonZero3.json OK + divByNonZero3.json OK
+ divByZero.json OK + divByZero.json OK
+ divByZero_2.json OK + divByZero_2.json OK
- exp0.json Fail + exp0.json OK
- exp1.json Fail + exp1.json OK
- exp2.json Fail + exp2.json OK
- exp3.json Fail + exp3.json OK
+ exp4.json OK + exp4.json OK
- exp5.json Fail + exp5.json OK
- exp6.json Fail + exp6.json OK
- exp7.json Fail + exp7.json OK
- expPowerOf256Of256_0.json Fail + expPowerOf256Of256_0.json OK
- expPowerOf256Of256_1.json Fail + expPowerOf256Of256_1.json OK
- expPowerOf256Of256_10.json Fail + expPowerOf256Of256_10.json OK
- expPowerOf256Of256_11.json Fail + expPowerOf256Of256_11.json OK
- expPowerOf256Of256_12.json Fail + expPowerOf256Of256_12.json OK
- expPowerOf256Of256_13.json Fail + expPowerOf256Of256_13.json OK
- expPowerOf256Of256_14.json Fail + expPowerOf256Of256_14.json OK
- expPowerOf256Of256_15.json Fail + expPowerOf256Of256_15.json OK
- expPowerOf256Of256_16.json Fail + expPowerOf256Of256_16.json OK
- expPowerOf256Of256_17.json Fail + expPowerOf256Of256_17.json OK
- expPowerOf256Of256_18.json Fail + expPowerOf256Of256_18.json OK
- expPowerOf256Of256_19.json Fail + expPowerOf256Of256_19.json OK
- expPowerOf256Of256_2.json Fail + expPowerOf256Of256_2.json OK
- expPowerOf256Of256_20.json Fail + expPowerOf256Of256_20.json OK
- expPowerOf256Of256_21.json Fail + expPowerOf256Of256_21.json OK
- expPowerOf256Of256_22.json Fail + expPowerOf256Of256_22.json OK
- expPowerOf256Of256_23.json Fail + expPowerOf256Of256_23.json OK
- expPowerOf256Of256_24.json Fail + expPowerOf256Of256_24.json OK
- expPowerOf256Of256_25.json Fail + expPowerOf256Of256_25.json OK
- expPowerOf256Of256_26.json Fail + expPowerOf256Of256_26.json OK
- expPowerOf256Of256_27.json Fail + expPowerOf256Of256_27.json OK
- expPowerOf256Of256_28.json Fail + expPowerOf256Of256_28.json OK
- expPowerOf256Of256_29.json Fail + expPowerOf256Of256_29.json OK
- expPowerOf256Of256_3.json Fail + expPowerOf256Of256_3.json OK
- expPowerOf256Of256_30.json Fail + expPowerOf256Of256_30.json OK
- expPowerOf256Of256_31.json Fail + expPowerOf256Of256_31.json OK
- expPowerOf256Of256_32.json Fail - expPowerOf256Of256_32.json Fail
- expPowerOf256Of256_33.json Fail - expPowerOf256Of256_33.json Fail
- expPowerOf256Of256_4.json Fail + expPowerOf256Of256_4.json OK
- expPowerOf256Of256_5.json Fail + expPowerOf256Of256_5.json OK
- expPowerOf256Of256_6.json Fail + expPowerOf256Of256_6.json OK
- expPowerOf256Of256_7.json Fail + expPowerOf256Of256_7.json OK
- expPowerOf256Of256_8.json Fail + expPowerOf256Of256_8.json OK
- expPowerOf256Of256_9.json Fail + expPowerOf256Of256_9.json OK
- expPowerOf256_1.json Fail + expPowerOf256_1.json OK
- expPowerOf256_10.json Fail + expPowerOf256_10.json OK
- expPowerOf256_11.json Fail + expPowerOf256_11.json OK
- expPowerOf256_12.json Fail + expPowerOf256_12.json OK
- expPowerOf256_13.json Fail + expPowerOf256_13.json OK
- expPowerOf256_14.json Fail + expPowerOf256_14.json OK
- expPowerOf256_15.json Fail + expPowerOf256_15.json OK
- expPowerOf256_16.json Fail + expPowerOf256_16.json OK
- expPowerOf256_17.json Fail + expPowerOf256_17.json OK
- expPowerOf256_18.json Fail + expPowerOf256_18.json OK
- expPowerOf256_19.json Fail + expPowerOf256_19.json OK
- expPowerOf256_2.json Fail + expPowerOf256_2.json OK
- expPowerOf256_20.json Fail + expPowerOf256_20.json OK
- expPowerOf256_21.json Fail + expPowerOf256_21.json OK
- expPowerOf256_22.json Fail + expPowerOf256_22.json OK
- expPowerOf256_23.json Fail + expPowerOf256_23.json OK
- expPowerOf256_24.json Fail + expPowerOf256_24.json OK
- expPowerOf256_25.json Fail + expPowerOf256_25.json OK
- expPowerOf256_26.json Fail + expPowerOf256_26.json OK
- expPowerOf256_27.json Fail + expPowerOf256_27.json OK
- expPowerOf256_28.json Fail + expPowerOf256_28.json OK
- expPowerOf256_29.json Fail + expPowerOf256_29.json OK
- expPowerOf256_3.json Fail + expPowerOf256_3.json OK
- expPowerOf256_30.json Fail + expPowerOf256_30.json OK
- expPowerOf256_31.json Fail + expPowerOf256_31.json OK
- expPowerOf256_32.json Fail + expPowerOf256_32.json OK
- expPowerOf256_33.json Fail + expPowerOf256_33.json OK
- expPowerOf256_4.json Fail + expPowerOf256_4.json OK
- expPowerOf256_5.json Fail + expPowerOf256_5.json OK
- expPowerOf256_6.json Fail + expPowerOf256_6.json OK
- expPowerOf256_7.json Fail + expPowerOf256_7.json OK
- expPowerOf256_8.json Fail + expPowerOf256_8.json OK
- expPowerOf256_9.json Fail + expPowerOf256_9.json OK
- expPowerOf2_128.json Fail + expPowerOf2_128.json OK
- expPowerOf2_16.json Fail + expPowerOf2_16.json OK
- expPowerOf2_2.json Fail + expPowerOf2_2.json OK
- expPowerOf2_256.json Fail + expPowerOf2_256.json OK
- expPowerOf2_32.json Fail + expPowerOf2_32.json OK
- expPowerOf2_4.json Fail + expPowerOf2_4.json OK
- expPowerOf2_64.json Fail + expPowerOf2_64.json OK
- expPowerOf2_8.json Fail + expPowerOf2_8.json OK
- expXY.json Fail - expXY.json Fail
- expXY_success.json Fail - expXY_success.json Fail
+ fibbonacci_unrolled.json OK + fibbonacci_unrolled.json OK
@ -198,7 +198,7 @@ VMTests
+ sub3.json OK + sub3.json OK
+ sub4.json OK + sub4.json OK
``` ```
OK: 105/195 Fail: 89/195 Skip: 1/195 OK: 185/195 Fail: 9/195 Skip: 1/195
## vmBitwiseLogicOperation ## vmBitwiseLogicOperation
```diff ```diff
+ and0.json OK + and0.json OK

View File

@ -11,7 +11,7 @@ proc memoryGasCost*(sizeInBytes: UInt256): UInt256 =
totalCost = linearCost + quadraticCost totalCost = linearCost + quadraticCost
result = totalCost result = totalCost
const VARIABLE_GAS_COST_OPS* = {Op.Exp} #const VARIABLE_GAS_COST_OPS* = {Op.Exp}
type type
BaseComputation* = ref object of RootObj BaseComputation* = ref object of RootObj
@ -38,10 +38,10 @@ type
erasesReturnData*: bool erasesReturnData*: bool
Opcode* = ref object of RootObj Opcode* = ref object of RootObj
case kind*: Op kind*: Op
of VARIABLE_GAS_COST_OPS: #of VARIABLE_GAS_COST_OPS:
gasCostHandler*: proc(computation: var BaseComputation): UInt256 # gasCostHandler*: proc(computation: var BaseComputation): UInt256
else: #else:
gasCostConstant*: UInt256 gasCostConstant*: UInt256
runLogic*: proc(computation: var BaseComputation) runLogic*: proc(computation: var BaseComputation)

View File

@ -154,7 +154,7 @@ let
GAS_HIGH* = 10.u256 GAS_HIGH* = 10.u256
GAS_EXT_CODE* = 20.u256 GAS_EXT_CODE* = 20.u256
GAS_BALANCE* = 20.u256 GAS_BALANCE* = 20.u256
GAS_SLOAD* = 50.u256 GAS_SLOAD* = 200.u256 # TODO: pre eip150
GAS_JUMP_DEST* = 1.u256 GAS_JUMP_DEST* = 1.u256
GAS_SSET* = 20_000.u256 GAS_SSET* = 20_000.u256
GAS_SRESET* = 5_000.u256 GAS_SRESET* = 5_000.u256

View File

@ -1,7 +1,7 @@
import import
../constants, ../utils_numeric, ../computation, ../constants, ../utils_numeric, ../computation,
.. / vm / [gas_meter, stack], ../opcode, ../opcode_values, .. / vm / [gas_meter, stack], ../opcode, ../opcode_values,
helpers, ttmath helpers, ttmath, strutils
proc add*(computation: var BaseComputation) = proc add*(computation: var BaseComputation) =
# Addition # Addition
@ -78,15 +78,15 @@ proc sdiv*(computation: var BaseComputation) =
# no curry # no curry
proc exp*(computation: var BaseComputation) = proc exp*(computation: var BaseComputation) =
# Exponentiation # Exponentiation
var (base, exponent) = computation.stack.popInt(2) let (base, exponent) = computation.stack.popInt(2)
var bitSize = 0.u256 # TODO exponent.bitLength() var gasCost = GAS_EXP_BYTE.u256
var byteSize = ceil8(bitSize) div 8 #if exponent != 0:
var res = if base == 0: 0.u256 else: (base.pow(exponent.getUInt)) mod UINT_256_CEILING # gasCost += GAS_EXP_BYTE * (1 + log256(exponent))
# computation.gasMeter.consumeGas( gasCost += (ceil8(exponent.bitLength()) div 8).u256 * GAS_EXP_BYTE # TODO
# gasPerByte * byteSize, computation.gasMeter.consumeGas(gasCost, reason="EXP: exponent bytes")
# reason="EXP: exponent bytes" #echo "exp", base, " ", exponent, " ", res
# ) var res = if base == 0: 0.u256 else: base.pow(exponent)
pushRes() pushRes()
proc signextend*(computation: var BaseComputation) = proc signextend*(computation: var BaseComputation) =

View File

@ -31,6 +31,7 @@ proc sstore*(computation) =
proc sload*(computation) = proc sload*(computation) =
let slot = stack.popInt() let slot = stack.popInt()
var value = 2.u256
# TODO: with # TODO: with
# with computation.vm_state.state_db(read_only=True) as state_db: # with computation.vm_state.state_db(read_only=True) as state_db:
@ -38,4 +39,4 @@ proc sload*(computation) =
# address=computation.msg.storage_address, # address=computation.msg.storage_address,
# slot=slot, # slot=slot,
# ) # )
# computation.stack.push(value) computation.stack.push(value)

View File

@ -11,9 +11,9 @@ method logger*(opcode: Opcode): Logger =
logging.getLogger(&"vm.opcode.{opcode.kind}") logging.getLogger(&"vm.opcode.{opcode.kind}")
method gasCost*(opcode: Opcode, computation: var BaseComputation): UInt256 = method gasCost*(opcode: Opcode, computation: var BaseComputation): UInt256 =
if opcode.kind in VARIABLE_GAS_COST_OPS: #if opcode.kind in VARIABLE_GAS_COST_OPS:
opcode.gasCostHandler(computation) # opcode.gasCostHandler(computation)
else: #else:
opcode.gasCostConstant opcode.gasCostConstant
template newOpcode*(kind: Op, gasCost: UInt256, logic: proc(computation: var BaseComputation)): Opcode = 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) Opcode(kind: kind, gasCostHandler: gasHandler, runLogic: logic)
method `$`*(opcode: Opcode): string = method `$`*(opcode: Opcode): string =
let gasCost = if opcode.kind in VARIABLE_GAS_COST_OPS: let gasCost = $opcode.gasCostConstant
"variable" # if opcode.kind in VARIABLE_GAS_COST_OPS:
else: # "variable"
$opcode.gasCostConstant # else:
# $opcode.gasCostConstant
&"{opcode.kind}(0x{opcode.kind.int.toHex(2)}: {gasCost})" &"{opcode.kind}(0x{opcode.kind.int.toHex(2)}: {gasCost})"
macro initOpcodes*(spec: untyped): untyped = macro initOpcodes*(spec: untyped): untyped =

View File

@ -15,7 +15,7 @@ var OPCODE_TABLE* = initOpcodes:
Op.SMod: GAS_LOW smod Op.SMod: GAS_LOW smod
Op.AddMod: GAS_MID addmod Op.AddMod: GAS_MID addmod
Op.MulMod: GAS_MID mulmod Op.MulMod: GAS_MID mulmod
Op.Exp: expGasCost arithmetic.exp Op.Exp: GAS_ZERO arithmetic.exp
Op.SignExtend: GAS_LOW signextend Op.SignExtend: GAS_LOW signextend
@ -75,7 +75,7 @@ var OPCODE_TABLE* = initOpcodes:
Op.MSize: GAS_BASE msize Op.MSize: GAS_BASE msize
# storage # storage
Op.SLoad: GAS_SLOAD_COST sload Op.SLoad: GAS_SLOAD sload
Op.SStore: GAS_ZERO sstore Op.SStore: GAS_ZERO sstore

View File

@ -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 # TODO improve
@ -18,6 +18,22 @@ proc bigEndianToInt*(value: Bytes): UInt256 =
#echo intToBigEndian("32482610168005790164680892356840817100452003984372336767666156211029086934369".u256) #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 = proc unsignedToSigned*(value: UInt256): Int256 =
0.i256 0.i256
# TODO # TODO

View File

@ -12,6 +12,7 @@ proc validTest*(folder: string, name: string): bool =
"callstatelessToReturn1" notin name and "callstatelessToReturn1" notin name and
"arith" notin name and "arith" notin name and
folder notin @["vmRandomTest", "vmSystemOperations", "vmPerformance", "vmEnvironmentalInfo", "vmLogTest", "vmSha3Test", "vmIOandFlowOperations"] folder notin @["vmRandomTest", "vmSystemOperations", "vmPerformance", "vmEnvironmentalInfo", "vmLogTest", "vmSha3Test", "vmIOandFlowOperations"]
#result = name == "exp2.json"
macro jsonTest*(s: static[string], handler: untyped): untyped = macro jsonTest*(s: static[string], handler: untyped): untyped =
let testStatusIMPL = ident("testStatusIMPL") let testStatusIMPL = ident("testStatusIMPL")

View File

@ -72,6 +72,7 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
let expectedGasRemaining = fixture{"gas"}.getHexadecimalInt.u256 let expectedGasRemaining = fixture{"gas"}.getHexadecimalInt.u256
let actualGasRemaining = gasMeter.gasRemaining let actualGasRemaining = gasMeter.gasRemaining
checkpoint(&"{actualGasRemaining} {expectedGasRemaining}")
check(actualGasRemaining == expectedGasRemaining or check(actualGasRemaining == expectedGasRemaining or
computation.code.hasSStore() and computation.code.hasSStore() and
(actualGasRemaining > expectedGasRemaining and (actualGasRemaining - expectedGasRemaining) mod 15_000 == 0 or (actualGasRemaining > expectedGasRemaining and (actualGasRemaining - expectedGasRemaining) mod 15_000 == 0 or