github.com/MetalBlockchain/subnet-evm@v0.4.9/trie/secure_trie_test.go (about)

     1  // (c) 2020-2021, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2015 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package trie
    28  
    29  import (
    30  	"bytes"
    31  	"fmt"
    32  	"runtime"
    33  	"sync"
    34  	"testing"
    35  
    36  	"github.com/MetalBlockchain/subnet-evm/ethdb/memorydb"
    37  	"github.com/ethereum/go-ethereum/common"
    38  	"github.com/ethereum/go-ethereum/crypto"
    39  )
    40  
    41  func newEmptySecure() *StateTrie {
    42  	trie, _ := NewStateTrie(common.Hash{}, common.Hash{}, NewDatabase(memorydb.New()))
    43  	return trie
    44  }
    45  
    46  // makeTestStateTrie creates a large enough secure trie for testing.
    47  func makeTestStateTrie() (*Database, *StateTrie, map[string][]byte) {
    48  	// Create an empty trie
    49  	triedb := NewDatabase(memorydb.New())
    50  	trie, _ := NewStateTrie(common.Hash{}, common.Hash{}, triedb)
    51  
    52  	// Fill it with some arbitrary data
    53  	content := make(map[string][]byte)
    54  	for i := byte(0); i < 255; i++ {
    55  		// Map the same data under multiple keys
    56  		key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i}
    57  		content[string(key)] = val
    58  		trie.Update(key, val)
    59  
    60  		key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i}
    61  		content[string(key)] = val
    62  		trie.Update(key, val)
    63  
    64  		// Add some other data to inflate the trie
    65  		for j := byte(3); j < 13; j++ {
    66  			key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i}
    67  			content[string(key)] = val
    68  			trie.Update(key, val)
    69  		}
    70  	}
    71  	root, nodes, err := trie.Commit(false)
    72  	if err != nil {
    73  		panic(fmt.Errorf("failed to commit trie %v", err))
    74  	}
    75  	if err := triedb.Update(NewWithNodeSet(nodes)); err != nil {
    76  		panic(fmt.Errorf("failed to commit db %v", err))
    77  	}
    78  	// Re-create the trie based on the new state
    79  	trie, _ = NewSecure(common.Hash{}, root, triedb)
    80  	return triedb, trie, content
    81  }
    82  
    83  func TestSecureDelete(t *testing.T) {
    84  	trie := newEmptySecure()
    85  	vals := []struct{ k, v string }{
    86  		{"do", "verb"},
    87  		{"ether", "wookiedoo"},
    88  		{"horse", "stallion"},
    89  		{"shaman", "horse"},
    90  		{"doge", "coin"},
    91  		{"ether", ""},
    92  		{"dog", "puppy"},
    93  		{"shaman", ""},
    94  	}
    95  	for _, val := range vals {
    96  		if val.v != "" {
    97  			trie.Update([]byte(val.k), []byte(val.v))
    98  		} else {
    99  			trie.Delete([]byte(val.k))
   100  		}
   101  	}
   102  	hash := trie.Hash()
   103  	exp := common.HexToHash("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d")
   104  	if hash != exp {
   105  		t.Errorf("expected %x got %x", exp, hash)
   106  	}
   107  }
   108  
   109  func TestSecureGetKey(t *testing.T) {
   110  	trie := newEmptySecure()
   111  	trie.Update([]byte("foo"), []byte("bar"))
   112  
   113  	key := []byte("foo")
   114  	value := []byte("bar")
   115  	seckey := crypto.Keccak256(key)
   116  
   117  	if !bytes.Equal(trie.Get(key), value) {
   118  		t.Errorf("Get did not return bar")
   119  	}
   120  	if k := trie.GetKey(seckey); !bytes.Equal(k, key) {
   121  		t.Errorf("GetKey returned %q, want %q", k, key)
   122  	}
   123  }
   124  
   125  func TestStateTrieConcurrency(t *testing.T) {
   126  	// Create an initial trie and copy if for concurrent access
   127  	_, trie, _ := makeTestStateTrie()
   128  
   129  	threads := runtime.NumCPU()
   130  	tries := make([]*StateTrie, threads)
   131  	for i := 0; i < threads; i++ {
   132  		tries[i] = trie.Copy()
   133  	}
   134  	// Start a batch of goroutines interactng with the trie
   135  	pend := new(sync.WaitGroup)
   136  	pend.Add(threads)
   137  	for i := 0; i < threads; i++ {
   138  		go func(index int) {
   139  			defer pend.Done()
   140  
   141  			for j := byte(0); j < 255; j++ {
   142  				// Map the same data under multiple keys
   143  				key, val := common.LeftPadBytes([]byte{byte(index), 1, j}, 32), []byte{j}
   144  				tries[index].Update(key, val)
   145  
   146  				key, val = common.LeftPadBytes([]byte{byte(index), 2, j}, 32), []byte{j}
   147  				tries[index].Update(key, val)
   148  
   149  				// Add some other data to inflate the trie
   150  				for k := byte(3); k < 13; k++ {
   151  					key, val = common.LeftPadBytes([]byte{byte(index), k, j}, 32), []byte{k, j}
   152  					tries[index].Update(key, val)
   153  				}
   154  			}
   155  			tries[index].Commit(false)
   156  		}(i)
   157  	}
   158  	// Wait for all threads to finish
   159  	pend.Wait()
   160  }