op-geth/tests/state_test_util.go

282 lines
8.7 KiB
Go
Raw Normal View History

// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
2015-07-07 00:54:22 +00:00
//
// The go-ethereum library is free software: you can redistribute it and/or modify
2015-07-07 00:54:22 +00:00
// 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,
2015-07-07 00:54:22 +00:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-07-07 00:54:22 +00:00
// 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-07-07 00:54:22 +00:00
2015-06-10 18:38:39 +00:00
package tests
import (
"bytes"
"encoding/hex"
"encoding/json"
2015-06-10 18:38:39 +00:00
"fmt"
"math/big"
"reflect"
"strings"
2015-06-10 18:38:39 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
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 21:21:51 +00:00
"github.com/ethereum/go-ethereum/common/math"
2015-06-10 18:38:39 +00:00
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
2015-06-10 18:38:39 +00:00
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params"
2015-06-10 18:38:39 +00:00
)
// StateTest checks transaction processing without block context.
// See https://github.com/ethereum/EIPs/issues/176 for the test format specification.
type StateTest struct {
json stJSON
}
2015-06-14 21:55:03 +00:00
// StateSubtest selects a specific configuration of a General State Test.
type StateSubtest struct {
Fork string
Index int
2015-06-14 21:55:03 +00:00
}
func (t *StateTest) UnmarshalJSON(in []byte) error {
return json.Unmarshal(in, &t.json)
}
2015-06-14 21:55:03 +00:00
type stJSON struct {
Env stEnv `json:"env"`
Pre core.GenesisAlloc `json:"pre"`
Tx stTransaction `json:"transaction"`
Out hexutil.Bytes `json:"out"`
Post map[string][]stPostState `json:"post"`
}
type stPostState struct {
Root common.UnprefixedHash `json:"hash"`
Logs *[]stLog `json:"logs"`
Indexes struct {
Data int `json:"data"`
Gas int `json:"gas"`
Value int `json:"value"`
2015-06-14 21:55:03 +00:00
}
}
2015-06-14 21:55:03 +00:00
//go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
2015-06-14 21:55:03 +00:00
type stEnv struct {
Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
Difficulty *big.Int `json:"currentDifficulty" gencodec:"required"`
GasLimit *big.Int `json:"currentGasLimit" gencodec:"required"`
Number uint64 `json:"currentNumber" gencodec:"required"`
Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
2015-06-14 21:55:03 +00:00
}
type stEnvMarshaling struct {
Coinbase common.UnprefixedAddress
Difficulty *math.HexOrDecimal256
GasLimit *math.HexOrDecimal256
Number math.HexOrDecimal64
Timestamp math.HexOrDecimal64
}
//go:generate gencodec -type stTransaction -field-override stTransactionMarshaling -out gen_sttransaction.go
type stTransaction struct {
GasPrice *big.Int `json:"gasPrice"`
Nonce uint64 `json:"nonce"`
To string `json:"to"`
Data []string `json:"data"`
GasLimit []uint64 `json:"gasLimit"`
Value []string `json:"value"`
PrivateKey []byte `json:"secretKey"`
}
type stTransactionMarshaling struct {
GasPrice *math.HexOrDecimal256
Nonce math.HexOrDecimal64
GasLimit []math.HexOrDecimal64
PrivateKey hexutil.Bytes
}
//go:generate gencodec -type stLog -field-override stLogMarshaling -out gen_stlog.go
type stLog struct {
Address common.Address `json:"address"`
Data []byte `json:"data"`
Topics []common.Hash `json:"topics"`
Bloom string `json:"bloom"`
}
type stLogMarshaling struct {
Address common.UnprefixedAddress
Data hexutil.Bytes
Topics []common.UnprefixedHash
}
// Subtests returns all valid subtests of the test.
func (t *StateTest) Subtests() []StateSubtest {
var sub []StateSubtest
for fork, pss := range t.json.Post {
for i, _ := range pss {
sub = append(sub, StateSubtest{fork, i})
}
}
return sub
}
// Run executes a specific subtest.
func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config) error {
config, ok := Forks[subtest.Fork]
if !ok {
return UnsupportedForkError{subtest.Fork}
}
block, _ := t.genesis(config).ToBlock()
db, _ := ethdb.NewMemDatabase()
statedb := makePreState(db, t.json.Pre)
post := t.json.Post[subtest.Fork][subtest.Index]
msg, err := t.json.Tx.toMessage(post)
if err != nil {
return err
}
context := core.NewEVMContext(msg, block.Header(), nil, &t.json.Env.Coinbase)
context.GetHash = vmTestBlockHash
evm := vm.NewEVM(context, statedb, config, vmconfig)
gaspool := new(core.GasPool)
gaspool.AddGas(block.GasLimit())
snapshot := statedb.Snapshot()
if _, _, _, err := core.ApplyMessage(evm, msg, gaspool); err != nil {
statedb.RevertToSnapshot(snapshot)
}
if post.Logs != nil {
if err := checkLogs(statedb.Logs(), *post.Logs); err != nil {
return err
}
}
root, _ := statedb.CommitTo(db, config.IsEIP158(block.Number()))
if root != common.Hash(post.Root) {
return fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root)
}
return nil
}
func (t *StateTest) gasLimit(subtest StateSubtest) uint64 {
return t.json.Tx.GasLimit[t.json.Post[subtest.Fork][subtest.Index].Indexes.Gas]
}
func makePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB {
sdb := state.NewDatabase(db)
statedb, _ := state.New(common.Hash{}, sdb)
for addr, a := range accounts {
statedb.SetCode(addr, a.Code)
statedb.SetNonce(addr, a.Nonce)
statedb.SetBalance(addr, a.Balance)
for k, v := range a.Storage {
statedb.SetState(addr, k, v)
}
}
// Commit and re-open to start with a clean state.
root, _ := statedb.CommitTo(db, false)
statedb, _ = state.New(root, sdb)
return statedb
}
func (t *StateTest) genesis(config *params.ChainConfig) *core.Genesis {
return &core.Genesis{
Config: config,
Coinbase: t.json.Env.Coinbase,
Difficulty: t.json.Env.Difficulty,
GasLimit: t.json.Env.GasLimit.Uint64(),
Number: t.json.Env.Number,
Timestamp: t.json.Env.Timestamp,
Alloc: t.json.Pre,
2015-06-14 21:55:03 +00:00
}
}
2015-06-10 18:38:39 +00:00
func (tx *stTransaction) toMessage(ps stPostState) (core.Message, error) {
// Derive sender from private key if present.
var from common.Address
if len(tx.PrivateKey) > 0 {
key, err := crypto.ToECDSA(tx.PrivateKey)
if err != nil {
return nil, fmt.Errorf("invalid private key: %v", err)
}
from = crypto.PubkeyToAddress(key.PublicKey)
}
// Parse recipient if present.
var to *common.Address
if tx.To != "" {
to = new(common.Address)
if err := to.UnmarshalText([]byte(tx.To)); err != nil {
return nil, fmt.Errorf("invalid to address: %v", err)
2015-06-10 18:38:39 +00:00
}
}
2015-06-10 18:38:39 +00:00
// Get values specific to this post state.
if ps.Indexes.Data > len(tx.Data) {
return nil, fmt.Errorf("tx data index %d out of bounds", ps.Indexes.Data)
}
if ps.Indexes.Value > len(tx.Value) {
return nil, fmt.Errorf("tx value index %d out of bounds", ps.Indexes.Value)
}
if ps.Indexes.Gas > len(tx.GasLimit) {
return nil, fmt.Errorf("tx gas limit index %d out of bounds", ps.Indexes.Gas)
}
dataHex := tx.Data[ps.Indexes.Data]
valueHex := tx.Value[ps.Indexes.Value]
gasLimit := tx.GasLimit[ps.Indexes.Gas]
// Value, Data hex encoding is messy: https://github.com/ethereum/tests/issues/203
value := new(big.Int)
if valueHex != "0x" {
v, ok := math.ParseBig256(valueHex)
if !ok {
return nil, fmt.Errorf("invalid tx value %q", valueHex)
}
value = v
}
data, err := hex.DecodeString(strings.TrimPrefix(dataHex, "0x"))
if err != nil {
return nil, fmt.Errorf("invalid tx data %q", dataHex)
2015-06-14 21:55:03 +00:00
}
2015-06-10 18:38:39 +00:00
msg := types.NewMessage(from, to, tx.Nonce, value, new(big.Int).SetUint64(gasLimit), tx.GasPrice, data, true)
return msg, nil
2015-06-14 21:55:03 +00:00
}
2015-06-10 18:38:39 +00:00
func checkLogs(have []*types.Log, want []stLog) error {
if len(have) != len(want) {
return fmt.Errorf("logs length mismatch: got %d, want %d", len(have), len(want))
}
for i := range have {
if have[i].Address != want[i].Address {
return fmt.Errorf("log address %d: got %x, want %x", i, have[i].Address, want[i].Address)
}
if !bytes.Equal(have[i].Data, want[i].Data) {
return fmt.Errorf("log data %d: got %x, want %x", i, have[i].Data, want[i].Data)
2015-06-10 18:38:39 +00:00
}
if !reflect.DeepEqual(have[i].Topics, want[i].Topics) {
return fmt.Errorf("log topics %d:\ngot %x\nwant %x", i, have[i].Topics, want[i].Topics)
2015-06-10 18:38:39 +00:00
}
genBloom := math.PaddedBigBytes(types.LogsBloom([]*types.Log{have[i]}), 256)
var wantBloom types.Bloom
if err := hexutil.UnmarshalFixedUnprefixedText("Bloom", []byte(want[i].Bloom), wantBloom[:]); err != nil {
return fmt.Errorf("test log %d has invalid bloom: %v", i, err)
}
if !bytes.Equal(genBloom, wantBloom[:]) {
return fmt.Errorf("bloom mismatch")
2015-06-10 19:02:16 +00:00
}
2015-06-14 21:55:03 +00:00
}
return nil
2015-06-10 18:38:39 +00:00
}