From a9b0e68964f1fe048b7909cd1c93d239c409d9c6 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Wed, 23 Oct 2024 17:02:56 -0300 Subject: [PATCH] Filling fixes (#516) * fix makeCallVariantGasEIP4762 Signed-off-by: Ignacio Hagopian * fix Signed-off-by: Ignacio Hagopian * fix Signed-off-by: Ignacio Hagopian * fix Signed-off-by: Ignacio Hagopian * ci: target v0.0.6 Signed-off-by: Ignacio Hagopian * improve fix Signed-off-by: Ignacio Hagopian * add witness gas to "old" gas upon error Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> * workflow: remove target branch from list of branches --------- Signed-off-by: Ignacio Hagopian Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> --- .github/workflows/stable-spec-tests.yml | 2 +- core/state/access_witness.go | 2 +- core/vm/operations_verkle.go | 50 ++++++++++++------------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/.github/workflows/stable-spec-tests.yml b/.github/workflows/stable-spec-tests.yml index 7d8d69c2c2bc..07efa0ffbeab 100644 --- a/.github/workflows/stable-spec-tests.yml +++ b/.github/workflows/stable-spec-tests.yml @@ -8,7 +8,7 @@ on: workflow_dispatch: env: - FIXTURES_TAG: "verkle@v0.0.5" + FIXTURES_TAG: "verkle@v0.0.6" jobs: setup: diff --git a/core/state/access_witness.go b/core/state/access_witness.go index a3eb21f31ba4..940a507b0e4e 100644 --- a/core/state/access_witness.go +++ b/core/state/access_witness.go @@ -94,7 +94,7 @@ func (aw *AccessWitness) TouchFullAccount(addr []byte, isWrite bool, availableGa for i := utils.BasicDataLeafKey; i <= utils.CodeHashLeafKey; i++ { consumed, wanted := aw.touchAddressAndChargeGas(addr, zeroTreeIndex, byte(i), isWrite, availableGas) if consumed < wanted { - return wanted + return wanted + gas } availableGas -= consumed gas += consumed diff --git a/core/vm/operations_verkle.go b/core/vm/operations_verkle.go index 1e0fef78a5db..a8c673bc5e28 100644 --- a/core/vm/operations_verkle.go +++ b/core/vm/operations_verkle.go @@ -53,11 +53,10 @@ func gasExtCodeHash4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, return evm.Accesses.TouchCodeHash(address[:], false, contract.Gas, true), nil } -func makeCallVariantGasEIP4762(oldCalculator gasFunc) gasFunc { +func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) gasFunc { return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { var ( target = common.Address(stack.Back(1).Bytes20()) - transfersValue = !stack.Back(2).IsZero() witnessGas uint64 _, isPrecompile = evm.precompile(target) isSystemContract = evm.isSystemContract(target) @@ -65,7 +64,7 @@ func makeCallVariantGasEIP4762(oldCalculator gasFunc) gasFunc { // If value is transferred, it is charged before 1/64th // is subtracted from the available gas pool. - if transfersValue { + if withTransferCosts && !stack.Back(2).IsZero() { wantedValueTransferWitnessGas := evm.Accesses.TouchAndChargeValueTransfer(contract.Address().Bytes()[:], target[:], contract.Gas) if wantedValueTransferWitnessGas > contract.Gas { return wantedValueTransferWitnessGas, nil @@ -73,38 +72,39 @@ func makeCallVariantGasEIP4762(oldCalculator gasFunc) gasFunc { witnessGas = wantedValueTransferWitnessGas } else if isPrecompile || isSystemContract { witnessGas = params.WarmStorageReadCostEIP2929 + } else { + // The charging for the value transfer is done BEFORE subtracting + // the 1/64th gas, as this is considered part of the CALL instruction. + // (so before we get to this point) + // But the message call is part of the subcall, for which only 63/64th + // of the gas should be available. + wantedMessageCallWitnessGas := evm.Accesses.TouchAndChargeMessageCall(target.Bytes(), contract.Gas-witnessGas) + var overflow bool + if witnessGas, overflow = math.SafeAdd(witnessGas, wantedMessageCallWitnessGas); overflow { + return 0, ErrGasUintOverflow + } + if witnessGas > contract.Gas { + return witnessGas, nil + } } - // The charging for the value transfer is done BEFORE subtracting - // the 1/64th gas, as this is considered part of the CALL instruction. - // (so before we get to this point) - // But the message call is part of the subcall, for which only 63/64th - // of the gas should be available. - wantedMessageCallWitnessGas := evm.Accesses.TouchAndChargeMessageCall(target.Bytes(), contract.Gas-witnessGas) - var overflow bool - if witnessGas, overflow = math.SafeAdd(witnessGas, wantedMessageCallWitnessGas); overflow { - return 0, ErrGasUintOverflow - } - if witnessGas > contract.Gas { - return witnessGas, nil - } - + contract.Gas -= witnessGas + // if the operation fails, adds witness gas to the gas before returning the error gas, err := oldCalculator(evm, contract, stack, mem, memorySize) - if err != nil { - return 0, err - } + contract.Gas += witnessGas // restore witness gas so that it can be charged at the callsite + var overflow bool if gas, overflow = math.SafeAdd(gas, witnessGas); overflow { return 0, ErrGasUintOverflow } - return gas, nil + return gas, err } } var ( - gasCallEIP4762 = makeCallVariantGasEIP4762(gasCall) - gasCallCodeEIP4762 = makeCallVariantGasEIP4762(gasCallCode) - gasStaticCallEIP4762 = makeCallVariantGasEIP4762(gasStaticCall) - gasDelegateCallEIP4762 = makeCallVariantGasEIP4762(gasDelegateCall) + gasCallEIP4762 = makeCallVariantGasEIP4762(gasCall, true) + gasCallCodeEIP4762 = makeCallVariantGasEIP4762(gasCallCode, false) + gasStaticCallEIP4762 = makeCallVariantGasEIP4762(gasStaticCall, false) + gasDelegateCallEIP4762 = makeCallVariantGasEIP4762(gasDelegateCall, false) ) func gasSelfdestructEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {