-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
trie_node.go
243 lines (209 loc) · 7.31 KB
/
trie_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
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package trie
import (
"fmt"
"sort"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/util"
)
type trieNode struct {
trieKey *trieKey
value []byte
childrenCryptoHashes map[int][]byte
valueUpdated bool
childrenCryptoHashesUpdated map[int]bool
markedForDeletion bool
}
func newTrieNode(key *trieKey, value []byte, updated bool) *trieNode {
return &trieNode{
trieKey: key,
value: value,
childrenCryptoHashes: make(map[int][]byte),
valueUpdated: updated,
childrenCryptoHashesUpdated: make(map[int]bool),
}
}
func (trieNode *trieNode) getLevel() int {
return trieNode.trieKey.getLevel()
}
func (trieNode *trieNode) isRootNode() bool {
return trieNode.trieKey.isRootKey()
}
func (trieNode *trieNode) setChildCryptoHash(index int, childCryptoHash []byte) {
if index >= trieKeyEncoderImpl.getMaxTrieWidth() {
panic(fmt.Errorf("Index for child crypto-hash cannot be greater than [%d]. Tried to access index value [%d]", trieKeyEncoderImpl.getMaxTrieWidth(), index))
}
if childCryptoHash != nil {
trieNode.childrenCryptoHashes[index] = childCryptoHash
}
trieNode.childrenCryptoHashesUpdated[index] = true
}
func (trieNode *trieNode) getParentTrieKey() *trieKey {
return trieNode.trieKey.getParentTrieKey()
}
func (trieNode *trieNode) getParentLevel() int {
return trieNode.trieKey.getParentLevel()
}
func (trieNode *trieNode) getIndexInParent() int {
return trieNode.trieKey.getIndexInParent()
}
func (trieNode *trieNode) mergeMissingAttributesFrom(dbTrieNode *trieNode) {
stateTrieLogger.Debugf("Enter mergeMissingAttributesFrom() baseNode=[%s], mergeNode=[%s]", trieNode, dbTrieNode)
if !trieNode.valueUpdated {
trieNode.value = dbTrieNode.value
}
for k, v := range dbTrieNode.childrenCryptoHashes {
if !trieNode.childrenCryptoHashesUpdated[k] {
trieNode.childrenCryptoHashes[k] = v
}
}
stateTrieLogger.Debugf("Exit mergeMissingAttributesFrom() mergedNode=[%s]", trieNode)
}
func (trieNode *trieNode) computeCryptoHash() []byte {
stateTrieLogger.Debugf("Enter computeCryptoHash() for trieNode [%s]", trieNode)
var cryptoHashContent []byte
if trieNode.containsValue() {
stateTrieLogger.Debugf("Adding value to hash computation for trieNode [%s]", trieNode)
key := trieNode.trieKey.getEncodedBytes()
cryptoHashContent = append(cryptoHashContent, proto.EncodeVarint(uint64(len(key)))...)
cryptoHashContent = append(cryptoHashContent, key...)
cryptoHashContent = append(cryptoHashContent, trieNode.value...)
}
sortedChildrenIndexes := trieNode.getSortedChildrenIndex()
for _, index := range sortedChildrenIndexes {
childCryptoHash := trieNode.childrenCryptoHashes[index]
stateTrieLogger.Debugf("Adding hash [%#v] for child number [%d] to hash computation for trieNode [%s]", childCryptoHash, index, trieNode)
cryptoHashContent = append(cryptoHashContent, childCryptoHash...)
}
if cryptoHashContent == nil {
// node has no associated value and no associated children.
stateTrieLogger.Debugf("Returning nil as hash for trieNode = [%s]. Also, marking this key for deletion.", trieNode)
trieNode.markedForDeletion = true
return nil
}
if !trieNode.containsValue() && trieNode.getNumChildren() == 1 {
// node has no associated value and has a single child. Propagate the child hash up
stateTrieLogger.Debugf("Returning hash as of a single child for trieKey = [%s]", trieNode.trieKey)
return cryptoHashContent
}
stateTrieLogger.Debugf("Recomputing hash for trieKey = [%s]", trieNode)
return util.ComputeCryptoHash(cryptoHashContent)
}
func (trieNode *trieNode) containsValue() bool {
if trieNode.isRootNode() {
return false
}
return trieNode.value != nil
}
func (trieNode *trieNode) marshal() ([]byte, error) {
buffer := proto.NewBuffer([]byte{})
// write value marker explicitly because rocksdb apis convertes a nil into an empty array and protobuf does it other-way around
var valueMarker uint64 = 0 // ignore golint warning. Dropping '= 0' makes assignment less clear
if trieNode.value != nil {
valueMarker = 1
}
err := buffer.EncodeVarint(valueMarker)
if err != nil {
return nil, err
}
if trieNode.value != nil {
// write value
err = buffer.EncodeRawBytes(trieNode.value)
if err != nil {
return nil, err
}
}
//write number of crypto-hashes
numCryptoHashes := trieNode.getNumChildren()
err = buffer.EncodeVarint(uint64(numCryptoHashes))
if err != nil {
return nil, err
}
if numCryptoHashes == 0 {
return buffer.Bytes(), nil
}
for i, cryptoHash := range trieNode.childrenCryptoHashes {
//write crypto-hash Index
err = buffer.EncodeVarint(uint64(i))
if err != nil {
return nil, err
}
// write crypto-hash
err = buffer.EncodeRawBytes(cryptoHash)
if err != nil {
return nil, err
}
}
serializedBytes := buffer.Bytes()
stateTrieLogger.Debugf("Marshalled trieNode [%s]. Serialized bytes size = %d", trieNode.trieKey, len(serializedBytes))
return serializedBytes, nil
}
func unmarshalTrieNode(key *trieKey, serializedContent []byte) (*trieNode, error) {
stateTrieLogger.Debugf("key = [%s], len(serializedContent) = %d", key, len(serializedContent))
trieNode := newTrieNode(key, nil, false)
buffer := proto.NewBuffer(serializedContent)
trieNode.value = unmarshalTrieNodeValueFromBuffer(buffer)
numCryptoHashes, err := buffer.DecodeVarint()
stateTrieLogger.Debugf("numCryptoHashes = [%d]", numCryptoHashes)
if err != nil {
return nil, err
}
for i := uint64(0); i < numCryptoHashes; i++ {
index, err := buffer.DecodeVarint()
if err != nil {
return nil, err
}
cryptoHash, err := buffer.DecodeRawBytes(false)
if err != nil {
return nil, err
}
trieNode.childrenCryptoHashes[int(index)] = cryptoHash
}
stateTrieLogger.Debugf("unmarshalled trieNode = [%s]", trieNode)
return trieNode, nil
}
func unmarshalTrieNodeValue(serializedContent []byte) []byte {
return unmarshalTrieNodeValueFromBuffer(proto.NewBuffer(serializedContent))
}
func unmarshalTrieNodeValueFromBuffer(buffer *proto.Buffer) []byte {
valueMarker, err := buffer.DecodeVarint()
if err != nil {
panic(fmt.Errorf("This error is not excpected: %s", err))
}
if valueMarker == 0 {
return nil
}
value, err := buffer.DecodeRawBytes(false)
if err != nil {
panic(fmt.Errorf("This error is not excpected: %s", err))
}
return value
}
func (trieNode *trieNode) String() string {
return fmt.Sprintf("trieKey=[%s], value=[%#v], Num children hashes=[%#v]",
trieNode.trieKey, trieNode.value, trieNode.getNumChildren())
}
func (trieNode *trieNode) getNumChildren() int {
return len(trieNode.childrenCryptoHashes)
}
func (trieNode *trieNode) getSortedChildrenIndex() []int {
keys := make([]int, trieNode.getNumChildren())
i := 0
for k := range trieNode.childrenCryptoHashes {
keys[i] = k
i++
}
sort.Ints(keys)
return keys
}