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