forked from ferranbt/fastssz
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wrapper.go
72 lines (59 loc) · 1.18 KB
/
wrapper.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
package ssz
import "fmt"
type Wrapper struct {
nodes []*Node
}
func (w *Wrapper) Indx() int {
return len(w.nodes)
}
func (w *Wrapper) AddBytes(b []byte) {
w.AddNode(LeafFromBytes(b))
}
func (w *Wrapper) AddUint64(i uint64) {
w.AddNode(LeafFromUint64(i))
}
func (w *Wrapper) AddUint32(i uint32) {
w.AddNode(LeafFromUint32(i))
}
func (w *Wrapper) AddUint16(i uint16) {
w.AddNode(LeafFromUint16(i))
}
func (w *Wrapper) AddUint8(i uint8) {
w.AddNode(LeafFromUint8(i))
}
func (w *Wrapper) AddNode(n *Node) {
if w.nodes == nil {
w.nodes = []*Node{}
}
w.nodes = append(w.nodes, n)
}
func (w *Wrapper) Node() *Node {
if len(w.nodes) != 1 {
fmt.Println(w.nodes)
panic("BAD")
}
return w.nodes[0]
}
func (w *Wrapper) Commit(i int) {
res, err := TreeFromNodes(w.nodes[i:])
if err != nil {
panic(err)
}
// remove the old nodes
w.nodes = w.nodes[:i]
// add the new node
w.AddNode(res)
}
func (w *Wrapper) CommitWithMixin(i, num, limit int) {
res, err := TreeFromNodesWithMixin(w.nodes[i:], num, limit)
if err != nil {
panic(err)
}
// remove the old nodes
w.nodes = w.nodes[:i]
// add the new node
w.AddNode(res)
}
func (w *Wrapper) AddEmpty() {
w.AddNode(EmptyLeaf())
}