-
Notifications
You must be signed in to change notification settings - Fork 7
/
node.go
115 lines (97 loc) · 2.82 KB
/
node.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
package merkle
import (
"crypto/sha1"
"fmt"
"hash"
)
var (
// DefaultHashMaker is for checksum of blocks and nodes
DefaultHashMaker = func() hash.Hash { return sha1.New() }
)
// HashMaker produces a new has for use in making checksums
type HashMaker func() hash.Hash
// NewNode returns a new Node with the DefaultHashMaker for checksums
func NewNode() *Node {
return NewNodeHash(DefaultHashMaker)
}
// NewNodeHash returns a new Node using the provided crypto.Hash for checksums
func NewNodeHash(h HashMaker) *Node {
return &Node{hash: h}
}
// NewNodeHashBlock returns a new Node using the provided crypto.Hash, and calculates the block's checksum
func NewNodeHashBlock(h HashMaker, b []byte) (*Node, error) {
n := &Node{hash: h}
h1 := n.hash()
if _, err := h1.Write(b); err != nil {
return nil, err
}
n.checksum = h1.Sum(nil)
return n, nil
}
// Node is a fundamental part of the tree.
type Node struct {
hash HashMaker
checksum []byte
Parent, Left, Right *Node
//pos int // XXX maybe keep their order when it is a direct block's hash
}
// IsLeaf indicates this node is for specific block (and has no children)
func (n Node) IsLeaf() bool {
return len(n.checksum) != 0 && (n.Left == nil && n.Right == nil)
}
// Checksum returns the checksum of the block, or the checksum of this nodes
// children (left.checksum + right.checksum)
// If it is a leaf (no children) Node, then the Checksum is of the block of a
// payload. Otherwise, the Checksum is of it's two children's Checksum.
func (n Node) Checksum() ([]byte, error) {
if n.checksum != nil {
return n.checksum, nil
}
if n.Left != nil && n.Right != nil {
// we'll ask our children for their sum and wait til they return
var (
lSumChan = make(chan childSumResponse)
rSumChan = make(chan childSumResponse)
)
go func() {
c, err := n.Left.Checksum()
lSumChan <- childSumResponse{checksum: c, err: err}
}()
go func() {
c, err := n.Right.Checksum()
rSumChan <- childSumResponse{checksum: c, err: err}
}()
h := n.hash()
// First left
lSum := <-lSumChan
if lSum.err != nil {
return nil, lSum.err
}
if _, err := h.Write(lSum.checksum); err != nil {
return nil, err
}
// then right
rSum := <-rSumChan
if rSum.err != nil {
return nil, rSum.err
}
if _, err := h.Write(rSum.checksum); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
return nil, ErrNoChecksumAvailable{node: &n}
}
// ErrNoChecksumAvailable is for nodes that do not have the means to provide
// their checksum
type ErrNoChecksumAvailable struct {
node *Node
}
// Error shows the message with information on the node
func (err ErrNoChecksumAvailable) Error() string {
return fmt.Sprintf("no block or children available to derive checksum from: %#v", *err.node)
}
type childSumResponse struct {
checksum []byte
err error
}