aboutsummaryrefslogtreecommitdiffstats
path: root/eth/api_test.go
blob: 900a82bb6a14dfdde8c969b797429cbf45b590a3 (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
// Copyright 2017 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 eth

import (
    "reflect"
    "testing"

    "github.com/davecgh/go-spew/spew"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/state"
    "github.com/ethereum/go-ethereum/ethdb"
)

var dumper = spew.ConfigState{Indent: "    "}

func TestStorageRangeAt(t *testing.T) {
    // Create a state where account 0x010000... has a few storage entries.
    var (
        db, _    = ethdb.NewMemDatabase()
        state, _ = state.New(common.Hash{}, state.NewDatabase(db))
        addr     = common.Address{0x01}
        keys     = []common.Hash{ // hashes of Keys of storage
            common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"),
            common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"),
            common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"),
            common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"),
        }
        storage = storageMap{
            keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}},
            keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}},
            keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}},
            keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}},
        }
    )
    for _, entry := range storage {
        state.SetState(addr, *entry.Key, entry.Value)
    }

    // Check a few combinations of limit and start/end.
    tests := []struct {
        start []byte
        limit int
        want  StorageRangeResult
    }{
        {
            start: []byte{}, limit: 0,
            want: StorageRangeResult{storageMap{}, &keys[0]},
        },
        {
            start: []byte{}, limit: 100,
            want: StorageRangeResult{storage, nil},
        },
        {
            start: []byte{}, limit: 2,
            want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]},
        },
        {
            start: []byte{0x00}, limit: 4,
            want: StorageRangeResult{storage, nil},
        },
        {
            start: []byte{0x40}, limit: 2,
            want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]},
        },
    }
    for _, test := range tests {
        result, err := storageRangeAt(state.StorageTrie(addr), test.start, test.limit)
        if err != nil {
            t.Error(err)
        }
        if !reflect.DeepEqual(result, test.want) {
            t.Fatalf("wrong result for range 0x%x.., limit %d:\ngot %s\nwant %s",
                test.start, test.limit, dumper.Sdump(result), dumper.Sdump(&test.want))
        }
    }
}