1 Star 0 Fork 0

13683679291/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
orderer_client.go 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
Gari Singh 提交于 2020-03-30 10:52 . Move comm pkg to internal
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package nwo
import (
"context"
"io/ioutil"
"path"
"time"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/orderer"
"github.com/hyperledger/fabric/internal/pkg/comm"
"github.com/pkg/errors"
)
// Broadcast sends given env to Broadcast API of specified orderer.
func Broadcast(n *Network, o *Orderer, env *common.Envelope) (*orderer.BroadcastResponse, error) {
gRPCclient, err := createOrdererGRPCClient(n, o)
if err != nil {
return nil, err
}
addr := n.OrdererAddress(o, ListenPort)
conn, err := gRPCclient.NewConnection(addr)
if err != nil {
return nil, err
}
defer conn.Close()
broadcaster, err := orderer.NewAtomicBroadcastClient(conn).Broadcast(context.Background())
if err != nil {
return nil, err
}
err = broadcaster.Send(env)
if err != nil {
return nil, err
}
resp, err := broadcaster.Recv()
if err != nil {
return nil, err
}
return resp, nil
}
// Deliver sends given env to Deliver API of specified orderer.
func Deliver(n *Network, o *Orderer, env *common.Envelope) (*common.Block, error) {
gRPCclient, err := createOrdererGRPCClient(n, o)
if err != nil {
return nil, err
}
addr := n.OrdererAddress(o, ListenPort)
conn, err := gRPCclient.NewConnection(addr)
if err != nil {
return nil, err
}
defer conn.Close()
deliverer, err := orderer.NewAtomicBroadcastClient(conn).Deliver(context.Background())
if err != nil {
return nil, err
}
err = deliverer.Send(env)
if err != nil {
return nil, err
}
resp, err := deliverer.Recv()
if err != nil {
return nil, err
}
blk := resp.GetBlock()
if blk == nil {
return nil, errors.Errorf("block not found")
}
return blk, nil
}
func createOrdererGRPCClient(n *Network, o *Orderer) (*comm.GRPCClient, error) {
config := comm.ClientConfig{}
config.Timeout = 5 * time.Second
secOpts := comm.SecureOptions{
UseTLS: true,
RequireClientCert: false,
}
caPEM, err := ioutil.ReadFile(path.Join(n.OrdererLocalTLSDir(o), "ca.crt"))
if err != nil {
return nil, err
}
secOpts.ServerRootCAs = [][]byte{caPEM}
config.SecOpts = secOpts
grpcClient, err := comm.NewGRPCClient(config)
if err != nil {
return nil, err
}
return grpcClient, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mmcro/fabric.git
git@gitee.com:mmcro/fabric.git
mmcro
fabric
fabric
v2.1.1

搜索帮助