-
Notifications
You must be signed in to change notification settings - Fork 0
/
merkle_test.go
38 lines (29 loc) · 935 Bytes
/
merkle_test.go
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
package main
import (
"fmt"
"math/rand"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
func TestTreap(t *testing.T) {
tree := New()
tree.Insert(crypto.Keccak256([]byte{1}), rand.Uint64())
tree.Insert(crypto.Keccak256([]byte{2}), rand.Uint64())
tree.Insert(crypto.Keccak256([]byte{3}), rand.Uint64())
tree.Insert(crypto.Keccak256([]byte{4}), rand.Uint64())
tree.Insert(crypto.Keccak256([]byte{5}), rand.Uint64())
fmt.Println("Root: ", hexutil.Encode(tree.MerkleRoot()))
path := tree.MerklePath(crypto.Keccak256([]byte{3}))
check := crypto.Keccak256([]byte{3})
fmt.Println("Path: ")
for _, p := range path {
fmt.Println(hexutil.Encode(p))
}
for i := len(path) - 1; i >= 0; i-- {
if len(path[i]) > 0 {
check = hash(check, path[i])
}
}
fmt.Println("Check:", hexutil.Encode(check), "should be equal to root:", hexutil.Encode(tree.MerkleRoot()))
}