forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_impl.go
302 lines (272 loc) · 10.3 KB
/
state_impl.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
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 buckettree
import (
"bytes"
"github.com/hyperledger/fabric/core/db"
"github.com/hyperledger/fabric/core/ledger/statemgmt"
"github.com/op/go-logging"
"github.com/tecbot/gorocksdb"
)
var logger = logging.MustGetLogger("buckettree")
// StateImpl - implements the interface - 'statemgmt.HashableState'
type StateImpl struct {
dataNodesDelta *dataNodesDelta
bucketTreeDelta *bucketTreeDelta
persistedStateHash []byte
lastComputedCryptoHash []byte
recomputeCryptoHash bool
bucketCache *bucketCache
}
// NewStateImpl constructs a new StateImpl
func NewStateImpl() *StateImpl {
return &StateImpl{}
}
// Initialize - method implementation for interface 'statemgmt.HashableState'
func (stateImpl *StateImpl) Initialize(configs map[string]interface{}) error {
initConfig(configs)
rootBucketNode, err := fetchBucketNodeFromDB(constructRootBucketKey())
if err != nil {
return err
}
if rootBucketNode != nil {
stateImpl.persistedStateHash = rootBucketNode.computeCryptoHash()
stateImpl.lastComputedCryptoHash = stateImpl.persistedStateHash
}
bucketCacheMaxSize, ok := configs["bucketCacheSize"].(int)
if !ok {
bucketCacheMaxSize = defaultBucketCacheMaxSize
}
stateImpl.bucketCache = newBucketCache(bucketCacheMaxSize)
stateImpl.bucketCache.loadAllBucketNodesFromDB()
return nil
}
// Get - method implementation for interface 'statemgmt.HashableState'
func (stateImpl *StateImpl) Get(chaincodeID string, key string) ([]byte, error) {
dataKey := newDataKey(chaincodeID, key)
dataNode, err := fetchDataNodeFromDB(dataKey)
if err != nil {
return nil, err
}
if dataNode == nil {
return nil, nil
}
return dataNode.value, nil
}
// PrepareWorkingSet - method implementation for interface 'statemgmt.HashableState'
func (stateImpl *StateImpl) PrepareWorkingSet(stateDelta *statemgmt.StateDelta) error {
logger.Debug("Enter - PrepareWorkingSet()")
if stateDelta.IsEmpty() {
logger.Debug("Ignoring working-set as it is empty")
return nil
}
stateImpl.dataNodesDelta = newDataNodesDelta(stateDelta)
stateImpl.bucketTreeDelta = newBucketTreeDelta()
stateImpl.recomputeCryptoHash = true
return nil
}
// ClearWorkingSet - method implementation for interface 'statemgmt.HashableState'
func (stateImpl *StateImpl) ClearWorkingSet(changesPersisted bool) {
logger.Debug("Enter - ClearWorkingSet()")
if changesPersisted {
stateImpl.persistedStateHash = stateImpl.lastComputedCryptoHash
stateImpl.updateBucketCache()
} else {
stateImpl.lastComputedCryptoHash = stateImpl.persistedStateHash
}
stateImpl.dataNodesDelta = nil
stateImpl.bucketTreeDelta = nil
stateImpl.recomputeCryptoHash = false
}
// ComputeCryptoHash - method implementation for interface 'statemgmt.HashableState'
func (stateImpl *StateImpl) ComputeCryptoHash() ([]byte, error) {
logger.Debug("Enter - ComputeCryptoHash()")
if stateImpl.recomputeCryptoHash {
logger.Debug("Recomputing crypto-hash...")
err := stateImpl.processDataNodeDelta()
if err != nil {
return nil, err
}
err = stateImpl.processBucketTreeDelta()
if err != nil {
return nil, err
}
stateImpl.lastComputedCryptoHash = stateImpl.computeRootNodeCryptoHash()
stateImpl.recomputeCryptoHash = false
} else {
logger.Debug("Returing existing crypto-hash as recomputation not required")
}
return stateImpl.lastComputedCryptoHash, nil
}
func (stateImpl *StateImpl) processDataNodeDelta() error {
afftectedBuckets := stateImpl.dataNodesDelta.getAffectedBuckets()
for _, bucketKey := range afftectedBuckets {
updatedDataNodes := stateImpl.dataNodesDelta.getSortedDataNodesFor(bucketKey)
existingDataNodes, err := fetchDataNodesFromDBFor(bucketKey)
if err != nil {
return err
}
cryptoHashForBucket := computeDataNodesCryptoHash(bucketKey, updatedDataNodes, existingDataNodes)
logger.Debugf("Crypto-hash for lowest-level bucket [%s] is [%x]", bucketKey, cryptoHashForBucket)
parentBucket := stateImpl.bucketTreeDelta.getOrCreateBucketNode(bucketKey.getParentKey())
parentBucket.setChildCryptoHash(bucketKey, cryptoHashForBucket)
}
return nil
}
func (stateImpl *StateImpl) processBucketTreeDelta() error {
secondLastLevel := conf.getLowestLevel() - 1
for level := secondLastLevel; level >= 0; level-- {
bucketNodes := stateImpl.bucketTreeDelta.getBucketNodesAt(level)
logger.Debugf("Bucket tree delta. Number of buckets at level [%d] are [%d]", level, len(bucketNodes))
for _, bucketNode := range bucketNodes {
logger.Debugf("bucketNode in tree-delta [%s]", bucketNode)
dbBucketNode, err := stateImpl.bucketCache.get(*bucketNode.bucketKey)
logger.Debugf("bucket node from db [%s]", dbBucketNode)
if err != nil {
return err
}
if dbBucketNode != nil {
bucketNode.mergeBucketNode(dbBucketNode)
logger.Debugf("After merge... bucketNode in tree-delta [%s]", bucketNode)
}
if level == 0 {
return nil
}
logger.Debugf("Computing cryptoHash for bucket [%s]", bucketNode)
cryptoHash := bucketNode.computeCryptoHash()
logger.Debugf("cryptoHash for bucket [%s] is [%x]", bucketNode, cryptoHash)
parentBucket := stateImpl.bucketTreeDelta.getOrCreateBucketNode(bucketNode.bucketKey.getParentKey())
parentBucket.setChildCryptoHash(bucketNode.bucketKey, cryptoHash)
}
}
return nil
}
func (stateImpl *StateImpl) computeRootNodeCryptoHash() []byte {
return stateImpl.bucketTreeDelta.getRootNode().computeCryptoHash()
}
func computeDataNodesCryptoHash(bucketKey *bucketKey, updatedNodes dataNodes, existingNodes dataNodes) []byte {
logger.Debugf("Computing crypto-hash for bucket [%s]. numUpdatedNodes=[%d], numExistingNodes=[%d]", bucketKey, len(updatedNodes), len(existingNodes))
bucketHashCalculator := newBucketHashCalculator(bucketKey)
i := 0
j := 0
for i < len(updatedNodes) && j < len(existingNodes) {
updatedNode := updatedNodes[i]
existingNode := existingNodes[j]
c := bytes.Compare(updatedNode.dataKey.compositeKey, existingNode.dataKey.compositeKey)
var nextNode *dataNode
switch c {
case -1:
nextNode = updatedNode
i++
case 0:
nextNode = updatedNode
i++
j++
case 1:
nextNode = existingNode
j++
}
if !nextNode.isDelete() {
bucketHashCalculator.addNextNode(nextNode)
}
}
var remainingNodes dataNodes
if i < len(updatedNodes) {
remainingNodes = updatedNodes[i:]
} else if j < len(existingNodes) {
remainingNodes = existingNodes[j:]
}
for _, remainingNode := range remainingNodes {
if !remainingNode.isDelete() {
bucketHashCalculator.addNextNode(remainingNode)
}
}
return bucketHashCalculator.computeCryptoHash()
}
// AddChangesForPersistence - method implementation for interface 'statemgmt.HashableState'
func (stateImpl *StateImpl) AddChangesForPersistence(writeBatch *gorocksdb.WriteBatch) error {
if stateImpl.dataNodesDelta == nil {
return nil
}
if stateImpl.recomputeCryptoHash {
_, err := stateImpl.ComputeCryptoHash()
if err != nil {
return nil
}
}
stateImpl.addDataNodeChangesForPersistence(writeBatch)
stateImpl.addBucketNodeChangesForPersistence(writeBatch)
return nil
}
func (stateImpl *StateImpl) addDataNodeChangesForPersistence(writeBatch *gorocksdb.WriteBatch) {
openchainDB := db.GetDBHandle()
affectedBuckets := stateImpl.dataNodesDelta.getAffectedBuckets()
for _, affectedBucket := range affectedBuckets {
dataNodes := stateImpl.dataNodesDelta.getSortedDataNodesFor(affectedBucket)
for _, dataNode := range dataNodes {
if dataNode.isDelete() {
logger.Debugf("Deleting data node key = %#v", dataNode.dataKey)
writeBatch.DeleteCF(openchainDB.StateCF, dataNode.dataKey.getEncodedBytes())
} else {
logger.Debugf("Adding data node with value = %#v", dataNode.value)
writeBatch.PutCF(openchainDB.StateCF, dataNode.dataKey.getEncodedBytes(), dataNode.value)
}
}
}
}
func (stateImpl *StateImpl) addBucketNodeChangesForPersistence(writeBatch *gorocksdb.WriteBatch) {
openchainDB := db.GetDBHandle()
secondLastLevel := conf.getLowestLevel() - 1
for level := secondLastLevel; level >= 0; level-- {
bucketNodes := stateImpl.bucketTreeDelta.getBucketNodesAt(level)
for _, bucketNode := range bucketNodes {
if bucketNode.markedForDeletion {
writeBatch.DeleteCF(openchainDB.StateCF, bucketNode.bucketKey.getEncodedBytes())
} else {
writeBatch.PutCF(openchainDB.StateCF, bucketNode.bucketKey.getEncodedBytes(), bucketNode.marshal())
}
}
}
}
func (stateImpl *StateImpl) updateBucketCache() {
if stateImpl.bucketTreeDelta == nil || stateImpl.bucketTreeDelta.isEmpty() {
return
}
stateImpl.bucketCache.lock.Lock()
defer stateImpl.bucketCache.lock.Unlock()
secondLastLevel := conf.getLowestLevel() - 1
for level := 0; level <= secondLastLevel; level++ {
bucketNodes := stateImpl.bucketTreeDelta.getBucketNodesAt(level)
for _, bucketNode := range bucketNodes {
key := *bucketNode.bucketKey
if bucketNode.markedForDeletion {
stateImpl.bucketCache.removeWithoutLock(key)
} else {
stateImpl.bucketCache.putWithoutLock(key, bucketNode)
}
}
}
}
// PerfHintKeyChanged - method implementation for interface 'statemgmt.HashableState'
func (stateImpl *StateImpl) PerfHintKeyChanged(chaincodeID string, key string) {
// We can create a cache. Pull all the keys for the bucket (to which given key belongs) in a separate thread
// This prefetching can help making method 'ComputeCryptoHash' faster.
}
// GetStateSnapshotIterator - method implementation for interface 'statemgmt.HashableState'
func (stateImpl *StateImpl) GetStateSnapshotIterator(snapshot *gorocksdb.Snapshot) (statemgmt.StateSnapshotIterator, error) {
return newStateSnapshotIterator(snapshot)
}
// GetRangeScanIterator - method implementation for interface 'statemgmt.HashableState'
func (stateImpl *StateImpl) GetRangeScanIterator(chaincodeID string, startKey string, endKey string) (statemgmt.RangeScanIterator, error) {
return newRangeScanIterator(chaincodeID, startKey, endKey)
}