2016-04-14 19:18:24 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-18 00:09:36 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-08-02 03:20:41 +03:00
|
|
|
|
2015-07-18 00:09:36 +03:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2017-08-15 11:23:23 +03:00
|
|
|
"errors"
|
2015-10-06 19:16:03 +03:00
|
|
|
"fmt"
|
2015-07-18 00:09:36 +03:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-11-02 14:43:15 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2017-01-05 16:03:50 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-07-18 00:09:36 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2019-01-04 01:15:26 +03:00
|
|
|
"golang.org/x/crypto/sha3"
|
2015-07-18 00:09:36 +03:00
|
|
|
)
|
|
|
|
|
2017-02-02 00:36:51 +03:00
|
|
|
var (
|
2017-08-16 13:36:48 +03:00
|
|
|
bigZero = new(big.Int)
|
2018-03-08 15:48:19 +03:00
|
|
|
tt255 = math.BigPow(2, 255)
|
2017-08-16 13:36:48 +03:00
|
|
|
errWriteProtection = errors.New("evm: write protection")
|
|
|
|
errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
|
2017-08-16 17:09:29 +03:00
|
|
|
errExecutionReverted = errors.New("evm: execution reverted")
|
2017-08-21 03:47:15 +03:00
|
|
|
errMaxCodeSizeExceeded = errors.New("evm: max code size exceeded")
|
2017-02-02 00:36:51 +03:00
|
|
|
)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opAdd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-26 12:28:46 +03:00
|
|
|
x, y := stack.pop(), stack.peek()
|
|
|
|
math.U256(y.Add(x, y))
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSub(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-26 12:28:46 +03:00
|
|
|
x, y := stack.pop(), stack.peek()
|
|
|
|
math.U256(y.Sub(x, y))
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opMul(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
x, y := stack.pop(), stack.pop()
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
stack.push(math.U256(x.Mul(x, y)))
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(y)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opDiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-26 12:28:46 +03:00
|
|
|
x, y := stack.pop(), stack.peek()
|
2017-02-28 17:09:11 +03:00
|
|
|
if y.Sign() != 0 {
|
2018-03-26 12:28:46 +03:00
|
|
|
math.U256(y.Div(x, y))
|
2015-08-07 00:06:47 +03:00
|
|
|
} else {
|
2018-03-26 12:28:46 +03:00
|
|
|
y.SetUint64(0)
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSdiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
x, y := math.S256(stack.pop()), math.S256(stack.pop())
|
2018-07-25 15:56:39 +03:00
|
|
|
res := interpreter.intPool.getZero()
|
2018-03-26 12:28:46 +03:00
|
|
|
|
|
|
|
if y.Sign() == 0 || x.Sign() == 0 {
|
|
|
|
stack.push(res)
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2018-03-26 12:28:46 +03:00
|
|
|
if x.Sign() != y.Sign() {
|
|
|
|
res.Div(x.Abs(x), y.Abs(y))
|
|
|
|
res.Neg(res)
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2018-03-26 12:28:46 +03:00
|
|
|
res.Div(x.Abs(x), y.Abs(y))
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
stack.push(math.U256(res))
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x, y)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opMod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
x, y := stack.pop(), stack.pop()
|
2017-02-28 17:09:11 +03:00
|
|
|
if y.Sign() == 0 {
|
2018-03-26 12:28:46 +03:00
|
|
|
stack.push(x.SetUint64(0))
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
stack.push(math.U256(x.Mod(x, y)))
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(y)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
x, y := math.S256(stack.pop()), math.S256(stack.pop())
|
2018-07-25 15:56:39 +03:00
|
|
|
res := interpreter.intPool.getZero()
|
2015-07-18 00:09:36 +03:00
|
|
|
|
2017-02-28 17:09:11 +03:00
|
|
|
if y.Sign() == 0 {
|
2018-03-26 12:28:46 +03:00
|
|
|
stack.push(res)
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2017-02-28 17:09:11 +03:00
|
|
|
if x.Sign() < 0 {
|
2018-03-26 12:28:46 +03:00
|
|
|
res.Mod(x.Abs(x), y.Abs(y))
|
|
|
|
res.Neg(res)
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2018-03-26 12:28:46 +03:00
|
|
|
res.Mod(x.Abs(x), y.Abs(y))
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
stack.push(math.U256(res))
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x, y)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opExp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2016-11-02 14:43:15 +03:00
|
|
|
base, exponent := stack.pop(), stack.pop()
|
2018-10-16 01:51:39 +03:00
|
|
|
// some shortcuts
|
|
|
|
cmpToOne := exponent.Cmp(big1)
|
|
|
|
if cmpToOne < 0 { // Exponent is zero
|
|
|
|
// x ^ 0 == 1
|
|
|
|
stack.push(base.SetUint64(1))
|
|
|
|
} else if base.Sign() == 0 {
|
|
|
|
// 0 ^ y, if y != 0, == 0
|
|
|
|
stack.push(base.SetUint64(0))
|
|
|
|
} else if cmpToOne == 0 { // Exponent is one
|
|
|
|
// x ^ 1 == x
|
|
|
|
stack.push(base)
|
|
|
|
} else {
|
|
|
|
stack.push(math.Exp(base, exponent))
|
|
|
|
interpreter.intPool.put(base)
|
|
|
|
}
|
|
|
|
interpreter.intPool.put(exponent)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSignExtend(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
back := stack.pop()
|
|
|
|
if back.Cmp(big.NewInt(31)) < 0 {
|
|
|
|
bit := uint(back.Uint64()*8 + 7)
|
|
|
|
num := stack.pop()
|
2015-08-07 00:06:47 +03:00
|
|
|
mask := back.Lsh(common.Big1, bit)
|
2015-07-18 00:09:36 +03:00
|
|
|
mask.Sub(mask, common.Big1)
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
if num.Bit(int(bit)) > 0 {
|
2015-07-18 00:09:36 +03:00
|
|
|
num.Or(num, mask.Not(mask))
|
|
|
|
} else {
|
|
|
|
num.And(num, mask)
|
|
|
|
}
|
|
|
|
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
stack.push(math.U256(num))
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(back)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opNot(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-26 12:28:46 +03:00
|
|
|
x := stack.peek()
|
|
|
|
math.U256(x.Not(x))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opLt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-26 12:28:46 +03:00
|
|
|
x, y := stack.pop(), stack.peek()
|
2015-07-18 00:09:36 +03:00
|
|
|
if x.Cmp(y) < 0 {
|
2018-03-26 12:28:46 +03:00
|
|
|
y.SetUint64(1)
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2018-03-26 12:28:46 +03:00
|
|
|
y.SetUint64(0)
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opGt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-26 12:28:46 +03:00
|
|
|
x, y := stack.pop(), stack.peek()
|
2015-07-18 00:09:36 +03:00
|
|
|
if x.Cmp(y) > 0 {
|
2018-03-26 12:28:46 +03:00
|
|
|
y.SetUint64(1)
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2018-03-26 12:28:46 +03:00
|
|
|
y.SetUint64(0)
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSlt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-08 15:48:19 +03:00
|
|
|
x, y := stack.pop(), stack.peek()
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-03-08 15:48:19 +03:00
|
|
|
xSign := x.Cmp(tt255)
|
|
|
|
ySign := y.Cmp(tt255)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case xSign >= 0 && ySign < 0:
|
|
|
|
y.SetUint64(1)
|
|
|
|
|
|
|
|
case xSign < 0 && ySign >= 0:
|
|
|
|
y.SetUint64(0)
|
|
|
|
|
|
|
|
default:
|
|
|
|
if x.Cmp(y) < 0 {
|
|
|
|
y.SetUint64(1)
|
|
|
|
} else {
|
|
|
|
y.SetUint64(0)
|
|
|
|
}
|
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSgt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-08 15:48:19 +03:00
|
|
|
x, y := stack.pop(), stack.peek()
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-03-08 15:48:19 +03:00
|
|
|
xSign := x.Cmp(tt255)
|
|
|
|
ySign := y.Cmp(tt255)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case xSign >= 0 && ySign < 0:
|
|
|
|
y.SetUint64(0)
|
|
|
|
|
|
|
|
case xSign < 0 && ySign >= 0:
|
|
|
|
y.SetUint64(1)
|
|
|
|
|
|
|
|
default:
|
|
|
|
if x.Cmp(y) > 0 {
|
|
|
|
y.SetUint64(1)
|
|
|
|
} else {
|
|
|
|
y.SetUint64(0)
|
|
|
|
}
|
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opEq(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-08 15:48:19 +03:00
|
|
|
x, y := stack.pop(), stack.peek()
|
2015-07-18 00:09:36 +03:00
|
|
|
if x.Cmp(y) == 0 {
|
2018-03-08 15:48:19 +03:00
|
|
|
y.SetUint64(1)
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2018-03-08 15:48:19 +03:00
|
|
|
y.SetUint64(0)
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opIszero(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-08 15:48:19 +03:00
|
|
|
x := stack.peek()
|
2017-02-28 17:09:11 +03:00
|
|
|
if x.Sign() > 0 {
|
2018-03-08 15:48:19 +03:00
|
|
|
x.SetUint64(0)
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2018-03-08 15:48:19 +03:00
|
|
|
x.SetUint64(1)
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opAnd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
x, y := stack.pop(), stack.pop()
|
2015-08-07 00:06:47 +03:00
|
|
|
stack.push(x.And(x, y))
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(y)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2017-08-16 13:36:48 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opOr(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-26 12:28:46 +03:00
|
|
|
x, y := stack.pop(), stack.peek()
|
|
|
|
y.Or(x, y)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2017-08-16 13:36:48 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opXor(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-26 12:28:46 +03:00
|
|
|
x, y := stack.pop(), stack.peek()
|
|
|
|
y.Xor(x, y)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(x)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2017-02-28 17:09:11 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opByte(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2017-05-29 00:39:33 +03:00
|
|
|
th, val := stack.pop(), stack.peek()
|
|
|
|
if th.Cmp(common.Big32) < 0 {
|
2017-06-06 19:38:38 +03:00
|
|
|
b := math.Byte(val, 32, int(th.Int64()))
|
|
|
|
val.SetUint64(uint64(b))
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2017-05-29 00:39:33 +03:00
|
|
|
val.SetUint64(0)
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(th)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2017-08-16 13:36:48 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opAddmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-08-07 00:06:47 +03:00
|
|
|
x, y, z := stack.pop(), stack.pop(), stack.pop()
|
2017-01-04 22:17:24 +03:00
|
|
|
if z.Cmp(bigZero) > 0 {
|
2018-03-26 12:28:46 +03:00
|
|
|
x.Add(x, y)
|
|
|
|
x.Mod(x, z)
|
|
|
|
stack.push(math.U256(x))
|
2015-08-07 00:06:47 +03:00
|
|
|
} else {
|
2018-03-26 12:28:46 +03:00
|
|
|
stack.push(x.SetUint64(0))
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(y, z)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2017-08-16 13:36:48 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opMulmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-08-07 00:06:47 +03:00
|
|
|
x, y, z := stack.pop(), stack.pop(), stack.pop()
|
2017-01-04 22:17:24 +03:00
|
|
|
if z.Cmp(bigZero) > 0 {
|
2018-03-26 12:28:46 +03:00
|
|
|
x.Mul(x, y)
|
|
|
|
x.Mod(x, z)
|
|
|
|
stack.push(math.U256(x))
|
2015-08-07 00:06:47 +03:00
|
|
|
} else {
|
2018-03-26 12:28:46 +03:00
|
|
|
stack.push(x.SetUint64(0))
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(y, z)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 13:32:57 +03:00
|
|
|
// opSHL implements Shift Left
|
|
|
|
// The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2,
|
|
|
|
// and pushes on the stack arg2 shifted to the left by arg1 number of bits.
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSHL(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-02-23 13:32:57 +03:00
|
|
|
// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
|
|
|
|
shift, value := math.U256(stack.pop()), math.U256(stack.peek())
|
2018-07-25 15:56:39 +03:00
|
|
|
defer interpreter.intPool.put(shift) // First operand back into the pool
|
2018-02-23 13:32:57 +03:00
|
|
|
|
|
|
|
if shift.Cmp(common.Big256) >= 0 {
|
|
|
|
value.SetUint64(0)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
n := uint(shift.Uint64())
|
|
|
|
math.U256(value.Lsh(value, n))
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// opSHR implements Logical Shift Right
|
|
|
|
// The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2,
|
|
|
|
// and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill.
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSHR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-02-23 13:32:57 +03:00
|
|
|
// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
|
|
|
|
shift, value := math.U256(stack.pop()), math.U256(stack.peek())
|
2018-07-25 15:56:39 +03:00
|
|
|
defer interpreter.intPool.put(shift) // First operand back into the pool
|
2018-02-23 13:32:57 +03:00
|
|
|
|
|
|
|
if shift.Cmp(common.Big256) >= 0 {
|
|
|
|
value.SetUint64(0)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
n := uint(shift.Uint64())
|
|
|
|
math.U256(value.Rsh(value, n))
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// opSAR implements Arithmetic Shift Right
|
|
|
|
// The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2,
|
|
|
|
// and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension.
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSAR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-02-23 13:32:57 +03:00
|
|
|
// Note, S256 returns (potentially) a new bigint, so we're popping, not peeking this one
|
|
|
|
shift, value := math.U256(stack.pop()), math.S256(stack.pop())
|
2018-07-25 15:56:39 +03:00
|
|
|
defer interpreter.intPool.put(shift) // First operand back into the pool
|
2018-02-23 13:32:57 +03:00
|
|
|
|
|
|
|
if shift.Cmp(common.Big256) >= 0 {
|
2018-09-04 11:49:18 +03:00
|
|
|
if value.Sign() >= 0 {
|
2018-02-23 13:32:57 +03:00
|
|
|
value.SetUint64(0)
|
|
|
|
} else {
|
|
|
|
value.SetInt64(-1)
|
|
|
|
}
|
|
|
|
stack.push(math.U256(value))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
n := uint(shift.Uint64())
|
|
|
|
value.Rsh(value, n)
|
|
|
|
stack.push(math.U256(value))
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSha3(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
offset, size := stack.pop(), stack.pop()
|
2017-01-17 14:19:50 +03:00
|
|
|
data := memory.Get(offset.Int64(), size.Int64())
|
|
|
|
|
2018-10-08 14:14:29 +03:00
|
|
|
if interpreter.hasher == nil {
|
2019-01-04 01:15:26 +03:00
|
|
|
interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState)
|
2018-10-08 14:14:29 +03:00
|
|
|
} else {
|
|
|
|
interpreter.hasher.Reset()
|
|
|
|
}
|
|
|
|
interpreter.hasher.Write(data)
|
|
|
|
interpreter.hasher.Read(interpreter.hasherBuf[:])
|
|
|
|
|
|
|
|
evm := interpreter.evm
|
2017-01-04 22:17:24 +03:00
|
|
|
if evm.vmConfig.EnablePreimageRecording {
|
2018-10-08 14:14:29 +03:00
|
|
|
evm.StateDB.AddPreimage(interpreter.hasherBuf, data)
|
2017-01-17 14:19:50 +03:00
|
|
|
}
|
2018-10-08 14:14:29 +03:00
|
|
|
stack.push(interpreter.intPool.get().SetBytes(interpreter.hasherBuf[:]))
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(offset, size)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opAddress(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
stack.push(contract.Address().Big())
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-26 12:28:46 +03:00
|
|
|
slot := stack.peek()
|
2018-07-25 15:56:39 +03:00
|
|
|
slot.Set(interpreter.evm.StateDB.GetBalance(common.BigToAddress(slot)))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opOrigin(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(interpreter.evm.Origin.Big())
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCaller(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2016-01-20 00:50:00 +02:00
|
|
|
stack.push(contract.Caller().Big())
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCallValue(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(interpreter.intPool.get().Set(contract.value))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(interpreter.intPool.get().SetBytes(getDataBig(contract.Input, stack.pop(), big32)))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(interpreter.intPool.get().SetInt64(int64(len(contract.Input))))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
var (
|
2017-08-16 13:36:48 +03:00
|
|
|
memOffset = stack.pop()
|
|
|
|
dataOffset = stack.pop()
|
|
|
|
length = stack.pop()
|
2015-07-18 00:09:36 +03:00
|
|
|
)
|
2017-08-16 13:36:48 +03:00
|
|
|
memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length))
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(memOffset, dataOffset, length)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(interpreter.intPool.get().SetUint64(uint64(len(interpreter.returnData))))
|
2017-08-16 13:07:33 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2017-08-16 13:07:33 +03:00
|
|
|
var (
|
2017-08-16 13:36:48 +03:00
|
|
|
memOffset = stack.pop()
|
|
|
|
dataOffset = stack.pop()
|
|
|
|
length = stack.pop()
|
2018-03-26 12:28:46 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
end = interpreter.intPool.get().Add(dataOffset, length)
|
2017-08-16 13:07:33 +03:00
|
|
|
)
|
2018-07-25 15:56:39 +03:00
|
|
|
defer interpreter.intPool.put(memOffset, dataOffset, length, end)
|
2017-08-16 13:07:33 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
if end.BitLen() > 64 || uint64(len(interpreter.returnData)) < end.Uint64() {
|
2017-08-16 13:36:48 +03:00
|
|
|
return nil, errReturnDataOutOfBounds
|
2017-08-16 13:07:33 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[dataOffset.Uint64():end.Uint64()])
|
2017-08-16 13:07:33 +03:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-03-26 12:28:46 +03:00
|
|
|
slot := stack.peek()
|
2018-07-25 15:56:39 +03:00
|
|
|
slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(common.BigToAddress(slot))))
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
l := interpreter.intPool.get().SetInt64(int64(len(contract.Code)))
|
2015-07-18 00:09:36 +03:00
|
|
|
stack.push(l)
|
2018-03-26 12:28:46 +03:00
|
|
|
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
var (
|
2017-08-16 13:36:48 +03:00
|
|
|
memOffset = stack.pop()
|
|
|
|
codeOffset = stack.pop()
|
|
|
|
length = stack.pop()
|
2015-07-18 00:09:36 +03:00
|
|
|
)
|
2017-08-16 13:36:48 +03:00
|
|
|
codeCopy := getDataBig(contract.Code, codeOffset, length)
|
|
|
|
memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(memOffset, codeOffset, length)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
var (
|
2017-08-16 13:36:48 +03:00
|
|
|
addr = common.BigToAddress(stack.pop())
|
|
|
|
memOffset = stack.pop()
|
|
|
|
codeOffset = stack.pop()
|
|
|
|
length = stack.pop()
|
2015-07-18 00:09:36 +03:00
|
|
|
)
|
2018-07-25 15:56:39 +03:00
|
|
|
codeCopy := getDataBig(interpreter.evm.StateDB.GetCode(addr), codeOffset, length)
|
2017-08-16 13:36:48 +03:00
|
|
|
memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(memOffset, codeOffset, length)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-24 18:06:40 +03:00
|
|
|
// opExtCodeHash returns the code hash of a specified account.
|
|
|
|
// There are several cases when the function is called, while we can relay everything
|
|
|
|
// to `state.GetCodeHash` function to ensure the correctness.
|
|
|
|
// (1) Caller tries to get the code hash of a normal contract account, state
|
|
|
|
// should return the relative code hash and set it as the result.
|
|
|
|
//
|
|
|
|
// (2) Caller tries to get the code hash of a non-existent account, state should
|
|
|
|
// return common.Hash{} and zero will be set as the result.
|
|
|
|
//
|
|
|
|
// (3) Caller tries to get the code hash for an account without contract code,
|
|
|
|
// state should return emptyCodeHash(0xc5d246...) as the result.
|
|
|
|
//
|
|
|
|
// (4) Caller tries to get the code hash of a precompiled account, the result
|
|
|
|
// should be zero or emptyCodeHash.
|
|
|
|
//
|
|
|
|
// It is worth noting that in order to avoid unnecessary create and clean,
|
|
|
|
// all precompile accounts on mainnet have been transferred 1 wei, so the return
|
|
|
|
// here should be emptyCodeHash.
|
|
|
|
// If the precompile account is not transferred any amount on a private or
|
|
|
|
// customized chain, the return value will be zero.
|
|
|
|
//
|
|
|
|
// (5) Caller tries to get the code hash for an account which is marked as suicided
|
|
|
|
// in the current transaction, the code hash of this account should be returned.
|
|
|
|
//
|
|
|
|
// (6) Caller tries to get the code hash for an account which is marked as deleted,
|
|
|
|
// this account should be regarded as a non-existent account and zero should be returned.
|
2018-07-25 15:56:39 +03:00
|
|
|
func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-07-24 18:06:40 +03:00
|
|
|
slot := stack.peek()
|
2018-10-26 09:52:41 +03:00
|
|
|
address := common.BigToAddress(slot)
|
|
|
|
if interpreter.evm.StateDB.Empty(address) {
|
|
|
|
slot.SetUint64(0)
|
|
|
|
} else {
|
|
|
|
slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes())
|
|
|
|
}
|
2018-07-24 18:06:40 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opGasprice(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(interpreter.intPool.get().Set(interpreter.evm.GasPrice))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opBlockhash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
num := stack.pop()
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
n := interpreter.intPool.get().Sub(interpreter.evm.BlockNumber, common.Big257)
|
|
|
|
if num.Cmp(n) > 0 && num.Cmp(interpreter.evm.BlockNumber) < 0 {
|
|
|
|
stack.push(interpreter.evm.GetHash(num.Uint64()).Big())
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.getZero())
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(num, n)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCoinbase(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(interpreter.evm.Coinbase.Big())
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opTimestamp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Time)))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opNumber(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.BlockNumber)))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opDifficulty(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Difficulty)))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opGasLimit(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(math.U256(interpreter.intPool.get().SetUint64(interpreter.evm.GasLimit)))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opPop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
interpreter.intPool.put(stack.pop())
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opMload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
offset := stack.pop()
|
2018-07-25 15:56:39 +03:00
|
|
|
val := interpreter.intPool.get().SetBytes(memory.Get(offset.Int64(), 32))
|
2015-07-18 00:09:36 +03:00
|
|
|
stack.push(val)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(offset)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opMstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
// pop value of the stack
|
|
|
|
mStart, val := stack.pop(), stack.pop()
|
2018-06-14 12:23:37 +03:00
|
|
|
memory.Set32(mStart.Uint64(), val)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(mStart, val)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opMstore8(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
off, val := stack.pop().Int64(), stack.pop().Int64()
|
|
|
|
memory.store[off] = byte(val & 0xff)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-06-14 12:23:37 +03:00
|
|
|
loc := stack.peek()
|
2018-07-25 15:56:39 +03:00
|
|
|
val := interpreter.evm.StateDB.GetState(contract.Address(), common.BigToHash(loc))
|
2018-06-14 12:23:37 +03:00
|
|
|
loc.SetBytes(val.Bytes())
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-07-18 00:09:36 +03:00
|
|
|
loc := common.BigToHash(stack.pop())
|
|
|
|
val := stack.pop()
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val))
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(val)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2017-01-05 13:52:10 +03:00
|
|
|
pos := stack.pop()
|
2018-10-04 18:15:37 +03:00
|
|
|
if !contract.validJumpdest(pos) {
|
2017-01-05 13:52:10 +03:00
|
|
|
nop := contract.GetOp(pos.Uint64())
|
|
|
|
return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
|
|
|
|
}
|
|
|
|
*pc = pos.Uint64()
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(pos)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-08-30 11:19:10 +03:00
|
|
|
}
|
2017-08-16 13:36:48 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opJumpi(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2017-01-05 13:52:10 +03:00
|
|
|
pos, cond := stack.pop(), stack.pop()
|
2017-02-28 17:09:11 +03:00
|
|
|
if cond.Sign() != 0 {
|
2018-10-04 18:15:37 +03:00
|
|
|
if !contract.validJumpdest(pos) {
|
2017-01-05 13:52:10 +03:00
|
|
|
nop := contract.GetOp(pos.Uint64())
|
|
|
|
return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
|
|
|
|
}
|
|
|
|
*pc = pos.Uint64()
|
|
|
|
} else {
|
|
|
|
*pc++
|
|
|
|
}
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(pos, cond)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-08-30 11:19:10 +03:00
|
|
|
}
|
2017-08-16 13:36:48 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opJumpdest(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-08-30 11:19:10 +03:00
|
|
|
}
|
2015-07-18 00:09:36 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opPc(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(interpreter.intPool.get().SetUint64(*pc))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opMsize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(interpreter.intPool.get().SetInt64(int64(memory.Len())))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opGas(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.push(interpreter.intPool.get().SetUint64(contract.Gas))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2016-01-20 00:50:00 +02:00
|
|
|
var (
|
|
|
|
value = stack.pop()
|
|
|
|
offset, size = stack.pop(), stack.pop()
|
|
|
|
input = memory.Get(offset.Int64(), size.Int64())
|
2017-01-04 22:17:24 +03:00
|
|
|
gas = contract.Gas
|
2016-01-20 00:50:00 +02:00
|
|
|
)
|
2018-07-25 15:56:39 +03:00
|
|
|
if interpreter.evm.ChainConfig().IsEIP150(interpreter.evm.BlockNumber) {
|
2017-01-04 22:17:24 +03:00
|
|
|
gas -= gas / 64
|
2016-10-08 01:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
contract.UseGas(gas)
|
2018-07-25 15:56:39 +03:00
|
|
|
res, addr, returnGas, suberr := interpreter.evm.Create(contract, input, gas, value)
|
2016-01-20 00:50:00 +02:00
|
|
|
// Push item on the stack based on the returned error. If the ruleset is
|
|
|
|
// homestead we must check for CodeStoreOutOfGasError (homestead only
|
|
|
|
// rule) and treat as an error, if the ruleset is frontier we must
|
|
|
|
// ignore this error and pretend the operation was successful.
|
2018-07-25 15:56:39 +03:00
|
|
|
if interpreter.evm.ChainConfig().IsHomestead(interpreter.evm.BlockNumber) && suberr == ErrCodeStoreOutOfGas {
|
|
|
|
stack.push(interpreter.intPool.getZero())
|
2017-01-05 13:52:10 +03:00
|
|
|
} else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.getZero())
|
2016-01-20 00:50:00 +02:00
|
|
|
} else {
|
|
|
|
stack.push(addr.Big())
|
|
|
|
}
|
2017-01-04 22:17:24 +03:00
|
|
|
contract.Gas += returnGas
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(value, offset, size)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2017-08-16 17:09:29 +03:00
|
|
|
if suberr == errExecutionReverted {
|
|
|
|
return res, nil
|
|
|
|
}
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-07-24 17:22:03 +03:00
|
|
|
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)
|
2018-07-25 15:56:39 +03:00
|
|
|
res, addr, returnGas, suberr := interpreter.evm.Create2(contract, input, gas, endowment, salt)
|
2018-07-24 17:22:03 +03:00
|
|
|
// Push item on the stack based on the returned error.
|
|
|
|
if suberr != nil {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.getZero())
|
2018-07-24 17:22:03 +03:00
|
|
|
} else {
|
|
|
|
stack.push(addr.Big())
|
|
|
|
}
|
|
|
|
contract.Gas += returnGas
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(endowment, offset, size, salt)
|
2018-07-24 17:22:03 +03:00
|
|
|
|
|
|
|
if suberr == errExecutionReverted {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2018-09-25 13:26:35 +03:00
|
|
|
// Pop gas. The actual gas in interpreter.evm.callGasTemp.
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(stack.pop())
|
|
|
|
gas := interpreter.evm.callGasTemp
|
2017-11-28 22:05:49 +03:00
|
|
|
// Pop other call parameters.
|
|
|
|
addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
|
|
|
|
toAddr := common.BigToAddress(addr)
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
value = math.U256(value)
|
2017-11-28 22:05:49 +03:00
|
|
|
// Get the arguments from the memory.
|
2015-07-18 00:09:36 +03:00
|
|
|
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
|
|
|
|
2017-02-28 17:09:11 +03:00
|
|
|
if value.Sign() != 0 {
|
2017-01-04 22:17:24 +03:00
|
|
|
gas += params.CallStipend
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value)
|
2015-07-18 00:09:36 +03:00
|
|
|
if err != nil {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.getZero())
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.get().SetUint64(1))
|
2017-08-16 17:09:29 +03:00
|
|
|
}
|
|
|
|
if err == nil || err == errExecutionReverted {
|
2015-07-18 00:09:36 +03:00
|
|
|
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
|
|
|
}
|
2017-01-04 22:17:24 +03:00
|
|
|
contract.Gas += returnGas
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
|
2017-02-02 00:36:51 +03:00
|
|
|
return ret, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
|
|
|
|
interpreter.intPool.put(stack.pop())
|
|
|
|
gas := interpreter.evm.callGasTemp
|
2017-11-28 22:05:49 +03:00
|
|
|
// Pop other call parameters.
|
|
|
|
addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
|
|
|
|
toAddr := common.BigToAddress(addr)
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
value = math.U256(value)
|
2017-11-28 22:05:49 +03:00
|
|
|
// Get arguments from the memory.
|
2015-07-18 00:09:36 +03:00
|
|
|
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
|
|
|
|
2017-02-28 17:09:11 +03:00
|
|
|
if value.Sign() != 0 {
|
2017-01-04 22:17:24 +03:00
|
|
|
gas += params.CallStipend
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
2018-07-25 15:56:39 +03:00
|
|
|
ret, returnGas, err := interpreter.evm.CallCode(contract, toAddr, args, gas, value)
|
2015-07-18 00:09:36 +03:00
|
|
|
if err != nil {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.getZero())
|
2015-07-18 00:09:36 +03:00
|
|
|
} else {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.get().SetUint64(1))
|
2017-08-16 17:09:29 +03:00
|
|
|
}
|
|
|
|
if err == nil || err == errExecutionReverted {
|
2015-07-18 00:09:36 +03:00
|
|
|
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
|
|
|
}
|
2017-01-04 22:17:24 +03:00
|
|
|
contract.Gas += returnGas
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
|
2017-02-02 00:36:51 +03:00
|
|
|
return ret, nil
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
|
|
|
|
interpreter.intPool.put(stack.pop())
|
|
|
|
gas := interpreter.evm.callGasTemp
|
2017-11-28 22:05:49 +03:00
|
|
|
// Pop other call parameters.
|
|
|
|
addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
|
|
|
|
toAddr := common.BigToAddress(addr)
|
|
|
|
// Get arguments from the memory.
|
2015-11-27 16:40:29 +02:00
|
|
|
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
ret, returnGas, err := interpreter.evm.DelegateCall(contract, toAddr, args, gas)
|
2015-11-27 16:40:29 +02:00
|
|
|
if err != nil {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.getZero())
|
2015-11-27 16:40:29 +02:00
|
|
|
} else {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.get().SetUint64(1))
|
2017-08-16 17:09:29 +03:00
|
|
|
}
|
|
|
|
if err == nil || err == errExecutionReverted {
|
2017-11-28 22:05:49 +03:00
|
|
|
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
2015-11-27 16:40:29 +02:00
|
|
|
}
|
2017-01-04 22:17:24 +03:00
|
|
|
contract.Gas += returnGas
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
|
2017-02-02 00:36:51 +03:00
|
|
|
return ret, nil
|
2015-11-27 16:40:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
|
|
|
|
interpreter.intPool.put(stack.pop())
|
|
|
|
gas := interpreter.evm.callGasTemp
|
2017-11-28 22:05:49 +03:00
|
|
|
// Pop other call parameters.
|
|
|
|
addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
|
|
|
|
toAddr := common.BigToAddress(addr)
|
|
|
|
// Get arguments from the memory.
|
2017-08-15 11:23:23 +03:00
|
|
|
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
ret, returnGas, err := interpreter.evm.StaticCall(contract, toAddr, args, gas)
|
2017-08-15 11:23:23 +03:00
|
|
|
if err != nil {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.getZero())
|
2017-08-15 11:23:23 +03:00
|
|
|
} else {
|
2018-07-25 15:56:39 +03:00
|
|
|
stack.push(interpreter.intPool.get().SetUint64(1))
|
2017-08-16 17:09:29 +03:00
|
|
|
}
|
|
|
|
if err == nil || err == errExecutionReverted {
|
2017-08-15 11:23:23 +03:00
|
|
|
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
|
|
|
}
|
|
|
|
contract.Gas += returnGas
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
|
2017-08-15 11:23:23 +03:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opReturn(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2017-01-05 13:52:10 +03:00
|
|
|
offset, size := stack.pop(), stack.pop()
|
|
|
|
ret := memory.GetPtr(offset.Int64(), size.Int64())
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(offset, size)
|
2017-08-16 15:32:59 +03:00
|
|
|
return ret, nil
|
|
|
|
}
|
2017-02-02 00:36:51 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opRevert(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2017-08-16 15:32:59 +03:00
|
|
|
offset, size := stack.pop(), stack.pop()
|
|
|
|
ret := memory.GetPtr(offset.Int64(), size.Int64())
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(offset, size)
|
2017-01-05 13:52:10 +03:00
|
|
|
return ret, nil
|
2015-08-30 11:19:10 +03:00
|
|
|
}
|
2017-01-05 13:52:10 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opStop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-08-30 11:19:10 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
func opSuicide(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
balance := interpreter.evm.StateDB.GetBalance(contract.Address())
|
|
|
|
interpreter.evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
|
2015-08-30 11:19:10 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.evm.StateDB.Suicide(contract.Address())
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-08-30 11:19:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// following functions are used by the instruction jump table
|
|
|
|
|
|
|
|
// make log instruction function
|
2017-01-05 13:52:10 +03:00
|
|
|
func makeLog(size int) executionFunc {
|
2018-07-25 15:56:39 +03:00
|
|
|
return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-08-30 11:19:10 +03:00
|
|
|
topics := make([]common.Hash, size)
|
|
|
|
mStart, mSize := stack.pop(), stack.pop()
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
topics[i] = common.BigToHash(stack.pop())
|
|
|
|
}
|
|
|
|
|
|
|
|
d := memory.Get(mStart.Int64(), mSize.Int64())
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.evm.StateDB.AddLog(&types.Log{
|
2017-01-05 16:03:50 +03:00
|
|
|
Address: contract.Address(),
|
|
|
|
Topics: topics,
|
|
|
|
Data: d,
|
|
|
|
// This is a non-consensus field, but assigned here because
|
|
|
|
// core/state doesn't know the current block number.
|
2018-07-25 15:56:39 +03:00
|
|
|
BlockNumber: interpreter.evm.BlockNumber.Uint64(),
|
2017-01-05 16:03:50 +03:00
|
|
|
})
|
2017-01-04 22:17:24 +03:00
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
interpreter.intPool.put(mStart, mSize)
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-08-30 11:19:10 +03:00
|
|
|
}
|
|
|
|
}
|
2015-07-18 00:09:36 +03:00
|
|
|
|
2015-08-30 11:19:10 +03:00
|
|
|
// make push instruction function
|
2017-05-23 11:39:53 +03:00
|
|
|
func makePush(size uint64, pushByteSize int) executionFunc {
|
2018-07-25 15:56:39 +03:00
|
|
|
return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2017-05-23 11:39:53 +03:00
|
|
|
codeLen := len(contract.Code)
|
|
|
|
|
|
|
|
startMin := codeLen
|
|
|
|
if int(*pc+1) < startMin {
|
|
|
|
startMin = int(*pc + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
endMin := codeLen
|
|
|
|
if startMin+pushByteSize < endMin {
|
|
|
|
endMin = startMin + pushByteSize
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:56:39 +03:00
|
|
|
integer := interpreter.intPool.get()
|
2017-05-23 11:39:53 +03:00
|
|
|
stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize)))
|
|
|
|
|
2015-08-30 11:19:10 +03:00
|
|
|
*pc += size
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-08-30 11:19:10 +03:00
|
|
|
}
|
|
|
|
}
|
2015-07-18 00:09:36 +03:00
|
|
|
|
2018-05-23 15:02:10 +03:00
|
|
|
// make dup instruction function
|
2017-01-05 13:52:10 +03:00
|
|
|
func makeDup(size int64) executionFunc {
|
2018-07-25 15:56:39 +03:00
|
|
|
return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
|
|
|
stack.dup(interpreter.intPool, int(size))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-08-30 11:19:10 +03:00
|
|
|
}
|
|
|
|
}
|
2015-07-18 00:09:36 +03:00
|
|
|
|
2015-08-30 11:19:10 +03:00
|
|
|
// make swap instruction function
|
2017-01-05 13:52:10 +03:00
|
|
|
func makeSwap(size int64) executionFunc {
|
2015-08-30 11:19:10 +03:00
|
|
|
// switch n + 1 otherwise n would be swapped with n
|
2018-06-26 15:56:25 +03:00
|
|
|
size++
|
2018-07-25 15:56:39 +03:00
|
|
|
return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
2015-08-30 11:19:10 +03:00
|
|
|
stack.swap(int(size))
|
2017-01-05 13:52:10 +03:00
|
|
|
return nil, nil
|
2015-08-30 11:19:10 +03:00
|
|
|
}
|
2015-07-18 00:09:36 +03:00
|
|
|
}
|