diff options
Diffstat (limited to 'trie')
-rw-r--r-- | trie/secure_trie.go | 36 | ||||
-rw-r--r-- | trie/trie_test.go | 33 |
2 files changed, 68 insertions, 1 deletions
diff --git a/trie/secure_trie.go b/trie/secure_trie.go new file mode 100644 index 000000000..b9fa376b8 --- /dev/null +++ b/trie/secure_trie.go @@ -0,0 +1,36 @@ +package trie + +import "github.com/ethereum/go-ethereum/crypto" + +type SecureTrie struct { + *Trie +} + +func NewSecure(root []byte, backend Backend) *SecureTrie { + return &SecureTrie{New(root, backend)} +} + +func (self *SecureTrie) Update(key, value []byte) Node { + return self.Trie.Update(crypto.Sha3(key), value) +} +func (self *SecureTrie) UpdateString(key, value string) Node { + return self.Update([]byte(key), []byte(value)) +} + +func (self *SecureTrie) Get(key []byte) []byte { + return self.Trie.Get(crypto.Sha3(key)) +} +func (self *SecureTrie) GetString(key string) []byte { + return self.Get([]byte(key)) +} + +func (self *SecureTrie) Delete(key []byte) Node { + return self.Trie.Delete(crypto.Sha3(key)) +} +func (self *SecureTrie) DeleteString(key string) Node { + return self.Delete([]byte(key)) +} + +func (self *SecureTrie) Copy() *SecureTrie { + return &SecureTrie{self.Trie.Copy()} +} diff --git a/trie/trie_test.go b/trie/trie_test.go index 4b185f355..92e0df8e2 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -19,6 +19,10 @@ func NewEmpty() *Trie { return New(nil, make(Db)) } +func NewEmptySecure() *SecureTrie { + return NewSecure(nil, make(Db)) +} + func TestEmptyTrie(t *testing.T) { trie := NewEmpty() res := trie.Hash() @@ -274,7 +278,6 @@ func TestLargeData(t *testing.T) { trie.Update(value2.k, value2.v) vals[string(value.k)] = value vals[string(value2.k)] = value2 - fmt.Println(value, "\n", value2) } it := trie.Iterator() @@ -296,3 +299,31 @@ func TestLargeData(t *testing.T) { } } } + +func TestSecureDelete(t *testing.T) { + trie := NewEmptySecure() + + vals := []struct{ k, v string }{ + {"do", "verb"}, + {"ether", "wookiedoo"}, + {"horse", "stallion"}, + {"shaman", "horse"}, + {"doge", "coin"}, + {"ether", ""}, + {"dog", "puppy"}, + {"shaman", ""}, + } + for _, val := range vals { + if val.v != "" { + trie.UpdateString(val.k, val.v) + } else { + trie.DeleteString(val.k) + } + } + + hash := trie.Hash() + exp := ethutil.Hex2Bytes("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d") + if !bytes.Equal(hash, exp) { + t.Errorf("expected %x got %x", exp, hash) + } +} |