From a86979eaa3e243c24c7ac8c0a336f9fbce33d504 Mon Sep 17 00:00:00 2001 From: andri lim Date: Wed, 8 May 2019 18:41:25 +0700 Subject: [PATCH] fix create2 gasCost --- GeneralStateTests.md | 14 +++--- nimbus/vm/interpreter/gas_costs.nim | 3 +- nimbus/vm/interpreter/opcodes_impl.nim | 23 +++++----- tests/test_generalstate_failing.nim | 63 -------------------------- tests/test_helpers.nim | 59 +++++++++++++++++++++++- 5 files changed, 77 insertions(+), 85 deletions(-) diff --git a/GeneralStateTests.md b/GeneralStateTests.md index 24642cfde..b58af3d4e 100644 --- a/GeneralStateTests.md +++ b/GeneralStateTests.md @@ -58,9 +58,9 @@ OK: 46/46 Fail: 0/46 Skip: 0/46 OK: 1/2 Fail: 0/2 Skip: 1/2 ## stBadOpcode ```diff - badOpcodes.json Skip ++ badOpcodes.json OK ``` -OK: 0/1 Fail: 0/1 Skip: 1/1 +OK: 1/1 Fail: 0/1 Skip: 0/1 ## stBugs ```diff + evmBytecode.json OK @@ -368,8 +368,8 @@ OK: 2/2 Fail: 0/2 Skip: 0/2 OK: 3/3 Fail: 0/3 Skip: 0/3 ## stCreate2 ```diff - CREATE2_Bounds.json Skip - CREATE2_Bounds2.json Skip ++ CREATE2_Bounds.json OK ++ CREATE2_Bounds2.json OK + CREATE2_Bounds3.json OK + CREATE2_ContractSuicideDuringInit_ThenStoreThenReturn.json OK + CREATE2_Suicide.json OK @@ -392,7 +392,7 @@ OK: 3/3 Fail: 0/3 Skip: 0/3 + RevertOpcodeInCreateReturnsCreate2.json OK + call_outsize_then_create2_successful_then_returndatasize.json OK + call_then_create2_successful_then_returndatasize.json OK - create2InitCodes.json Skip ++ create2InitCodes.json OK + create2SmartInitCode.json OK create2callPrecompiles.json Skip + create2checkFieldsInInitcode.json OK @@ -413,7 +413,7 @@ OK: 3/3 Fail: 0/3 Skip: 0/3 + returndatacopy_following_successful_create.json OK + returndatasize_following_successful_create.json OK ``` -OK: 38/44 Fail: 0/44 Skip: 6/44 +OK: 41/44 Fail: 0/44 Skip: 3/44 ## stCreateTest ```diff + CREATE_AcreateB_BSuicide_BStore.json OK @@ -2645,4 +2645,4 @@ OK: 133/133 Fail: 0/133 Skip: 0/133 OK: 130/130 Fail: 0/130 Skip: 0/130 ---TOTAL--- -OK: 2338/2447 Fail: 0/2447 Skip: 109/2447 +OK: 2342/2447 Fail: 0/2447 Skip: 105/2447 diff --git a/nimbus/vm/interpreter/gas_costs.nim b/nimbus/vm/interpreter/gas_costs.nim index 38252da8a..400b0f886 100644 --- a/nimbus/vm/interpreter/gas_costs.nim +++ b/nimbus/vm/interpreter/gas_costs.nim @@ -353,8 +353,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) = result.gasCost += static(FeeSchedule[GasNewAccount]) func `prefix gasCreate2`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = - result = static(FeeSchedule[GasCreate]) + - static(FeeSchedule[GasSha3Word]) * (memLength).wordCount + result = static(FeeSchedule[GasSha3Word]) * (memLength).wordCount # ################################################################################################### diff --git a/nimbus/vm/interpreter/opcodes_impl.nim b/nimbus/vm/interpreter/opcodes_impl.nim index de0961e6c..ece1c2b0d 100644 --- a/nimbus/vm/interpreter/opcodes_impl.nim +++ b/nimbus/vm/interpreter/opcodes_impl.nim @@ -504,17 +504,16 @@ genLog() # f0s: System operations. proc canTransfer(computation: BaseComputation, memPos, memLen: int, value: Uint256, opCode: static[Op]): bool = - when opCode == Create: - let gasParams = GasParams(kind: Create, - cr_currentMemSize: computation.memory.len, - cr_memOffset: memPos, - cr_memLength: memLen - ) - let gasCost = computation.gasCosts[Create].c_handler(1.u256, gasParams).gasCost - let reason = &"CREATE: GasCreate + {memLen} * memory expansion" - else: - let gasCost = computation.gasCosts[Create2].m_handler(0, 0, memLen) - let reason = &"CREATE2: GasCreate + {memLen} * GasSha3Word" + let gasParams = GasParams(kind: Create, + cr_currentMemSize: computation.memory.len, + cr_memOffset: memPos, + cr_memLength: memLen + ) + var gasCost = computation.gasCosts[Create].c_handler(1.u256, gasParams).gasCost + let reason = &"CREATE: GasCreate + {memLen} * memory expansion" + + when opCode == Create2: + gasCost = gasCost + computation.gasCosts[Create2].m_handler(0, 0, memLen) computation.gasMeter.consumeGas(gasCost, reason = reason) computation.memory.extend(memPos, memLen) @@ -599,7 +598,7 @@ proc setupCreate(computation: BaseComputation, memPos, len: int, value: Uint256, template genCreate(callName: untyped, opCode: Op): untyped = op callName, inline = false, value, startPosition, size: ## 0xf0, Create a new account with associated code. - let (memPos, len) = (startPosition.cleanMemRef, size.cleanMemRef) + let (memPos, len) = (startPosition.safeInt, size.safeInt) if not computation.canTransfer(memPos, len, value, opCode): push: 0 return diff --git a/tests/test_generalstate_failing.nim b/tests/test_generalstate_failing.nim index e0453d575..bbd00c0dc 100644 --- a/tests/test_generalstate_failing.nim +++ b/tests/test_generalstate_failing.nim @@ -14,70 +14,7 @@ func allowedFailingGeneralStateTest*(folder, name: string): bool = let allowedFailingGeneralStateTests = @[ "modexp.json", - - "CREATE2_Bounds.json", - "CREATE2_Bounds2.json", "create2callPrecompiles.json", - "create2InitCodes.json", "create2noCash.json", - - "badOpcodes.json", - - # all these tests below actually pass - # but they are very slow - - # constantinople slow tests - "Create2Recursive.json", - - # byzantium slow tests - "LoopCallsDepthThenRevert3.json", - "LoopCallsDepthThenRevert2.json", - "LoopCallsDepthThenRevert.json", - "static_Call50000.json", - "static_Call50000_ecrec.json", - "static_Call50000_identity.json", - "static_Call50000_identity2.json", - "static_Call50000_rip160.json", - "static_Call50000_sha256.json", - "LoopCallsThenRevert.json", - "LoopDelegateCallsDepthThenRevert.json", - "recursiveCreateReturnValue.json", - "static_Call1024PreCalls2.json", - "Callcode1024BalanceTooLow.json", - "static_Call1024BalanceTooLow.json", - "static_Call1024BalanceTooLow2.json", - "static_Call1024OOG.json", - "static_Call1024PreCalls3.json", - "static_Call1024PreCalls.json", - "static_Call1MB1024Calldepth.json", - - # Homestead recursives - "ContractCreationSpam.json", - "Call1024OOG.json", - "Call1024PreCalls.json", - "CallRecursiveBombPreCall.json", - "Delegatecall1024.json", - "Delegatecall1024OOG.json", - "JUMPDEST_Attack.json", - "JUMPDEST_AttackwithJump.json", - "ABAcalls1.json", - "ABAcalls2.json", - "CallRecursiveBomb0.json", - "CallRecursiveBomb0_OOG_atMaxCallDepth.json", - "CallRecursiveBomb1.json", - "CallRecursiveBomb2.json", - "CallRecursiveBombLog.json", - "CallRecursiveBombLog2.json", - "Call1024BalanceTooLow.json", - - # Frontier recursives - "Callcode1024OOG.json", - "callcallcodecall_ABCB_RECURSIVE.json", - "callcallcodecallcode_ABCB_RECURSIVE.json", - "callcodecallcall_ABCB_RECURSIVE.json", - "callcodecallcallcode_ABCB_RECURSIVE.json", - "callcodecallcodecall_ABCB_RECURSIVE.json", - "callcodecallcodecallcode_ABCB_RECURSIVE.json", - "callcallcallcode_ABCB_RECURSIVE.json" ] result = name in allowedFailingGeneralStateTests diff --git a/tests/test_helpers.nim b/tests/test_helpers.nim index 34e8794ef..477f457bc 100644 --- a/tests/test_helpers.nim +++ b/tests/test_helpers.nim @@ -43,7 +43,64 @@ func slowTest*(folder: string, name: string): bool = "DELEGATECALL_Bounds.json", "CALL_Bounds2a.json", "CALL_Bounds2.json", "CallToNameRegistratorMemOOGAndInsufficientBalance.json", - "CallToNameRegistratorTooMuchMemory0.json"] + "CallToNameRegistratorTooMuchMemory0.json", + + # all these tests below actually pass + # but they are very slow + + # constantinople slow tests + "Create2Recursive.json", + + # byzantium slow tests + "LoopCallsDepthThenRevert3.json", + "LoopCallsDepthThenRevert2.json", + "LoopCallsDepthThenRevert.json", + "static_Call50000.json", + "static_Call50000_ecrec.json", + "static_Call50000_identity.json", + "static_Call50000_identity2.json", + "static_Call50000_rip160.json", + "static_Call50000_sha256.json", + "LoopCallsThenRevert.json", + "LoopDelegateCallsDepthThenRevert.json", + "recursiveCreateReturnValue.json", + "static_Call1024PreCalls2.json", + "Callcode1024BalanceTooLow.json", + "static_Call1024BalanceTooLow.json", + "static_Call1024BalanceTooLow2.json", + "static_Call1024OOG.json", + "static_Call1024PreCalls3.json", + "static_Call1024PreCalls.json", + "static_Call1MB1024Calldepth.json", + + # Homestead recursives + "ContractCreationSpam.json", + "Call1024OOG.json", + "Call1024PreCalls.json", + "CallRecursiveBombPreCall.json", + "Delegatecall1024.json", + "Delegatecall1024OOG.json", + "JUMPDEST_Attack.json", + "JUMPDEST_AttackwithJump.json", + "ABAcalls1.json", + "ABAcalls2.json", + "CallRecursiveBomb0.json", + "CallRecursiveBomb0_OOG_atMaxCallDepth.json", + "CallRecursiveBomb1.json", + "CallRecursiveBomb2.json", + "CallRecursiveBombLog.json", + "CallRecursiveBombLog2.json", + "Call1024BalanceTooLow.json", + + # Frontier recursives + "Callcode1024OOG.json", + "callcallcodecall_ABCB_RECURSIVE.json", + "callcallcodecallcode_ABCB_RECURSIVE.json", + "callcodecallcall_ABCB_RECURSIVE.json", + "callcodecallcallcode_ABCB_RECURSIVE.json", + "callcodecallcodecall_ABCB_RECURSIVE.json", + "callcodecallcodecallcode_ABCB_RECURSIVE.json", + "callcallcallcode_ABCB_RECURSIVE.json"] func failIn32Bits(folder, name: string): bool = return name in @[