-
Notifications
You must be signed in to change notification settings - Fork 50
/
rwset.go
432 lines (354 loc) · 8.74 KB
/
rwset.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package vault
import (
"bytes"
"sort"
"github.com/google/go-cmp/cmp"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/proto"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/keys"
"github.com/hyperledger/fabric-protos-go/ledger/rwset"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"
"github.com/pkg/errors"
)
type readWriteSet struct {
readSet
writeSet
metaWriteSet
}
func (rws *readWriteSet) populate(rwsetBytes []byte, txid string, namespaces ...string) error {
txRWSet := &rwset.TxReadWriteSet{}
err := proto.Unmarshal(rwsetBytes, txRWSet)
if err != nil {
return errors.Wrapf(err, "provided invalid read-write set bytes for txid %s, unmarshal failed", txid)
}
rwsIn, err := rwsetutil.TxRwSetFromProtoMsg(txRWSet)
if err != nil {
return errors.Wrapf(err, "provided invalid read-write set bytes for txid %s, TxRwSetFromProtoMsg failed", txid)
}
for _, nsrws := range rwsIn.NsRwSets {
ns := nsrws.NameSpace
// skip if not in the list of namespaces
if len(namespaces) > 0 {
notFound := true
for _, s := range namespaces {
if s == ns {
notFound = false
break
}
}
if notFound {
continue
}
}
for _, read := range nsrws.KvRwSet.Reads {
bn := uint64(0)
txn := uint64(0)
if read.Version != nil {
bn = read.Version.BlockNum
txn = read.Version.TxNum
}
rws.readSet.add(ns, read.Key, bn, txn)
}
for _, write := range nsrws.KvRwSet.Writes {
if err := rws.writeSet.add(ns, write.Key, write.Value); err != nil {
return err
}
}
for _, metaWrite := range nsrws.KvRwSet.MetadataWrites {
metadata := map[string][]byte{}
for _, entry := range metaWrite.Entries {
metadata[entry.Name] = append([]byte(nil), entry.Value...)
}
if err := rws.metaWriteSet.add(ns, metaWrite.Key, metadata); err != nil {
return err
}
}
}
return nil
}
type metaWrites map[string][]byte
func (r metaWrites) Equals(o metaWrites) error {
if len(r) != len(o) {
return errors.Errorf("number of meta writes do not match [%v]!=[%v]", len(r), len(o))
}
for k, v := range r {
v2, ok := o[k]
if !ok {
return errors.Errorf("read not found [%s]", k)
}
if !bytes.Equal(v, v2) {
return errors.Errorf("writes for [%s] do not match [%v]!=[%v]", k, v, v2)
}
}
return nil
}
type keyedMetaWrites map[string]metaWrites
func (r keyedMetaWrites) Equals(o keyedMetaWrites) error {
rKeys := r.keys()
sort.Strings(rKeys)
oKeys := o.keys()
sort.Strings(oKeys)
if diff := cmp.Diff(rKeys, oKeys); len(diff) != 0 {
return errors.Errorf("namespaces do not match [%s]", diff)
}
for _, key := range rKeys {
if err := r[key].Equals(o[key]); err != nil {
return errors.Wrapf(err, "meta writes for key [%s] do not match", key)
}
}
return nil
}
func (r keyedMetaWrites) keys() []string {
var res []string
for k := range r {
res = append(res, k)
}
return res
}
type namespaceKeyedMetaWrites map[string]keyedMetaWrites
func (r namespaceKeyedMetaWrites) equals(o namespaceKeyedMetaWrites, nss ...string) error {
rKeys := r.keys(nss...)
sort.Strings(rKeys)
oKeys := o.keys(nss...)
sort.Strings(oKeys)
if diff := cmp.Diff(rKeys, oKeys); len(diff) != 0 {
return errors.Errorf("namespaces do not match [%s]", diff)
}
for _, key := range rKeys {
if err := r[key].
Equals(o[key]); err != nil {
return errors.Wrapf(err, "namespaces [%s] do not match", key)
}
}
return nil
}
func (r namespaceKeyedMetaWrites) keys(nss ...string) []string {
var res []string
for k := range r {
if len(nss) == 0 {
res = append(res, k)
continue
}
for _, s := range nss {
if s == k {
res = append(res, k)
break
}
}
}
return res
}
type metaWriteSet struct {
metawrites namespaceKeyedMetaWrites
}
func (w *metaWriteSet) add(ns, key string, meta map[string][]byte) error {
if err := keys.ValidateNs(ns); err != nil {
return err
}
nsMap, in := w.metawrites[ns]
if !in {
nsMap = keyedMetaWrites{}
w.metawrites[ns] = nsMap
}
metadata := map[string][]byte{}
for k, v := range meta {
metadata[k] = append([]byte(nil), v...)
}
nsMap[key] = metadata
return nil
}
func (w *metaWriteSet) get(ns, key string) map[string][]byte {
return w.metawrites[ns][key]
}
func (w *metaWriteSet) in(ns, key string) bool {
_, in := w.metawrites[ns][key]
return in
}
func (w *metaWriteSet) clear(ns string) {
w.metawrites[ns] = keyedMetaWrites{}
}
type namespaceWrites map[string][]byte
func (r namespaceWrites) Keys() []string {
var keys []string
for k := range r {
keys = append(keys, k)
}
return keys
}
func (r namespaceWrites) Equals(o namespaceWrites) error {
if len(r) != len(o) {
return errors.Errorf("number of writes do not match [%d]!=[%d], [%v]!=[%v]", len(r), len(o), r.Keys(), o.Keys())
}
for k, v := range r {
v2, ok := o[k]
if !ok {
return errors.Errorf("read not found [%s]", k)
}
if !bytes.Equal(v, v2) {
return errors.Errorf("writes for [%s] do not match [%v]!=[%v]", k, v, v2)
}
}
return nil
}
type writes map[string]namespaceWrites
func (r writes) equals(o writes, nss ...string) error {
rKeys := r.keys(nss...)
sort.Strings(rKeys)
oKeys := o.keys(nss...)
sort.Strings(oKeys)
if diff := cmp.Diff(rKeys, oKeys); len(diff) != 0 {
return errors.Errorf("namespaces do not match [%s]", diff)
}
for _, key := range rKeys {
if err := r[key].Equals(o[key]); err != nil {
return errors.Wrapf(err, "namespaces [%s] do not match", key)
}
}
return nil
}
func (r writes) keys(nss ...string) []string {
var res []string
for k := range r {
if len(nss) == 0 {
res = append(res, k)
continue
}
for _, s := range nss {
if s == k {
res = append(res, k)
break
}
}
}
return res
}
type writeSet struct {
writes writes
orderedWrites map[string][]string
}
func (w *writeSet) add(ns, key string, value []byte) error {
if err := keys.ValidateNs(ns); err != nil {
return err
}
nsMap, in := w.writes[ns]
if !in {
nsMap = map[string][]byte{}
w.writes[ns] = nsMap
w.orderedWrites[ns] = make([]string, 0, 8)
}
nsMap[key] = append([]byte(nil), value...)
w.orderedWrites[ns] = append(w.orderedWrites[ns], key)
return nil
}
func (w *writeSet) get(ns, key string) []byte {
return w.writes[ns][key]
}
func (w *writeSet) getAt(ns string, i int) (key string, in bool) {
slice := w.orderedWrites[ns]
if i < 0 || i > len(slice)-1 {
return "", false
}
return slice[i], true
}
func (w *writeSet) in(ns, key string) bool {
_, in := w.writes[ns][key]
return in
}
func (w *writeSet) clear(ns string) {
w.writes[ns] = map[string][]byte{}
w.orderedWrites[ns] = []string{}
}
type namespaceReads map[string]struct {
block uint64
txnum uint64
}
func (r namespaceReads) Equals(o namespaceReads) error {
if len(r) != len(o) {
return errors.Errorf("number of reads do not match [%v]!=[%v]", len(r), len(o))
}
for k, v := range r {
v2, ok := o[k]
if !ok {
return errors.Errorf("read not found [%s]", k)
}
if v.block != v2.block || v.txnum != v2.txnum {
return errors.Errorf("reads for [%s] do not match [%d,%d]!=[%d,%d]", k, v.block, v.txnum, v2.block, v2.txnum)
}
}
return nil
}
type reads map[string]namespaceReads
func (r reads) equals(o reads, nss ...string) error {
rKeys := r.keys(nss...)
sort.Strings(rKeys)
oKeys := o.keys(nss...)
sort.Strings(oKeys)
if diff := cmp.Diff(rKeys, oKeys); len(diff) != 0 {
return errors.Errorf("namespaces do not match [%s]", diff)
}
for _, key := range rKeys {
if err := r[key].Equals(o[key]); err != nil {
return errors.Wrapf(err, "namespaces [%s] do not match", key)
}
}
return nil
}
func (r reads) keys(nss ...string) []string {
var res []string
for k := range r {
if len(nss) == 0 {
res = append(res, k)
continue
}
for _, s := range nss {
if s == k {
res = append(res, k)
break
}
}
}
return res
}
type readSet struct {
reads reads
orderedReads map[string][]string
}
func (r *readSet) add(ns, key string, block, txnum uint64) {
nsMap, in := r.reads[ns]
if !in {
nsMap = make(map[string]struct {
block uint64
txnum uint64
})
r.reads[ns] = nsMap
r.orderedReads[ns] = make([]string, 0, 8)
}
nsMap[key] = struct {
block uint64
txnum uint64
}{block, txnum}
r.orderedReads[ns] = append(r.orderedReads[ns], key)
}
func (r *readSet) get(ns, key string) (block, txnum uint64, in bool) {
entry, in := r.reads[ns][key]
block = entry.block
txnum = entry.txnum
return
}
func (r *readSet) getAt(ns string, i int) (key string, in bool) {
slice := r.orderedReads[ns]
if i < 0 || i > len(slice)-1 {
return "", false
}
return slice[i], true
}
func (r *readSet) clear(ns string) {
r.reads[ns] = map[string]struct {
block uint64
txnum uint64
}{}
r.orderedReads[ns] = []string{}
}