aboutsummaryrefslogtreecommitdiffstats
path: root/tests/state_test_util.go
blob: de4af6d8214dac0cb4c12a4de80786880579ed06 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package tests

import (
    "bytes"
    // "errors"
    "fmt"
    "math/big"
    "strconv"
    // "testing"

    "github.com/ethereum/go-ethereum/common"
    "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/ethdb"
    // "github.com/ethereum/go-ethereum/logger"
)

func RunStateTest(p string) error {

    tests := make(map[string]VmTest)
    CreateFileTests(p, &tests)

    for name, test := range tests {
        /*
           vm.Debug = true
           glog.SetV(4)
           glog.SetToStderr(true)
           if name != "Call50000_sha256" {
             continue
           }
        */
        db, _ := ethdb.NewMemDatabase()
        statedb := state.New(common.Hash{}, db)
        for addr, account := range test.Pre {
            obj := StateObjectFromAccount(db, addr, account)
            statedb.SetStateObject(obj)
            for a, v := range account.Storage {
                obj.SetState(common.HexToHash(a), common.HexToHash(s))
            }
        }

        // XXX Yeah, yeah...
        env := make(map[string]string)
        env["currentCoinbase"] = test.Env.CurrentCoinbase
        env["currentDifficulty"] = test.Env.CurrentDifficulty
        env["currentGasLimit"] = test.Env.CurrentGasLimit
        env["currentNumber"] = test.Env.CurrentNumber
        env["previousHash"] = test.Env.PreviousHash
        if n, ok := test.Env.CurrentTimestamp.(float64); ok {
            env["currentTimestamp"] = strconv.Itoa(int(n))
        } else {
            env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
        }

        var (
            ret []byte
            // gas  *big.Int
            // err  error
            logs state.Logs
        )

        ret, logs, _, _ = RunState(statedb, env, test.Transaction)

        // Compare expected  and actual return
        switch name {
        // the memory required for these tests (4294967297 bytes) would take too much time.
        // on 19 May 2015 decided to skip these tests their output.
        case "mload32bitBound_return", "mload32bitBound_return2":
        default:
            rexp := common.FromHex(test.Out)
            if bytes.Compare(rexp, ret) != 0 {
                return fmt.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
            }
        }

        // check post state
        for addr, account := range test.Post {
            obj := statedb.GetStateObject(common.HexToAddress(addr))
            if obj == nil {
                continue
            }

            if obj.Balance().Cmp(common.Big(account.Balance)) != 0 {
                return fmt.Errorf("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address().Bytes()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance()))
            }

            if obj.Nonce() != common.String2Big(account.Nonce).Uint64() {
                return fmt.Errorf("%s's : (%x) nonce failed. Expected %v, got %v\n", name, obj.Address().Bytes()[:4], account.Nonce, obj.Nonce())
            }

            for addr, value := range account.Storage {
                v := obj.GetState(common.HexToHash(addr)).Bytes()
                vexp := common.FromHex(value)

                if bytes.Compare(v, vexp) != 0 {
                    return fmt.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address().Bytes()[0:4], addr, vexp, v, common.BigD(vexp), common.BigD(v))
                }
            }
        }

        statedb.Sync()
        //if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
        if common.HexToHash(test.PostStateRoot) != statedb.Root() {
            return fmt.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
        }

        // check logs
        if len(test.Logs) > 0 {
            lerr := CheckLogs(test.Logs, logs, t)
            if lerr != nil {
                return fmt.Errorf("'%s' ", name, lerr.Error())
            }
        }

        fmt.Println("State test passed: ", name)
        //fmt.Println(string(statedb.Dump()))
    }
    // logger.Flush()
}

func CheckLogs(tlog []Log, logs state.Logs, t *testing.T) error {

    if len(tlog) != len(logs) {
        return fmt.Errorf("log length mismatch. Expected %d, got %d", len(tlog), len(logs))
    } else {
        for i, log := range tlog {
            if common.HexToAddress(log.AddressF) != logs[i].Address {
                return fmt.Errorf("log address expected %v got %x", log.AddressF, logs[i].Address)
            }

            if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) {
                return fmt.Errorf("log data expected %v got %x", log.DataF, logs[i].Data)
            }

            if len(log.TopicsF) != len(logs[i].Topics) {
                return fmt.Errorf("log topics length expected %d got %d", len(log.TopicsF), logs[i].Topics)
            } else {
                for j, topic := range log.TopicsF {
                    if common.HexToHash(topic) != logs[i].Topics[j] {
                        return fmt.Errorf("log topic[%d] expected %v got %x", j, topic, logs[i].Topics[j])
                    }
                }
            }
            genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)

            if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
                return fmt.Errorf("bloom mismatch")
            }
        }
    }
    return nil
}

func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) {
    var (
        keyPair, _ = crypto.NewKeyPairFromSec([]byte(common.Hex2Bytes(tx["secretKey"])))
        data       = common.FromHex(tx["data"])
        gas        = common.Big(tx["gasLimit"])
        price      = common.Big(tx["gasPrice"])
        value      = common.Big(tx["value"])
        nonce      = common.Big(tx["nonce"]).Uint64()
        caddr      = common.HexToAddress(env["currentCoinbase"])
    )

    var to *common.Address
    if len(tx["to"]) > 2 {
        t := common.HexToAddress(tx["to"])
        to = &t
    }
    // Set pre compiled contracts
    vm.Precompiled = vm.PrecompiledContracts()

    snapshot := statedb.Copy()
    coinbase := statedb.GetOrNewStateObject(caddr)
    coinbase.SetGasPool(common.Big(env["currentGasLimit"]))

    message := NewMessage(common.BytesToAddress(keyPair.Address()), to, data, value, gas, price, nonce)
    vmenv := NewEnvFromMap(statedb, env, tx)
    vmenv.origin = common.BytesToAddress(keyPair.Address())
    ret, _, err := core.ApplyMessage(vmenv, message, coinbase)
    if core.IsNonceErr(err) || core.IsInvalidTxErr(err) {
        statedb.Set(snapshot)
    }
    statedb.Update()

    return ret, vmenv.state.Logs(), vmenv.Gas, err
}