github.com/phillinzzz/newBsc@v1.1.6/eth/api_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package eth
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"math/big"
    23  	"reflect"
    24  	"sort"
    25  	"testing"
    26  
    27  	"github.com/davecgh/go-spew/spew"
    28  	"github.com/phillinzzz/newBsc/common"
    29  	"github.com/phillinzzz/newBsc/core/rawdb"
    30  	"github.com/phillinzzz/newBsc/core/state"
    31  	"github.com/phillinzzz/newBsc/crypto"
    32  )
    33  
    34  var dumper = spew.ConfigState{Indent: "    "}
    35  
    36  func accountRangeTest(t *testing.T, trie *state.Trie, statedb *state.StateDB, start common.Hash, requestedNum int, expectedNum int) state.IteratorDump {
    37  	result := statedb.IteratorDump(true, true, false, start.Bytes(), requestedNum)
    38  
    39  	if len(result.Accounts) != expectedNum {
    40  		t.Fatalf("expected %d results, got %d", expectedNum, len(result.Accounts))
    41  	}
    42  	for address := range result.Accounts {
    43  		if address == (common.Address{}) {
    44  			t.Fatalf("empty address returned")
    45  		}
    46  		if !statedb.Exist(address) {
    47  			t.Fatalf("account not found in state %s", address.Hex())
    48  		}
    49  	}
    50  	return result
    51  }
    52  
    53  type resultHash []common.Hash
    54  
    55  func (h resultHash) Len() int           { return len(h) }
    56  func (h resultHash) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
    57  func (h resultHash) Less(i, j int) bool { return bytes.Compare(h[i].Bytes(), h[j].Bytes()) < 0 }
    58  
    59  func TestAccountRange(t *testing.T) {
    60  	t.Parallel()
    61  
    62  	var (
    63  		statedb  = state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), nil)
    64  		state, _ = state.New(common.Hash{}, statedb, nil)
    65  		addrs    = [AccountRangeMaxResults * 2]common.Address{}
    66  		m        = map[common.Address]bool{}
    67  	)
    68  
    69  	for i := range addrs {
    70  		hash := common.HexToHash(fmt.Sprintf("%x", i))
    71  		addr := common.BytesToAddress(crypto.Keccak256Hash(hash.Bytes()).Bytes())
    72  		addrs[i] = addr
    73  		state.SetBalance(addrs[i], big.NewInt(1))
    74  		if _, ok := m[addr]; ok {
    75  			t.Fatalf("bad")
    76  		} else {
    77  			m[addr] = true
    78  		}
    79  	}
    80  	state.Commit(true)
    81  	root := state.IntermediateRoot(true)
    82  
    83  	trie, err := statedb.OpenTrie(root)
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	accountRangeTest(t, &trie, state, common.Hash{}, AccountRangeMaxResults/2, AccountRangeMaxResults/2)
    88  	// test pagination
    89  	firstResult := accountRangeTest(t, &trie, state, common.Hash{}, AccountRangeMaxResults, AccountRangeMaxResults)
    90  	secondResult := accountRangeTest(t, &trie, state, common.BytesToHash(firstResult.Next), AccountRangeMaxResults, AccountRangeMaxResults)
    91  
    92  	hList := make(resultHash, 0)
    93  	for addr1 := range firstResult.Accounts {
    94  		// If address is empty, then it makes no sense to compare
    95  		// them as they might be two different accounts.
    96  		if addr1 == (common.Address{}) {
    97  			continue
    98  		}
    99  		if _, duplicate := secondResult.Accounts[addr1]; duplicate {
   100  			t.Fatalf("pagination test failed:  results should not overlap")
   101  		}
   102  		hList = append(hList, crypto.Keccak256Hash(addr1.Bytes()))
   103  	}
   104  	// Test to see if it's possible to recover from the middle of the previous
   105  	// set and get an even split between the first and second sets.
   106  	sort.Sort(hList)
   107  	middleH := hList[AccountRangeMaxResults/2]
   108  	middleResult := accountRangeTest(t, &trie, state, middleH, AccountRangeMaxResults, AccountRangeMaxResults)
   109  	missing, infirst, insecond := 0, 0, 0
   110  	for h := range middleResult.Accounts {
   111  		if _, ok := firstResult.Accounts[h]; ok {
   112  			infirst++
   113  		} else if _, ok := secondResult.Accounts[h]; ok {
   114  			insecond++
   115  		} else {
   116  			missing++
   117  		}
   118  	}
   119  	if missing != 0 {
   120  		t.Fatalf("%d hashes in the 'middle' set were neither in the first not the second set", missing)
   121  	}
   122  	if infirst != AccountRangeMaxResults/2 {
   123  		t.Fatalf("Imbalance in the number of first-test results: %d != %d", infirst, AccountRangeMaxResults/2)
   124  	}
   125  	if insecond != AccountRangeMaxResults/2 {
   126  		t.Fatalf("Imbalance in the number of second-test results: %d != %d", insecond, AccountRangeMaxResults/2)
   127  	}
   128  }
   129  
   130  func TestEmptyAccountRange(t *testing.T) {
   131  	t.Parallel()
   132  
   133  	var (
   134  		statedb  = state.NewDatabase(rawdb.NewMemoryDatabase())
   135  		state, _ = state.New(common.Hash{}, statedb, nil)
   136  	)
   137  	state.Commit(true)
   138  	state.IntermediateRoot(true)
   139  	results := state.IteratorDump(true, true, true, (common.Hash{}).Bytes(), AccountRangeMaxResults)
   140  	if bytes.Equal(results.Next, (common.Hash{}).Bytes()) {
   141  		t.Fatalf("Empty results should not return a second page")
   142  	}
   143  	if len(results.Accounts) != 0 {
   144  		t.Fatalf("Empty state should not return addresses: %v", results.Accounts)
   145  	}
   146  }
   147  
   148  func TestStorageRangeAt(t *testing.T) {
   149  	t.Parallel()
   150  
   151  	// Create a state where account 0x010000... has a few storage entries.
   152  	var (
   153  		state, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   154  		addr     = common.Address{0x01}
   155  		keys     = []common.Hash{ // hashes of Keys of storage
   156  			common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"),
   157  			common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"),
   158  			common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"),
   159  			common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"),
   160  		}
   161  		storage = storageMap{
   162  			keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}},
   163  			keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}},
   164  			keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}},
   165  			keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}},
   166  		}
   167  	)
   168  	for _, entry := range storage {
   169  		state.SetState(addr, *entry.Key, entry.Value)
   170  	}
   171  
   172  	// Check a few combinations of limit and start/end.
   173  	tests := []struct {
   174  		start []byte
   175  		limit int
   176  		want  StorageRangeResult
   177  	}{
   178  		{
   179  			start: []byte{}, limit: 0,
   180  			want: StorageRangeResult{storageMap{}, &keys[0]},
   181  		},
   182  		{
   183  			start: []byte{}, limit: 100,
   184  			want: StorageRangeResult{storage, nil},
   185  		},
   186  		{
   187  			start: []byte{}, limit: 2,
   188  			want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]},
   189  		},
   190  		{
   191  			start: []byte{0x00}, limit: 4,
   192  			want: StorageRangeResult{storage, nil},
   193  		},
   194  		{
   195  			start: []byte{0x40}, limit: 2,
   196  			want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]},
   197  		},
   198  	}
   199  	for _, test := range tests {
   200  		result, err := storageRangeAt(state.StorageTrie(addr), test.start, test.limit)
   201  		if err != nil {
   202  			t.Error(err)
   203  		}
   204  		if !reflect.DeepEqual(result, test.want) {
   205  			t.Fatalf("wrong result for range 0x%x.., limit %d:\ngot %s\nwant %s",
   206  				test.start, test.limit, dumper.Sdump(result), dumper.Sdump(&test.want))
   207  		}
   208  	}
   209  }