github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/internal/peer/common/ordererclient.go (about)

     1  /*
     2  Copyright IBM Corp. 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/osdi23p228/fabric/internal/pkg/comm"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // OrdererClient represents a client for communicating with an ordering
    18  // service
    19  type OrdererClient struct {
    20  	CommonClient
    21  }
    22  
    23  // NewOrdererClientFromEnv creates an instance of an OrdererClient from the
    24  // global Viper instance
    25  func NewOrdererClientFromEnv() (*OrdererClient, error) {
    26  	address, override, clientConfig, err := configFromEnv("orderer")
    27  	if err != nil {
    28  		return nil, errors.WithMessage(err, "failed to load config for OrdererClient")
    29  	}
    30  	gClient, err := comm.NewGRPCClient(clientConfig)
    31  	if err != nil {
    32  		return nil, errors.WithMessage(err, "failed to create OrdererClient from config")
    33  	}
    34  	oClient := &OrdererClient{
    35  		CommonClient: CommonClient{
    36  			GRPCClient: gClient,
    37  			Address:    address,
    38  			sn:         override,
    39  		},
    40  	}
    41  	return oClient, nil
    42  }
    43  
    44  // Broadcast returns a broadcast client for the AtomicBroadcast service
    45  func (oc *OrdererClient) Broadcast() (ab.AtomicBroadcast_BroadcastClient, error) {
    46  	conn, err := oc.CommonClient.NewConnection(oc.Address, comm.ServerNameOverride(oc.sn))
    47  	if err != nil {
    48  		return nil, errors.WithMessagef(err, "orderer client failed to connect to %s", oc.Address)
    49  	}
    50  	// TODO: check to see if we should actually handle error before returning
    51  	return ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO())
    52  }
    53  
    54  // Deliver returns a deliver client for the AtomicBroadcast service
    55  func (oc *OrdererClient) Deliver() (ab.AtomicBroadcast_DeliverClient, error) {
    56  	conn, err := oc.CommonClient.NewConnection(oc.Address, comm.ServerNameOverride(oc.sn))
    57  	if err != nil {
    58  		return nil, errors.WithMessagef(err, "orderer client failed to connect to %s", oc.Address)
    59  	}
    60  	// TODO: check to see if we should actually handle error before returning
    61  	return ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO())
    62  
    63  }
    64  
    65  // Certificate returns the TLS client certificate (if available)
    66  func (oc *OrdererClient) Certificate() tls.Certificate {
    67  	return oc.CommonClient.Certificate()
    68  }