2014-05-19 20:46:41 -07:00
|
|
|
package merkle
|
|
|
|
|
|
|
|
import (
|
2014-07-01 14:50:24 -07:00
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
2014-10-06 13:55:56 -07:00
|
|
|
"time"
|
2014-07-02 16:03:04 -07:00
|
|
|
|
2014-10-06 13:55:56 -07:00
|
|
|
. "github.com/tendermint/tendermint/binary"
|
2014-07-02 16:03:04 -07:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
2014-07-01 14:50:24 -07:00
|
|
|
"github.com/tendermint/tendermint/db"
|
2014-07-02 16:03:04 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
"runtime"
|
|
|
|
"testing"
|
2014-05-19 20:46:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-07-02 16:03:04 -07:00
|
|
|
// TODO: seed rand?
|
|
|
|
}
|
|
|
|
|
2014-09-03 19:21:19 -07:00
|
|
|
func randstr(length int) string {
|
|
|
|
return RandStr(length)
|
2014-05-19 20:46:41 -07:00
|
|
|
}
|
|
|
|
|
2014-05-27 22:31:47 -07:00
|
|
|
func TestUnit(t *testing.T) {
|
2014-05-19 20:46:41 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
// Convenience for a new node
|
|
|
|
N := func(l, r interface{}) *IAVLNode {
|
|
|
|
var left, right *IAVLNode
|
|
|
|
if _, ok := l.(*IAVLNode); ok {
|
|
|
|
left = l.(*IAVLNode)
|
|
|
|
} else {
|
2014-09-03 19:21:19 -07:00
|
|
|
left = NewIAVLNode([]byte{byte(l.(int))}, nil)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
if _, ok := r.(*IAVLNode); ok {
|
|
|
|
right = r.(*IAVLNode)
|
|
|
|
} else {
|
2014-09-03 19:21:19 -07:00
|
|
|
right = NewIAVLNode([]byte{byte(r.(int))}, nil)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
n := &IAVLNode{
|
2014-10-06 01:46:39 -07:00
|
|
|
key: right.lmd(nil).key,
|
|
|
|
leftNode: left,
|
|
|
|
rightNode: right,
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
n.calcHeightAndSize(nil)
|
2014-09-03 19:21:19 -07:00
|
|
|
n.HashWithCount()
|
2014-07-01 14:50:24 -07:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience for simple printing of keys & tree structure
|
|
|
|
var P func(*IAVLNode) string
|
|
|
|
P = func(n *IAVLNode) string {
|
|
|
|
if n.height == 0 {
|
2014-09-03 19:21:19 -07:00
|
|
|
return fmt.Sprintf("%v", n.key[0])
|
2014-07-01 14:50:24 -07:00
|
|
|
} else {
|
2014-10-06 01:46:39 -07:00
|
|
|
return fmt.Sprintf("(%v %v)", P(n.leftNode), P(n.rightNode))
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectHash := func(n2 *IAVLNode, hashCount uint64) {
|
|
|
|
// ensure number of new hash calculations is as expected.
|
2014-09-03 19:21:19 -07:00
|
|
|
hash, count := n2.HashWithCount()
|
2014-07-01 14:50:24 -07:00
|
|
|
if count != hashCount {
|
|
|
|
t.Fatalf("Expected %v new hashes, got %v", hashCount, count)
|
|
|
|
}
|
|
|
|
// nuke hashes and reconstruct hash, ensure it's the same.
|
2014-09-03 19:21:19 -07:00
|
|
|
n2.traverse(nil, func(node *IAVLNode) bool {
|
|
|
|
node.hash = nil
|
2014-07-01 14:50:24 -07:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
// ensure that the new hash after nuking is the same as the old.
|
2014-09-03 19:21:19 -07:00
|
|
|
newHash, _ := n2.HashWithCount()
|
2014-07-01 14:50:24 -07:00
|
|
|
if bytes.Compare(hash, newHash) != 0 {
|
|
|
|
t.Fatalf("Expected hash %v but got %v after nuking", hash, newHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 15:19:07 -07:00
|
|
|
expectSet := func(n *IAVLNode, i int, repr string, hashCount uint64) {
|
2014-09-03 19:21:19 -07:00
|
|
|
n2, updated := n.set(nil, []byte{byte(i)}, nil)
|
2014-07-01 14:50:24 -07:00
|
|
|
// ensure node was added & structure is as expected.
|
|
|
|
if updated == true || P(n2) != repr {
|
|
|
|
t.Fatalf("Adding %v to %v:\nExpected %v\nUnexpectedly got %v updated:%v",
|
|
|
|
i, P(n), repr, P(n2), updated)
|
|
|
|
}
|
|
|
|
// ensure hash calculation requirements
|
|
|
|
expectHash(n2, hashCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectRemove := func(n *IAVLNode, i int, repr string, hashCount uint64) {
|
2014-10-06 00:15:37 -07:00
|
|
|
_, n2, _, value, err := n.remove(nil, []byte{byte(i)})
|
2014-07-01 14:50:24 -07:00
|
|
|
// ensure node was added & structure is as expected.
|
|
|
|
if value != nil || err != nil || P(n2) != repr {
|
|
|
|
t.Fatalf("Removing %v from %v:\nExpected %v\nUnexpectedly got %v value:%v err:%v",
|
|
|
|
i, P(n), repr, P(n2), value, err)
|
|
|
|
}
|
|
|
|
// ensure hash calculation requirements
|
|
|
|
expectHash(n2, hashCount)
|
|
|
|
}
|
|
|
|
|
2014-07-19 15:19:07 -07:00
|
|
|
//////// Test Set cases:
|
2014-07-01 14:50:24 -07:00
|
|
|
|
|
|
|
// Case 1:
|
|
|
|
n1 := N(4, 20)
|
|
|
|
|
2014-07-19 15:19:07 -07:00
|
|
|
expectSet(n1, 8, "((4 8) 20)", 3)
|
|
|
|
expectSet(n1, 25, "(4 (20 25))", 3)
|
2014-07-01 14:50:24 -07:00
|
|
|
|
|
|
|
n2 := N(4, N(20, 25))
|
|
|
|
|
2014-07-19 15:19:07 -07:00
|
|
|
expectSet(n2, 8, "((4 8) (20 25))", 3)
|
|
|
|
expectSet(n2, 30, "((4 20) (25 30))", 4)
|
2014-07-01 14:50:24 -07:00
|
|
|
|
|
|
|
n3 := N(N(1, 2), 6)
|
|
|
|
|
2014-07-19 15:19:07 -07:00
|
|
|
expectSet(n3, 4, "((1 2) (4 6))", 4)
|
|
|
|
expectSet(n3, 8, "((1 2) (6 8))", 3)
|
2014-07-01 14:50:24 -07:00
|
|
|
|
|
|
|
n4 := N(N(1, 2), N(N(5, 6), N(7, 9)))
|
|
|
|
|
2014-07-19 15:19:07 -07:00
|
|
|
expectSet(n4, 8, "(((1 2) (5 6)) ((7 8) 9))", 5)
|
|
|
|
expectSet(n4, 10, "(((1 2) (5 6)) (7 (9 10)))", 5)
|
2014-07-01 14:50:24 -07:00
|
|
|
|
|
|
|
//////// Test Remove cases:
|
|
|
|
|
|
|
|
n10 := N(N(1, 2), 3)
|
|
|
|
|
|
|
|
expectRemove(n10, 2, "(1 3)", 1)
|
|
|
|
expectRemove(n10, 3, "(1 2)", 0)
|
|
|
|
|
|
|
|
n11 := N(N(N(1, 2), 3), N(4, 5))
|
|
|
|
|
|
|
|
expectRemove(n11, 4, "((1 2) (3 5))", 2)
|
|
|
|
expectRemove(n11, 3, "((1 2) (4 5))", 1)
|
2014-05-27 22:31:47 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegration(t *testing.T) {
|
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
type record struct {
|
2014-09-03 19:21:19 -07:00
|
|
|
key string
|
|
|
|
value string
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
records := make([]*record, 400)
|
|
|
|
var tree *IAVLTree = NewIAVLTree(nil)
|
|
|
|
var err error
|
2014-09-03 19:21:19 -07:00
|
|
|
var val []byte
|
2014-07-01 14:50:24 -07:00
|
|
|
var updated bool
|
|
|
|
|
|
|
|
randomRecord := func() *record {
|
|
|
|
return &record{randstr(20), randstr(20)}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range records {
|
|
|
|
r := randomRecord()
|
|
|
|
records[i] = r
|
|
|
|
//t.Log("New record", r)
|
|
|
|
//PrintIAVLNode(tree.root)
|
2014-09-03 19:21:19 -07:00
|
|
|
updated = tree.Set([]byte(r.key), []byte(""))
|
2014-07-01 14:50:24 -07:00
|
|
|
if updated {
|
|
|
|
t.Error("should have not been updated")
|
|
|
|
}
|
2014-09-03 19:21:19 -07:00
|
|
|
updated = tree.Set([]byte(r.key), []byte(r.value))
|
2014-07-01 14:50:24 -07:00
|
|
|
if !updated {
|
|
|
|
t.Error("should have been updated")
|
|
|
|
}
|
|
|
|
if tree.Size() != uint64(i+1) {
|
|
|
|
t.Error("size was wrong", tree.Size(), i+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range records {
|
2014-09-03 19:21:19 -07:00
|
|
|
if has := tree.Has([]byte(r.key)); !has {
|
2014-07-01 14:50:24 -07:00
|
|
|
t.Error("Missing key", r.key)
|
|
|
|
}
|
2014-09-03 19:21:19 -07:00
|
|
|
if has := tree.Has([]byte(randstr(12))); has {
|
2014-07-01 14:50:24 -07:00
|
|
|
t.Error("Table has extra key")
|
|
|
|
}
|
2014-09-03 19:21:19 -07:00
|
|
|
if val := tree.Get([]byte(r.key)); string(val) != r.value {
|
2014-07-01 14:50:24 -07:00
|
|
|
t.Error("wrong value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, x := range records {
|
2014-09-03 19:21:19 -07:00
|
|
|
if val, err = tree.Remove([]byte(x.key)); err != nil {
|
2014-07-01 14:50:24 -07:00
|
|
|
t.Error(err)
|
2014-09-03 19:21:19 -07:00
|
|
|
} else if string(val) != x.value {
|
2014-07-01 14:50:24 -07:00
|
|
|
t.Error("wrong value")
|
|
|
|
}
|
|
|
|
for _, r := range records[i+1:] {
|
2014-09-03 19:21:19 -07:00
|
|
|
if has := tree.Has([]byte(r.key)); !has {
|
2014-07-01 14:50:24 -07:00
|
|
|
t.Error("Missing key", r.key)
|
|
|
|
}
|
2014-09-03 19:21:19 -07:00
|
|
|
if has := tree.Has([]byte(randstr(12))); has {
|
2014-07-01 14:50:24 -07:00
|
|
|
t.Error("Table has extra key")
|
|
|
|
}
|
2014-09-03 19:21:19 -07:00
|
|
|
val := tree.Get([]byte(r.key))
|
|
|
|
if string(val) != r.value {
|
2014-07-01 14:50:24 -07:00
|
|
|
t.Error("wrong value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tree.Size() != uint64(len(records)-(i+1)) {
|
|
|
|
t.Error("size was wrong", tree.Size(), (len(records) - (i + 1)))
|
|
|
|
}
|
|
|
|
}
|
2014-05-23 17:49:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersistence(t *testing.T) {
|
2014-07-01 14:50:24 -07:00
|
|
|
db := db.NewMemDB()
|
|
|
|
|
|
|
|
// Create some random key value pairs
|
2014-09-03 19:21:19 -07:00
|
|
|
records := make(map[string]string)
|
2014-07-01 14:50:24 -07:00
|
|
|
for i := 0; i < 10000; i++ {
|
2014-09-03 19:21:19 -07:00
|
|
|
records[randstr(20)] = randstr(20)
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Construct some tree and save it
|
|
|
|
t1 := NewIAVLTree(db)
|
|
|
|
for key, value := range records {
|
2014-09-03 19:21:19 -07:00
|
|
|
t1.Set([]byte(key), []byte(value))
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
t1.Save()
|
|
|
|
|
2014-09-03 19:21:19 -07:00
|
|
|
hash, _ := t1.HashWithCount()
|
2014-07-01 14:50:24 -07:00
|
|
|
|
|
|
|
// Load a tree
|
2014-10-06 00:15:37 -07:00
|
|
|
t2 := LoadIAVLTreeFromHash(db, hash)
|
2014-07-01 14:50:24 -07:00
|
|
|
for key, value := range records {
|
2014-09-03 19:21:19 -07:00
|
|
|
t2value := t2.Get([]byte(key))
|
|
|
|
if string(t2value) != value {
|
2014-07-01 14:50:24 -07:00
|
|
|
t.Fatalf("Invalid value. Expected %v, got %v", value, t2value)
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 16:24:50 -07:00
|
|
|
}
|
2014-05-24 00:32:50 -07:00
|
|
|
|
2014-10-06 13:55:56 -07:00
|
|
|
func TestTypedTree(t *testing.T) {
|
|
|
|
db := db.NewMemDB()
|
|
|
|
|
|
|
|
// Construct some tree and save it
|
|
|
|
t1 := NewTypedTree(NewIAVLTree(db), BasicCodec, BasicCodec)
|
|
|
|
t1.Set(uint8(1), "uint8(1)")
|
|
|
|
t1.Set(uint16(1), "uint16(1)")
|
|
|
|
t1.Set(uint32(1), "uint32(1)")
|
|
|
|
t1.Set(uint64(1), "uint64(1)")
|
|
|
|
t1.Set("byteslice01", []byte{byte(0x00), byte(0x01)})
|
|
|
|
t1.Set("byteslice23", []byte{byte(0x02), byte(0x03)})
|
|
|
|
t1.Set("time", time.Unix(123, 0))
|
|
|
|
t1.Set("nil", nil)
|
|
|
|
t1Hash := t1.Tree.Save()
|
|
|
|
|
|
|
|
// Reconstruct tree
|
|
|
|
t2 := NewTypedTree(LoadIAVLTreeFromHash(db, t1Hash), BasicCodec, BasicCodec)
|
|
|
|
if t2.Get(uint8(1)).(string) != "uint8(1)" {
|
|
|
|
t.Errorf("Expected string uint8(1)")
|
|
|
|
}
|
|
|
|
if t2.Get(uint16(1)).(string) != "uint16(1)" {
|
|
|
|
t.Errorf("Expected string uint16(1)")
|
|
|
|
}
|
|
|
|
if t2.Get(uint32(1)).(string) != "uint32(1)" {
|
|
|
|
t.Errorf("Expected string uint32(1)")
|
|
|
|
}
|
|
|
|
if t2.Get(uint64(1)).(string) != "uint64(1)" {
|
|
|
|
t.Errorf("Expected string uint64(1)")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(t2.Get("byteslice01").([]byte), []byte{byte(0x00), byte(0x01)}) {
|
|
|
|
t.Errorf("Expected byteslice 0x00 0x01")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(t2.Get("byteslice23").([]byte), []byte{byte(0x02), byte(0x03)}) {
|
|
|
|
t.Errorf("Expected byteslice 0x02 0x03")
|
|
|
|
}
|
|
|
|
if t2.Get("time").(time.Time).Unix() != 123 {
|
|
|
|
t.Errorf("Expected time 123")
|
|
|
|
}
|
|
|
|
if t2.Get("nil") != nil {
|
|
|
|
t.Errorf("Expected nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 00:32:50 -07:00
|
|
|
func BenchmarkHash(b *testing.B) {
|
2014-07-01 14:50:24 -07:00
|
|
|
b.StopTimer()
|
2014-05-24 00:32:50 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
s := randstr(128)
|
2014-05-24 00:32:50 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(s))
|
|
|
|
hasher.Sum(nil)
|
|
|
|
}
|
2014-05-24 00:32:50 -07:00
|
|
|
}
|
2014-05-27 22:31:47 -07:00
|
|
|
|
|
|
|
func BenchmarkImmutableAvlTree(b *testing.B) {
|
2014-07-01 14:50:24 -07:00
|
|
|
b.StopTimer()
|
2014-05-27 22:31:47 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
type record struct {
|
2014-09-03 19:21:19 -07:00
|
|
|
key string
|
|
|
|
value string
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-05-27 22:31:47 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
randomRecord := func() *record {
|
|
|
|
return &record{randstr(32), randstr(32)}
|
|
|
|
}
|
2014-05-27 22:31:47 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
t := NewIAVLTree(nil)
|
|
|
|
for i := 0; i < 1000000; i++ {
|
|
|
|
r := randomRecord()
|
2014-09-03 19:21:19 -07:00
|
|
|
t.Set([]byte(r.key), []byte(r.value))
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-05-27 22:31:47 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
fmt.Println("ok, starting")
|
2014-05-28 01:00:05 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
runtime.GC()
|
2014-06-03 17:03:29 -07:00
|
|
|
|
2014-07-01 14:50:24 -07:00
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
r := randomRecord()
|
2014-09-03 19:21:19 -07:00
|
|
|
t.Set([]byte(r.key), []byte(r.value))
|
|
|
|
t.Remove([]byte(r.key))
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
2014-05-27 22:31:47 -07:00
|
|
|
}
|