github.com/anthdm/go-ethereum@v1.8.4-0.20180412101906-60516c83b011/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  	"runtime"
    22  	"sync"
    23  	"testing"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/crypto"
    27  	"github.com/ethereum/go-ethereum/ethdb"
    28  )
    29  
    30  func newEmptySecure() *SecureTrie {
    31  	diskdb, _ := ethdb.NewMemDatabase()
    32  	triedb := NewDatabase(diskdb)
    33  
    34  	trie, _ := NewSecure(common.Hash{}, triedb, 0)
    35  	return trie
    36  }
    37  
    38  // makeTestSecureTrie creates a large enough secure trie for testing.
    39  func makeTestSecureTrie() (*Database, *SecureTrie, map[string][]byte) {
    40  	// Create an empty trie
    41  	diskdb, _ := ethdb.NewMemDatabase()
    42  	triedb := NewDatabase(diskdb)
    43  
    44  	trie, _ := NewSecure(common.Hash{}, triedb, 0)
    45  
    46  	// Fill it with some arbitrary data
    47  	content := make(map[string][]byte)
    48  	for i := byte(0); i < 255; i++ {
    49  		// Map the same data under multiple keys
    50  		key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i}
    51  		content[string(key)] = val
    52  		trie.Update(key, val)
    53  
    54  		key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i}
    55  		content[string(key)] = val
    56  		trie.Update(key, val)
    57  
    58  		// Add some other data to inflate the trie
    59  		for j := byte(3); j < 13; j++ {
    60  			key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i}
    61  			content[string(key)] = val
    62  			trie.Update(key, val)
    63  		}
    64  	}
    65  	trie.Commit(nil)
    66  
    67  	// Return the generated trie
    68  	return triedb, trie, content
    69  }
    70  
    71  func TestSecureDelete(t *testing.T) {
    72  	trie := newEmptySecure()
    73  	vals := []struct{ k, v string }{
    74  		{"do", "verb"},
    75  		{"ether", "wookiedoo"},
    76  		{"horse", "stallion"},
    77  		{"shaman", "horse"},
    78  		{"doge", "coin"},
    79  		{"ether", ""},
    80  		{"dog", "puppy"},
    81  		{"shaman", ""},
    82  	}
    83  	for _, val := range vals {
    84  		if val.v != "" {
    85  			trie.Update([]byte(val.k), []byte(val.v))
    86  		} else {
    87  			trie.Delete([]byte(val.k))
    88  		}
    89  	}
    90  	hash := trie.Hash()
    91  	exp := common.HexToHash("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d")
    92  	if hash != exp {
    93  		t.Errorf("expected %x got %x", exp, hash)
    94  	}
    95  }
    96  
    97  func TestSecureGetKey(t *testing.T) {
    98  	trie := newEmptySecure()
    99  	trie.Update([]byte("foo"), []byte("bar"))
   100  
   101  	key := []byte("foo")
   102  	value := []byte("bar")
   103  	seckey := crypto.Keccak256(key)
   104  
   105  	if !bytes.Equal(trie.Get(key), value) {
   106  		t.Errorf("Get did not return bar")
   107  	}
   108  	if k := trie.GetKey(seckey); !bytes.Equal(k, key) {
   109  		t.Errorf("GetKey returned %q, want %q", k, key)
   110  	}
   111  }
   112  
   113  func TestSecureTrieConcurrency(t *testing.T) {
   114  	// Create an initial trie and copy if for concurrent access
   115  	_, trie, _ := makeTestSecureTrie()
   116  
   117  	threads := runtime.NumCPU()
   118  	tries := make([]*SecureTrie, threads)
   119  	for i := 0; i < threads; i++ {
   120  		cpy := *trie
   121  		tries[i] = &cpy
   122  	}
   123  	// Start a batch of goroutines interactng 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].Update(key, val)
   134  
   135  				key, val = common.LeftPadBytes([]byte{byte(index), 2, j}, 32), []byte{j}
   136  				tries[index].Update(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].Update(key, val)
   142  				}
   143  			}
   144  			tries[index].Commit(nil)
   145  		}(i)
   146  	}
   147  	// Wait for all threads to finish
   148  	pend.Wait()
   149  }