Skip to content

Commit 339175d

Browse files
committed
refactor!: revert to non-deferred call (not at start)
1 parent 3148f7f commit 339175d

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

core/vm/evm.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/core/types"
2525
"github.com/ethereum/go-ethereum/crypto"
26+
"github.com/ethereum/go-ethereum/libevm"
2627
"github.com/ethereum/go-ethereum/params"
2728
"github.com/holiman/uint256"
2829
)
@@ -426,10 +427,7 @@ func (c *codeAndHash) Hash() common.Hash {
426427
}
427428

428429
// create creates a new contract using code as deployment code.
429-
func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, value *uint256.Int, address common.Address, typ OpCode) (deployedCode []byte, deployedAddress common.Address, gasRemaining uint64, retErr error) {
430-
defer func() {
431-
deployedCode, deployedAddress, gasRemaining, retErr = evm.canCreateContract(caller, deployedCode, deployedAddress, gasRemaining, retErr)
432-
}()
430+
func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, value *uint256.Int, address common.Address, typ OpCode) ([]byte, common.Address, uint64, error) {
433431
// Depth check execution. Fail if we're trying to execute above the
434432
// limit.
435433
if evm.depth > int(params.CallCreateDepth) {
@@ -453,6 +451,19 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
453451
if evm.StateDB.GetNonce(address) != 0 || (contractHash != (common.Hash{}) && contractHash != types.EmptyCodeHash) {
454452
return nil, common.Address{}, 0, ErrContractAddressCollision
455453
}
454+
455+
//libevm:start
456+
//
457+
// This check MUST be placed after the caller's nonce is incremented but
458+
// before all other state-modifying behaviour, even if changes may be
459+
// reverted to the snapshot.
460+
addrs := &libevm.AddressContext{Origin: evm.Origin, Caller: caller.Address(), Self: address}
461+
gas, err := evm.chainRules.Hooks().CanCreateContract(addrs, gas, evm.StateDB)
462+
if err != nil {
463+
return nil, common.Address{}, gas, err
464+
}
465+
//libevm:end
466+
456467
// Create a new account on the state
457468
snapshot := evm.StateDB.Snapshot()
458469
evm.StateDB.CreateAccount(address)

core/vm/evm.libevm.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

params/hooks.libevm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type RulesHooks interface {
3232
// RulesAllowlistHooks are a subset of [RulesHooks] that gate actions, signalled
3333
// by returning a nil (allowed) or non-nil (blocked) error.
3434
type RulesAllowlistHooks interface {
35+
// CanCreateContract is called after the deployer's nonce is incremented but
36+
// before all other state-modifying actions.
3537
CanCreateContract(_ *libevm.AddressContext, gas uint64, _ libevm.StateReader) (gasRemaining uint64, _ error)
3638
CanExecuteTransaction(from common.Address, to *common.Address, _ libevm.StateReader) error
3739
}

0 commit comments

Comments
 (0)