2015-10-09 07:56:12 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
2016-02-09 16:20:42 -06:00
|
|
|
"math/big"
|
2015-10-09 07:56:12 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-02-09 16:20:42 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2015-10-09 07:56:12 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2016-02-09 16:20:42 -06:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2018-10-04 10:15:37 -05:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-10-09 07:56:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDefaults(t *testing.T) {
|
|
|
|
cfg := new(Config)
|
|
|
|
setDefaults(cfg)
|
|
|
|
|
|
|
|
if cfg.Difficulty == nil {
|
|
|
|
t.Error("expected difficulty to be non nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Time == nil {
|
|
|
|
t.Error("expected time to be non nil")
|
|
|
|
}
|
2017-01-04 13:17:24 -06:00
|
|
|
if cfg.GasLimit == 0 {
|
|
|
|
t.Error("didn't expect gaslimit to be zero")
|
2015-10-09 07:56:12 -05:00
|
|
|
}
|
|
|
|
if cfg.GasPrice == nil {
|
|
|
|
t.Error("expected time to be non nil")
|
|
|
|
}
|
|
|
|
if cfg.Value == nil {
|
|
|
|
t.Error("expected time to be non nil")
|
|
|
|
}
|
|
|
|
if cfg.GetHashFn == nil {
|
|
|
|
t.Error("expected time to be non nil")
|
|
|
|
}
|
|
|
|
if cfg.BlockNumber == nil {
|
|
|
|
t.Error("expected block number to be non nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 04:52:10 -06:00
|
|
|
func TestEVM(t *testing.T) {
|
2015-10-09 07:56:12 -05:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Fatalf("crashed with: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
Execute([]byte{
|
|
|
|
byte(vm.DIFFICULTY),
|
|
|
|
byte(vm.TIMESTAMP),
|
|
|
|
byte(vm.GASLIMIT),
|
|
|
|
byte(vm.PUSH1),
|
|
|
|
byte(vm.ORIGIN),
|
|
|
|
byte(vm.BLOCKHASH),
|
|
|
|
byte(vm.COINBASE),
|
|
|
|
}, nil, nil)
|
|
|
|
}
|
|
|
|
|
2016-02-09 16:20:42 -06:00
|
|
|
func TestExecute(t *testing.T) {
|
|
|
|
ret, _, err := Execute([]byte{
|
|
|
|
byte(vm.PUSH1), 10,
|
|
|
|
byte(vm.PUSH1), 0,
|
|
|
|
byte(vm.MSTORE),
|
|
|
|
byte(vm.PUSH1), 32,
|
|
|
|
byte(vm.PUSH1), 0,
|
|
|
|
byte(vm.RETURN),
|
|
|
|
}, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("didn't expect error", err)
|
|
|
|
}
|
|
|
|
|
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-26 15:21:51 -06:00
|
|
|
num := new(big.Int).SetBytes(ret)
|
2016-02-09 16:20:42 -06:00
|
|
|
if num.Cmp(big.NewInt(10)) != 0 {
|
|
|
|
t.Error("Expected 10, got", num)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCall(t *testing.T) {
|
2018-05-09 07:24:25 -05:00
|
|
|
state, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
|
2016-02-09 16:20:42 -06:00
|
|
|
address := common.HexToAddress("0x0a")
|
|
|
|
state.SetCode(address, []byte{
|
|
|
|
byte(vm.PUSH1), 10,
|
|
|
|
byte(vm.PUSH1), 0,
|
|
|
|
byte(vm.MSTORE),
|
|
|
|
byte(vm.PUSH1), 32,
|
|
|
|
byte(vm.PUSH1), 0,
|
|
|
|
byte(vm.RETURN),
|
|
|
|
})
|
|
|
|
|
2017-06-07 10:09:08 -05:00
|
|
|
ret, _, err := Call(address, nil, &Config{State: state})
|
2016-02-09 16:20:42 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("didn't expect error", err)
|
|
|
|
}
|
|
|
|
|
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-26 15:21:51 -06:00
|
|
|
num := new(big.Int).SetBytes(ret)
|
2016-02-09 16:20:42 -06:00
|
|
|
if num.Cmp(big.NewInt(10)) != 0 {
|
|
|
|
t.Error("Expected 10, got", num)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:56:12 -05:00
|
|
|
func BenchmarkCall(b *testing.B) {
|
|
|
|
var definition = `[{"constant":true,"inputs":[],"name":"seller","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"abort","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[],"name":"refund","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"buyer","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"confirmReceived","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"state","outputs":[{"name":"","type":"uint8"}],"type":"function"},{"constant":false,"inputs":[],"name":"confirmPurchase","outputs":[],"type":"function"},{"inputs":[],"type":"constructor"},{"anonymous":false,"inputs":[],"name":"Aborted","type":"event"},{"anonymous":false,"inputs":[],"name":"PurchaseConfirmed","type":"event"},{"anonymous":false,"inputs":[],"name":"ItemReceived","type":"event"},{"anonymous":false,"inputs":[],"name":"Refunded","type":"event"}]`
|
|
|
|
|
|
|
|
var code = common.Hex2Bytes("6060604052361561006c5760e060020a600035046308551a53811461007457806335a063b4146100865780633fa4f245146100a6578063590e1ae3146100af5780637150d8ae146100cf57806373fac6f0146100e1578063c19d93fb146100fe578063d696069714610112575b610131610002565b610133600154600160a060020a031681565b610131600154600160a060020a0390811633919091161461015057610002565b61014660005481565b610131600154600160a060020a039081163391909116146102d557610002565b610133600254600160a060020a031681565b610131600254600160a060020a0333811691161461023757610002565b61014660025460ff60a060020a9091041681565b61013160025460009060ff60a060020a9091041681146101cc57610002565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60025460009060a060020a900460ff16811461016b57610002565b600154600160a060020a03908116908290301631606082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f72c874aeff0b183a56e2b79c71b46e1aed4dee5e09862134b8821ba2fddbf8bf9250a150565b80546002023414806101dd57610002565b6002805460a060020a60ff021973ffffffffffffffffffffffffffffffffffffffff1990911633171660a060020a1790557fd5d55c8a68912e9a110618df8d5e2e83b8d83211c57a8ddd1203df92885dc881826060a15050565b60025460019060a060020a900460ff16811461025257610002565b60025460008054600160a060020a0390921691606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517fe89152acd703c9d8c7d28829d443260b411454d45394e7995815140c8cbcbcf79250a150565b60025460019060a060020a900460ff1681146102f057610002565b6002805460008054600160a060020a0390921692909102606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f8616bbbbad963e4e65b1366f1d75dfb63f9e9704bbbf91fb01bec70849906cf79250a15056")
|
|
|
|
|
|
|
|
abi, err := abi.JSON(strings.NewReader(definition))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cpurchase, err := abi.Pack("confirmPurchase")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
creceived, err := abi.Pack("confirmReceived")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
refund, err := abi.Pack("refund")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for j := 0; j < 400; j++ {
|
|
|
|
Execute(code, cpurchase, nil)
|
|
|
|
Execute(code, creceived, nil)
|
|
|
|
Execute(code, refund, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-04 10:15:37 -05:00
|
|
|
func benchmarkEVM_Create(bench *testing.B, code string) {
|
|
|
|
var (
|
|
|
|
statedb, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
|
|
|
|
sender = common.BytesToAddress([]byte("sender"))
|
|
|
|
receiver = common.BytesToAddress([]byte("receiver"))
|
|
|
|
)
|
|
|
|
|
|
|
|
statedb.CreateAccount(sender)
|
|
|
|
statedb.SetCode(receiver, common.FromHex(code))
|
|
|
|
runtimeConfig := Config{
|
|
|
|
Origin: sender,
|
|
|
|
State: statedb,
|
|
|
|
GasLimit: 10000000,
|
|
|
|
Difficulty: big.NewInt(0x200000),
|
|
|
|
Time: new(big.Int).SetUint64(0),
|
|
|
|
Coinbase: common.Address{},
|
|
|
|
BlockNumber: new(big.Int).SetUint64(1),
|
|
|
|
ChainConfig: ¶ms.ChainConfig{
|
|
|
|
ChainID: big.NewInt(1),
|
|
|
|
HomesteadBlock: new(big.Int),
|
|
|
|
ByzantiumBlock: new(big.Int),
|
|
|
|
ConstantinopleBlock: new(big.Int),
|
|
|
|
DAOForkBlock: new(big.Int),
|
|
|
|
DAOForkSupport: false,
|
|
|
|
EIP150Block: new(big.Int),
|
|
|
|
EIP155Block: new(big.Int),
|
|
|
|
EIP158Block: new(big.Int),
|
|
|
|
},
|
|
|
|
EVMConfig: vm.Config{},
|
|
|
|
}
|
|
|
|
// Warm up the intpools and stuff
|
|
|
|
bench.ResetTimer()
|
|
|
|
for i := 0; i < bench.N; i++ {
|
|
|
|
Call(receiver, []byte{}, &runtimeConfig)
|
|
|
|
}
|
|
|
|
bench.StopTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEVM_CREATE_500(bench *testing.B) {
|
|
|
|
// initcode size 500K, repeatedly calls CREATE and then modifies the mem contents
|
|
|
|
benchmarkEVM_Create(bench, "5b6207a120600080f0600152600056")
|
|
|
|
}
|
|
|
|
func BenchmarkEVM_CREATE2_500(bench *testing.B) {
|
|
|
|
// initcode size 500K, repeatedly calls CREATE2 and then modifies the mem contents
|
|
|
|
benchmarkEVM_Create(bench, "5b586207a120600080f5600152600056")
|
|
|
|
}
|
|
|
|
func BenchmarkEVM_CREATE_1200(bench *testing.B) {
|
|
|
|
// initcode size 1200K, repeatedly calls CREATE and then modifies the mem contents
|
|
|
|
benchmarkEVM_Create(bench, "5b62124f80600080f0600152600056")
|
|
|
|
}
|
|
|
|
func BenchmarkEVM_CREATE2_1200(bench *testing.B) {
|
|
|
|
// initcode size 1200K, repeatedly calls CREATE2 and then modifies the mem contents
|
|
|
|
benchmarkEVM_Create(bench, "5b5862124f80600080f5600152600056")
|
|
|
|
}
|