|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package privacyenabledstate |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/base64" |
| 11 | + "fmt" |
| 12 | + |
| 13 | + "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb" |
| 14 | + "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb" |
| 15 | + "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/stateleveldb" |
| 16 | + "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version" |
| 17 | + "github.com/hyperledger/fabric/core/ledger/ledgerconfig" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + nsJoiner = "/" |
| 22 | + pvtDataPrefix = "p" |
| 23 | + hashDataPrefix = "h" |
| 24 | +) |
| 25 | + |
| 26 | +// CommonStorageDBProvider implements interface DBProvider |
| 27 | +type CommonStorageDBProvider struct { |
| 28 | + statedb.VersionedDBProvider |
| 29 | +} |
| 30 | + |
| 31 | +// NewCommonStorageDBProvider constructs an instance of DBProvider |
| 32 | +func NewCommonStorageDBProvider() (DBProvider, error) { |
| 33 | + var vdbProvider statedb.VersionedDBProvider |
| 34 | + var err error |
| 35 | + if ledgerconfig.IsCouchDBEnabled() { |
| 36 | + if vdbProvider, err = statecouchdb.NewVersionedDBProvider(); err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + } else { |
| 40 | + vdbProvider = stateleveldb.NewVersionedDBProvider() |
| 41 | + } |
| 42 | + return &CommonStorageDBProvider{vdbProvider}, nil |
| 43 | +} |
| 44 | + |
| 45 | +// GetDBHandle implements function from interface DBProvider |
| 46 | +func (p *CommonStorageDBProvider) GetDBHandle(id string) (DB, error) { |
| 47 | + vdb, err := p.VersionedDBProvider.GetDBHandle(id) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + return NewCommonStorageDB(vdb, id) |
| 52 | +} |
| 53 | + |
| 54 | +// Close implements function from interface DBProvider |
| 55 | +func (p *CommonStorageDBProvider) Close() { |
| 56 | + p.VersionedDBProvider.Close() |
| 57 | +} |
| 58 | + |
| 59 | +// CommonStorageDB implements interface DB. This implementation uses a single database to maintain |
| 60 | +// both the public and private data |
| 61 | +type CommonStorageDB struct { |
| 62 | + statedb.VersionedDB |
| 63 | +} |
| 64 | + |
| 65 | +// NewCommonStorageDB wraps a VersionedDB instance. The public data is managed directly by the wrapped versionedDB. |
| 66 | +// For managing the hashed data and private data, this implementation creates separate namespaces in the wrapped db |
| 67 | +func NewCommonStorageDB(vdb statedb.VersionedDB, ledgerid string) (DB, error) { |
| 68 | + return &CommonStorageDB{VersionedDB: vdb}, nil |
| 69 | +} |
| 70 | + |
| 71 | +// GetPrivateData implements corresponding function in interface DB |
| 72 | +func (s *CommonStorageDB) GetPrivateData(namespace, collection, key string) (*statedb.VersionedValue, error) { |
| 73 | + return s.GetState(derivePvtDataNs(namespace, collection), key) |
| 74 | +} |
| 75 | + |
| 76 | +// GetValueHash implements corresponding function in interface DB |
| 77 | +func (s *CommonStorageDB) GetValueHash(namespace, collection string, keyHash []byte) (*statedb.VersionedValue, error) { |
| 78 | + keyHashStr := string(keyHash) |
| 79 | + if !s.BytesKeySuppoted() { |
| 80 | + keyHashStr = base64.StdEncoding.EncodeToString(keyHash) |
| 81 | + } |
| 82 | + return s.GetState(deriveHashedDataNs(namespace, collection), keyHashStr) |
| 83 | +} |
| 84 | + |
| 85 | +// GetPrivateDataMultipleKeys implements corresponding function in interface DB |
| 86 | +func (s *CommonStorageDB) GetPrivateDataMultipleKeys(namespace, collection string, keys []string) ([]*statedb.VersionedValue, error) { |
| 87 | + return s.GetStateMultipleKeys(derivePvtDataNs(namespace, collection), keys) |
| 88 | +} |
| 89 | + |
| 90 | +// GetPrivateDataRangeScanIterator implements corresponding function in interface DB |
| 91 | +func (s *CommonStorageDB) GetPrivateDataRangeScanIterator(namespace, collection, startKey, endKey string) (statedb.ResultsIterator, error) { |
| 92 | + return s.GetStateRangeScanIterator(derivePvtDataNs(namespace, collection), startKey, endKey) |
| 93 | +} |
| 94 | + |
| 95 | +// ExecuteQueryOnPrivateData implements corresponding function in interface DB |
| 96 | +func (s CommonStorageDB) ExecuteQueryOnPrivateData(namespace, collection, query string) (statedb.ResultsIterator, error) { |
| 97 | + return s.ExecuteQuery(derivePvtDataNs(namespace, collection), query) |
| 98 | +} |
| 99 | + |
| 100 | +// ApplyUpdates overrides the funciton in statedb.VersionedDB and throws appropriate error message |
| 101 | +// Otherwise, somewhere in the code, usage of this function could lead to updating only public data. |
| 102 | +func (s *CommonStorageDB) ApplyUpdates(batch *statedb.UpdateBatch, height *version.Height) error { |
| 103 | + return fmt.Errorf("This function should not be invoked on this type. Please invoke function 'ApplyPrivacyAwareUpdates'") |
| 104 | +} |
| 105 | + |
| 106 | +// ApplyPrivacyAwareUpdates implements corresponding function in interface DB |
| 107 | +func (s *CommonStorageDB) ApplyPrivacyAwareUpdates(updates *UpdateBatch, height *version.Height) error { |
| 108 | + addPvtUpdates(updates.PubUpdates, updates.PvtUpdates) |
| 109 | + addHashedUpdates(updates.PubUpdates, updates.HashUpdates, !s.BytesKeySuppoted()) |
| 110 | + return s.VersionedDB.ApplyUpdates(updates.PubUpdates.UpdateBatch, height) |
| 111 | +} |
| 112 | + |
| 113 | +func derivePvtDataNs(namespace, collection string) string { |
| 114 | + return namespace + nsJoiner + pvtDataPrefix + collection |
| 115 | +} |
| 116 | + |
| 117 | +func deriveHashedDataNs(namespace, collection string) string { |
| 118 | + return namespace + nsJoiner + hashDataPrefix + collection |
| 119 | +} |
| 120 | + |
| 121 | +func addPvtUpdates(pubUpdateBatch *PubUpdateBatch, pvtUpdateBatch *PvtUpdateBatch) { |
| 122 | + for ns, nsBatch := range pvtUpdateBatch.UpdateMap { |
| 123 | + for _, coll := range nsBatch.GetCollectionNames() { |
| 124 | + for key, vv := range nsBatch.GetUpdates(coll) { |
| 125 | + pubUpdateBatch.Update(derivePvtDataNs(ns, coll), key, vv) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func addHashedUpdates(pubUpdateBatch *PubUpdateBatch, hashedUpdateBatch *HashedUpdateBatch, base64Key bool) { |
| 132 | + for ns, nsBatch := range hashedUpdateBatch.UpdateMap { |
| 133 | + for _, coll := range nsBatch.GetCollectionNames() { |
| 134 | + for key, vv := range nsBatch.GetUpdates(coll) { |
| 135 | + if base64Key { |
| 136 | + key = base64.StdEncoding.EncodeToString([]byte(key)) |
| 137 | + } |
| 138 | + pubUpdateBatch.Update(deriveHashedDataNs(ns, coll), key, vv) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments