This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rwset_builder.go
365 lines (321 loc) · 12.3 KB
/
rwset_builder.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
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 rwsetutil
import (
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/protos/ledger/rwset"
"github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset"
)
var logger = flogging.MustGetLogger("rwsetutil")
// RWSetBuilder helps building the read-write set
type RWSetBuilder struct {
pubRwBuilderMap map[string]*nsPubRwBuilder
pvtRwBuilderMap map[string]*nsPvtRwBuilder
}
type nsPubRwBuilder struct {
namespace string
readMap map[string]*kvrwset.KVRead //for mvcc validation
writeMap map[string]*kvrwset.KVWrite
metadataWriteMap map[string]*kvrwset.KVMetadataWrite
rangeQueriesMap map[rangeQueryKey]*kvrwset.RangeQueryInfo //for phantom read validation
rangeQueriesKeys []rangeQueryKey
collHashRwBuilder map[string]*collHashRwBuilder
}
type collHashRwBuilder struct {
collName string
readMap map[string]*kvrwset.KVReadHash
writeMap map[string]*kvrwset.KVWriteHash
metadataWriteMap map[string]*kvrwset.KVMetadataWriteHash
pvtDataHash []byte
}
type nsPvtRwBuilder struct {
namespace string
collPvtRwBuilders map[string]*collPvtRwBuilder
}
type collPvtRwBuilder struct {
collectionName string
writeMap map[string]*kvrwset.KVWrite
metadataWriteMap map[string]*kvrwset.KVMetadataWrite
}
type rangeQueryKey struct {
startKey string
endKey string
itrExhausted bool
}
// NewRWSetBuilder constructs a new instance of RWSetBuilder
func NewRWSetBuilder() *RWSetBuilder {
return &RWSetBuilder{make(map[string]*nsPubRwBuilder), make(map[string]*nsPvtRwBuilder)}
}
// AddToReadSet adds a key and corresponding version to the read-set
func (b *RWSetBuilder) AddToReadSet(ns string, key string, version *version.Height) {
nsPubRwBuilder := b.getOrCreateNsPubRwBuilder(ns)
nsPubRwBuilder.readMap[key] = NewKVRead(key, version)
}
// AddToWriteSet adds a key and value to the write-set
func (b *RWSetBuilder) AddToWriteSet(ns string, key string, value []byte) {
nsPubRwBuilder := b.getOrCreateNsPubRwBuilder(ns)
nsPubRwBuilder.writeMap[key] = newKVWrite(key, value)
}
// AddToMetadataWriteSet adds a metadata to a key in the write-set
// A nil/empty-map for 'metadata' parameter indicates the delete of the metadata
func (b *RWSetBuilder) AddToMetadataWriteSet(ns, key string, metadata map[string][]byte) {
b.getOrCreateNsPubRwBuilder(ns).
metadataWriteMap[key] = mapToMetadataWrite(key, metadata)
}
// AddToRangeQuerySet adds a range query info for performing phantom read validation
func (b *RWSetBuilder) AddToRangeQuerySet(ns string, rqi *kvrwset.RangeQueryInfo) {
nsPubRwBuilder := b.getOrCreateNsPubRwBuilder(ns)
key := rangeQueryKey{rqi.StartKey, rqi.EndKey, rqi.ItrExhausted}
_, ok := nsPubRwBuilder.rangeQueriesMap[key]
if !ok {
nsPubRwBuilder.rangeQueriesMap[key] = rqi
nsPubRwBuilder.rangeQueriesKeys = append(nsPubRwBuilder.rangeQueriesKeys, key)
}
}
// AddToHashedReadSet adds a key and corresponding version to the hashed read-set
func (b *RWSetBuilder) AddToHashedReadSet(ns string, coll string, key string, version *version.Height) {
kvReadHash := newPvtKVReadHash(key, version)
b.getOrCreateCollHashedRwBuilder(ns, coll).readMap[key] = kvReadHash
}
// AddToPvtAndHashedWriteSet adds a key and value to the private and hashed write-set
func (b *RWSetBuilder) AddToPvtAndHashedWriteSet(ns string, coll string, key string, value []byte) {
kvWrite, kvWriteHash := newPvtKVWriteAndHash(key, value)
b.getOrCreateCollPvtRwBuilder(ns, coll).writeMap[key] = kvWrite
b.getOrCreateCollHashedRwBuilder(ns, coll).writeMap[key] = kvWriteHash
}
// AddToHashedMetadataWriteSet adds a metadata to a key in the hashed write-set
func (b *RWSetBuilder) AddToHashedMetadataWriteSet(ns, coll, key string, metadata map[string][]byte) {
// pvt write set just need the key; not the entire metadata. The metadata is stored only
// by the hashed key. Pvt write-set need to know the key for handling a special case where only
// metadata is updated so, the version of the key present in the pvt data should be incremented
b.getOrCreateCollPvtRwBuilder(ns, coll).
metadataWriteMap[key] = &kvrwset.KVMetadataWrite{Key: key, Entries: nil}
b.getOrCreateCollHashedRwBuilder(ns, coll).
metadataWriteMap[key] = mapToMetadataWriteHash(key, metadata)
}
// GetTxSimulationResults returns the proto bytes of public rwset
// (public data + hashes of private data) and the private rwset for the transaction
func (b *RWSetBuilder) GetTxSimulationResults() (*ledger.TxSimulationResults, error) {
pvtData := b.getTxPvtReadWriteSet()
var err error
var pubDataProto *rwset.TxReadWriteSet
var pvtDataProto *rwset.TxPvtReadWriteSet
// Populate the collection-level hashes into pub rwset and compute the proto bytes for pvt rwset
if pvtData != nil {
if pvtDataProto, err = pvtData.toProtoMsg(); err != nil {
return nil, err
}
for _, ns := range pvtDataProto.NsPvtRwset {
for _, coll := range ns.CollectionPvtRwset {
b.setPvtCollectionHash(ns.Namespace, coll.CollectionName, coll.Rwset)
}
}
}
// Compute the proto bytes for pub rwset
pubSet := b.GetTxReadWriteSet()
if pubSet != nil {
if pubDataProto, err = b.GetTxReadWriteSet().toProtoMsg(); err != nil {
return nil, err
}
}
return &ledger.TxSimulationResults{
PubSimulationResults: pubDataProto,
PvtSimulationResults: pvtDataProto,
}, nil
}
func (b *RWSetBuilder) setPvtCollectionHash(ns string, coll string, pvtDataProto []byte) {
collHashedBuilder := b.getOrCreateCollHashedRwBuilder(ns, coll)
collHashedBuilder.pvtDataHash = util.ComputeHash(pvtDataProto)
}
// GetTxReadWriteSet returns the read-write set
// TODO make this function private once txmgr starts using new function `GetTxSimulationResults` introduced here
func (b *RWSetBuilder) GetTxReadWriteSet() *TxRwSet {
sortedNsPubBuilders := []*nsPubRwBuilder{}
util.GetValuesBySortedKeys(&(b.pubRwBuilderMap), &sortedNsPubBuilders)
var nsPubRwSets []*NsRwSet
for _, nsPubRwBuilder := range sortedNsPubBuilders {
nsPubRwSets = append(nsPubRwSets, nsPubRwBuilder.build())
}
return &TxRwSet{NsRwSets: nsPubRwSets}
}
// getTxPvtReadWriteSet returns the private read-write set
func (b *RWSetBuilder) getTxPvtReadWriteSet() *TxPvtRwSet {
sortedNsPvtBuilders := []*nsPvtRwBuilder{}
util.GetValuesBySortedKeys(&(b.pvtRwBuilderMap), &sortedNsPvtBuilders)
var nsPvtRwSets []*NsPvtRwSet
for _, nsPvtRwBuilder := range sortedNsPvtBuilders {
nsPvtRwSets = append(nsPvtRwSets, nsPvtRwBuilder.build())
}
if len(nsPvtRwSets) == 0 {
return nil
}
return &TxPvtRwSet{NsPvtRwSet: nsPvtRwSets}
}
func (b *nsPubRwBuilder) build() *NsRwSet {
var readSet []*kvrwset.KVRead
var writeSet []*kvrwset.KVWrite
var metadataWriteSet []*kvrwset.KVMetadataWrite
var rangeQueriesInfo []*kvrwset.RangeQueryInfo
var collHashedRwSet []*CollHashedRwSet
//add read set
util.GetValuesBySortedKeys(&(b.readMap), &readSet)
//add write set
util.GetValuesBySortedKeys(&(b.writeMap), &writeSet)
util.GetValuesBySortedKeys(&(b.metadataWriteMap), &metadataWriteSet)
//add range query info
for _, key := range b.rangeQueriesKeys {
rangeQueriesInfo = append(rangeQueriesInfo, b.rangeQueriesMap[key])
}
// add hashed rws for private collections
sortedCollBuilders := []*collHashRwBuilder{}
util.GetValuesBySortedKeys(&(b.collHashRwBuilder), &sortedCollBuilders)
for _, collBuilder := range sortedCollBuilders {
collHashedRwSet = append(collHashedRwSet, collBuilder.build())
}
return &NsRwSet{
NameSpace: b.namespace,
KvRwSet: &kvrwset.KVRWSet{
Reads: readSet,
Writes: writeSet,
MetadataWrites: metadataWriteSet,
RangeQueriesInfo: rangeQueriesInfo,
},
CollHashedRwSets: collHashedRwSet,
}
}
func (b *nsPvtRwBuilder) build() *NsPvtRwSet {
sortedCollBuilders := []*collPvtRwBuilder{}
util.GetValuesBySortedKeys(&(b.collPvtRwBuilders), &sortedCollBuilders)
var collPvtRwSets []*CollPvtRwSet
for _, collBuilder := range sortedCollBuilders {
collPvtRwSets = append(collPvtRwSets, collBuilder.build())
}
return &NsPvtRwSet{NameSpace: b.namespace, CollPvtRwSets: collPvtRwSets}
}
func (b *collHashRwBuilder) build() *CollHashedRwSet {
var readSet []*kvrwset.KVReadHash
var writeSet []*kvrwset.KVWriteHash
var metadataWriteSet []*kvrwset.KVMetadataWriteHash
util.GetValuesBySortedKeys(&(b.readMap), &readSet)
util.GetValuesBySortedKeys(&(b.writeMap), &writeSet)
util.GetValuesBySortedKeys(&(b.metadataWriteMap), &metadataWriteSet)
return &CollHashedRwSet{
CollectionName: b.collName,
HashedRwSet: &kvrwset.HashedRWSet{
HashedReads: readSet,
HashedWrites: writeSet,
MetadataWrites: metadataWriteSet,
},
PvtRwSetHash: b.pvtDataHash,
}
}
func (b *collPvtRwBuilder) build() *CollPvtRwSet {
var writeSet []*kvrwset.KVWrite
var metadataWriteSet []*kvrwset.KVMetadataWrite
util.GetValuesBySortedKeys(&(b.writeMap), &writeSet)
util.GetValuesBySortedKeys(&(b.metadataWriteMap), &metadataWriteSet)
return &CollPvtRwSet{
CollectionName: b.collectionName,
KvRwSet: &kvrwset.KVRWSet{
Writes: writeSet,
MetadataWrites: metadataWriteSet,
},
}
}
func (b *RWSetBuilder) getOrCreateNsPubRwBuilder(ns string) *nsPubRwBuilder {
nsPubRwBuilder, ok := b.pubRwBuilderMap[ns]
if !ok {
nsPubRwBuilder = newNsPubRwBuilder(ns)
b.pubRwBuilderMap[ns] = nsPubRwBuilder
}
return nsPubRwBuilder
}
func (b *RWSetBuilder) getOrCreateNsPvtRwBuilder(ns string) *nsPvtRwBuilder {
nsPvtRwBuilder, ok := b.pvtRwBuilderMap[ns]
if !ok {
nsPvtRwBuilder = newNsPvtRwBuilder(ns)
b.pvtRwBuilderMap[ns] = nsPvtRwBuilder
}
return nsPvtRwBuilder
}
func (b *RWSetBuilder) getOrCreateCollHashedRwBuilder(ns string, coll string) *collHashRwBuilder {
nsPubRwBuilder := b.getOrCreateNsPubRwBuilder(ns)
collHashRwBuilder, ok := nsPubRwBuilder.collHashRwBuilder[coll]
if !ok {
collHashRwBuilder = newCollHashRwBuilder(coll)
nsPubRwBuilder.collHashRwBuilder[coll] = collHashRwBuilder
}
return collHashRwBuilder
}
func (b *RWSetBuilder) getOrCreateCollPvtRwBuilder(ns string, coll string) *collPvtRwBuilder {
nsPvtRwBuilder := b.getOrCreateNsPvtRwBuilder(ns)
collPvtRwBuilder, ok := nsPvtRwBuilder.collPvtRwBuilders[coll]
if !ok {
collPvtRwBuilder = newCollPvtRwBuilder(coll)
nsPvtRwBuilder.collPvtRwBuilders[coll] = collPvtRwBuilder
}
return collPvtRwBuilder
}
func newNsPubRwBuilder(namespace string) *nsPubRwBuilder {
return &nsPubRwBuilder{
namespace,
make(map[string]*kvrwset.KVRead),
make(map[string]*kvrwset.KVWrite),
make(map[string]*kvrwset.KVMetadataWrite),
make(map[rangeQueryKey]*kvrwset.RangeQueryInfo),
nil,
make(map[string]*collHashRwBuilder),
}
}
func newNsPvtRwBuilder(namespace string) *nsPvtRwBuilder {
return &nsPvtRwBuilder{namespace, make(map[string]*collPvtRwBuilder)}
}
func newCollHashRwBuilder(collName string) *collHashRwBuilder {
return &collHashRwBuilder{
collName,
make(map[string]*kvrwset.KVReadHash),
make(map[string]*kvrwset.KVWriteHash),
make(map[string]*kvrwset.KVMetadataWriteHash),
nil,
}
}
func newCollPvtRwBuilder(collName string) *collPvtRwBuilder {
return &collPvtRwBuilder{
collName,
make(map[string]*kvrwset.KVWrite),
make(map[string]*kvrwset.KVMetadataWrite),
}
}
func mapToMetadataWrite(key string, m map[string][]byte) *kvrwset.KVMetadataWrite {
proto := &kvrwset.KVMetadataWrite{Key: key}
names := util.GetSortedKeys(m)
for _, name := range names {
proto.Entries = append(proto.Entries,
&kvrwset.KVMetadataEntry{Name: name, Value: m[name]},
)
}
return proto
}
func mapToMetadataWriteHash(key string, m map[string][]byte) *kvrwset.KVMetadataWriteHash {
proto := &kvrwset.KVMetadataWriteHash{KeyHash: util.ComputeStringHash(key)}
names := util.GetSortedKeys(m)
for _, name := range names {
proto.Entries = append(proto.Entries,
&kvrwset.KVMetadataEntry{Name: name, Value: m[name]},
)
}
return proto
}