github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/ledger/blkstorage/fsblkstorage/fs_blockstore.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package fsblkstorage
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/hyperledger/fabric-protos-go/common"
    23  	"github.com/hyperledger/fabric-protos-go/peer"
    24  	"github.com/hyperledger/fabric/common/ledger"
    25  	"github.com/hyperledger/fabric/common/ledger/blkstorage"
    26  	"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
    27  )
    28  
    29  // fsBlockStore - filesystem based implementation for `BlockStore`
    30  type fsBlockStore struct {
    31  	id      string
    32  	conf    *Conf
    33  	fileMgr *blockfileMgr
    34  	stats   *ledgerStats
    35  }
    36  
    37  // NewFsBlockStore constructs a `FsBlockStore`
    38  func newFsBlockStore(id string, conf *Conf, indexConfig *blkstorage.IndexConfig,
    39  	dbHandle *leveldbhelper.DBHandle, stats *stats) *fsBlockStore {
    40  	fileMgr := newBlockfileMgr(id, conf, indexConfig, dbHandle)
    41  
    42  	// create ledgerStats and initialize blockchain_height stat
    43  	ledgerStats := stats.ledgerStats(id)
    44  	info := fileMgr.getBlockchainInfo()
    45  	ledgerStats.updateBlockchainHeight(info.Height)
    46  
    47  	return &fsBlockStore{id, conf, fileMgr, ledgerStats}
    48  }
    49  
    50  // AddBlock adds a new block
    51  func (store *fsBlockStore) AddBlock(block *common.Block) error {
    52  	// track elapsed time to collect block commit time
    53  	startBlockCommit := time.Now()
    54  	result := store.fileMgr.addBlock(block)
    55  	elapsedBlockCommit := time.Since(startBlockCommit)
    56  
    57  	store.updateBlockStats(block.Header.Number, elapsedBlockCommit)
    58  
    59  	return result
    60  }
    61  
    62  // GetBlockchainInfo returns the current info about blockchain
    63  func (store *fsBlockStore) GetBlockchainInfo() (*common.BlockchainInfo, error) {
    64  	return store.fileMgr.getBlockchainInfo(), nil
    65  }
    66  
    67  // RetrieveBlocks returns an iterator that can be used for iterating over a range of blocks
    68  func (store *fsBlockStore) RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error) {
    69  	return store.fileMgr.retrieveBlocks(startNum)
    70  }
    71  
    72  // RetrieveBlockByHash returns the block for given block-hash
    73  func (store *fsBlockStore) RetrieveBlockByHash(blockHash []byte) (*common.Block, error) {
    74  	return store.fileMgr.retrieveBlockByHash(blockHash)
    75  }
    76  
    77  // RetrieveBlockByNumber returns the block at a given blockchain height
    78  func (store *fsBlockStore) RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) {
    79  	return store.fileMgr.retrieveBlockByNumber(blockNum)
    80  }
    81  
    82  // RetrieveTxByID returns a transaction for given transaction id
    83  func (store *fsBlockStore) RetrieveTxByID(txID string) (*common.Envelope, error) {
    84  	return store.fileMgr.retrieveTransactionByID(txID)
    85  }
    86  
    87  // RetrieveTxByID returns a transaction for given transaction id
    88  func (store *fsBlockStore) RetrieveTxByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error) {
    89  	return store.fileMgr.retrieveTransactionByBlockNumTranNum(blockNum, tranNum)
    90  }
    91  
    92  func (store *fsBlockStore) RetrieveBlockByTxID(txID string) (*common.Block, error) {
    93  	return store.fileMgr.retrieveBlockByTxID(txID)
    94  }
    95  
    96  func (store *fsBlockStore) RetrieveTxValidationCodeByTxID(txID string) (peer.TxValidationCode, error) {
    97  	return store.fileMgr.retrieveTxValidationCodeByTxID(txID)
    98  }
    99  
   100  // Shutdown shuts down the block store
   101  func (store *fsBlockStore) Shutdown() {
   102  	logger.Debugf("closing fs blockStore:%s", store.id)
   103  	store.fileMgr.close()
   104  }
   105  
   106  func (store *fsBlockStore) updateBlockStats(blockNum uint64, blockstorageCommitTime time.Duration) {
   107  	store.stats.updateBlockchainHeight(blockNum + 1)
   108  	store.stats.updateBlockstorageCommitTime(blockstorageCommitTime)
   109  }