github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/peer/common/ordererclient.go (about)

     1  /*
     2  Copyright hechain. 2016-2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  package common
     7  
     8  import (
     9  	"context"
    10  	"crypto/tls"
    11  
    12  	ab "github.com/hyperledger/fabric-protos-go/orderer"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  // OrdererClient represents a client for communicating with an ordering
    17  // service
    18  type OrdererClient struct {
    19  	*CommonClient
    20  }
    21  
    22  // NewOrdererClientFromEnv creates an instance of an OrdererClient from the
    23  // global Viper instance
    24  func NewOrdererClientFromEnv() (*OrdererClient, error) {
    25  	address, clientConfig, err := configFromEnv("orderer")
    26  	if err != nil {
    27  		return nil, errors.WithMessage(err, "failed to load config for OrdererClient")
    28  	}
    29  	cc, err := newCommonClient(address, clientConfig)
    30  	if err != nil {
    31  		return nil, errors.WithMessage(err, "failed to create OrdererClient from config")
    32  	}
    33  	return &OrdererClient{CommonClient: cc}, nil
    34  }
    35  
    36  // Broadcast returns a broadcast client for the AtomicBroadcast service
    37  func (oc *OrdererClient) Broadcast() (ab.AtomicBroadcast_BroadcastClient, error) {
    38  	conn, err := oc.CommonClient.clientConfig.Dial(oc.address)
    39  	if err != nil {
    40  		return nil, errors.WithMessagef(err, "orderer client failed to connect to %s", oc.address)
    41  	}
    42  	// TODO: check to see if we should actually handle error before returning
    43  	return ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO())
    44  }
    45  
    46  // Deliver returns a deliver client for the AtomicBroadcast service
    47  func (oc *OrdererClient) Deliver() (ab.AtomicBroadcast_DeliverClient, error) {
    48  	conn, err := oc.CommonClient.clientConfig.Dial(oc.address)
    49  	if err != nil {
    50  		return nil, errors.WithMessagef(err, "orderer client failed to connect to %s", oc.address)
    51  	}
    52  	// TODO: check to see if we should actually handle error before returning
    53  	return ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO())
    54  }
    55  
    56  // Certificate returns the TLS client certificate (if available)
    57  func (oc *OrdererClient) Certificate() tls.Certificate {
    58  	return oc.CommonClient.Certificate()
    59  }