This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
/
client.go
268 lines (226 loc) · 8.66 KB
/
client.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package client
import (
"encoding/hex"
"sync"
cb "github.com/hyperledger/fabric-protos-go/common"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric-protos-go/transientstore"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/common/crypto"
"github.com/hyperledger/fabric/common/flogging"
commonledger "github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/extensions/collections/api/support"
"github.com/pkg/errors"
collcommon "github.com/trustbloc/fabric-peer-ext/pkg/collections/common"
)
var logger = flogging.MustGetLogger("ext_offledger")
// PeerLedger defines the ledger functions required by the client
type PeerLedger interface {
// NewQueryExecutor gives handle to a query executor.
// A client can obtain more than one 'QueryExecutor's for parallel execution.
// Any synchronization should be performed at the implementation level if required
NewQueryExecutor() (ledger.QueryExecutor, error)
// NewTxSimulator gives handle to a transaction simulator.
// A client can obtain more than one 'TxSimulator's for parallel execution.
// Any snapshoting/synchronization should be performed at the implementation level if required
NewTxSimulator(txid string) (ledger.TxSimulator, error)
// GetBlockchainInfo returns basic info about blockchain
GetBlockchainInfo() (*cb.BlockchainInfo, error)
}
// PvtDataDistributor distributes private data to the peers in the collections
// according to policies induced by the PolicyStore and PolicyParser
type PvtDataDistributor interface {
DistributePrivateData(chainID string, txID string, privateData *transientstore.TxPvtReadWriteSetWithConfigInfo, blkHt uint64) error
}
// KeyValue holds a key-value pair
type KeyValue struct {
Key string
Value []byte
}
// ChannelProviders holds all of the providers required by the client
type ChannelProviders struct {
Ledger PeerLedger
Distributor PvtDataDistributor
ConfigRetriever support.CollectionConfigRetriever
IdentityProvider collcommon.IdentityProvider
}
// Client allows you to put and get Client from outside of a chaincode
type Client struct {
*ChannelProviders
channelID string
creator []byte
mutex sync.RWMutex
}
// New returns a new collection client
func New(channelID string, providers *ChannelProviders) *Client {
return &Client{
ChannelProviders: providers,
channelID: channelID,
}
}
// Put puts the value for the given key
func (d *Client) Put(ns, coll, key string, value []byte) error {
return d.PutMultipleValues(ns, coll, []*KeyValue{{Key: key, Value: value}})
}
// PutMultipleValues puts the given key/values
func (d *Client) PutMultipleValues(ns, coll string, kvs []*KeyValue) error {
bcInfo, err := d.Ledger.GetBlockchainInfo()
if err != nil {
logger.Warningf("[%s] Error getting blockchain info: %s", d.channelID, err)
return errors.WithMessagef(err, "error getting blockchain info in channel [%s]", d.channelID)
}
// Generate a new TxID. The TxID doesn't really matter since this transaction is never committed.
// It just has to be unique.
txID, err := d.newTxID()
if err != nil {
logger.Warningf("[%s] Error generating transaction ID: %s", d.channelID, err)
return errors.WithMessagef(err, "error generating transaction ID in channel [%s]", d.channelID)
}
sim, err := d.Ledger.NewTxSimulator(txID)
if err != nil {
logger.Warningf("[%s] Error getting TxSimulator for transaction [%s]: %s", d.channelID, txID, err)
return errors.WithMessagef(err, "error getting TxSimulator for transaction [%s] in channel [%s]", txID, d.channelID)
}
defer sim.Done()
mapByKey := make(map[string][]byte)
for _, kv := range kvs {
mapByKey[kv.Key] = kv.Value
}
err = sim.SetPrivateDataMultipleKeys(ns, coll, mapByKey)
if err != nil {
logger.Warningf("[%s] Error setting values for transaction [%s]: %s", d.channelID, txID, err)
return errors.WithMessagef(err, "error setting keys for transaction [%s] in channel [%s]", txID, d.channelID)
}
results, err := sim.GetTxSimulationResults()
if err != nil {
logger.Warningf("[%s] Error generating simulation results for transaction [%s]: %s", d.channelID, txID, err)
return errors.WithMessagef(err, "error generating simulation results for transaction [%s] in channel [%s]", txID, d.channelID)
}
configPkg, err := d.getCollectionConfigPackage(ns, coll)
if err != nil {
logger.Warningf("[%s] Error getting collection config for [%s:%s]: %s", d.channelID, ns, coll, err)
return errors.WithMessagef(err, "error getting collection config for [%s:%s] in channel [%s]", ns, coll, d.channelID)
}
pvtData := &transientstore.TxPvtReadWriteSetWithConfigInfo{
EndorsedAt: bcInfo.Height,
PvtRwset: results.PvtSimulationResults,
CollectionConfigs: map[string]*pb.CollectionConfigPackage{
ns: configPkg,
},
}
err = d.Distributor.DistributePrivateData(d.channelID, txID, pvtData, bcInfo.Height)
if err != nil {
logger.Warningf("[%s] Failed to distribute private data: %s", d.channelID, err)
return errors.WithMessagef(err, "error distributing private data in channel [%s]", d.channelID)
}
return nil
}
// Delete deletes the given key(s)
func (d *Client) Delete(ns, coll string, keys ...string) error {
kvs := make([]*KeyValue, len(keys))
for i, key := range keys {
kvs[i] = &KeyValue{Key: key}
}
return d.PutMultipleValues(ns, coll, kvs)
}
// Get retrieves the value for the given key
func (d *Client) Get(ns, coll, key string) ([]byte, error) {
qe, err := d.Ledger.NewQueryExecutor()
if err != nil {
logger.Warningf("[%s] Error getting QueryExecutor: %s", d.channelID, err)
return nil, errors.WithMessagef(err, "error getting QueryExecutor in channel [%s]", d.channelID)
}
defer qe.Done()
return qe.GetPrivateData(ns, coll, key)
}
// GetMultipleKeys retrieves the values for the given keys
func (d *Client) GetMultipleKeys(ns, coll string, keys ...string) ([][]byte, error) {
qe, err := d.Ledger.NewQueryExecutor()
if err != nil {
logger.Warningf("[%s] Error getting QueryExecutor: %s", d.channelID, err)
return nil, errors.WithMessagef(err, "error getting QueryExecutor in channel [%s]", d.channelID)
}
defer qe.Done()
return qe.GetPrivateDataMultipleKeys(ns, coll, keys)
}
// Query executes the given query and returns an iterator that contains results.
// Only used for state databases that support query.
// (Note that this function is not supported by transient data collections)
// The returned ResultsIterator contains results of type *KV which is defined in protos/ledger/queryresult.
func (d *Client) Query(ns, coll, query string) (commonledger.ResultsIterator, error) {
qe, err := d.Ledger.NewQueryExecutor()
if err != nil {
logger.Warningf("[%s] Error getting QueryExecutor: %s", d.channelID, err)
return nil, errors.WithMessagef(err, "error getting QueryExecutor in channel [%s]", d.channelID)
}
defer qe.Done()
return qe.ExecuteQueryOnPrivateData(ns, coll, query)
}
func (d *Client) getCollectionConfigPackage(ns, coll string) (*pb.CollectionConfigPackage, error) {
collConfig, err := d.ConfigRetriever.Config(ns, coll)
if err != nil {
return nil, err
}
return &pb.CollectionConfigPackage{
Config: []*pb.CollectionConfig{
{
Payload: &pb.CollectionConfig_StaticCollectionConfig{
StaticCollectionConfig: collConfig,
},
},
},
}, nil
}
func (d *Client) newTxID() (string, error) {
creator, err := d.getCreator()
if err != nil {
return "", errors.WithMessage(err, "error serializing local signing identity")
}
nonce, err := crypto.GetRandomNonce()
if err != nil {
return "", errors.WithMessage(err, "nonce creation failed")
}
txnID, err := computeTxID(nonce, creator)
if err != nil {
return "", errors.WithMessage(err, "txn ID computation failed")
}
return txnID, nil
}
func (d *Client) getCreator() ([]byte, error) {
d.mutex.RLock()
c := d.creator
d.mutex.RUnlock()
if c != nil {
return c, nil
}
d.mutex.Lock()
defer d.mutex.Unlock()
if d.creator == nil {
creator, err := d.newCreator()
if err != nil {
return nil, errors.WithMessage(err, "error serializing local signing identity")
}
d.creator = creator
}
return d.creator, nil
}
func (d *Client) newCreator() ([]byte, error) {
id, err := d.IdentityProvider.GetDefaultSigningIdentity()
if err != nil {
return nil, errors.WithMessage(err, "error getting local signing identity")
}
return id.Serialize()
}
func computeTxID(nonce, creator []byte) (string, error) {
digest, err := factory.GetDefault().Hash(append(nonce, creator...), &bccsp.SHA256Opts{})
if err != nil {
return "", err
}
return hex.EncodeToString(digest), nil
}