aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/vm/evm.go48
-rw-r--r--core/vm/gas_table.go12
-rw-r--r--core/vm/instructions.go28
-rw-r--r--core/vm/jump_table.go9
-rw-r--r--core/vm/memory_table.go4
-rw-r--r--core/vm/opcodes.go3
6 files changed, 87 insertions, 17 deletions
diff --git a/core/vm/evm.go b/core/vm/evm.go
index 69c8ec478..0189351e7 100644
--- a/core/vm/evm.go
+++ b/core/vm/evm.go
@@ -319,9 +319,8 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
return ret, contract.Gas, err
}
-// Create creates a new contract using code as deployment code.
-func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
-
+// create creates a new contract using code as deployment code.
+func (evm *EVM) create(caller ContractRef, code []byte, gas uint64, value *big.Int, address common.Address) ([]byte, common.Address, uint64, error) {
// Depth check execution. Fail if we're trying to execute above the
// limit.
if evm.depth > int(params.CallCreateDepth) {
@@ -330,39 +329,38 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I
if !evm.CanTransfer(evm.StateDB, caller.Address(), value) {
return nil, common.Address{}, gas, ErrInsufficientBalance
}
- // Ensure there's no existing contract already at the designated address
nonce := evm.StateDB.GetNonce(caller.Address())
evm.StateDB.SetNonce(caller.Address(), nonce+1)
- contractAddr = crypto.CreateAddress(caller.Address(), nonce)
- contractHash := evm.StateDB.GetCodeHash(contractAddr)
- if evm.StateDB.GetNonce(contractAddr) != 0 || (contractHash != (common.Hash{}) && contractHash != emptyCodeHash) {
+ // Ensure there's no existing contract already at the designated address
+ contractHash := evm.StateDB.GetCodeHash(address)
+ if evm.StateDB.GetNonce(address) != 0 || (contractHash != (common.Hash{}) && contractHash != emptyCodeHash) {
return nil, common.Address{}, 0, ErrContractAddressCollision
}
// Create a new account on the state
snapshot := evm.StateDB.Snapshot()
- evm.StateDB.CreateAccount(contractAddr)
+ evm.StateDB.CreateAccount(address)
if evm.ChainConfig().IsEIP158(evm.BlockNumber) {
- evm.StateDB.SetNonce(contractAddr, 1)
+ evm.StateDB.SetNonce(address, 1)
}
- evm.Transfer(evm.StateDB, caller.Address(), contractAddr, value)
+ evm.Transfer(evm.StateDB, caller.Address(), address, value)
// initialise a new contract and set the code that is to be used by the
// EVM. The contract is a scoped environment for this execution context
// only.
- contract := NewContract(caller, AccountRef(contractAddr), value, gas)
- contract.SetCallCode(&contractAddr, crypto.Keccak256Hash(code), code)
+ contract := NewContract(caller, AccountRef(address), value, gas)
+ contract.SetCallCode(&address, crypto.Keccak256Hash(code), code)
if evm.vmConfig.NoRecursion && evm.depth > 0 {
- return nil, contractAddr, gas, nil
+ return nil, address, gas, nil
}
if evm.vmConfig.Debug && evm.depth == 0 {
- evm.vmConfig.Tracer.CaptureStart(caller.Address(), contractAddr, true, code, gas, value)
+ evm.vmConfig.Tracer.CaptureStart(caller.Address(), address, true, code, gas, value)
}
start := time.Now()
- ret, err = run(evm, contract, nil)
+ ret, err := run(evm, contract, nil)
// check whether the max code size has been exceeded
maxCodeSizeExceeded := evm.ChainConfig().IsEIP158(evm.BlockNumber) && len(ret) > params.MaxCodeSize
@@ -373,7 +371,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I
if err == nil && !maxCodeSizeExceeded {
createDataGas := uint64(len(ret)) * params.CreateDataGas
if contract.UseGas(createDataGas) {
- evm.StateDB.SetCode(contractAddr, ret)
+ evm.StateDB.SetCode(address, ret)
} else {
err = ErrCodeStoreOutOfGas
}
@@ -395,7 +393,23 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I
if evm.vmConfig.Debug && evm.depth == 0 {
evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
}
- return ret, contractAddr, contract.Gas, err
+ return ret, address, contract.Gas, err
+
+}
+
+// Create creates a new contract using code as deployment code.
+func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
+ contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
+ return evm.create(caller, code, gas, value, contractAddr)
+}
+
+// Create2 creates a new contract using code as deployment code.
+//
+// The different between Create2 with Create is Create2 uses sha3(msg.sender ++ salt ++ init_code)[12:]
+// instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
+func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *big.Int, salt *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
+ contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
+ return evm.create(caller, code, gas, endowment, contractAddr)
}
// ChainConfig returns the environment's chain configuration
diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go
index 0764c67a4..e130406ec 100644
--- a/core/vm/gas_table.go
+++ b/core/vm/gas_table.go
@@ -289,6 +289,18 @@ func gasCreate(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, m
return gas, nil
}
+func gasCreate2(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ var overflow bool
+ gas, err := memoryGasCost(mem, memorySize)
+ if err != nil {
+ return 0, err
+ }
+ if gas, overflow = math.SafeAdd(gas, params.Create2Gas); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+}
+
func gasBalance(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
return gt.Balance, nil
}
diff --git a/core/vm/instructions.go b/core/vm/instructions.go
index 1ec13ba35..b25c0111a 100644
--- a/core/vm/instructions.go
+++ b/core/vm/instructions.go
@@ -665,6 +665,34 @@ func opCreate(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
return nil, nil
}
+func opCreate2(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ var (
+ endowment = stack.pop()
+ offset, size = stack.pop(), stack.pop()
+ salt = stack.pop()
+ input = memory.Get(offset.Int64(), size.Int64())
+ gas = contract.Gas
+ )
+
+ // Apply EIP150
+ gas -= gas / 64
+ contract.UseGas(gas)
+ res, addr, returnGas, suberr := evm.Create2(contract, input, gas, endowment, salt)
+ // Push item on the stack based on the returned error.
+ if suberr != nil {
+ stack.push(evm.interpreter.intPool.getZero())
+ } else {
+ stack.push(addr.Big())
+ }
+ contract.Gas += returnGas
+ evm.interpreter.intPool.put(endowment, offset, size, salt)
+
+ if suberr == errExecutionReverted {
+ return res, nil
+ }
+ return nil, nil
+}
+
func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
// Pop gas. The actual gas in in evm.callGasTemp.
evm.interpreter.intPool.put(stack.pop())
diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go
index 111a9b798..014496567 100644
--- a/core/vm/jump_table.go
+++ b/core/vm/jump_table.go
@@ -80,6 +80,15 @@ func newConstantinopleInstructionSet() [256]operation {
validateStack: makeStackFunc(2, 1),
valid: true,
}
+ instructionSet[CREATE2] = operation{
+ execute: opCreate2,
+ gasCost: gasCreate2,
+ validateStack: makeStackFunc(4, 1),
+ memorySize: memoryCreate2,
+ valid: true,
+ writes: true,
+ returns: true,
+ }
return instructionSet
}
diff --git a/core/vm/memory_table.go b/core/vm/memory_table.go
index ab49ebb38..8fa6c90ca 100644
--- a/core/vm/memory_table.go
+++ b/core/vm/memory_table.go
@@ -58,6 +58,10 @@ func memoryCreate(stack *Stack) *big.Int {
return calcMemSize(stack.Back(1), stack.Back(2))
}
+func memoryCreate2(stack *Stack) *big.Int {
+ return calcMemSize(stack.Back(1), stack.Back(2))
+}
+
func memoryCall(stack *Stack) *big.Int {
x := calcMemSize(stack.Back(5), stack.Back(6))
y := calcMemSize(stack.Back(3), stack.Back(4))
diff --git a/core/vm/opcodes.go b/core/vm/opcodes.go
index 6c12c50e5..84426a28a 100644
--- a/core/vm/opcodes.go
+++ b/core/vm/opcodes.go
@@ -209,6 +209,7 @@ const (
CALLCODE
RETURN
DELEGATECALL
+ CREATE2
STATICCALL = 0xfa
REVERT = 0xfd
@@ -370,6 +371,7 @@ var opCodeToString = map[OpCode]string{
RETURN: "RETURN",
CALLCODE: "CALLCODE",
DELEGATECALL: "DELEGATECALL",
+ CREATE2: "CREATE2",
STATICCALL: "STATICCALL",
REVERT: "REVERT",
SELFDESTRUCT: "SELFDESTRUCT",
@@ -521,6 +523,7 @@ var stringToOp = map[string]OpCode{
"LOG3": LOG3,
"LOG4": LOG4,
"CREATE": CREATE,
+ "CREATE2": CREATE2,
"CALL": CALL,
"RETURN": RETURN,
"CALLCODE": CALLCODE,