aboutsummaryrefslogtreecommitdiffstats
path: root/tests/state_test_util.go
blob: 84581fae18d9e30807861b8fdd6b187e76f2c759 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
10Jeffrey Wilcke <jeffrey@ethereum.org>
Jeffrey Wilcke <jeffrey@ethereum.org> <geffobscura@gmail.com>
Jeffrey Wilcke <jeffrey@ethereum.org> <obscuren@obscura.com>
Jeffrey Wilcke <jeffrey@ethereum.org> <obscuren@users.noreply.github.com>

Viktor Trón <viktor.tron@gmail.com>

Joseph Goulden <joegoulden@gmail.com>

Nick Savers <nicksavers@gmail.com>

Maran Hidskes <maran.hidskes@gmail.com>

Taylor Gerring <taylor.gerring@gmail.com>
Taylor Gerring <taylor.gerring@gmail.com> <taylor.gerring@ethereum.org>

Bas van Kervel <bas@ethdev.com>
Bas van Kervel <bas@ethdev.com> <basvankervel@ziggo.nl>
Bas van Kervel <bas@ethdev.com> <basvankervel@gmail.com>
Bas van Kervel <bas@ethdev.com> <bas-vk@users.noreply.github.com>

Sven Ehlert <sven@ethdev.com>

Vitalik Buterin <v@buterin.com>

Marian Oancea <contact@siteshop.ro>

Christoph Jentzsch <jentzsch.software@gmail.com>

Heiko Hees <heiko@heiko.org>

Alex Leverington <alex@ethdev.com>
Alex Leverington <alex@ethdev.com> <subtly@users.noreply.github.com>

Zsolt Felföldi <zsfelfoldi@gmail.com>

Gavin Wood <i@gavwood.com>

Martin Becze <mjbecze@gmail.com>
Martin Becze <mjbecze@gmail.com> <wanderer@users.noreply.github.com>

Dimitry Khokhlov <winsvega@mail.ru>

Roman Mandeleil <roman.mandeleil@gmail.com>

Alec Perseghin <aperseghin@gmail.com>

Alon Muroch <alonmuroch@gmail.com>

Arkadiy Paronyan <arkadiy@ethdev.com>

Jae Kwon <jkwon.work@gmail.com>

Aaron Kumavis <kumavis@users.noreply.github.com>

Nick Dodson <silentcicero@outlook.com>

Jason Carver <jacarver@linkedin.com>
Jason Carver <jacarver@linkedin.com> <ut96caarrs@snkmail.com>

Joseph Chow <ethereum@outlook.com>
Joseph Chow <ethereum@outlook.com> ethers <TODO>

Enrique Fynn <enriquefynn@gmail.com>

Vincent G <caktux@gmail.com>

RJ Catalano <catalanor0220@gmail.com>
RJ Catalano <catalanor0220@gmail.com> <rj@erisindustries.com>

Nchinda Nchinda <nchinda2@gmail.com>

Aron Fischer <github@aron.guru> <homotopycolimit@users.noreply.github.com>

Vlad Gluhovsky <gluk256@users.noreply.github.com>

Ville Sundell <github@solarius.fi>

Elliot Shepherd <elliot@identitii.com>

Yohann Léon <sybiload@gmail.com>

Gregg Dourgarian <greggd@tempworks.com>

Casey Detrio <cdetrio@gmail.com>

Jens Agerberg <github@agerberg.me>

Nick Johnson <arachnid@notdot.net>

Henning Diedrich <hd@eonblast.com>
Henning Diedrich <hd@eonblast.com> Drake Burroughs <wildfyre@hotmail.com>

Felix Lange <fjl@twurst.com>
Felix Lange <fjl@twurst.com> <fjl@users.noreply.github.com>

Максим Чусовлянов <mchusovlianov@gmail.com>

Louis Holbrook <dev@holbrook.no>
Louis Holbrook <dev@holbrook.no> <nolash@users.noreply.github.com>

Thomas Bocek <tom@tomp2p.net>

Victor Tran <vu.tran54@gmail.com>

Justin Drake <drakefjustin@gmail.com>

Frank Wang <eternnoir@gmail.com>

Gary Rong <garyrong0905@gmail.com>

Guillaume Nicolas <guin56@gmail.com>

Sorin Neacsu <sorin.neacsu@gmail.com>
Sorin Neacsu <sorin.neacsu@gmail.com> <sorin@users.noreply.github.com>

Valentin Wüstholz <wuestholz@gmail.com>
Valentin Wüstholz <wuestholz@gmail.com> <wuestholz@users.noreply.github.com>

Armin Braun <me@obrown.io>

Ernesto del Toro <ernesto.deltoro@gmail.com>
Ernesto del Toro <ernesto.deltoro@gmail.com> <ernestodeltoro@users.noreply.github.com>
href='#n238'>238 239 240 241 242 243 244
// 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 tests

import (
    "encoding/hex"
    "encoding/json"
    "fmt"
    "math/big"
    "strings"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/common/hexutil"
    "github.com/ethereum/go-ethereum/common/math"
    "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"
    "github.com/ethereum/go-ethereum/crypto/sha3"
    "github.com/ethereum/go-ethereum/ethdb"
    "github.com/ethereum/go-ethereum/params"
    "github.com/ethereum/go-ethereum/rlp"
)

// 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
}

// StateSubtest selects a specific configuration of a General State Test.
type StateSubtest struct {
    Fork  string
    Index int
}

func (t *StateTest) UnmarshalJSON(in []byte) error {
    return json.Unmarshal(in, &t.json)
}

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    common.UnprefixedHash `json:"logs"`
    Indexes struct {
        Data  int `json:"data"`
        Gas   int `json:"gas"`
        Value int `json:"value"`
    }
}

//go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go

type stEnv struct {
    Coinbase   common.Address `json:"currentCoinbase"   gencodec:"required"`
    Difficulty *big.Int       `json:"currentDifficulty" gencodec:"required"`
    GasLimit   uint64         `json:"currentGasLimit"   gencodec:"required"`
    Number     uint64         `json:"currentNumber"     gencodec:"required"`
    Timestamp  uint64         `json:"currentTimestamp"  gencodec:"required"`
}

type stEnvMarshaling struct {
    Coinbase   common.UnprefixedAddress
    Difficulty *math.HexOrDecimal256
    GasLimit   math.HexOrDecimal64
    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
}

// 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) (*state.StateDB, error) {
    config, ok := Forks[subtest.Fork]
    if !ok {
        return nil, UnsupportedForkError{subtest.Fork}
    }
    block := t.genesis(config).ToBlock(nil)
    statedb := MakePreState(ethdb.NewMemDatabase(), t.json.Pre)

    post := t.json.Post[subtest.Fork][subtest.Index]
    msg, err := t.json.Tx.toMessage(post)
    if err != nil {
        return nil, 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 logs := rlpHash(statedb.Logs()); logs != common.Hash(post.Logs) {
        return statedb, fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs)
    }
    root, _ := statedb.Commit(config.IsEIP158(block.Number()))
    if root != common.Hash(post.Root) {
        return statedb, fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root)
    }
    return statedb, 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.Commit(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,
        Number:     t.json.Env.Number,
        Timestamp:  t.json.Env.Timestamp,
        Alloc:      t.json.Pre,
    }
}

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)
        }
    }

    // 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)
    }

    msg := types.NewMessage(from, to, tx.Nonce, value, gasLimit, tx.GasPrice, data, true)
    return msg, nil
}

func rlpHash(x interface{}) (h common.Hash) {
    hw := sha3.NewKeccak256()
    rlp.Encode(hw, x)
    hw.Sum(h[:0])
    return h
}