-
Notifications
You must be signed in to change notification settings - Fork 175
/
node.go
261 lines (234 loc) · 7.46 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package node
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/ledger/common"
"github.com/onflow/flow-go/ledger/common/utils"
)
// Node defines an Mtrie node
//
// DEFINITIONS:
// * HEIGHT of a node v in a tree is the number of edges on the longest
// downward path between v and a tree leaf.
//
// Conceptually, an MTrie is a sparse Merkle Trie, which has three different types of nodes:
// * LEAF node: fully defined by a storage path, a key-value pair and a height
// hash is pre-computed, lChild and rChild are nil)
// * INTERIOR node: at least one of lChild or rChild is not nil.
// Height, and Hash value are set; (key-value is nil)
// * ROOT of empty trie node: this is a special case, where the node
// has no children, and no key-value
// Currently, we represent both data structures by Node instances
//
// Nodes are supposed to be used in READ-ONLY fashion. However,
// for performance reasons, we not not copy read.
// TODO: optimized data structures might be able to reduce memory consumption
type Node struct {
lChild *Node // Left Child
rChild *Node // Right Child
height int // height where the Node is at
path ledger.Path // the storage path (leaf nodes only)
payload *ledger.Payload // the payload this node is storing (leaf nodes only)
hashValue []byte // hash value of node (cached)
maxDepth uint16 // captures the longest path from this node to compacted leafs in the subtree
regCount uint64 // number of registers allocated in the subtree
}
// NewNode creates a new Node.
// UNCHECKED requirement: combination of values must conform to
// a valid node type (see documentation of `Node` for details)
func NewNode(height int,
lchild,
rchild *Node,
path ledger.Path,
payload *ledger.Payload,
hashValue []byte,
maxDepth uint16,
regCount uint64) *Node {
var pl *ledger.Payload
var p ledger.Path
if payload != nil {
pl = payload.DeepCopy()
}
if len(path) > 0 {
p = path.DeepCopy()
}
n := &Node{
lChild: lchild,
rChild: rchild,
height: height,
path: p,
payload: pl,
hashValue: hashValue,
maxDepth: maxDepth,
regCount: regCount,
}
return n
}
// NewEmptyTreeRoot creates a compact leaf Node
// UNCHECKED requirement: height must be non-negative
func NewEmptyTreeRoot(height int) *Node {
n := &Node{
lChild: nil,
rChild: nil,
height: height,
path: nil,
payload: nil,
hashValue: nil,
maxDepth: 0,
regCount: 0,
}
n.hashValue = n.computeHash()
return n
}
// NewLeaf creates a compact leaf Node
// UNCHECKED requirement: height must be non-negative
func NewLeaf(path ledger.Path,
payload *ledger.Payload,
height int) *Node {
regCount := uint64(0)
if path != nil {
regCount = uint64(1)
}
n := &Node{
lChild: nil,
rChild: nil,
height: height,
path: path.DeepCopy(),
payload: payload.DeepCopy(),
maxDepth: 0,
regCount: regCount,
}
n.hashValue = n.computeHash()
return n
}
// NewInterimNode creates a new Node with the provided value and no children.
// UNCHECKED requirement: lchild.height and rchild.height must be smaller than height
func NewInterimNode(height int, lchild, rchild *Node) *Node {
var lMaxDepth, rMaxDepth uint16
var lRegCount, rRegCount uint64
if lchild != nil {
lMaxDepth = lchild.maxDepth
lRegCount = lchild.regCount
}
if rchild != nil {
rMaxDepth = rchild.maxDepth
rRegCount = rchild.regCount
}
n := &Node{
lChild: lchild,
rChild: rchild,
height: height,
path: nil,
payload: nil,
maxDepth: utils.MaxUint16(lMaxDepth, rMaxDepth) + uint16(1),
regCount: lRegCount + rRegCount,
}
n.hashValue = n.computeHash()
return n
}
// computeHash computes the hashValue for the given Node
// we kept it this way to stay compatible with the previous versions
func (n *Node) computeHash() []byte {
if n.lChild == nil && n.rChild == nil {
// both ROOT NODE and LEAF NODE have n.lChild == n.rChild == nil
if n.payload != nil {
// LEAF node: defined by key-value pair
return common.ComputeCompactValue(n.path, n.payload, n.height)
}
// ROOT NODE: no children, no key-value pair
return common.GetDefaultHashForHeight(n.height)
}
// this is an INTERIOR node at least one of lChild or rChild is not nil.
h1 := common.GetDefaultHashForHeight(n.height - 1)
if n.lChild != nil {
h1 = n.lChild.Hash()
}
h2 := common.GetDefaultHashForHeight(n.height - 1)
if n.rChild != nil {
h2 = n.rChild.Hash()
}
return common.HashInterNode(h1, h2)
}
func (n *Node) VerifyCachedHash() bool {
if n.lChild != nil {
if !n.lChild.VerifyCachedHash() {
return false
}
}
if n.rChild != nil {
if !n.rChild.VerifyCachedHash() {
return false
}
}
if n.hashValue != nil {
return bytes.Equal(n.hashValue, n.computeHash())
}
return true
}
// Hash returns the Node's hash value.
// Do NOT MODIFY returned slice!
func (n *Node) Hash() []byte { return n.hashValue }
// Height returns the Node's height.
// Per definition, the height of a node v in a tree is the number
// of edges on the longest downward path between v and a tree leaf.
func (n *Node) Height() int { return n.height }
// MaxDepth returns the longest path from this node to compacted leafs in the subtree.
// in contrast to the Height, this value captures compactness of the subtrie.
func (n *Node) MaxDepth() uint16 { return n.maxDepth }
// RegCount returns number of registers allocated in the subtrie of this node.
func (n *Node) RegCount() uint64 { return n.regCount }
// Path returns the the Node's register storage path.
func (n *Node) Path() ledger.Path { return n.path }
// Payload returns the the Node's payload.
// only leaf nodes have children.
// Do NOT MODIFY returned slices!
func (n *Node) Payload() *ledger.Payload { return n.payload }
// LeftChild returns the the Node's left child.
// Only INTERIOR nodes have children.
// Do NOT MODIFY returned Node!
func (n *Node) LeftChild() *Node { return n.lChild }
// RightChild returns the the Node's right child.
// Only INTERIOR nodes have children.
// Do NOT MODIFY returned Node!
func (n *Node) RightChild() *Node { return n.rChild }
// IsLeaf returns true if and only if Node is a LEAF.
func (n *Node) IsLeaf() bool {
// Per definition, a node is a leaf if and only if it has defined path
return n.path != nil && len(n.path) > 0
}
// FmtStr provides formatted string representation of the Node and sub tree
func (n *Node) FmtStr(prefix string, subpath string) string {
right := ""
if n.rChild != nil {
right = fmt.Sprintf("\n%v", n.rChild.FmtStr(prefix+"\t", subpath+"1"))
}
left := ""
if n.lChild != nil {
left = fmt.Sprintf("\n%v", n.lChild.FmtStr(prefix+"\t", subpath+"0"))
}
payloadSize := 0
if n.payload != nil {
payloadSize = n.payload.Size()
}
hashStr := hex.EncodeToString(n.hashValue)
hashStr = hashStr[:3] + "..." + hashStr[len(hashStr)-3:]
return fmt.Sprintf("%v%v: (path:%v, payloadSize:%d hash:%v)[%s] (obj %p) %v %v ", prefix, n.height, n.path, payloadSize, hashStr, subpath, n, left, right)
}
// AllPayloads returns the payload of this node and all payloads of the subtrie
func (n *Node) AllPayloads() []ledger.Payload {
payloads := make([]ledger.Payload, 0)
if n.IsLeaf() {
payloads = append(payloads, *n.Payload())
}
if lChild := n.LeftChild(); lChild != nil {
cp := lChild.AllPayloads()
payloads = append(payloads, cp...)
}
if rChild := n.RightChild(); rChild != nil {
cp := rChild.AllPayloads()
payloads = append(payloads, cp...)
}
return payloads
}