代码拉取完成,页面将自动刷新
/*
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。