|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package privacyenabledstate |
| 7 | + |
| 8 | +import ( |
| 9 | + "sort" |
| 10 | + |
| 11 | + "github.com/golang/protobuf/proto" |
| 12 | + "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb" |
| 13 | + "github.com/hyperledger/fabric/core/ledger/util" |
| 14 | + "github.com/pkg/errors" |
| 15 | +) |
| 16 | + |
| 17 | +type updatesBytesBuilder struct { |
| 18 | +} |
| 19 | + |
| 20 | +// DeterministicBytesForPubAndHashUpdates constructs the bytes for a given UpdateBatch |
| 21 | +// while constructing the bytes, it considers only public writes and hashed writes for |
| 22 | +// the collections. For achieveing the determinism, it constructs a slice of proto messages |
| 23 | +// of type 'KVWriteProto'. In the slice, all the writes for a namespace "ns1" appear before |
| 24 | +// the writes for another namespace "ns2" if "ns1" < "ns2" (lexicographically). Within a |
| 25 | +// namespace, all the public writes appear before the collection writes. Like namespaces, |
| 26 | +// the collections writes within a namespace appear in the order of lexicographical order. |
| 27 | +// If an entry has the same namespace as its preceding entry, the namespcae field is skipped. |
| 28 | +// A Similar treatment is given to the repeative entries for a collection within a namespace. |
| 29 | +// For illustration, see the corresponding unit tests |
| 30 | +func (bb *updatesBytesBuilder) DeterministicBytesForPubAndHashUpdates(u *UpdateBatch) ([]byte, error) { |
| 31 | + pubUpdates := u.PubUpdates |
| 32 | + hashUpdates := u.HashUpdates.UpdateMap |
| 33 | + namespaces := dedupAndSort( |
| 34 | + pubUpdates.GetUpdatedNamespaces(), |
| 35 | + hashUpdates.getUpdatedNamespaces(), |
| 36 | + ) |
| 37 | + |
| 38 | + kvWritesProto := []*KVWriteProto{} |
| 39 | + for _, ns := range namespaces { |
| 40 | + p := bb.buildForKeys(pubUpdates.GetUpdates(ns)) |
| 41 | + collsForNs, ok := hashUpdates[ns] |
| 42 | + if ok { |
| 43 | + p = append(p, bb.buildForColls(collsForNs)...) |
| 44 | + } |
| 45 | + p[0].Namespace = ns |
| 46 | + kvWritesProto = append(kvWritesProto, p...) |
| 47 | + } |
| 48 | + batchProto := &KVWritesBatchProto{ |
| 49 | + Kvwrites: kvWritesProto, |
| 50 | + } |
| 51 | + |
| 52 | + batchBytes, err := proto.Marshal(batchProto) |
| 53 | + return batchBytes, errors.Wrap(err, "error constructing deterministic bytes from update batch") |
| 54 | +} |
| 55 | + |
| 56 | +func (bb *updatesBytesBuilder) buildForColls(colls nsBatch) []*KVWriteProto { |
| 57 | + collNames := colls.GetCollectionNames() |
| 58 | + sort.Strings(collNames) |
| 59 | + collsProto := []*KVWriteProto{} |
| 60 | + for _, collName := range collNames { |
| 61 | + collUpdates := colls.getCollectionUpdates(collName) |
| 62 | + p := bb.buildForKeys(collUpdates) |
| 63 | + p[0].Collection = collName |
| 64 | + collsProto = append(collsProto, p...) |
| 65 | + } |
| 66 | + return collsProto |
| 67 | +} |
| 68 | + |
| 69 | +func (bb *updatesBytesBuilder) buildForKeys(kv map[string]*statedb.VersionedValue) []*KVWriteProto { |
| 70 | + keys := util.GetSortedKeys(kv) |
| 71 | + p := []*KVWriteProto{} |
| 72 | + for _, key := range keys { |
| 73 | + val := kv[key] |
| 74 | + p = append( |
| 75 | + p, |
| 76 | + &KVWriteProto{ |
| 77 | + Key: []byte(key), |
| 78 | + Value: val.Value, |
| 79 | + IsDelete: val.Value == nil, |
| 80 | + VersionBytes: val.Version.ToBytes(), |
| 81 | + }, |
| 82 | + ) |
| 83 | + } |
| 84 | + return p |
| 85 | +} |
| 86 | + |
| 87 | +func dedupAndSort(a, b []string) []string { |
| 88 | + m := map[string]struct{}{} |
| 89 | + for _, entry := range a { |
| 90 | + m[entry] = struct{}{} |
| 91 | + } |
| 92 | + for _, entry := range b { |
| 93 | + m[entry] = struct{}{} |
| 94 | + } |
| 95 | + return util.GetSortedKeys(m) |
| 96 | +} |
0 commit comments