Skip to content

Commit

Permalink
Add permissioned create
Browse files Browse the repository at this point in the history
  • Loading branch information
mdehoog committed Dec 13, 2024
1 parent d5dbfcf commit b6a11ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions core/vm/can_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package vm

import (
"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
)

const canCreateGasLimit = uint64(1000000)

// `bytes32(uint256(keccak256('eip1967.proxy.implementation')) - 1)`
var proxyImplementationSlot = common.HexToHash("0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc")

// Predeploy address (may or may not have an implementation behind it)
var letPredeploy = common.HexToAddress("0x42000000000000000000000000000000000001e7")

// 4-byte signature of `canCreate(address contractCaller)` + 12-byte padding
var canCreatePrefix = common.FromHex("0x7804a5dc000000000000000000000000")

func CanCreate(evm *EVM, caller ContractRef) error {
implementation := evm.StateDB.GetState(letPredeploy, proxyImplementationSlot)
if implementation == (common.Hash{}) {
return nil
}
var contractCaller common.Address
if contract, ok := caller.(*Contract); ok {
contractCaller = contract.Caller()
}
data := append(canCreatePrefix, contractCaller[:]...)
_, _, err := evm.Call(caller, letPredeploy, data, canCreateGasLimit, new(uint256.Int))
return err
}
1 change: 1 addition & 0 deletions core/vm/can_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package vm
3 changes: 3 additions & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
return nil, common.Address{}, gas, ErrInsufficientBalance
}
if err := CanCreate(evm, caller); err != nil {
return nil, common.Address{}, gas, err
}
nonce := evm.StateDB.GetNonce(caller.Address())
if nonce+1 < nonce {
return nil, common.Address{}, gas, ErrNonceUintOverflow
Expand Down

0 comments on commit b6a11ac

Please sign in to comment.