forked from ferranbt/fastssz
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tree_test.go
135 lines (118 loc) · 2.99 KB
/
tree_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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package ssz
import (
"bytes"
"encoding/hex"
"testing"
)
func TestTreeFromChunks(t *testing.T) {
chunks := [][]byte{
{0x01, 0x01},
{0x02, 0x02},
{0x03, 0x03},
{0x00, 0x00},
}
r, err := TreeFromChunks(chunks)
if err != nil {
t.Errorf("Failed to construct tree: %v\n", err)
}
for i := 4; i < 8; i++ {
l, err := r.Get(i)
if err != nil {
t.Errorf("Failed getting leaf: %v\n", err)
}
if !bytes.Equal(l.value, chunks[i-4]) {
t.Errorf("Incorrect leaf at index %d\n", i)
}
}
}
func TestHashTree(t *testing.T) {
expectedRootHex := "6621edd5d039d27d1ced186d57691a04903ac79b389187c2d453b5d3cd65180e"
expectedRoot, err := hex.DecodeString(expectedRootHex)
if err != nil {
t.Errorf("Failed to decode hex string\n")
}
chunks := [][]byte{
{0x01, 0x01},
{0x02, 0x02},
{0x03, 0x03},
{0x00, 0x00},
}
r, err := TreeFromChunks(chunks)
if err != nil {
t.Errorf("Failed to construct tree: %v\n", err)
}
h := r.Hash()
if !bytes.Equal(h, expectedRoot) {
t.Errorf("Computed hash is incorrect. Expected %s, got %s\n", expectedRootHex, hex.EncodeToString(h))
}
}
func TestProve(t *testing.T) {
expectedProofHex := []string{
"0000",
"5db57a86b859d1c286b5f1f585048bf8f6b5e626573a8dc728ed5080f6f43e2c",
}
chunks := [][]byte{
{0x01, 0x01},
{0x02, 0x02},
{0x03, 0x03},
{0x00, 0x00},
}
r, err := TreeFromChunks(chunks)
if err != nil {
t.Errorf("Failed to construct tree: %v\n", err)
}
p, err := r.Prove(6)
if err != nil {
t.Errorf("Failed to generate proof: %v\n", err)
}
if p.Index != 6 {
t.Errorf("Proof has invalid index. Expected %d, got %d\n", 6, p.Index)
}
if !bytes.Equal(p.Leaf, chunks[2]) {
t.Errorf("Proof has invalid leaf. Expected %v, got %v\n", chunks[2], p.Leaf)
}
if len(p.Hashes) != len(expectedProofHex) {
t.Errorf("Proof has invalid length. Expected %d, got %d\n", len(expectedProofHex), len(p.Hashes))
}
for i, n := range p.Hashes {
e, err := hex.DecodeString(expectedProofHex[i])
if err != nil {
t.Errorf("Failed to decode hex string: %v\n", err)
}
if !bytes.Equal(e, n) {
t.Errorf("Invalid proof item. Expected %s, got %s\n", expectedProofHex[i], hex.EncodeToString(n))
}
}
}
func TestProveMulti(t *testing.T) {
chunks := [][]byte{
{0x01, 0x01},
{0x02, 0x02},
{0x03, 0x03},
{0x04, 0x04},
}
r, err := TreeFromChunks(chunks)
if err != nil {
t.Errorf("Failed to construct tree: %v\n", err)
}
p, err := r.ProveMulti([]int{6, 7})
if err != nil {
t.Errorf("Failed to generate proof: %v\n", err)
}
if len(p.Hashes) != 1 {
t.Errorf("Incorrect number of hashes in proof. Expected 1, got %d\n", len(p.Hashes))
}
}
func TestGetRequiredIndices(t *testing.T) {
indices := []int{10, 48, 49}
expected := []int{25, 13, 11, 7, 4}
req := getRequiredIndices(indices)
if len(expected) != len(req) {
t.Fatalf("Required indices has wrong length. Expected %d, got %d\n", len(expected), len(req))
}
for i, r := range req {
if r != expected[i] {
t.Errorf("Invalid required index. Expected %d, got %d\n", expected[i], r)
}
}
}