github.com/anjalikarhana/fabric@v2.1.1+incompatible/integration/nwo/orderer_client.go (about) 1 /* 2 Copyright IBM Corp All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package nwo 8 9 import ( 10 "context" 11 "io/ioutil" 12 "path" 13 "time" 14 15 "github.com/hyperledger/fabric-protos-go/common" 16 "github.com/hyperledger/fabric-protos-go/orderer" 17 "github.com/hyperledger/fabric/internal/pkg/comm" 18 "github.com/pkg/errors" 19 ) 20 21 // Broadcast sends given env to Broadcast API of specified orderer. 22 func Broadcast(n *Network, o *Orderer, env *common.Envelope) (*orderer.BroadcastResponse, error) { 23 gRPCclient, err := createOrdererGRPCClient(n, o) 24 if err != nil { 25 return nil, err 26 } 27 28 addr := n.OrdererAddress(o, ListenPort) 29 conn, err := gRPCclient.NewConnection(addr) 30 if err != nil { 31 return nil, err 32 } 33 defer conn.Close() 34 35 broadcaster, err := orderer.NewAtomicBroadcastClient(conn).Broadcast(context.Background()) 36 if err != nil { 37 return nil, err 38 } 39 40 err = broadcaster.Send(env) 41 if err != nil { 42 return nil, err 43 } 44 45 resp, err := broadcaster.Recv() 46 if err != nil { 47 return nil, err 48 } 49 50 return resp, nil 51 } 52 53 // Deliver sends given env to Deliver API of specified orderer. 54 func Deliver(n *Network, o *Orderer, env *common.Envelope) (*common.Block, error) { 55 gRPCclient, err := createOrdererGRPCClient(n, o) 56 if err != nil { 57 return nil, err 58 } 59 60 addr := n.OrdererAddress(o, ListenPort) 61 conn, err := gRPCclient.NewConnection(addr) 62 if err != nil { 63 return nil, err 64 } 65 defer conn.Close() 66 67 deliverer, err := orderer.NewAtomicBroadcastClient(conn).Deliver(context.Background()) 68 if err != nil { 69 return nil, err 70 } 71 72 err = deliverer.Send(env) 73 if err != nil { 74 return nil, err 75 } 76 77 resp, err := deliverer.Recv() 78 if err != nil { 79 return nil, err 80 } 81 82 blk := resp.GetBlock() 83 if blk == nil { 84 return nil, errors.Errorf("block not found") 85 } 86 87 return blk, nil 88 } 89 90 func createOrdererGRPCClient(n *Network, o *Orderer) (*comm.GRPCClient, error) { 91 config := comm.ClientConfig{} 92 config.Timeout = 5 * time.Second 93 94 secOpts := comm.SecureOptions{ 95 UseTLS: true, 96 RequireClientCert: false, 97 } 98 99 caPEM, err := ioutil.ReadFile(path.Join(n.OrdererLocalTLSDir(o), "ca.crt")) 100 if err != nil { 101 return nil, err 102 } 103 104 secOpts.ServerRootCAs = [][]byte{caPEM} 105 config.SecOpts = secOpts 106 107 grpcClient, err := comm.NewGRPCClient(config) 108 if err != nil { 109 return nil, err 110 } 111 112 return grpcClient, nil 113 }