Skip to content

Commit 75539a1

Browse files
committed
chore: add comments
1 parent a03e7b9 commit 75539a1

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

merkle.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ func (node *Leaf) Clone() *Leaf {
7676
return &clone
7777
}
7878

79+
// Marshal returns bytes of tree
7980
func (node *Root) Marshal() ([]byte, error) {
8081
return json.Marshal(node)
8182
}
8283

8384
type Leaves []Leaf
8485

86+
// Length returns length of leaves
8587
func (obj *Leaves) Length() int {
8688
if obj == nil {
8789
return 0
@@ -90,10 +92,12 @@ func (obj *Leaves) Length() int {
9092
return len(*obj)
9193
}
9294

95+
// IsEmpty returns if leaves if empty
9396
func (obj *Leaves) IsEmpty() bool {
9497
return obj.Length() == 0
9598
}
9699

100+
// LastLeaf returns the last leaf
97101
func (obj *Leaves) LastLeaf() *Leaf {
98102
if obj == nil {
99103
return nil
@@ -102,6 +106,7 @@ func (obj *Leaves) LastLeaf() *Leaf {
102106
return &(*obj)[obj.Length()-1]
103107
}
104108

109+
// BuildTree build tree by options, returns tree & root
105110
func (obj *Leaves) BuildTree(opt ...OptionFunc) (*Tree, *Root, error) {
106111
if obj == nil || obj.IsEmpty() {
107112
return nil, nil, errors.New("not found leaf")
@@ -134,6 +139,7 @@ func (obj *Leaves) BuildTree(opt ...OptionFunc) (*Tree, *Root, error) {
134139
return tree, root, nil
135140
}
136141

142+
// Hash calc hash of leaves
137143
func (obj *Leaves) Hash(h IHashFunc) error {
138144
for i := 0; i < obj.Length(); i++ {
139145
digest, err := h.Hash((*obj)[i].Payload)
@@ -147,6 +153,7 @@ func (obj *Leaves) Hash(h IHashFunc) error {
147153
return nil
148154
}
149155

156+
// initTree init a tree
150157
func (obj *Leaves) initTree() (*Tree, error) {
151158
if obj.Length() == 0 {
152159
return nil, errors.New("not found")
@@ -164,6 +171,7 @@ func (obj *Leaves) initTree() (*Tree, error) {
164171
return &tree, nil
165172
}
166173

174+
// buildBranch build branch, fill the tree & returns root
167175
func (obj *Leaves) buildBranch(nodes []Node, tree *Tree, h IHashFunc) (*Root, error) {
168176
var branches []Node
169177
var hashSet []Hash

0 commit comments

Comments
 (0)