github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/eth/api_test.go (about)

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