diff --git a/trie/ctrie/ctrie.go b/trie/ctrie/ctrie.go index f146b36..fed475d 100644 --- a/trie/ctrie/ctrie.go +++ b/trie/ctrie/ctrie.go @@ -229,7 +229,7 @@ func (l *lNode) lookup(e *Entry) (interface{}, bool) { // inserted creates a new L-node with the added entry. func (l *lNode) inserted(entry *Entry) *lNode { - return &lNode{l.Add(&sNode{entry})} + return &lNode{l.removed(entry).Add(&sNode{entry})} } // removed creates a new L-node with the entry removed. diff --git a/trie/ctrie/ctrie_test.go b/trie/ctrie/ctrie_test.go index e62701e..e4dd4a2 100644 --- a/trie/ctrie/ctrie_test.go +++ b/trie/ctrie/ctrie_test.go @@ -429,6 +429,51 @@ func TestClear(t *testing.T) { assert.Equal(uint(10), snapshot.Size()) } +type fakehash struct{} + +func (h *fakehash) Sum32() uint32 { + return 42 +} + +func (h *fakehash) Sum(b []byte) []byte { + return nil +} + +func (h *fakehash) Size() int { + return 0 +} + +func (h *fakehash) BlockSize() int { + return 0 +} + +func (h *fakehash) Reset() { + +} + +func (h *fakehash) Write(b []byte) (int, error) { + return 0, nil +} + +func factory() hash.Hash32 { + return &fakehash{} +} + +func TestHashCollision(t *testing.T) { + trie := New(factory) + trie.Insert([]byte("foobar"), 1) + trie.Insert([]byte("zogzog"), 2) + trie.Insert([]byte("foobar"), 3) + val, exists := trie.Lookup([]byte("foobar")) + assert.True(t, exists) + assert.Equal(t, 3, val) + + trie.Remove([]byte("foobar")) + + _, exists = trie.Lookup([]byte("foobar")) + assert.False(t, exists) +} + func BenchmarkInsert(b *testing.B) { ctrie := New(nil) b.ResetTimer()