added tests

This commit is contained in:
zsfelfoldi 2015-11-18 19:52:51 +01:00
parent ab8ae84f08
commit 0ceb181030
4 changed files with 49 additions and 0 deletions

View file

@ -62,6 +62,17 @@ func newARC(c int) *arc {
} }
} }
func (a *arc) Clear() {
a.mutex.Lock()
defer a.mutex.Unlock()
a.p = 0
a.t1 = list.New()
a.b1 = list.New()
a.t2 = list.New()
a.b2 = list.New()
a.cache = make(map[string]*entry, a.c)
}
// Put inserts a new key-value pair into the cache. // Put inserts a new key-value pair into the cache.
// This optimizes future access to this entry (side effect). // This optimizes future access to this entry (side effect).
func (a *arc) Put(key hashNode, value node) bool { func (a *arc) Put(key hashNode, value node) bool {

View file

@ -57,6 +57,12 @@ func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) {
c.Assert(res, checker.DeepEquals, exp) c.Assert(res, checker.DeepEquals, exp)
} }
func (s *TrieEncodingSuite) TestCompactHexEncode(c *checker.C) {
exp := []byte("verb")
res := compactHexEncode([]byte{7, 6, 6, 5, 7, 2, 6, 2, 16})
c.Assert(res, checker.DeepEquals, exp)
}
func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) { func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) {
// odd compact decode // odd compact decode
exp := []byte{1, 2, 3, 4, 5} exp := []byte{1, 2, 3, 4, 5}

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
) )
@ -33,6 +34,33 @@ func TestProof(t *testing.T) {
} }
} }
func TestStoreProof(t *testing.T) {
trie, vals := randomTrie(500)
root := trie.Hash()
mdb, _ := ethdb.NewMemDatabase()
for _, kv := range vals {
proof := trie.Prove(kv.k)
if proof == nil {
t.Fatalf("missing key %x while constructing proof", kv.k)
}
val, err := VerifyProof(root, kv.k, proof)
if err != nil {
t.Fatalf("VerifyProof error for key %x: %v\nraw proof: %x", kv.k, err, proof)
}
if !bytes.Equal(val, kv.v) {
t.Fatalf("VerifyProof returned wrong value for key %x: got %x, want %x", kv.k, val, kv.v)
}
StoreProof(mdb, proof)
}
ClearGlobalCache()
mtrie, _ := New(root, mdb)
for _, kv := range vals {
if !bytes.Equal(mtrie.Get(kv.k), kv.v) {
t.Fatalf("Can't retrieve stored proof")
}
}
}
func TestOneElementProof(t *testing.T) { func TestOneElementProof(t *testing.T) {
trie := new(Trie) trie := new(Trie)
updateString(trie, "k", "v") updateString(trie, "k", "v")

View file

@ -46,6 +46,10 @@ var (
emptyState = crypto.Sha3Hash(nil) emptyState = crypto.Sha3Hash(nil)
) )
func ClearGlobalCache() {
globalCache.Clear()
}
var ErrMissingRoot = errors.New("missing root node") var ErrMissingRoot = errors.New("missing root node")
// OdrAccess is an interface to on-demand network access layer // OdrAccess is an interface to on-demand network access layer