aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgary rong <garyrong0905@gmail.com>2018-07-24 22:22:03 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-07-24 22:22:03 +0800
commitcab1cff11cbcd4ff60f1a149deb71ec87413b487 (patch)
tree2e7c89b0a541a03e23294d4b444892a9420cbc9c
parent2909f6d7a2ceb5b1cdeb4cc3966531018a0b8334 (diff)
downloadgo-tangerine-cab1cff11cbcd4ff60f1a149deb71ec87413b487.tar
go-tangerine-cab1cff11cbcd4ff60f1a149deb71ec87413b487.tar.gz
go-tangerine-cab1cff11cbcd4ff60f1a149deb71ec87413b487.tar.bz2
go-tangerine-cab1cff11cbcd4ff60f1a149deb71ec87413b487.tar.lz
go-tangerine-cab1cff11cbcd4ff60f1a149deb71ec87413b487.tar.xz
go-tangerine-cab1cff11cbcd4ff60f1a149deb71ec87413b487.tar.zst
go-tangerine-cab1cff11cbcd4ff60f1a149deb71ec87413b487.zip
core, crypto, params: implement CREATE2 evm instrction (#17196)
* core, crypto, params: implement CREATE2 evm instrction * core/vm: add opcode to string mapping * core: remove past fork checking * core, crypto: use option2 to generate new address
-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
-rw-r--r--crypto/crypto.go6
-rw-r--r--params/protocol_params.go1
8 files changed, 94 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,
diff --git a/crypto/crypto.go b/crypto/crypto.go
index 619440e81..dec6e3c19 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -76,6 +76,12 @@ func CreateAddress(b common.Address, nonce uint64) common.Address {
return common.BytesToAddress(Keccak256(data)[12:])
}
+// CreateAddress2 creates an ethereum address given the address bytes, initial
+// contract code and a salt.
+func CreateAddress2(b common.Address, salt common.Hash, code []byte) common.Address {
+ return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt.Bytes(), code)[12:])
+}
+
// ToECDSA creates a private key with the given D value.
func ToECDSA(d []byte) (*ecdsa.PrivateKey, error) {
return toECDSA(d, true)
diff --git a/params/protocol_params.go b/params/protocol_params.go
index 1ea9c5813..46624ac9a 100644
--- a/params/protocol_params.go
+++ b/params/protocol_params.go
@@ -57,6 +57,7 @@ const (
TierStepGas uint64 = 0 // Once per operation, for a selection of them.
LogTopicGas uint64 = 375 // Multiplied by the * of the LOG*, per LOG transaction. e.g. LOG0 incurs 0 * c_txLogTopicGas, LOG4 incurs 4 * c_txLogTopicGas.
CreateGas uint64 = 32000 // Once per CREATE operation & contract-creation transaction.
+ Create2Gas uint64 = 32000 // Once per CREATE2 operation
SuicideRefundGas uint64 = 24000 // Refunded following a suicide operation.
MemoryGas uint64 = 3 // Times the address of the (highest referenced byte in memory + 1). NOTE: referencing happens on read, write and in instructions such as RETURN and CALL.
TxDataNonZeroGas uint64 = 68 // Per byte of data attached to a transaction that is not equal to zero. NOTE: Not payable on data of calls between transactions.