aboutsummaryrefslogtreecommitdiffstats
path: root/merkle_test.go
blob: c10d17cb71fcf0babaebb511bdc8a30894205469 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main

import (
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"sort"
	"testing"
	"time"
)

const N = 200000

func testMerkle(mtype string) bool {
	m := NewMerkle(mtype, 5, "test.db")
	root := m.Build()
	id := m.Size() / 2
	proof := m.Proof(id)
	v := verify(id, proof)
	if !bytes.Equal(v, root) {
		return false
	}

	id = m.Size()/2 + 1
	proof = m.Proof(id)
	v = verify(id, proof)
	if !bytes.Equal(v, root) {
		return false
	}
	return true
}

func TestMerkle(t *testing.T) {
	tests := []string{"bfs", "post"}
	for _, test := range tests {
		fmt.Println("Testing", test)
		if !testMerkle(test) {
			t.Error(test)
		}
	}
}

var proofs [][][]byte

func TestProofsBFSMerkle(t *testing.T) {
	m := NewBFSMerkle(25, "/mnt/data/bfs.db")
	root := make([]byte, hashSize)
	m.Read(root, 0)
	start := time.Now()
	proofs = m.Proofs(ids)
	elapsed := time.Since(start)
	fmt.Println(elapsed, elapsed.Nanoseconds()/N)
}

func TestProofsPostMerkle(t *testing.T) {
	m := NewPostMerkle(25, "/mnt/data/post.db")
	root := make([]byte, hashSize)
	m.Read(root, 0)
	start := time.Now()
	proofs = m.Proofs(ids)
	elapsed := time.Since(start)
	fmt.Println(elapsed, elapsed.Nanoseconds()/N)
}

func TestBatchProofsBFSMerkle(t *testing.T) {
	m := NewBFSMerkle(25, "/mnt/data/bfs.db")
	root := make([]byte, hashSize)
	m.Read(root, 0)
	start := time.Now()
	proofs = m.BatchProofs(ids)
	elapsed := time.Since(start)
	fmt.Println(elapsed, elapsed.Nanoseconds()/N)
}

var ids []int64

type Int64Slice []int64

func (p Int64Slice) Len() int           { return len(p) }
func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Int64Slice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func TestMain(m *testing.M) {
	ids = make([]int64, N)
	tmp := NewBFSMerkle(25, "/mnt/data/bfs.db")
	for i := 0; i < N; i++ {
		ids[i] = rand.Int63n(tmp.Size()/2) + tmp.Size()/2
	}
	sort.Sort(Int64Slice(ids))
	os.Exit(m.Run())
}