github.com/true-sqn/fabric@v2.1.1+incompatible/core/committer/committer_impl.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package committer
     8  
     9  import (
    10  	"github.com/hyperledger/fabric-protos-go/common"
    11  	"github.com/hyperledger/fabric/common/flogging"
    12  	"github.com/hyperledger/fabric/core/ledger"
    13  )
    14  
    15  var logger = flogging.MustGetLogger("committer")
    16  
    17  //--------!!!IMPORTANT!!-!!IMPORTANT!!-!!IMPORTANT!!---------
    18  // This is used merely to complete the loop for the "skeleton"
    19  // path so we can reason about and  modify committer component
    20  // more effectively using code.
    21  
    22  // PeerLedgerSupport abstract out the API's of ledger.PeerLedger interface
    23  // required to implement LedgerCommitter
    24  type PeerLedgerSupport interface {
    25  	GetPvtDataAndBlockByNum(blockNum uint64, filter ledger.PvtNsCollFilter) (*ledger.BlockAndPvtData, error)
    26  
    27  	GetPvtDataByNum(blockNum uint64, filter ledger.PvtNsCollFilter) ([]*ledger.TxPvtData, error)
    28  
    29  	CommitLegacy(blockAndPvtdata *ledger.BlockAndPvtData, commitOpts *ledger.CommitOptions) error
    30  
    31  	CommitPvtDataOfOldBlocks(reconciledPvtdata []*ledger.ReconciledPvtdata) ([]*ledger.PvtdataHashMismatch, error)
    32  
    33  	GetBlockchainInfo() (*common.BlockchainInfo, error)
    34  
    35  	DoesPvtDataInfoExist(blockNum uint64) (bool, error)
    36  
    37  	GetBlockByNumber(blockNumber uint64) (*common.Block, error)
    38  
    39  	GetConfigHistoryRetriever() (ledger.ConfigHistoryRetriever, error)
    40  
    41  	GetMissingPvtDataTracker() (ledger.MissingPvtDataTracker, error)
    42  
    43  	Close()
    44  }
    45  
    46  // LedgerCommitter is the implementation of  Committer interface
    47  // it keeps the reference to the ledger to commit blocks and retrieve
    48  // chain information
    49  type LedgerCommitter struct {
    50  	PeerLedgerSupport
    51  }
    52  
    53  // NewLedgerCommitter is a factory function to create an instance of the committer
    54  // which passes incoming blocks via validation and commits them into the ledger.
    55  func NewLedgerCommitter(ledger PeerLedgerSupport) *LedgerCommitter {
    56  	return &LedgerCommitter{PeerLedgerSupport: ledger}
    57  }
    58  
    59  // CommitLegacy commits blocks atomically with private data
    60  func (lc *LedgerCommitter) CommitLegacy(blockAndPvtData *ledger.BlockAndPvtData, commitOpts *ledger.CommitOptions) error {
    61  	// Committing new block
    62  	if err := lc.PeerLedgerSupport.CommitLegacy(blockAndPvtData, commitOpts); err != nil {
    63  		return err
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  // GetPvtDataAndBlockByNum retrieves private data and block for given sequence number
    70  func (lc *LedgerCommitter) GetPvtDataAndBlockByNum(seqNum uint64) (*ledger.BlockAndPvtData, error) {
    71  	return lc.PeerLedgerSupport.GetPvtDataAndBlockByNum(seqNum, nil)
    72  }
    73  
    74  // LedgerHeight returns recently committed block sequence number
    75  func (lc *LedgerCommitter) LedgerHeight() (uint64, error) {
    76  	info, err := lc.GetBlockchainInfo()
    77  	if err != nil {
    78  		logger.Errorf("Cannot get blockchain info, %s", info)
    79  		return 0, err
    80  	}
    81  
    82  	return info.Height, nil
    83  }
    84  
    85  // DoesPvtDataInfoExistInLedger returns true if the ledger has pvtdata info
    86  // about a given block number.
    87  func (lc *LedgerCommitter) DoesPvtDataInfoExistInLedger(blockNum uint64) (bool, error) {
    88  	return lc.DoesPvtDataInfoExist(blockNum)
    89  }
    90  
    91  // GetBlocks used to retrieve blocks with sequence numbers provided in the slice
    92  func (lc *LedgerCommitter) GetBlocks(blockSeqs []uint64) []*common.Block {
    93  	var blocks []*common.Block
    94  
    95  	for _, seqNum := range blockSeqs {
    96  		if blck, err := lc.GetBlockByNumber(seqNum); err != nil {
    97  			logger.Errorf("Not able to acquire block num %d, from the ledger skipping...", seqNum)
    98  			continue
    99  		} else {
   100  			logger.Debug("Appending next block with seqNum = ", seqNum, " to the resulting set")
   101  			blocks = append(blocks, blck)
   102  		}
   103  	}
   104  
   105  	return blocks
   106  }