forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwset_builder.go
310 lines (271 loc) · 9.97 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
/*
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
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
pvtDataHash []byte
}
type nsPvtRwBuilder struct {
namespace string
collPvtRwBuilders map[string]*collPvtRwBuilder
}
type collPvtRwBuilder struct {
collectionName string
writeMap map[string]*kvrwset.KVWrite
}
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)
}
// 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) error {
kvReadHash, err := newPvtKVReadHash(key, version)
if err != nil {
return err
}
b.getOrCreateCollHashedRwBuilder(ns, coll).readMap[key] = kvReadHash
return nil
}
// 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) error {
kvWrite, kvWriteHash, err := newPvtKVWriteAndHash(key, value)
if err != nil {
return err
}
b.getOrCreateCollPvtRwBuilder(ns, coll).writeMap[key] = kvWrite
b.getOrCreateCollHashedRwBuilder(ns, coll).writeMap[key] = kvWriteHash
return nil
}
// 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 rangeQueriesInfo []*kvrwset.RangeQueryInfo
var collHashedRwSet []*CollHashedRwSet
//add read set
util.GetValuesBySortedKeys(&(b.readMap), &readSet)
//add write set
util.GetValuesBySortedKeys(&(b.writeMap), &writeSet)
//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, 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
util.GetValuesBySortedKeys(&(b.readMap), &readSet)
util.GetValuesBySortedKeys(&(b.writeMap), &writeSet)
return &CollHashedRwSet{
CollectionName: b.collName,
HashedRwSet: &kvrwset.HashedRWSet{
HashedReads: readSet,
HashedWrites: writeSet,
},
PvtRwSetHash: b.pvtDataHash,
}
}
func (b *collPvtRwBuilder) build() *CollPvtRwSet {
var writeSet []*kvrwset.KVWrite
util.GetValuesBySortedKeys(&(b.writeMap), &writeSet)
return &CollPvtRwSet{
CollectionName: b.collectionName,
KvRwSet: &kvrwset.KVRWSet{
Writes: writeSet,
},
}
}
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[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),
nil,
}
}
func newCollPvtRwBuilder(collName string) *collPvtRwBuilder {
return &collPvtRwBuilder{collName, make(map[string]*kvrwset.KVWrite)}
}