forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwset_proto_util.go
357 lines (316 loc) · 11.8 KB
/
rwset_proto_util.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
/*
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/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/ledger/rwset"
"github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/hyperledger/fabric/core/ledger/util"
)
/////////////////////////////////////////////////////////////////
// Messages related to PUBLIC read-write set
/////////////////////////////////////////////////////////////////
// TxRwSet acts as a proxy of 'rwset.TxReadWriteSet' proto message and helps constructing Read-write set specifically for KV data model
type TxRwSet struct {
NsRwSets []*NsRwSet
}
// NsRwSet encapsulates 'kvrwset.KVRWSet' proto message for a specific name space (chaincode)
type NsRwSet struct {
NameSpace string
KvRwSet *kvrwset.KVRWSet
CollHashedRwSets []*CollHashedRwSet
}
// CollHashedRwSet encapsulates 'kvrwset.HashedRWSet' proto message for a specific collection
type CollHashedRwSet struct {
CollectionName string
HashedRwSet *kvrwset.HashedRWSet
PvtRwSetHash []byte
}
// GetPvtDataHash returns the PvtRwSetHash for a given namespace and collection
func (txRwSet *TxRwSet) GetPvtDataHash(ns, coll string) []byte {
// we could build and use a map to reduce the number of lookup
// in the future call. However, we decided to defer such optimization
// due to the following assumptions (mainly to avoid additional LOC).
// we assume that the number of namespaces and collections in a txRWSet
// to be very minimal (in a single digit),
for _, nsRwSet := range txRwSet.NsRwSets {
if nsRwSet.NameSpace != ns {
continue
}
return nsRwSet.getPvtDataHash(coll)
}
return nil
}
func (nsRwSet *NsRwSet) getPvtDataHash(coll string) []byte {
for _, collHashedRwSet := range nsRwSet.CollHashedRwSets {
if collHashedRwSet.CollectionName != coll {
continue
}
return collHashedRwSet.PvtRwSetHash
}
return nil
}
/////////////////////////////////////////////////////////////////
// Messages related to PRIVATE read-write set
/////////////////////////////////////////////////////////////////
// TxPvtRwSet represents 'rwset.TxPvtReadWriteSet' proto message
type TxPvtRwSet struct {
NsPvtRwSet []*NsPvtRwSet
}
// NsPvtRwSet represents 'rwset.NsPvtReadWriteSet' proto message
type NsPvtRwSet struct {
NameSpace string
CollPvtRwSets []*CollPvtRwSet
}
// CollPvtRwSet encapsulates 'kvrwset.KVRWSet' proto message for a private rwset for a specific collection
// KvRwSet in a private RwSet should not contain range query info
type CollPvtRwSet struct {
CollectionName string
KvRwSet *kvrwset.KVRWSet
}
/////////////////////////////////////////////////////////////////
// FUNCTIONS for converting messages to/from proto bytes
/////////////////////////////////////////////////////////////////
// ToProtoBytes constructs TxReadWriteSet proto message and serializes using protobuf Marshal
func (txRwSet *TxRwSet) ToProtoBytes() ([]byte, error) {
var protoMsg *rwset.TxReadWriteSet
var err error
if protoMsg, err = txRwSet.toProtoMsg(); err != nil {
return nil, err
}
return proto.Marshal(protoMsg)
}
// FromProtoBytes deserializes protobytes into TxReadWriteSet proto message and populates 'TxRwSet'
func (txRwSet *TxRwSet) FromProtoBytes(protoBytes []byte) error {
protoMsg := &rwset.TxReadWriteSet{}
var err error
var txRwSetTemp *TxRwSet
if err = proto.Unmarshal(protoBytes, protoMsg); err != nil {
return err
}
if txRwSetTemp, err = TxRwSetFromProtoMsg(protoMsg); err != nil {
return err
}
txRwSet.NsRwSets = txRwSetTemp.NsRwSets
return nil
}
// ToProtoBytes constructs 'TxPvtReadWriteSet' proto message and serializes using protobuf Marshal
func (txPvtRwSet *TxPvtRwSet) ToProtoBytes() ([]byte, error) {
var protoMsg *rwset.TxPvtReadWriteSet
var err error
if protoMsg, err = txPvtRwSet.toProtoMsg(); err != nil {
return nil, err
}
return proto.Marshal(protoMsg)
}
// FromProtoBytes deserializes protobytes into 'TxPvtReadWriteSet' proto message and populates 'TxPvtRwSet'
func (txPvtRwSet *TxPvtRwSet) FromProtoBytes(protoBytes []byte) error {
protoMsg := &rwset.TxPvtReadWriteSet{}
var err error
var txPvtRwSetTemp *TxPvtRwSet
if err = proto.Unmarshal(protoBytes, protoMsg); err != nil {
return err
}
if txPvtRwSetTemp, err = TxPvtRwSetFromProtoMsg(protoMsg); err != nil {
return err
}
txPvtRwSet.NsPvtRwSet = txPvtRwSetTemp.NsPvtRwSet
return nil
}
func (txRwSet *TxRwSet) toProtoMsg() (*rwset.TxReadWriteSet, error) {
protoMsg := &rwset.TxReadWriteSet{DataModel: rwset.TxReadWriteSet_KV}
var nsRwSetProtoMsg *rwset.NsReadWriteSet
var err error
for _, nsRwSet := range txRwSet.NsRwSets {
if nsRwSetProtoMsg, err = nsRwSet.toProtoMsg(); err != nil {
return nil, err
}
protoMsg.NsRwset = append(protoMsg.NsRwset, nsRwSetProtoMsg)
}
return protoMsg, nil
}
func TxRwSetFromProtoMsg(protoMsg *rwset.TxReadWriteSet) (*TxRwSet, error) {
txRwSet := &TxRwSet{}
var nsRwSet *NsRwSet
var err error
for _, nsRwSetProtoMsg := range protoMsg.NsRwset {
if nsRwSet, err = nsRwSetFromProtoMsg(nsRwSetProtoMsg); err != nil {
return nil, err
}
txRwSet.NsRwSets = append(txRwSet.NsRwSets, nsRwSet)
}
return txRwSet, nil
}
func (nsRwSet *NsRwSet) toProtoMsg() (*rwset.NsReadWriteSet, error) {
var err error
protoMsg := &rwset.NsReadWriteSet{Namespace: nsRwSet.NameSpace}
if protoMsg.Rwset, err = proto.Marshal(nsRwSet.KvRwSet); err != nil {
return nil, err
}
var collHashedRwSetProtoMsg *rwset.CollectionHashedReadWriteSet
for _, collHashedRwSet := range nsRwSet.CollHashedRwSets {
if collHashedRwSetProtoMsg, err = collHashedRwSet.toProtoMsg(); err != nil {
return nil, err
}
protoMsg.CollectionHashedRwset = append(protoMsg.CollectionHashedRwset, collHashedRwSetProtoMsg)
}
return protoMsg, nil
}
func nsRwSetFromProtoMsg(protoMsg *rwset.NsReadWriteSet) (*NsRwSet, error) {
nsRwSet := &NsRwSet{NameSpace: protoMsg.Namespace, KvRwSet: &kvrwset.KVRWSet{}}
if err := proto.Unmarshal(protoMsg.Rwset, nsRwSet.KvRwSet); err != nil {
return nil, err
}
var err error
var collHashedRwSet *CollHashedRwSet
for _, collHashedRwSetProtoMsg := range protoMsg.CollectionHashedRwset {
if collHashedRwSet, err = collHashedRwSetFromProtoMsg(collHashedRwSetProtoMsg); err != nil {
return nil, err
}
nsRwSet.CollHashedRwSets = append(nsRwSet.CollHashedRwSets, collHashedRwSet)
}
return nsRwSet, nil
}
func (collHashedRwSet *CollHashedRwSet) toProtoMsg() (*rwset.CollectionHashedReadWriteSet, error) {
var err error
protoMsg := &rwset.CollectionHashedReadWriteSet{
CollectionName: collHashedRwSet.CollectionName,
PvtRwsetHash: collHashedRwSet.PvtRwSetHash,
}
if protoMsg.HashedRwset, err = proto.Marshal(collHashedRwSet.HashedRwSet); err != nil {
return nil, err
}
return protoMsg, nil
}
func collHashedRwSetFromProtoMsg(protoMsg *rwset.CollectionHashedReadWriteSet) (*CollHashedRwSet, error) {
colHashedRwSet := &CollHashedRwSet{
CollectionName: protoMsg.CollectionName,
PvtRwSetHash: protoMsg.PvtRwsetHash,
HashedRwSet: &kvrwset.HashedRWSet{},
}
if err := proto.Unmarshal(protoMsg.HashedRwset, colHashedRwSet.HashedRwSet); err != nil {
return nil, err
}
return colHashedRwSet, nil
}
func (txRwSet *TxRwSet) NumCollections() int {
if txRwSet == nil {
return 0
}
numColls := 0
for _, nsRwset := range txRwSet.NsRwSets {
for range nsRwset.CollHashedRwSets {
numColls++
}
}
return numColls
}
///////////////////////////////////////////////////////////////////////////////
// functions for private read-write set
///////////////////////////////////////////////////////////////////////////////
func (txPvtRwSet *TxPvtRwSet) toProtoMsg() (*rwset.TxPvtReadWriteSet, error) {
protoMsg := &rwset.TxPvtReadWriteSet{DataModel: rwset.TxReadWriteSet_KV}
var nsProtoMsg *rwset.NsPvtReadWriteSet
var err error
for _, nsPvtRwSet := range txPvtRwSet.NsPvtRwSet {
if nsProtoMsg, err = nsPvtRwSet.toProtoMsg(); err != nil {
return nil, err
}
protoMsg.NsPvtRwset = append(protoMsg.NsPvtRwset, nsProtoMsg)
}
return protoMsg, nil
}
func TxPvtRwSetFromProtoMsg(protoMsg *rwset.TxPvtReadWriteSet) (*TxPvtRwSet, error) {
txPvtRwset := &TxPvtRwSet{}
var nsPvtRwSet *NsPvtRwSet
var err error
for _, nsRwSetProtoMsg := range protoMsg.NsPvtRwset {
if nsPvtRwSet, err = nsPvtRwSetFromProtoMsg(nsRwSetProtoMsg); err != nil {
return nil, err
}
txPvtRwset.NsPvtRwSet = append(txPvtRwset.NsPvtRwSet, nsPvtRwSet)
}
return txPvtRwset, nil
}
func (nsPvtRwSet *NsPvtRwSet) toProtoMsg() (*rwset.NsPvtReadWriteSet, error) {
protoMsg := &rwset.NsPvtReadWriteSet{Namespace: nsPvtRwSet.NameSpace}
var err error
var collPvtRwSetProtoMsg *rwset.CollectionPvtReadWriteSet
for _, collPvtRwSet := range nsPvtRwSet.CollPvtRwSets {
if collPvtRwSetProtoMsg, err = collPvtRwSet.toProtoMsg(); err != nil {
return nil, err
}
protoMsg.CollectionPvtRwset = append(protoMsg.CollectionPvtRwset, collPvtRwSetProtoMsg)
}
return protoMsg, err
}
func nsPvtRwSetFromProtoMsg(protoMsg *rwset.NsPvtReadWriteSet) (*NsPvtRwSet, error) {
nsPvtRwSet := &NsPvtRwSet{NameSpace: protoMsg.Namespace}
for _, collPvtRwSetProtoMsg := range protoMsg.CollectionPvtRwset {
var err error
var collPvtRwSet *CollPvtRwSet
if collPvtRwSet, err = collPvtRwSetFromProtoMsg(collPvtRwSetProtoMsg); err != nil {
return nil, err
}
nsPvtRwSet.CollPvtRwSets = append(nsPvtRwSet.CollPvtRwSets, collPvtRwSet)
}
return nsPvtRwSet, nil
}
func (collPvtRwSet *CollPvtRwSet) toProtoMsg() (*rwset.CollectionPvtReadWriteSet, error) {
var err error
protoMsg := &rwset.CollectionPvtReadWriteSet{CollectionName: collPvtRwSet.CollectionName}
if protoMsg.Rwset, err = proto.Marshal(collPvtRwSet.KvRwSet); err != nil {
return nil, err
}
return protoMsg, nil
}
func collPvtRwSetFromProtoMsg(protoMsg *rwset.CollectionPvtReadWriteSet) (*CollPvtRwSet, error) {
collPvtRwSet := &CollPvtRwSet{CollectionName: protoMsg.CollectionName, KvRwSet: &kvrwset.KVRWSet{}}
if err := proto.Unmarshal(protoMsg.Rwset, collPvtRwSet.KvRwSet); err != nil {
return nil, err
}
return collPvtRwSet, nil
}
// NewKVRead helps constructing proto message kvrwset.KVRead
func NewKVRead(key string, version *version.Height) *kvrwset.KVRead {
return &kvrwset.KVRead{Key: key, Version: newProtoVersion(version)}
}
// NewVersion helps converting proto message kvrwset.Version to version.Height
func NewVersion(protoVersion *kvrwset.Version) *version.Height {
if protoVersion == nil {
return nil
}
return version.NewHeight(protoVersion.BlockNum, protoVersion.TxNum)
}
func newProtoVersion(height *version.Height) *kvrwset.Version {
if height == nil {
return nil
}
return &kvrwset.Version{BlockNum: height.BlockNum, TxNum: height.TxNum}
}
func newKVWrite(key string, value []byte) *kvrwset.KVWrite {
return &kvrwset.KVWrite{Key: key, IsDelete: value == nil, Value: value}
}
func newPvtKVReadHash(key string, version *version.Height) *kvrwset.KVReadHash {
return &kvrwset.KVReadHash{KeyHash: util.ComputeStringHash(key), Version: newProtoVersion(version)}
}
func newPvtKVWriteAndHash(key string, value []byte) (*kvrwset.KVWrite, *kvrwset.KVWriteHash) {
kvWrite := newKVWrite(key, value)
var keyHash, valueHash []byte
keyHash = util.ComputeStringHash(key)
if !kvWrite.IsDelete {
valueHash = util.ComputeHash(value)
}
return kvWrite, &kvrwset.KVWriteHash{KeyHash: keyHash, IsDelete: kvWrite.IsDelete, ValueHash: valueHash}
}