(t *testing.T)
| 29 | var dumper = spew.ConfigState{Indent: " "} |
| 30 | |
| 31 | func TestStorageRangeAt(t *testing.T) { |
| 32 | // Create a state where account 0x010000... has a few storage entries. |
| 33 | var ( |
| 34 | state, _ = state.New(common.Hash{}, state.NewDatabase(database.NewMemDatabase())) |
| 35 | addr = common.Address{0x01} |
| 36 | keys = []common.Hash{ // hashes of Keys of storage |
| 37 | common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"), |
| 38 | common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"), |
| 39 | common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"), |
| 40 | common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"), |
| 41 | } |
| 42 | storage = storageMap{ |
| 43 | keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}}, |
| 44 | keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}}, |
| 45 | keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}}, |
| 46 | keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}}, |
| 47 | } |
| 48 | ) |
| 49 | for _, entry := range storage { |
| 50 | state.SetState(addr, *entry.Key, entry.Value) |
| 51 | } |
| 52 | |
| 53 | // Check a few combinations of limit and start/end. |
| 54 | tests := []struct { |
| 55 | start []byte |
| 56 | limit int |
| 57 | want StorageRangeResult |
| 58 | }{ |
| 59 | { |
| 60 | start: []byte{}, limit: 0, |
| 61 | want: StorageRangeResult{storageMap{}, &keys[0]}, |
| 62 | }, |
| 63 | { |
| 64 | start: []byte{}, limit: 100, |
| 65 | want: StorageRangeResult{storage, nil}, |
| 66 | }, |
| 67 | { |
| 68 | start: []byte{}, limit: 2, |
| 69 | want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]}, |
| 70 | }, |
| 71 | { |
| 72 | start: []byte{0x00}, limit: 4, |
| 73 | want: StorageRangeResult{storage, nil}, |
| 74 | }, |
| 75 | { |
| 76 | start: []byte{0x40}, limit: 2, |
| 77 | want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]}, |
| 78 | }, |
| 79 | } |
| 80 | for _, test := range tests { |
| 81 | result, err := storageRangeAt(state.StorageTrie(addr), test.start, test.limit) |
| 82 | if err != nil { |
| 83 | t.Error(err) |
| 84 | } |
| 85 | if !reflect.DeepEqual(result, test.want) { |
| 86 | t.Fatalf("wrong result for range 0x%x.., limit %d:\ngot %s\nwant %s", |
| 87 | test.start, test.limit, dumper.Sdump(result), dumper.Sdump(&test.want)) |
| 88 | } |
nothing calls this directly
no test coverage detected