github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/eth/api_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package eth 13 14 import ( 15 "reflect" 16 "testing" 17 18 "github.com/davecgh/go-spew/spew" 19 "github.com/Sberex/go-sberex/common" 20 "github.com/Sberex/go-sberex/core/state" 21 "github.com/Sberex/go-sberex/ethdb" 22 ) 23 24 var dumper = spew.ConfigState{Indent: " "} 25 26 func TestStorageRangeAt(t *testing.T) { 27 // Create a state where account 0x010000... has a few storage entries. 28 var ( 29 db, _ = ethdb.NewMemDatabase() 30 state, _ = state.New(common.Hash{}, state.NewDatabase(db)) 31 addr = common.Address{0x01} 32 keys = []common.Hash{ // hashes of Keys of storage 33 common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"), 34 common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"), 35 common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"), 36 common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"), 37 } 38 storage = storageMap{ 39 keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}}, 40 keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}}, 41 keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}}, 42 keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}}, 43 } 44 ) 45 for _, entry := range storage { 46 state.SetState(addr, *entry.Key, entry.Value) 47 } 48 49 // Check a few combinations of limit and start/end. 50 tests := []struct { 51 start []byte 52 limit int 53 want StorageRangeResult 54 }{ 55 { 56 start: []byte{}, limit: 0, 57 want: StorageRangeResult{storageMap{}, &keys[0]}, 58 }, 59 { 60 start: []byte{}, limit: 100, 61 want: StorageRangeResult{storage, nil}, 62 }, 63 { 64 start: []byte{}, limit: 2, 65 want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]}, 66 }, 67 { 68 start: []byte{0x00}, limit: 4, 69 want: StorageRangeResult{storage, nil}, 70 }, 71 { 72 start: []byte{0x40}, limit: 2, 73 want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]}, 74 }, 75 } 76 for _, test := range tests { 77 result, err := storageRangeAt(state.StorageTrie(addr), test.start, test.limit) 78 if err != nil { 79 t.Error(err) 80 } 81 if !reflect.DeepEqual(result, test.want) { 82 t.Fatalf("wrong result for range 0x%x.., limit %d:\ngot %s\nwant %s", 83 test.start, test.limit, dumper.Sdump(result), dumper.Sdump(&test.want)) 84 } 85 } 86 }