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()) }