github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/neatptc/api_test.go (about)

     1  package neatptc
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/davecgh/go-spew/spew"
     8  	"github.com/neatlab/neatio/chain/core/rawdb"
     9  	"github.com/neatlab/neatio/chain/core/state"
    10  	"github.com/neatlab/neatio/utilities/common"
    11  )
    12  
    13  var dumper = spew.ConfigState{Indent: "    "}
    14  
    15  func TestStorageRangeAt(t *testing.T) {
    16  
    17  	var (
    18  		db       = rawdb.NewMemoryDatabase()
    19  		state, _ = state.New(common.Hash{}, state.NewDatabase(db))
    20  		addr     = common.Address{0x01}
    21  		keys     = []common.Hash{
    22  			common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"),
    23  			common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"),
    24  			common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"),
    25  			common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"),
    26  		}
    27  		storage = storageMap{
    28  			keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}},
    29  			keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}},
    30  			keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}},
    31  			keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}},
    32  		}
    33  	)
    34  	for _, entry := range storage {
    35  		state.SetState(addr, *entry.Key, entry.Value)
    36  	}
    37  
    38  	tests := []struct {
    39  		start []byte
    40  		limit int
    41  		want  StorageRangeResult
    42  	}{
    43  		{
    44  			start: []byte{}, limit: 0,
    45  			want: StorageRangeResult{storageMap{}, &keys[0]},
    46  		},
    47  		{
    48  			start: []byte{}, limit: 100,
    49  			want: StorageRangeResult{storage, nil},
    50  		},
    51  		{
    52  			start: []byte{}, limit: 2,
    53  			want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]},
    54  		},
    55  		{
    56  			start: []byte{0x00}, limit: 4,
    57  			want: StorageRangeResult{storage, nil},
    58  		},
    59  		{
    60  			start: []byte{0x40}, limit: 2,
    61  			want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]},
    62  		},
    63  	}
    64  	for _, test := range tests {
    65  		result, err := storageRangeAt(state.StorageTrie(addr), test.start, test.limit)
    66  		if err != nil {
    67  			t.Error(err)
    68  		}
    69  		if !reflect.DeepEqual(result, test.want) {
    70  			t.Fatalf("wrong result for range 0x%x.., limit %d:\ngot %s\nwant %s",
    71  				test.start, test.limit, dumper.Sdump(result), dumper.Sdump(&test.want))
    72  		}
    73  	}
    74  }