Skip to content

Commit

Permalink
import most changes from jsign-witness-fix
Browse files Browse the repository at this point in the history
Co-authored-by: Ignacio Hagopian <[email protected]>

Signed-off-by: Guillaume Ballet <[email protected]>
  • Loading branch information
gballet committed Oct 22, 2024
1 parent 762282e commit 17fd37d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
16 changes: 12 additions & 4 deletions core/state/access_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ func (aw *AccessWitness) TouchAndChargeMessageCall(addr []byte, availableGas uin
func (aw *AccessWitness) TouchAndChargeValueTransfer(callerAddr, targetAddr []byte, availableGas uint64) uint64 {
chargedGas1, _ := aw.touchAddressAndChargeGas(callerAddr, zeroTreeIndex, utils.BasicDataLeafKey, true, availableGas)
chargedGas2, _ := aw.touchAddressAndChargeGas(targetAddr, zeroTreeIndex, utils.BasicDataLeafKey, true, availableGas-chargedGas1)
if chargedGas1+chargedGas2 == 0 {
return params.WarmStorageReadCostEIP2929
}
return chargedGas1 + chargedGas2
}

Expand Down Expand Up @@ -140,9 +143,14 @@ func (aw *AccessWitness) TouchTxOriginAndComputeGas(originAddr []byte) {
}
}

func (aw *AccessWitness) TouchTxExistingAndComputeGas(targetAddr []byte, sendsValue bool) {
func (aw *AccessWitness) TouchTxTarget(targetAddr []byte, sendsValue, doesntExist bool) {
aw.touchAddressAndChargeGas(targetAddr, zeroTreeIndex, utils.BasicDataLeafKey, sendsValue, math.MaxUint64)
aw.touchAddressAndChargeGas(targetAddr, zeroTreeIndex, utils.CodeHashLeafKey, false, math.MaxUint64)
// Note that we do a write-event in CodeHash without distinguishing if the tx target account
// exists or not. Pre-7702, there's no situation in which an existing codeHash can be mutated, thus
// doing a write-event shouldn't cause an observable difference in gas usage.
// TODO(7702): re-check this in the spec and implementation to be sure is a correct solution after
// EIP-7702 is implemented.
aw.touchAddressAndChargeGas(targetAddr, zeroTreeIndex, utils.CodeHashLeafKey, doesntExist, math.MaxUint64)
}

func (aw *AccessWitness) TouchSlotAndChargeGas(addr []byte, slot common.Hash, isWrite bool, availableGas uint64, warmCostCharging bool) uint64 {
Expand Down Expand Up @@ -294,9 +302,9 @@ func (aw *AccessWitness) TouchBasicData(addr []byte, isWrite bool, availableGas
return wanted
}

func (aw *AccessWitness) TouchCodeHash(addr []byte, isWrite bool, availableGas uint64) uint64 {
func (aw *AccessWitness) TouchCodeHash(addr []byte, isWrite bool, availableGas uint64, chargeWarmCosts bool) uint64 {
_, wanted := aw.touchAddressAndChargeGas(addr, zeroTreeIndex, utils.CodeHashLeafKey, isWrite, availableGas)
if wanted == 0 {
if wanted == 0 && chargeWarmCosts {
if availableGas < params.WarmStorageReadCostEIP2929 {
return availableGas
}
Expand Down
2 changes: 1 addition & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
st.evm.Accesses.TouchTxOriginAndComputeGas(originAddr.Bytes())

if msg.To != nil {
st.evm.Accesses.TouchTxExistingAndComputeGas(targetAddr.Bytes(), msg.Value.Sign() != 0)
st.evm.Accesses.TouchTxTarget(targetAddr.Bytes(), msg.Value.Sign() != 0, !st.state.Exist(*targetAddr))

// ensure the code size ends up in the access witness
st.evm.StateDB.GetCodeSize(*targetAddr)
Expand Down
10 changes: 8 additions & 2 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,15 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
debug := evm.Config.Tracer != nil

if !evm.StateDB.Exist(addr) {
if !isPrecompile && evm.chainRules.IsEIP4762 {
if !isPrecompile && evm.chainRules.IsEIP4762 && value.Sign() != 0 {
// add proof of absence to witness
wgas := evm.Accesses.TouchFullAccount(addr.Bytes(), false, gas)
// At this point, the read costs have already been charged, either because this
// is a direct tx call, in which case it's covered by the intrinsic gas, or because
// of a CALL instruction, in which case BASIC_DATA has been added to the access
// list in write mode. If there is enough gas paying for the addition of the code
// hash leaf to the access list, then account creation will proceed unimpaired.
// Thus, only pay for the creation of the code hash leaf here.
wgas := evm.Accesses.TouchCodeHash(addr.Bytes(), false, gas)
if gas < wgas {
evm.StateDB.RevertToSnapshot(snapshot)
return nil, 0, ErrOutOfGas
Expand Down
10 changes: 7 additions & 3 deletions core/vm/operations_verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func gasBalance4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem

func gasExtCodeSize4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
address := stack.peek().Bytes20()
if _, isPrecompile := evm.precompile(address); isPrecompile {
isSystemContract := evm.isSystemContract(address)
_, isPrecompile := evm.precompile(address)
if isPrecompile || isSystemContract {
return params.WarmStorageReadCostEIP2929, nil
}
return evm.Accesses.TouchBasicData(address[:], false, contract.Gas, true), nil
Expand All @@ -48,7 +50,7 @@ func gasExtCodeHash4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
if _, isPrecompile := evm.precompile(address); isPrecompile || evm.isSystemContract(address) {
return params.WarmStorageReadCostEIP2929, nil
}
return evm.Accesses.TouchCodeHash(address[:], false, contract.Gas), nil
return evm.Accesses.TouchCodeHash(address[:], false, contract.Gas, true), nil
}

func makeCallVariantGasEIP4762(oldCalculator gasFunc) gasFunc {
Expand Down Expand Up @@ -126,7 +128,9 @@ func gasExtCodeCopyEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Memo
}
addr := common.Address(stack.peek().Bytes20())

if _, isPrecompile := evm.precompile(addr); isPrecompile {
isSystemContract := evm.isSystemContract(addr)
_, isPrecompile := evm.precompile(addr)
if isPrecompile || isSystemContract {
var overflow bool
if gas, overflow = math.SafeAdd(gas, params.WarmStorageReadCostEIP2929); overflow {
return 0, ErrGasUintOverflow
Expand Down

0 comments on commit 17fd37d

Please sign in to comment.