-
Notifications
You must be signed in to change notification settings - Fork 52
/
invoke.go
538 lines (481 loc) · 15.6 KB
/
invoke.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"bytes"
"context"
"encoding/base64"
"strconv"
"strings"
"sync"
"time"
peer2 "github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/peer"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/transaction"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/grpc"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"github.com/hyperledger/fabric-protos-go/common"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
)
type Invoke struct {
Chaincode *Chaincode
ServiceProvider view2.ServiceProvider
Network Network
Channel Channel
TxID driver.TxID
SignerIdentity view.Identity
ChaincodePath string
ChaincodeName string
ChaincodeVersion string
TransientMap map[string][]byte
EndorsersMSPIDs []string
ImplicitCollectionMSPIDs []string
EndorsersFromMyOrg bool
EndorsersByConnConfig []*grpc.ConnectionConfig
DiscoveredEndorsersByEndpoints []string
Function string
Args []interface{}
MatchEndorsementPolicy bool
NumRetries int
RetrySleep time.Duration
Context context.Context
}
func NewInvoke(chaincode *Chaincode, function string, args ...interface{}) *Invoke {
return &Invoke{
Chaincode: chaincode,
ServiceProvider: chaincode.sp,
Network: chaincode.network,
Channel: chaincode.channel,
ChaincodeName: chaincode.name,
Function: function,
Args: args,
NumRetries: int(chaincode.NumRetries),
RetrySleep: chaincode.RetrySleep,
}
}
func (i *Invoke) Endorse() (driver.Envelope, error) {
for j := 0; j < i.NumRetries; j++ {
res, err := i.endorse()
if err != nil {
if j+1 >= i.NumRetries {
return nil, err
}
time.Sleep(i.RetrySleep)
continue
}
return res, nil
}
return nil, errors.Errorf("failed to perform endorse")
}
func (i *Invoke) endorse() (driver.Envelope, error) {
_, prop, responses, signer, err := i.prepare(false)
if err != nil {
return nil, err
}
proposalResp := responses[0]
if proposalResp == nil {
return nil, errors.New("error during query: received nil proposal response")
}
// assemble a signed transaction (it's an Envelope message)
env, err := protoutil.CreateSignedTx(prop, signer, responses...)
if err != nil {
return nil, errors.WithMessage(err, "could not assemble transaction")
}
return transaction.NewEnvelopeFromEnv(env)
}
func (i *Invoke) Query() ([]byte, error) {
for j := 0; j < i.NumRetries; j++ {
res, err := i.query()
if err != nil {
if j+1 >= i.NumRetries {
return nil, err
}
time.Sleep(i.RetrySleep)
continue
}
return res, nil
}
return nil, errors.Errorf("failed to perform query")
}
func (i *Invoke) query() ([]byte, error) {
_, _, responses, _, err := i.prepare(!i.MatchEndorsementPolicy)
if err != nil {
return nil, err
}
// check all responses match
resp := responses[0]
if resp == nil {
return nil, errors.New("error during query: received nil proposal response")
}
if resp.Endorsement == nil {
return nil, errors.Errorf("endorsement failure during query. endorsement is nil: [%v]", resp.Response)
}
if resp.Response == nil {
return nil, errors.Errorf("endorsement failure during query. response is nil: [%v]", resp.Endorsement)
}
for i := 1; i < len(responses); i++ {
if responses[i].Endorsement == nil {
return nil, errors.Errorf("endorsement failure during query. endorsement is nil: [%v]", resp.Response)
}
if responses[i].Response == nil {
return nil, errors.Errorf("endorsement failure during query. response is nil: [%v]", resp.Endorsement)
}
if !bytes.Equal(responses[i].Response.Payload, resp.Response.Payload) {
return nil, errors.Errorf("endorsement failure during query. response payload does not match")
}
if responses[i].Response.Status != resp.Response.Status {
return nil, errors.Errorf("endorsement failure during query. response status does not match")
}
if responses[i].Response.Message != resp.Response.Message {
return nil, errors.Errorf("endorsement failure during query. response message does not match")
}
}
return resp.Response.Payload, nil
}
func (i *Invoke) Submit() (string, []byte, error) {
for j := 0; j < i.NumRetries; j++ {
txID, res, err := i.submit()
if err != nil {
if j+1 >= i.NumRetries {
return "", nil, err
}
time.Sleep(i.RetrySleep)
continue
}
return txID, res, nil
}
return "", nil, errors.Errorf("failed to perform submit")
}
func (i *Invoke) submit() (string, []byte, error) {
txID, prop, responses, signer, err := i.prepare(false)
if err != nil {
return "", nil, err
}
proposalResp := responses[0]
if proposalResp == nil {
return "", nil, errors.New("error during query: received nil proposal response")
}
// assemble a signed transaction (it's an Envelope message)
env, err := protoutil.CreateSignedTx(prop, signer, responses...)
if err != nil {
return txID, proposalResp.Response.Payload, errors.WithMessage(err, "could not assemble transaction")
}
// Broadcast envelope and wait for finality
err = i.broadcast(txID, env)
if err != nil {
return "", nil, err
}
return txID, proposalResp.Response.Payload, nil
}
func (i *Invoke) WithContext(context context.Context) driver.ChaincodeInvocation {
i.Context = context
return i
}
func (i *Invoke) WithTransientEntry(k string, v interface{}) driver.ChaincodeInvocation {
if i.TransientMap == nil {
i.TransientMap = map[string][]byte{}
}
b, err := i.toBytes(v)
if err != nil {
panic(err)
}
i.TransientMap[k] = b
return i
}
func (i *Invoke) WithEndorsersByMSPIDs(mspIDs ...string) driver.ChaincodeInvocation {
i.EndorsersMSPIDs = mspIDs
return i
}
func (i *Invoke) WithEndorsersFromMyOrg() driver.ChaincodeInvocation {
i.EndorsersFromMyOrg = true
return i
}
// WithDiscoveredEndorsersByEndpoints sets the endpoints to be used to filter the result of
// discovery. Discovery is used to identify the chaincode's endorsers, if not set otherwise.
func (i *Invoke) WithDiscoveredEndorsersByEndpoints(endpoints ...string) driver.ChaincodeInvocation {
i.DiscoveredEndorsersByEndpoints = endpoints
return i
}
func (i *Invoke) WithMatchEndorsementPolicy() driver.ChaincodeInvocation {
i.MatchEndorsementPolicy = true
return i
}
func (i *Invoke) WithSignerIdentity(id view.Identity) driver.ChaincodeInvocation {
i.SignerIdentity = id
return i
}
func (i *Invoke) WithEndorsersByConnConfig(ccs ...*grpc.ConnectionConfig) driver.ChaincodeInvocation {
i.EndorsersByConnConfig = ccs
return i
}
func (i *Invoke) WithImplicitCollections(mspIDs ...string) driver.ChaincodeInvocation {
i.ImplicitCollectionMSPIDs = mspIDs
return i
}
func (i *Invoke) WithTxID(id driver.TxID) driver.ChaincodeInvocation {
i.TxID = id
return i
}
func (i *Invoke) WithNumRetries(numRetries uint) driver.ChaincodeInvocation {
i.NumRetries = int(numRetries)
return i
}
func (i *Invoke) WithRetrySleep(duration time.Duration) driver.ChaincodeInvocation {
i.RetrySleep = duration
return i
}
func (i *Invoke) prepare(query bool) (string, *pb.Proposal, []*pb.ProposalResponse, driver.SigningIdentity, error) {
// TODO: improve by providing grpc connection pool
var peerClients []peer2.Client
defer func() {
for _, pCli := range peerClients {
pCli.Close()
}
}()
if i.SignerIdentity.IsNone() {
return "", nil, nil, nil, errors.Errorf("no invoker specified")
}
if len(i.ChaincodeName) == 0 {
return "", nil, nil, nil, errors.Errorf("no chaincode specified")
}
// load endorser clients
var endorserClients []pb.EndorserClient
var discoveredPeers []driver.DiscoveredPeer
switch {
case len(i.EndorsersByConnConfig) != 0:
// get a peer client for each connection config
for _, config := range i.EndorsersByConnConfig {
peerClient, err := i.Channel.NewPeerClientForAddress(*config)
if err != nil {
return "", nil, nil, nil, err
}
peerClients = append(peerClients, peerClient)
}
default:
if i.EndorsersFromMyOrg && len(i.EndorsersMSPIDs) == 0 {
// retrieve invoker's MSP-ID
invokerMSPID, err := i.Channel.MSPManager().DeserializeIdentity(i.SignerIdentity)
if err != nil {
return "", nil, nil, nil, errors.WithMessagef(err, "failed to deserializer the invoker identity")
}
i.EndorsersMSPIDs = []string{invokerMSPID.GetMSPIdentifier()}
}
// discover
var err error
discovery := NewDiscovery(
i.Chaincode,
)
discovery.WithFilterByMSPIDs(
i.EndorsersMSPIDs...,
).WithImplicitCollections(
i.ImplicitCollectionMSPIDs...,
)
if query {
discovery.WithForQuery()
}
peers, err := discovery.Call()
if err != nil {
return "", nil, nil, nil, err
}
if len(i.DiscoveredEndorsersByEndpoints) != 0 {
for _, peer := range peers {
for _, endpoint := range i.DiscoveredEndorsersByEndpoints {
if peer.Endpoint == endpoint {
// append
discoveredPeers = append(discoveredPeers, peer)
break
}
}
}
} else {
discoveredPeers = peers
}
}
// get a peer client for all discovered peers
for _, peer := range discoveredPeers {
peerClient, err := i.Channel.NewPeerClientForAddress(grpc.ConnectionConfig{
Address: peer.Endpoint,
TLSEnabled: i.Network.Config().TLSEnabled(),
TLSRootCertBytes: peer.TLSRootCerts,
})
if err != nil {
return "", nil, nil, nil, errors.WithMessagef(err, "error getting endorser client for %s", peer.Endpoint)
}
peerClients = append(peerClients, peerClient)
}
// get endorser clients
for _, client := range peerClients {
endorserClient, err := client.Endorser()
if err != nil {
return "", nil, nil, nil, errors.WithMessagef(err, "error getting endorser client for %s", client.Address())
}
endorserClients = append(endorserClients, endorserClient)
}
if len(endorserClients) == 0 {
return "", nil, nil, nil, errors.New("no endorser clients retrieved with the current filters")
}
// load signer
signer, err := i.Network.SignerService().GetSigningIdentity(i.SignerIdentity)
if err != nil {
return "", nil, nil, nil, err
}
// prepare proposal
signedProp, prop, txID, err := i.prepareProposal(signer)
if err != nil {
return "", nil, nil, nil, err
}
// collect responses
responses, err := i.collectResponses(endorserClients, signedProp)
if err != nil {
return "", nil, nil, nil, errors.Wrapf(err, "failed collecting proposal responses")
}
if len(responses) == 0 {
// this should only happen if some new code has introduced a bug
return "", nil, nil, nil, errors.New("no proposal responses received - this might indicate a bug")
}
return txID, prop, responses, signer, nil
}
func (i *Invoke) prepareProposal(signer SerializableSigner) (*pb.SignedProposal, *pb.Proposal, string, error) {
spec, err := i.getChaincodeSpec()
if err != nil {
return nil, nil, "", err
}
invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}
creator, err := signer.Serialize()
if err != nil {
return nil, nil, "", errors.WithMessage(err, "error serializing identity")
}
funcName := "invoke"
prop, txID, err := i.createChaincodeProposalWithTxIDAndTransient(
common.HeaderType_ENDORSER_TRANSACTION,
i.Channel.Name(),
invocation,
creator,
i.TransientMap)
if err != nil {
return nil, nil, "", errors.WithMessagef(err, "error creating proposal for %s", funcName)
}
signedProp, err := protoutil.GetSignedProposal(prop, signer)
if err != nil {
return nil, nil, "", errors.WithMessagef(err, "error creating signed proposal for %s", funcName)
}
return signedProp, prop, txID, nil
}
// createChaincodeProposalWithTxIDAndTransient creates a proposal from given
// input. It returns the proposal and the transaction id associated with the
// proposal
func (i *Invoke) createChaincodeProposalWithTxIDAndTransient(typ common.HeaderType, channelID string, cis *pb.ChaincodeInvocationSpec, creator []byte, transientMap map[string][]byte) (*pb.Proposal, string, error) {
var nonce []byte
var txID string
if len(i.TxID.Creator) == 0 {
i.TxID.Creator = creator
}
if len(i.TxID.Nonce) == 0 {
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("generate nonce and tx-id for [%s,%s]", view.Identity(i.TxID.Creator).String(), base64.StdEncoding.EncodeToString(nonce))
}
txID = transaction.ComputeTxID(&i.TxID)
nonce = i.TxID.Nonce
} else {
nonce = i.TxID.Nonce
txID = transaction.ComputeTxID(&i.TxID)
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("no need to generate nonce and tx-id [%s,%s]", base64.StdEncoding.EncodeToString(nonce), txID)
}
}
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("create chaincode proposal with tx id [%s], nonce [%s]", txID, base64.StdEncoding.EncodeToString(nonce))
}
return protoutil.CreateChaincodeProposalWithTxIDNonceAndTransient(txID, typ, channelID, cis, nonce, creator, transientMap)
}
// collectResponses sends a signed proposal to a set of peers, and gathers all the responses.
func (i *Invoke) collectResponses(endorserClients []pb.EndorserClient, signedProposal *pb.SignedProposal) ([]*pb.ProposalResponse, error) {
responsesCh := make(chan *pb.ProposalResponse, len(endorserClients))
errorCh := make(chan error, len(endorserClients))
wg := sync.WaitGroup{}
for _, endorser := range endorserClients {
wg.Add(1)
go func(endorser pb.EndorserClient) {
defer wg.Done()
proposalResp, err := endorser.ProcessProposal(context.Background(), signedProposal)
if err != nil {
errorCh <- err
return
}
responsesCh <- proposalResp
}(endorser)
}
wg.Wait()
close(responsesCh)
close(errorCh)
for err := range errorCh {
return nil, err
}
var responses []*pb.ProposalResponse
for response := range responsesCh {
responses = append(responses, response)
}
return responses, nil
}
// getChaincodeSpec get chaincode spec from the fsccli cmd parameters
func (i *Invoke) getChaincodeSpec() (*pb.ChaincodeSpec, error) {
// prepare args
args, err := i.prepareArgs()
if err != nil {
return nil, err
}
// Build the spec
input := &pb.ChaincodeInput{
Args: args,
Decorations: nil,
IsInit: false,
}
chaincodeLang := "golang"
chaincodeLang = strings.ToUpper(chaincodeLang)
spec := &pb.ChaincodeSpec{
Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value[chaincodeLang]),
ChaincodeId: &pb.ChaincodeID{Path: i.ChaincodePath, Name: i.ChaincodeName, Version: i.ChaincodeVersion},
Input: input,
}
return spec, nil
}
func (i *Invoke) prepareArgs() ([][]byte, error) {
var args [][]byte
args = append(args, []byte(i.Function))
for _, arg := range i.Args {
b, err := i.toBytes(arg)
if err != nil {
return nil, err
}
args = append(args, b)
}
return args, nil
}
func (i *Invoke) toBytes(arg interface{}) ([]byte, error) {
switch v := arg.(type) {
case []byte:
return v, nil
case string:
return []byte(v), nil
case int:
return []byte(strconv.Itoa(v)), nil
case int64:
return []byte(strconv.FormatInt(v, 10)), nil
case uint64:
return []byte(strconv.FormatUint(v, 10)), nil
default:
return nil, errors.Errorf("arg type [%T] not recognized.", v)
}
}
func (i *Invoke) broadcast(txID string, env *common.Envelope) error {
if err := i.Network.Broadcast(i.Context, env); err != nil {
return err
}
return i.Channel.IsFinal(context.Background(), txID)
}