| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // Copyright 2014 The go-ethereum Authors | 
					
						
							| 
									
										
										
										
											2015-07-22 18:48:40 +02:00
										 |  |  | // This file is part of the go-ethereum library. | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2015-07-23 18:35:11 +02:00
										 |  |  | // The go-ethereum library is free software: you can redistribute it and/or modify | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // it under the terms of the GNU Lesser General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2015-07-22 18:48:40 +02:00
										 |  |  | // The go-ethereum library is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							| 
									
										
										
										
											2015-07-22 18:48:40 +02:00
										 |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | // GNU Lesser General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Lesser General Public License | 
					
						
							| 
									
										
										
										
											2015-07-22 18:48:40 +02:00
										 |  |  | // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2015-07-07 02:54:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 14:45:03 +01:00
										 |  |  | package trie | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2014-11-18 19:52:45 +01:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | 	"encoding/binary" | 
					
						
							| 
									
										
										
										
											2014-05-26 00:09:38 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2016-09-29 16:51:32 +02:00
										 |  |  | 	"math/rand" | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2016-09-29 16:51:32 +02:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2014-11-18 19:52:45 +01:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2016-09-29 16:51:32 +02:00
										 |  |  | 	"testing/quick" | 
					
						
							| 
									
										
										
										
											2014-11-15 20:21:55 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	"github.com/davecgh/go-spew/spew" | 
					
						
							| 
									
										
										
										
											2015-03-16 11:27:38 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/ethdb" | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | func init() { | 
					
						
							|  |  |  | 	spew.Config.Indent = "    " | 
					
						
							|  |  |  | 	spew.Config.DisableMethods = true | 
					
						
							| 
									
										
										
										
											2014-02-15 13:21:11 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | // Used for testing | 
					
						
							|  |  |  | func newEmpty() *Trie { | 
					
						
							|  |  |  | 	db, _ := ethdb.NewMemDatabase() | 
					
						
							|  |  |  | 	trie, _ := New(common.Hash{}, db) | 
					
						
							|  |  |  | 	return trie | 
					
						
							| 
									
										
										
										
											2015-03-03 12:15:58 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | func TestEmptyTrie(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	var trie Trie | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	res := trie.Hash() | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	exp := emptyRoot | 
					
						
							|  |  |  | 	if res != common.Hash(exp) { | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 		t.Errorf("expected %x got %x", exp, res) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-15 13:21:11 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 10:57:02 +01:00
										 |  |  | func TestNull(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	var trie Trie | 
					
						
							| 
									
										
										
										
											2015-03-19 10:57:02 +01:00
										 |  |  | 	key := make([]byte, 32) | 
					
						
							| 
									
										
										
										
											2017-01-09 11:16:06 +01:00
										 |  |  | 	value := []byte("test") | 
					
						
							| 
									
										
										
										
											2015-03-19 10:57:02 +01:00
										 |  |  | 	trie.Update(key, value) | 
					
						
							| 
									
										
										
										
											2017-01-09 11:16:06 +01:00
										 |  |  | 	if !bytes.Equal(trie.Get(key), value) { | 
					
						
							|  |  |  | 		t.Fatal("wrong value") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-19 10:57:02 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | func TestMissingRoot(t *testing.T) { | 
					
						
							|  |  |  | 	db, _ := ethdb.NewMemDatabase() | 
					
						
							|  |  |  | 	trie, err := New(common.HexToHash("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"), db) | 
					
						
							|  |  |  | 	if trie != nil { | 
					
						
							|  |  |  | 		t.Error("New returned non-nil trie for invalid root") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-25 18:28:21 +01:00
										 |  |  | 	if _, ok := err.(*MissingNodeError); !ok { | 
					
						
							| 
									
										
										
										
											2016-04-15 11:06:57 +02:00
										 |  |  | 		t.Errorf("New returned wrong error: %v", err) | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-25 18:28:21 +01:00
										 |  |  | func TestMissingNode(t *testing.T) { | 
					
						
							|  |  |  | 	db, _ := ethdb.NewMemDatabase() | 
					
						
							|  |  |  | 	trie, _ := New(common.Hash{}, db) | 
					
						
							|  |  |  | 	updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer") | 
					
						
							|  |  |  | 	updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf") | 
					
						
							|  |  |  | 	root, _ := trie.Commit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	_, err := trie.TryGet([]byte("120000")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	_, err = trie.TryGet([]byte("120099")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	_, err = trie.TryGet([]byte("123456")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	err = trie.TryUpdate([]byte("120099"), []byte("zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	err = trie.TryDelete([]byte("123456")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	db.Delete(common.FromHex("e1d943cc8f061a0c0b98162830b970395ac9315654824bf21b73b891365262f9")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	_, err = trie.TryGet([]byte("120000")) | 
					
						
							|  |  |  | 	if _, ok := err.(*MissingNodeError); !ok { | 
					
						
							|  |  |  | 		t.Errorf("Wrong error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	_, err = trie.TryGet([]byte("120099")) | 
					
						
							|  |  |  | 	if _, ok := err.(*MissingNodeError); !ok { | 
					
						
							|  |  |  | 		t.Errorf("Wrong error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	_, err = trie.TryGet([]byte("123456")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Unexpected error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	err = trie.TryUpdate([]byte("120099"), []byte("zxcv")) | 
					
						
							|  |  |  | 	if _, ok := err.(*MissingNodeError); !ok { | 
					
						
							|  |  |  | 		t.Errorf("Wrong error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	err = trie.TryDelete([]byte("123456")) | 
					
						
							|  |  |  | 	if _, ok := err.(*MissingNodeError); !ok { | 
					
						
							|  |  |  | 		t.Errorf("Wrong error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | func TestInsert(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	trie := newEmpty() | 
					
						
							| 
									
										
										
										
											2014-02-15 13:21:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	updateString(trie, "doe", "reindeer") | 
					
						
							|  |  |  | 	updateString(trie, "dog", "puppy") | 
					
						
							|  |  |  | 	updateString(trie, "dogglesworth", "cat") | 
					
						
							| 
									
										
										
										
											2014-02-15 13:21:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	exp := common.HexToHash("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3") | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	root := trie.Hash() | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	if root != exp { | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 		t.Errorf("exp %x got %x", exp, root) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-20 14:40:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	trie = newEmpty() | 
					
						
							|  |  |  | 	updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") | 
					
						
							| 
									
										
										
										
											2014-02-20 14:40:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab") | 
					
						
							|  |  |  | 	root, err := trie.Commit() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("commit error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if root != exp { | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 		t.Errorf("exp %x got %x", exp, root) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-20 14:40:00 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-24 12:11:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | func TestGet(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	trie := newEmpty() | 
					
						
							|  |  |  | 	updateString(trie, "doe", "reindeer") | 
					
						
							|  |  |  | 	updateString(trie, "dog", "puppy") | 
					
						
							|  |  |  | 	updateString(trie, "dogglesworth", "cat") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i := 0; i < 2; i++ { | 
					
						
							|  |  |  | 		res := getString(trie, "dog") | 
					
						
							|  |  |  | 		if !bytes.Equal(res, []byte("puppy")) { | 
					
						
							|  |  |  | 			t.Errorf("expected puppy got %x", res) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-05-26 00:09:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 		unknown := getString(trie, "unknown") | 
					
						
							|  |  |  | 		if unknown != nil { | 
					
						
							|  |  |  | 			t.Errorf("expected nil got %x", unknown) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-06-30 13:08:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 		if i == 1 { | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		trie.Commit() | 
					
						
							| 
									
										
										
										
											2014-06-17 18:05:46 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-06-30 13:08:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | func TestDelete(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	trie := newEmpty() | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	vals := []struct{ k, v string }{ | 
					
						
							|  |  |  | 		{"do", "verb"}, | 
					
						
							|  |  |  | 		{"ether", "wookiedoo"}, | 
					
						
							|  |  |  | 		{"horse", "stallion"}, | 
					
						
							|  |  |  | 		{"shaman", "horse"}, | 
					
						
							|  |  |  | 		{"doge", "coin"}, | 
					
						
							|  |  |  | 		{"ether", ""}, | 
					
						
							|  |  |  | 		{"dog", "puppy"}, | 
					
						
							|  |  |  | 		{"shaman", ""}, | 
					
						
							| 
									
										
										
										
											2014-06-17 18:05:46 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	for _, val := range vals { | 
					
						
							|  |  |  | 		if val.v != "" { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 			updateString(trie, val.k, val.v) | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 			deleteString(trie, val.k) | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-06-30 13:08:00 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	hash := trie.Hash() | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	exp := common.HexToHash("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84") | 
					
						
							|  |  |  | 	if hash != exp { | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 		t.Errorf("expected %x got %x", exp, hash) | 
					
						
							| 
									
										
										
										
											2014-06-30 13:08:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-27 01:08:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | func TestEmptyValues(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	trie := newEmpty() | 
					
						
							| 
									
										
										
										
											2014-06-30 13:08:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	vals := []struct{ k, v string }{ | 
					
						
							|  |  |  | 		{"do", "verb"}, | 
					
						
							|  |  |  | 		{"ether", "wookiedoo"}, | 
					
						
							|  |  |  | 		{"horse", "stallion"}, | 
					
						
							|  |  |  | 		{"shaman", "horse"}, | 
					
						
							|  |  |  | 		{"doge", "coin"}, | 
					
						
							|  |  |  | 		{"ether", ""}, | 
					
						
							|  |  |  | 		{"dog", "puppy"}, | 
					
						
							|  |  |  | 		{"shaman", ""}, | 
					
						
							| 
									
										
										
										
											2014-06-30 13:08:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	for _, val := range vals { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 		updateString(trie, val.k, val.v) | 
					
						
							| 
									
										
										
										
											2014-07-01 09:55:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	hash := trie.Hash() | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	exp := common.HexToHash("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84") | 
					
						
							|  |  |  | 	if hash != exp { | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 		t.Errorf("expected %x got %x", exp, hash) | 
					
						
							| 
									
										
										
										
											2014-06-17 18:05:46 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-01 09:55:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | func TestReplication(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	trie := newEmpty() | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	vals := []struct{ k, v string }{ | 
					
						
							|  |  |  | 		{"do", "verb"}, | 
					
						
							|  |  |  | 		{"ether", "wookiedoo"}, | 
					
						
							|  |  |  | 		{"horse", "stallion"}, | 
					
						
							|  |  |  | 		{"shaman", "horse"}, | 
					
						
							|  |  |  | 		{"doge", "coin"}, | 
					
						
							|  |  |  | 		{"dog", "puppy"}, | 
					
						
							|  |  |  | 		{"somethingveryoddindeedthis is", "myothernodedata"}, | 
					
						
							| 
									
										
										
										
											2014-06-17 18:05:46 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	for _, val := range vals { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 		updateString(trie, val.k, val.v) | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	exp, err := trie.Commit() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("commit error: %v", err) | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-15 15:29:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	// create a new trie on top of the database and check that lookups work. | 
					
						
							|  |  |  | 	trie2, err := New(exp, trie.db) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("can't recreate trie at %x: %v", exp, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, kv := range vals { | 
					
						
							|  |  |  | 		if string(getString(trie2, kv.k)) != kv.v { | 
					
						
							|  |  |  | 			t.Errorf("trie2 doesn't have %q => %q", kv.k, kv.v) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	hash, err := trie2.Commit() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("commit error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if hash != exp { | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 		t.Errorf("root failure. expected %x got %x", exp, hash) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-17 11:21:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	// perform some insertions on the new trie. | 
					
						
							|  |  |  | 	vals2 := []struct{ k, v string }{ | 
					
						
							| 
									
										
										
										
											2015-01-08 11:47:04 +01:00
										 |  |  | 		{"do", "verb"}, | 
					
						
							|  |  |  | 		{"ether", "wookiedoo"}, | 
					
						
							|  |  |  | 		{"horse", "stallion"}, | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 		// {"shaman", "horse"}, | 
					
						
							|  |  |  | 		// {"doge", "coin"}, | 
					
						
							|  |  |  | 		// {"ether", ""}, | 
					
						
							|  |  |  | 		// {"dog", "puppy"}, | 
					
						
							|  |  |  | 		// {"somethingveryoddindeedthis is", "myothernodedata"}, | 
					
						
							|  |  |  | 		// {"shaman", ""}, | 
					
						
							| 
									
										
										
										
											2014-10-10 17:00:06 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	for _, val := range vals2 { | 
					
						
							|  |  |  | 		updateString(trie2, val.k, val.v) | 
					
						
							| 
									
										
										
										
											2014-10-10 17:00:06 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-19 13:24:14 +03:00
										 |  |  | 	if hash := trie2.Hash(); hash != exp { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 		t.Errorf("root failure. expected %x got %x", exp, hash) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-14 15:19:50 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | func TestLargeValue(t *testing.T) { | 
					
						
							|  |  |  | 	trie := newEmpty() | 
					
						
							|  |  |  | 	trie.Update([]byte("key1"), []byte{99, 99, 99, 99}) | 
					
						
							|  |  |  | 	trie.Update([]byte("key2"), bytes.Repeat([]byte{1}, 32)) | 
					
						
							|  |  |  | 	trie.Hash() | 
					
						
							| 
									
										
										
										
											2016-09-29 16:51:32 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-17 16:13:50 +02:00
										 |  |  | type countingDB struct { | 
					
						
							|  |  |  | 	Database | 
					
						
							|  |  |  | 	gets map[string]int | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (db *countingDB) Get(key []byte) ([]byte, error) { | 
					
						
							|  |  |  | 	db.gets[string(key)]++ | 
					
						
							|  |  |  | 	return db.Database.Get(key) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TestCacheUnload checks that decoded nodes are unloaded after a | 
					
						
							|  |  |  | // certain number of commit operations. | 
					
						
							|  |  |  | func TestCacheUnload(t *testing.T) { | 
					
						
							|  |  |  | 	// Create test trie with two branches. | 
					
						
							|  |  |  | 	trie := newEmpty() | 
					
						
							|  |  |  | 	key1 := "---------------------------------" | 
					
						
							|  |  |  | 	key2 := "---some other branch" | 
					
						
							|  |  |  | 	updateString(trie, key1, "this is the branch of key1.") | 
					
						
							|  |  |  | 	updateString(trie, key2, "this is the branch of key2.") | 
					
						
							|  |  |  | 	root, _ := trie.Commit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Commit the trie repeatedly and access key1. | 
					
						
							|  |  |  | 	// The branch containing it is loaded from DB exactly two times: | 
					
						
							|  |  |  | 	// in the 0th and 6th iteration. | 
					
						
							|  |  |  | 	db := &countingDB{Database: trie.db, gets: make(map[string]int)} | 
					
						
							|  |  |  | 	trie, _ = New(root, db) | 
					
						
							|  |  |  | 	trie.SetCacheLimit(5) | 
					
						
							|  |  |  | 	for i := 0; i < 12; i++ { | 
					
						
							|  |  |  | 		getString(trie, key1) | 
					
						
							|  |  |  | 		trie.Commit() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check that it got loaded two times. | 
					
						
							|  |  |  | 	for dbkey, count := range db.gets { | 
					
						
							|  |  |  | 		if count != 2 { | 
					
						
							|  |  |  | 			t.Errorf("db key %x loaded %d times, want %d times", []byte(dbkey), count, 2) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // randTest performs random trie operations. | 
					
						
							|  |  |  | // Instances of this test are created by Generate. | 
					
						
							|  |  |  | type randTest []randTestStep | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-29 16:51:32 +02:00
										 |  |  | type randTestStep struct { | 
					
						
							|  |  |  | 	op    int | 
					
						
							|  |  |  | 	key   []byte // for opUpdate, opDelete, opGet | 
					
						
							|  |  |  | 	value []byte // for opUpdate | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	opUpdate = iota | 
					
						
							|  |  |  | 	opDelete | 
					
						
							|  |  |  | 	opGet | 
					
						
							|  |  |  | 	opCommit | 
					
						
							|  |  |  | 	opHash | 
					
						
							|  |  |  | 	opReset | 
					
						
							|  |  |  | 	opItercheckhash | 
					
						
							| 
									
										
										
										
											2016-10-17 16:13:50 +02:00
										 |  |  | 	opCheckCacheInvariant | 
					
						
							| 
									
										
										
										
											2016-09-29 16:51:32 +02:00
										 |  |  | 	opMax // boundary value, not an actual op | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (randTest) Generate(r *rand.Rand, size int) reflect.Value { | 
					
						
							|  |  |  | 	var allKeys [][]byte | 
					
						
							|  |  |  | 	genKey := func() []byte { | 
					
						
							|  |  |  | 		if len(allKeys) < 2 || r.Intn(100) < 10 { | 
					
						
							|  |  |  | 			// new key | 
					
						
							|  |  |  | 			key := make([]byte, r.Intn(50)) | 
					
						
							| 
									
										
										
										
											2017-03-22 19:48:51 +01:00
										 |  |  | 			r.Read(key) | 
					
						
							| 
									
										
										
										
											2016-09-29 16:51:32 +02:00
										 |  |  | 			allKeys = append(allKeys, key) | 
					
						
							|  |  |  | 			return key | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// use existing key | 
					
						
							|  |  |  | 		return allKeys[r.Intn(len(allKeys))] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var steps randTest | 
					
						
							|  |  |  | 	for i := 0; i < size; i++ { | 
					
						
							|  |  |  | 		step := randTestStep{op: r.Intn(opMax)} | 
					
						
							|  |  |  | 		switch step.op { | 
					
						
							|  |  |  | 		case opUpdate: | 
					
						
							|  |  |  | 			step.key = genKey() | 
					
						
							|  |  |  | 			step.value = make([]byte, 8) | 
					
						
							|  |  |  | 			binary.BigEndian.PutUint64(step.value, uint64(i)) | 
					
						
							|  |  |  | 		case opGet, opDelete: | 
					
						
							|  |  |  | 			step.key = genKey() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		steps = append(steps, step) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return reflect.ValueOf(steps) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func runRandTest(rt randTest) bool { | 
					
						
							|  |  |  | 	db, _ := ethdb.NewMemDatabase() | 
					
						
							|  |  |  | 	tr, _ := New(common.Hash{}, db) | 
					
						
							|  |  |  | 	values := make(map[string]string) // tracks content of the trie | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, step := range rt { | 
					
						
							|  |  |  | 		switch step.op { | 
					
						
							|  |  |  | 		case opUpdate: | 
					
						
							|  |  |  | 			tr.Update(step.key, step.value) | 
					
						
							|  |  |  | 			values[string(step.key)] = string(step.value) | 
					
						
							|  |  |  | 		case opDelete: | 
					
						
							|  |  |  | 			tr.Delete(step.key) | 
					
						
							|  |  |  | 			delete(values, string(step.key)) | 
					
						
							|  |  |  | 		case opGet: | 
					
						
							|  |  |  | 			v := tr.Get(step.key) | 
					
						
							|  |  |  | 			want := values[string(step.key)] | 
					
						
							|  |  |  | 			if string(v) != want { | 
					
						
							|  |  |  | 				fmt.Printf("mismatch for key 0x%x, got 0x%x want 0x%x", step.key, v, want) | 
					
						
							|  |  |  | 				return false | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		case opCommit: | 
					
						
							|  |  |  | 			if _, err := tr.Commit(); err != nil { | 
					
						
							|  |  |  | 				panic(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		case opHash: | 
					
						
							|  |  |  | 			tr.Hash() | 
					
						
							|  |  |  | 		case opReset: | 
					
						
							|  |  |  | 			hash, err := tr.Commit() | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				panic(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			newtr, err := New(hash, db) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				panic(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			tr = newtr | 
					
						
							|  |  |  | 		case opItercheckhash: | 
					
						
							|  |  |  | 			checktr, _ := New(common.Hash{}, nil) | 
					
						
							| 
									
										
										
										
											2017-04-13 14:41:24 +02:00
										 |  |  | 			it := NewIterator(tr.NodeIterator(nil)) | 
					
						
							| 
									
										
										
										
											2016-09-29 16:51:32 +02:00
										 |  |  | 			for it.Next() { | 
					
						
							|  |  |  | 				checktr.Update(it.Key, it.Value) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if tr.Hash() != checktr.Hash() { | 
					
						
							|  |  |  | 				fmt.Println("hashes not equal") | 
					
						
							|  |  |  | 				return false | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-10-17 16:13:50 +02:00
										 |  |  | 		case opCheckCacheInvariant: | 
					
						
							| 
									
										
										
										
											2016-10-17 23:01:29 +02:00
										 |  |  | 			return checkCacheInvariant(tr.root, nil, tr.cachegen, false, 0) | 
					
						
							| 
									
										
										
										
											2016-10-17 16:13:50 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-17 23:01:29 +02:00
										 |  |  | func checkCacheInvariant(n, parent node, parentCachegen uint16, parentDirty bool, depth int) bool { | 
					
						
							|  |  |  | 	var children []node | 
					
						
							|  |  |  | 	var flag nodeFlag | 
					
						
							| 
									
										
										
										
											2016-10-17 16:13:50 +02:00
										 |  |  | 	switch n := n.(type) { | 
					
						
							|  |  |  | 	case *shortNode: | 
					
						
							| 
									
										
										
										
											2016-10-17 23:01:29 +02:00
										 |  |  | 		flag = n.flags | 
					
						
							|  |  |  | 		children = []node{n.Val} | 
					
						
							| 
									
										
										
										
											2016-10-17 16:13:50 +02:00
										 |  |  | 	case *fullNode: | 
					
						
							| 
									
										
										
										
											2016-10-17 23:01:29 +02:00
										 |  |  | 		flag = n.flags | 
					
						
							|  |  |  | 		children = n.Children[:] | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	showerror := func() { | 
					
						
							|  |  |  | 		fmt.Printf("at depth %d node %s", depth, spew.Sdump(n)) | 
					
						
							|  |  |  | 		fmt.Printf("parent: %s", spew.Sdump(parent)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if flag.gen > parentCachegen { | 
					
						
							|  |  |  | 		fmt.Printf("cache invariant violation: %d > %d\n", flag.gen, parentCachegen) | 
					
						
							|  |  |  | 		showerror() | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if depth > 0 && !parentDirty && flag.dirty { | 
					
						
							|  |  |  | 		fmt.Printf("cache invariant violation: child is dirty but parent isn't\n") | 
					
						
							|  |  |  | 		showerror() | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, child := range children { | 
					
						
							|  |  |  | 		if !checkCacheInvariant(child, n, flag.gen, flag.dirty, depth+1) { | 
					
						
							| 
									
										
										
										
											2016-10-17 16:13:50 +02:00
										 |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-09-29 16:51:32 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestRandom(t *testing.T) { | 
					
						
							|  |  |  | 	if err := quick.Check(runRandTest, nil); err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | func BenchmarkGet(b *testing.B)      { benchGet(b, false) } | 
					
						
							|  |  |  | func BenchmarkGetDB(b *testing.B)    { benchGet(b, true) } | 
					
						
							|  |  |  | func BenchmarkUpdateBE(b *testing.B) { benchUpdate(b, binary.BigEndian) } | 
					
						
							|  |  |  | func BenchmarkUpdateLE(b *testing.B) { benchUpdate(b, binary.LittleEndian) } | 
					
						
							|  |  |  | func BenchmarkHashBE(b *testing.B)   { benchHash(b, binary.BigEndian) } | 
					
						
							|  |  |  | func BenchmarkHashLE(b *testing.B)   { benchHash(b, binary.LittleEndian) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const benchElemCount = 20000 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func benchGet(b *testing.B, commit bool) { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	trie := new(Trie) | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | 	if commit { | 
					
						
							| 
									
										
										
										
											2016-10-14 18:04:33 +02:00
										 |  |  | 		_, tmpdb := tempDB() | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 		trie, _ = New(common.Hash{}, tmpdb) | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	k := make([]byte, 32) | 
					
						
							|  |  |  | 	for i := 0; i < benchElemCount; i++ { | 
					
						
							|  |  |  | 		binary.LittleEndian.PutUint64(k, uint64(i)) | 
					
						
							|  |  |  | 		trie.Update(k, k) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	binary.LittleEndian.PutUint64(k, benchElemCount/2) | 
					
						
							|  |  |  | 	if commit { | 
					
						
							|  |  |  | 		trie.Commit() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b.ResetTimer() | 
					
						
							|  |  |  | 	for i := 0; i < b.N; i++ { | 
					
						
							|  |  |  | 		trie.Get(k) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-14 18:04:33 +02:00
										 |  |  | 	b.StopTimer() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if commit { | 
					
						
							|  |  |  | 		ldb := trie.db.(*ethdb.LDBDatabase) | 
					
						
							|  |  |  | 		ldb.Close() | 
					
						
							|  |  |  | 		os.RemoveAll(ldb.Path()) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	trie := newEmpty() | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | 	k := make([]byte, 32) | 
					
						
							|  |  |  | 	for i := 0; i < b.N; i++ { | 
					
						
							|  |  |  | 		e.PutUint64(k, uint64(i)) | 
					
						
							|  |  |  | 		trie.Update(k, k) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return trie | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func benchHash(b *testing.B, e binary.ByteOrder) { | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 	trie := newEmpty() | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | 	k := make([]byte, 32) | 
					
						
							|  |  |  | 	for i := 0; i < benchElemCount; i++ { | 
					
						
							|  |  |  | 		e.PutUint64(k, uint64(i)) | 
					
						
							|  |  |  | 		trie.Update(k, k) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b.ResetTimer() | 
					
						
							|  |  |  | 	for i := 0; i < b.N; i++ { | 
					
						
							|  |  |  | 		trie.Hash() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | func tempDB() (string, Database) { | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | 	dir, err := ioutil.TempDir("", "trie-bench") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(fmt.Sprintf("can't create temporary directory: %v", err)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-19 14:29:19 +02:00
										 |  |  | 	db, err := ethdb.NewLDBDatabase(dir, 256, 0) | 
					
						
							| 
									
										
										
										
											2015-06-30 14:54:37 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(fmt.Sprintf("can't create temporary database: %v", err)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return dir, db | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-06 01:19:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func getString(trie *Trie, k string) []byte { | 
					
						
							|  |  |  | 	return trie.Get([]byte(k)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func updateString(trie *Trie, k, v string) { | 
					
						
							|  |  |  | 	trie.Update([]byte(k), []byte(v)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func deleteString(trie *Trie, k string) { | 
					
						
							|  |  |  | 	trie.Delete([]byte(k)) | 
					
						
							|  |  |  | } |