/
common_storage_db.go
223 lines (189 loc) · 7.92 KB
/
common_storage_db.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package privacyenabledstate
import (
"encoding/base64"
"fmt"
"github.com/hyperledger/fabric/core/ledger/cceventmgmt"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/stateleveldb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
)
const (
nsJoiner = "$$"
pvtDataPrefix = "p"
hashDataPrefix = "h"
)
// CommonStorageDBProvider implements interface DBProvider
type CommonStorageDBProvider struct {
statedb.VersionedDBProvider
}
// NewCommonStorageDBProvider constructs an instance of DBProvider
func NewCommonStorageDBProvider() (DBProvider, error) {
var vdbProvider statedb.VersionedDBProvider
var err error
if ledgerconfig.IsCouchDBEnabled() {
if vdbProvider, err = statecouchdb.NewVersionedDBProvider(); err != nil {
return nil, err
}
} else {
vdbProvider = stateleveldb.NewVersionedDBProvider()
}
return &CommonStorageDBProvider{vdbProvider}, nil
}
// GetDBHandle implements function from interface DBProvider
func (p *CommonStorageDBProvider) GetDBHandle(id string) (DB, error) {
vdb, err := p.VersionedDBProvider.GetDBHandle(id)
if err != nil {
return nil, err
}
return NewCommonStorageDB(vdb, id)
}
// Close implements function from interface DBProvider
func (p *CommonStorageDBProvider) Close() {
p.VersionedDBProvider.Close()
}
// CommonStorageDB implements interface DB. This implementation uses a single database to maintain
// both the public and private data
type CommonStorageDB struct {
statedb.VersionedDB
}
// NewCommonStorageDB wraps a VersionedDB instance. The public data is managed directly by the wrapped versionedDB.
// For managing the hashed data and private data, this implementation creates separate namespaces in the wrapped db
func NewCommonStorageDB(vdb statedb.VersionedDB, ledgerid string) (DB, error) {
return &CommonStorageDB{VersionedDB: vdb}, nil
}
// IsBulkOptimizable implements corresponding function in interface DB
func (s *CommonStorageDB) IsBulkOptimizable() bool {
_, ok := s.VersionedDB.(statedb.BulkOptimizable)
return ok
}
// LoadCommittedVersionsOfPubAndHashedKeys implements corresponding function in interface DB
func (s *CommonStorageDB) LoadCommittedVersionsOfPubAndHashedKeys(pubKeys []*statedb.CompositeKey,
hashedKeys []*HashedCompositeKey) error {
bulkOptimizable, ok := s.VersionedDB.(statedb.BulkOptimizable)
if !ok {
return nil
}
// Here, hashedKeys are merged into pubKeys to get a combined set of keys for combined loading
for _, key := range hashedKeys {
ns := deriveHashedDataNs(key.Namespace, key.CollectionName)
// No need to check for duplicates as hashedKeys are in separate namespace
var keyHashStr string
if !s.BytesKeySuppoted() {
keyHashStr = base64.StdEncoding.EncodeToString([]byte(key.KeyHash))
} else {
keyHashStr = key.KeyHash
}
pubKeys = append(pubKeys, &statedb.CompositeKey{
Namespace: ns,
Key: keyHashStr,
})
}
err := bulkOptimizable.LoadCommittedVersions(pubKeys)
if err != nil {
return err
}
return nil
}
// ClearCachedVersions implements corresponding function in interface DB
func (s *CommonStorageDB) ClearCachedVersions() {
bulkOptimizable, ok := s.VersionedDB.(statedb.BulkOptimizable)
if ok {
bulkOptimizable.ClearCachedVersions()
}
}
// GetChaincodeEventListener implements corresponding function in interface DB
func (s *CommonStorageDB) GetChaincodeEventListener() cceventmgmt.ChaincodeLifecycleEventListener {
ccListener, ok := s.VersionedDB.(cceventmgmt.ChaincodeLifecycleEventListener)
if ok {
return ccListener
}
return nil
}
// GetPrivateData implements corresponding function in interface DB
func (s *CommonStorageDB) GetPrivateData(namespace, collection, key string) (*statedb.VersionedValue, error) {
return s.GetState(derivePvtDataNs(namespace, collection), key)
}
// GetValueHash implements corresponding function in interface DB
func (s *CommonStorageDB) GetValueHash(namespace, collection string, keyHash []byte) (*statedb.VersionedValue, error) {
keyHashStr := string(keyHash)
if !s.BytesKeySuppoted() {
keyHashStr = base64.StdEncoding.EncodeToString(keyHash)
}
return s.GetState(deriveHashedDataNs(namespace, collection), keyHashStr)
}
// GetKeyHashVersion implements corresponding function in interface DB
func (s *CommonStorageDB) GetKeyHashVersion(namespace, collection string, keyHash []byte) (*version.Height, error) {
keyHashStr := string(keyHash)
if !s.BytesKeySuppoted() {
keyHashStr = base64.StdEncoding.EncodeToString(keyHash)
}
return s.GetVersion(deriveHashedDataNs(namespace, collection), keyHashStr)
}
// GetCachedKeyHashVersion retrieves the keyhash version from cache
func (s *CommonStorageDB) GetCachedKeyHashVersion(namespace, collection string, keyHash []byte) (*version.Height, bool) {
bulkOptimizable, ok := s.VersionedDB.(statedb.BulkOptimizable)
if !ok {
return nil, false
}
keyHashStr := string(keyHash)
if !s.BytesKeySuppoted() {
keyHashStr = base64.StdEncoding.EncodeToString(keyHash)
}
return bulkOptimizable.GetCachedVersion(deriveHashedDataNs(namespace, collection), keyHashStr)
}
// GetPrivateDataMultipleKeys implements corresponding function in interface DB
func (s *CommonStorageDB) GetPrivateDataMultipleKeys(namespace, collection string, keys []string) ([]*statedb.VersionedValue, error) {
return s.GetStateMultipleKeys(derivePvtDataNs(namespace, collection), keys)
}
// GetPrivateDataRangeScanIterator implements corresponding function in interface DB
func (s *CommonStorageDB) GetPrivateDataRangeScanIterator(namespace, collection, startKey, endKey string) (statedb.ResultsIterator, error) {
return s.GetStateRangeScanIterator(derivePvtDataNs(namespace, collection), startKey, endKey)
}
// ExecuteQueryOnPrivateData implements corresponding function in interface DB
func (s CommonStorageDB) ExecuteQueryOnPrivateData(namespace, collection, query string) (statedb.ResultsIterator, error) {
return s.ExecuteQuery(derivePvtDataNs(namespace, collection), query)
}
// ApplyUpdates overrides the funciton in statedb.VersionedDB and throws appropriate error message
// Otherwise, somewhere in the code, usage of this function could lead to updating only public data.
func (s *CommonStorageDB) ApplyUpdates(batch *statedb.UpdateBatch, height *version.Height) error {
return fmt.Errorf("This function should not be invoked on this type. Please invoke function 'ApplyPrivacyAwareUpdates'")
}
// ApplyPrivacyAwareUpdates implements corresponding function in interface DB
func (s *CommonStorageDB) ApplyPrivacyAwareUpdates(updates *UpdateBatch, height *version.Height) error {
addPvtUpdates(updates.PubUpdates, updates.PvtUpdates)
addHashedUpdates(updates.PubUpdates, updates.HashUpdates, !s.BytesKeySuppoted())
return s.VersionedDB.ApplyUpdates(updates.PubUpdates.UpdateBatch, height)
}
func derivePvtDataNs(namespace, collection string) string {
return namespace + nsJoiner + pvtDataPrefix + collection
}
func deriveHashedDataNs(namespace, collection string) string {
return namespace + nsJoiner + hashDataPrefix + collection
}
func addPvtUpdates(pubUpdateBatch *PubUpdateBatch, pvtUpdateBatch *PvtUpdateBatch) {
for ns, nsBatch := range pvtUpdateBatch.UpdateMap {
for _, coll := range nsBatch.GetCollectionNames() {
for key, vv := range nsBatch.GetUpdates(coll) {
pubUpdateBatch.Update(derivePvtDataNs(ns, coll), key, vv)
}
}
}
}
func addHashedUpdates(pubUpdateBatch *PubUpdateBatch, hashedUpdateBatch *HashedUpdateBatch, base64Key bool) {
for ns, nsBatch := range hashedUpdateBatch.UpdateMap {
for _, coll := range nsBatch.GetCollectionNames() {
for key, vv := range nsBatch.GetUpdates(coll) {
if base64Key {
key = base64.StdEncoding.EncodeToString([]byte(key))
}
pubUpdateBatch.Update(deriveHashedDataNs(ns, coll), key, vv)
}
}
}
}