github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/orderer/consensus/etcdraft/blockpuller.go (about) 1 /* 2 Copyright hechain. 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/hechain20/hechain/bccsp" 13 "github.com/hechain20/hechain/common/flogging" 14 "github.com/hechain20/hechain/orderer/common/cluster" 15 "github.com/hechain20/hechain/orderer/common/localconfig" 16 "github.com/hechain20/hechain/orderer/consensus" 17 "github.com/hyperledger/fabric-protos-go/common" 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 verifyBlockSequence := func(blocks []*common.Block, _ string) error { 69 return cluster.VerifyBlocks(blocks, support) 70 } 71 72 stdDialer := &cluster.StandardDialer{ 73 Config: baseDialer.Config, 74 } 75 stdDialer.Config.AsyncConnect = false 76 stdDialer.Config.SecOpts.VerifyCertificate = nil 77 78 // Extract the TLS CA certs and endpoints from the configuration, 79 endpoints, err := EndpointconfigFromSupport(support, bccsp) 80 if err != nil { 81 return nil, err 82 } 83 84 der, _ := pem.Decode(stdDialer.Config.SecOpts.Certificate) 85 if der == nil { 86 return nil, errors.Errorf("client certificate isn't in PEM format: %v", 87 string(stdDialer.Config.SecOpts.Certificate)) 88 } 89 90 bp := &cluster.BlockPuller{ 91 VerifyBlockSequence: verifyBlockSequence, 92 Logger: flogging.MustGetLogger("orderer.common.cluster.puller").With("channel", support.ChannelID()), 93 RetryTimeout: clusterConfig.ReplicationRetryTimeout, 94 MaxTotalBufferBytes: clusterConfig.ReplicationBufferSize, 95 FetchTimeout: clusterConfig.ReplicationPullTimeout, 96 Endpoints: endpoints, 97 Signer: support, 98 TLSCert: der.Bytes, 99 Channel: support.ChannelID(), 100 Dialer: stdDialer, 101 StopChannel: make(chan struct{}), 102 } 103 104 return &LedgerBlockPuller{ 105 Height: support.Height, 106 BlockRetriever: support, 107 BlockPuller: bp, 108 }, nil 109 }