github.com/theQRL/go-zond@v0.1.1/trie/secure_trie_test.go (about)

     1  // Copyright 2015 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 trie
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"runtime"
    23  	"sync"
    24  	"testing"
    25  
    26  	"github.com/theQRL/go-zond/common"
    27  	"github.com/theQRL/go-zond/core/rawdb"
    28  	"github.com/theQRL/go-zond/core/types"
    29  	"github.com/theQRL/go-zond/crypto"
    30  	"github.com/theQRL/go-zond/trie/trienode"
    31  )
    32  
    33  func newEmptySecure() *StateTrie {
    34  	trie, _ := NewStateTrie(TrieID(types.EmptyRootHash), NewDatabase(rawdb.NewMemoryDatabase(), nil))
    35  	return trie
    36  }
    37  
    38  // makeTestStateTrie creates a large enough secure trie for testing.
    39  func makeTestStateTrie() (*Database, *StateTrie, map[string][]byte) {
    40  	// Create an empty trie
    41  	triedb := NewDatabase(rawdb.NewMemoryDatabase(), nil)
    42  	trie, _ := NewStateTrie(TrieID(types.EmptyRootHash), triedb)
    43  
    44  	// Fill it with some arbitrary data
    45  	content := make(map[string][]byte)
    46  	for i := byte(0); i < 255; i++ {
    47  		// Map the same data under multiple keys
    48  		key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i}
    49  		content[string(key)] = val
    50  		trie.MustUpdate(key, val)
    51  
    52  		key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i}
    53  		content[string(key)] = val
    54  		trie.MustUpdate(key, val)
    55  
    56  		// Add some other data to inflate the trie
    57  		for j := byte(3); j < 13; j++ {
    58  			key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i}
    59  			content[string(key)] = val
    60  			trie.MustUpdate(key, val)
    61  		}
    62  	}
    63  	root, nodes, _ := trie.Commit(false)
    64  	if err := triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil); err != nil {
    65  		panic(fmt.Errorf("failed to commit db %v", err))
    66  	}
    67  	// Re-create the trie based on the new state
    68  	trie, _ = NewStateTrie(TrieID(root), triedb)
    69  	return triedb, trie, content
    70  }
    71  
    72  func TestSecureDelete(t *testing.T) {
    73  	trie := newEmptySecure()
    74  	vals := []struct{ k, v string }{
    75  		{"do", "verb"},
    76  		{"ether", "wookiedoo"},
    77  		{"horse", "stallion"},
    78  		{"shaman", "horse"},
    79  		{"doge", "coin"},
    80  		{"ether", ""},
    81  		{"dog", "puppy"},
    82  		{"shaman", ""},
    83  	}
    84  	for _, val := range vals {
    85  		if val.v != "" {
    86  			trie.MustUpdate([]byte(val.k), []byte(val.v))
    87  		} else {
    88  			trie.MustDelete([]byte(val.k))
    89  		}
    90  	}
    91  	hash := trie.Hash()
    92  	exp := common.HexToHash("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d")
    93  	if hash != exp {
    94  		t.Errorf("expected %x got %x", exp, hash)
    95  	}
    96  }
    97  
    98  func TestSecureGetKey(t *testing.T) {
    99  	trie := newEmptySecure()
   100  	trie.MustUpdate([]byte("foo"), []byte("bar"))
   101  
   102  	key := []byte("foo")
   103  	value := []byte("bar")
   104  	seckey := crypto.Keccak256(key)
   105  
   106  	if !bytes.Equal(trie.MustGet(key), value) {
   107  		t.Errorf("Get did not return bar")
   108  	}
   109  	if k := trie.GetKey(seckey); !bytes.Equal(k, key) {
   110  		t.Errorf("GetKey returned %q, want %q", k, key)
   111  	}
   112  }
   113  
   114  func TestStateTrieConcurrency(t *testing.T) {
   115  	// Create an initial trie and copy if for concurrent access
   116  	_, trie, _ := makeTestStateTrie()
   117  
   118  	threads := runtime.NumCPU()
   119  	tries := make([]*StateTrie, threads)
   120  	for i := 0; i < threads; i++ {
   121  		tries[i] = trie.Copy()
   122  	}
   123  	// Start a batch of goroutines interacting with the trie
   124  	pend := new(sync.WaitGroup)
   125  	pend.Add(threads)
   126  	for i := 0; i < threads; i++ {
   127  		go func(index int) {
   128  			defer pend.Done()
   129  
   130  			for j := byte(0); j < 255; j++ {
   131  				// Map the same data under multiple keys
   132  				key, val := common.LeftPadBytes([]byte{byte(index), 1, j}, 32), []byte{j}
   133  				tries[index].MustUpdate(key, val)
   134  
   135  				key, val = common.LeftPadBytes([]byte{byte(index), 2, j}, 32), []byte{j}
   136  				tries[index].MustUpdate(key, val)
   137  
   138  				// Add some other data to inflate the trie
   139  				for k := byte(3); k < 13; k++ {
   140  					key, val = common.LeftPadBytes([]byte{byte(index), k, j}, 32), []byte{k, j}
   141  					tries[index].MustUpdate(key, val)
   142  				}
   143  			}
   144  			tries[index].Commit(false)
   145  		}(i)
   146  	}
   147  	// Wait for all threads to finish
   148  	pend.Wait()
   149  }