github.com/hyperledger-labs/bdls@v2.1.1+incompatible/core/chaincode/extcc/extcc_handler.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package extcc 8 9 import ( 10 "context" 11 12 "github.com/hyperledger/fabric/common/flogging" 13 "github.com/hyperledger/fabric/core/container/ccintf" 14 "github.com/hyperledger/fabric/internal/pkg/comm" 15 "github.com/pkg/errors" 16 17 pb "github.com/hyperledger/fabric-protos-go/peer" 18 19 "google.golang.org/grpc" 20 ) 21 22 var extccLogger = flogging.MustGetLogger("extcc") 23 24 // StreamHandler handles the `Chaincode` gRPC service with peer as client 25 type StreamHandler interface { 26 HandleChaincodeStream(stream ccintf.ChaincodeStream) error 27 } 28 29 type ExternalChaincodeRuntime struct { 30 } 31 32 // createConnection - standard grpc client creating using ClientConfig info (surprised there isn't 33 // a helper method for this) 34 func (i *ExternalChaincodeRuntime) createConnection(ccid string, ccinfo *ccintf.ChaincodeServerInfo) (*grpc.ClientConn, error) { 35 grpcClient, err := comm.NewGRPCClient(ccinfo.ClientConfig) 36 if err != nil { 37 return nil, errors.WithMessagef(err, "error creating grpc client to %s", ccid) 38 } 39 40 conn, err := grpcClient.NewConnection(ccinfo.Address) 41 if err != nil { 42 return nil, errors.WithMessagef(err, "error creating grpc connection to %s", ccinfo.Address) 43 } 44 45 extccLogger.Debugf("Created external chaincode connection: %s", ccid) 46 47 return conn, nil 48 } 49 50 func (i *ExternalChaincodeRuntime) Stream(ccid string, ccinfo *ccintf.ChaincodeServerInfo, sHandler StreamHandler) error { 51 extccLogger.Debugf("Starting external chaincode connection: %s", ccid) 52 conn, err := i.createConnection(ccid, ccinfo) 53 if err != nil { 54 return errors.WithMessagef(err, "error cannot create connection for %s", ccid) 55 } 56 57 defer conn.Close() 58 59 //create the client and start streaming 60 client := pb.NewChaincodeClient(conn) 61 62 stream, err := client.Connect(context.Background()) 63 if err != nil { 64 return errors.WithMessagef(err, "error creating grpc client connection to %s", ccid) 65 } 66 67 //peer as client has to initiate the stream. Rest of the process is unchanged 68 sHandler.HandleChaincodeStream(stream) 69 70 extccLogger.Debugf("External chaincode %s client exited", ccid) 71 72 return nil 73 }