-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
54 lines (44 loc) · 1.08 KB
/
tree.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
package rsmt2d
import (
"crypto/sha256"
"github.com/celestiaorg/merkletree"
)
// TreeConstructorFn creates a fresh Tree instance to be used as the Merkle tree
// inside of rsmt2d.
type TreeConstructorFn = func(axis Axis, index uint) Tree
// SquareIndex contains all information needed to identify the cell that is being
// pushed
type SquareIndex struct {
Axis, Cell uint
}
// Tree wraps Merkle tree implementations to work with rsmt2d
type Tree interface {
Push(data []byte) error
Root() ([]byte, error)
}
var _ Tree = &DefaultTree{}
type DefaultTree struct {
*merkletree.Tree
leaves [][]byte
root []byte
}
func NewDefaultTree(_ Axis, _ uint) Tree {
return &DefaultTree{
Tree: merkletree.New(sha256.New()),
leaves: make([][]byte, 0, 128),
}
}
func (d *DefaultTree) Push(data []byte) error {
// ignore the idx, as this implementation doesn't need that info
d.leaves = append(d.leaves, data)
return nil
}
func (d *DefaultTree) Root() ([]byte, error) {
if d.root == nil {
for _, l := range d.leaves {
d.Tree.Push(l)
}
d.root = d.Tree.Root()
}
return d.root, nil
}