Skip to content

Commit 032e1ab

Browse files
committed
import most changes from jsign-witness-fix
Co-authored-by: Ignacio Hagopian <[email protected]> Signed-off-by: Guillaume Ballet <[email protected]>
1 parent 762282e commit 032e1ab

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

core/state/access_witness.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ func (aw *AccessWitness) TouchAndChargeMessageCall(addr []byte, availableGas uin
113113
func (aw *AccessWitness) TouchAndChargeValueTransfer(callerAddr, targetAddr []byte, availableGas uint64) uint64 {
114114
chargedGas1, _ := aw.touchAddressAndChargeGas(callerAddr, zeroTreeIndex, utils.BasicDataLeafKey, true, availableGas)
115115
chargedGas2, _ := aw.touchAddressAndChargeGas(targetAddr, zeroTreeIndex, utils.BasicDataLeafKey, true, availableGas-chargedGas1)
116+
if chargedGas1+chargedGas2 == 0 {
117+
return params.WarmStorageReadCostEIP2929
118+
}
116119
return chargedGas1 + chargedGas2
117120
}
118121

@@ -140,9 +143,14 @@ func (aw *AccessWitness) TouchTxOriginAndComputeGas(originAddr []byte) {
140143
}
141144
}
142145

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

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

297-
func (aw *AccessWitness) TouchCodeHash(addr []byte, isWrite bool, availableGas uint64) uint64 {
305+
func (aw *AccessWitness) TouchCodeHash(addr []byte, isWrite bool, availableGas uint64, chargeWarmCosts bool) uint64 {
298306
_, wanted := aw.touchAddressAndChargeGas(addr, zeroTreeIndex, utils.CodeHashLeafKey, isWrite, availableGas)
299-
if wanted == 0 {
307+
if wanted == 0 && chargeWarmCosts {
300308
if availableGas < params.WarmStorageReadCostEIP2929 {
301309
return availableGas
302310
}

core/state_transition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
396396
st.evm.Accesses.TouchTxOriginAndComputeGas(originAddr.Bytes())
397397

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

401401
// ensure the code size ends up in the access witness
402402
st.evm.StateDB.GetCodeSize(*targetAddr)

core/vm/evm.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,15 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
200200
debug := evm.Config.Tracer != nil
201201

202202
if !evm.StateDB.Exist(addr) {
203-
if !isPrecompile && evm.chainRules.IsEIP4762 {
203+
if !isPrecompile && evm.chainRules.IsEIP4762 && value.Sign() != 0 {
204204
// add proof of absence to witness
205-
wgas := evm.Accesses.TouchFullAccount(addr.Bytes(), false, gas)
205+
// At this point, the read costs have already been charged, either because this
206+
// is a direct tx call, in which case it's covered by the intrinsic gas, or because
207+
// of a CALL instruction, in which case BASIC_DATA has been added to the access
208+
// list in write mode. If there is enough gas paying for the addition of the code
209+
// hash leaf to the access list, then account creation will proceed unimpaired.
210+
// Thus, only pay for the creation of the code hash leaf here.
211+
wgas := evm.Accesses.TouchCodeHash(addr.Bytes(), true, gas, false)
206212
if gas < wgas {
207213
evm.StateDB.RevertToSnapshot(snapshot)
208214
return nil, 0, ErrOutOfGas

core/vm/operations_verkle.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ func gasBalance4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem
3737

3838
func gasExtCodeSize4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
3939
address := stack.peek().Bytes20()
40-
if _, isPrecompile := evm.precompile(address); isPrecompile {
40+
isSystemContract := evm.isSystemContract(address)
41+
_, isPrecompile := evm.precompile(address)
42+
if isPrecompile || isSystemContract {
4143
return params.WarmStorageReadCostEIP2929, nil
4244
}
4345
return evm.Accesses.TouchBasicData(address[:], false, contract.Gas, true), nil
@@ -48,7 +50,7 @@ func gasExtCodeHash4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
4850
if _, isPrecompile := evm.precompile(address); isPrecompile || evm.isSystemContract(address) {
4951
return params.WarmStorageReadCostEIP2929, nil
5052
}
51-
return evm.Accesses.TouchCodeHash(address[:], false, contract.Gas), nil
53+
return evm.Accesses.TouchCodeHash(address[:], false, contract.Gas, true), nil
5254
}
5355

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

129-
if _, isPrecompile := evm.precompile(addr); isPrecompile {
131+
isSystemContract := evm.isSystemContract(addr)
132+
_, isPrecompile := evm.precompile(addr)
133+
if isPrecompile || isSystemContract {
130134
var overflow bool
131135
if gas, overflow = math.SafeAdd(gas, params.WarmStorageReadCostEIP2929); overflow {
132136
return 0, ErrGasUintOverflow

0 commit comments

Comments
 (0)