github.com/true-sqn/fabric@v2.1.1+incompatible/orderer/consensus/etcdraft/blockpuller.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package etcdraft
     8  
     9  import (
    10  	"encoding/pem"
    11  
    12  	"github.com/hyperledger/fabric-protos-go/common"
    13  	"github.com/hyperledger/fabric/bccsp"
    14  	"github.com/hyperledger/fabric/common/flogging"
    15  	"github.com/hyperledger/fabric/orderer/common/cluster"
    16  	"github.com/hyperledger/fabric/orderer/common/localconfig"
    17  	"github.com/hyperledger/fabric/orderer/consensus"
    18  	"github.com/pkg/errors"
    19  )
    20  
    21  // LedgerBlockPuller pulls blocks upon demand, or fetches them from the ledger
    22  type LedgerBlockPuller struct {
    23  	BlockPuller
    24  	BlockRetriever cluster.BlockRetriever
    25  	Height         func() uint64
    26  }
    27  
    28  func (lp *LedgerBlockPuller) PullBlock(seq uint64) *common.Block {
    29  	lastSeq := lp.Height() - 1
    30  	if lastSeq >= seq {
    31  		return lp.BlockRetriever.Block(seq)
    32  	}
    33  	return lp.BlockPuller.PullBlock(seq)
    34  }
    35  
    36  // EndpointconfigFromSupport extracts TLS CA certificates and endpoints from the ConsenterSupport
    37  func EndpointconfigFromSupport(support consensus.ConsenterSupport, bccsp bccsp.BCCSP) ([]cluster.EndpointCriteria, error) {
    38  	lastConfigBlock, err := lastConfigBlockFromSupport(support)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	endpointconf, err := cluster.EndpointconfigFromConfigBlock(lastConfigBlock, bccsp)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	return endpointconf, nil
    47  }
    48  
    49  func lastConfigBlockFromSupport(support consensus.ConsenterSupport) (*common.Block, error) {
    50  	lastBlockSeq := support.Height() - 1
    51  	lastBlock := support.Block(lastBlockSeq)
    52  	if lastBlock == nil {
    53  		return nil, errors.Errorf("unable to retrieve block [%d]", lastBlockSeq)
    54  	}
    55  	lastConfigBlock, err := cluster.LastConfigBlock(lastBlock, support)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return lastConfigBlock, nil
    60  }
    61  
    62  // NewBlockPuller creates a new block puller
    63  func NewBlockPuller(support consensus.ConsenterSupport,
    64  	baseDialer *cluster.PredicateDialer,
    65  	clusterConfig localconfig.Cluster,
    66  	bccsp bccsp.BCCSP,
    67  ) (BlockPuller, error) {
    68  
    69  	verifyBlockSequence := func(blocks []*common.Block, _ string) error {
    70  		return cluster.VerifyBlocks(blocks, support)
    71  	}
    72  
    73  	stdDialer := &cluster.StandardDialer{
    74  		Config: baseDialer.Config.Clone(),
    75  	}
    76  	stdDialer.Config.AsyncConnect = false
    77  	stdDialer.Config.SecOpts.VerifyCertificate = nil
    78  
    79  	// Extract the TLS CA certs and endpoints from the configuration,
    80  	endpoints, err := EndpointconfigFromSupport(support, bccsp)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	der, _ := pem.Decode(stdDialer.Config.SecOpts.Certificate)
    86  	if der == nil {
    87  		return nil, errors.Errorf("client certificate isn't in PEM format: %v",
    88  			string(stdDialer.Config.SecOpts.Certificate))
    89  	}
    90  
    91  	bp := &cluster.BlockPuller{
    92  		VerifyBlockSequence: verifyBlockSequence,
    93  		Logger:              flogging.MustGetLogger("orderer.common.cluster.puller").With("channel", support.ChannelID()),
    94  		RetryTimeout:        clusterConfig.ReplicationRetryTimeout,
    95  		MaxTotalBufferBytes: clusterConfig.ReplicationBufferSize,
    96  		FetchTimeout:        clusterConfig.ReplicationPullTimeout,
    97  		Endpoints:           endpoints,
    98  		Signer:              support,
    99  		TLSCert:             der.Bytes,
   100  		Channel:             support.ChannelID(),
   101  		Dialer:              stdDialer,
   102  	}
   103  
   104  	return &LedgerBlockPuller{
   105  		Height:         support.Height,
   106  		BlockRetriever: support,
   107  		BlockPuller:    bp,
   108  	}, nil
   109  }