github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/common/ledger/blkstorage/blockstore.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package blkstorage 8 9 import ( 10 "time" 11 12 "github.com/hyperledger/fabric-protos-go/common" 13 "github.com/hyperledger/fabric-protos-go/peer" 14 "github.com/osdi23p228/fabric/common/ledger" 15 "github.com/osdi23p228/fabric/common/ledger/snapshot" 16 "github.com/osdi23p228/fabric/common/ledger/util/leveldbhelper" 17 ) 18 19 // BlockStore - filesystem based implementation for `BlockStore` 20 type BlockStore struct { 21 id string 22 conf *Conf 23 fileMgr *blockfileMgr 24 stats *ledgerStats 25 } 26 27 // newBlockStore constructs a `BlockStore` 28 func newBlockStore(id string, conf *Conf, indexConfig *IndexConfig, 29 dbHandle *leveldbhelper.DBHandle, stats *stats) (*BlockStore, error) { 30 fileMgr, err := newBlockfileMgr(id, conf, indexConfig, dbHandle) 31 if err != nil { 32 return nil, err 33 } 34 35 // create ledgerStats and initialize blockchain_height stat 36 ledgerStats := stats.ledgerStats(id) 37 info := fileMgr.getBlockchainInfo() 38 ledgerStats.updateBlockchainHeight(info.Height) 39 40 return &BlockStore{id, conf, fileMgr, ledgerStats}, nil 41 } 42 43 // AddBlock adds a new block 44 func (store *BlockStore) AddBlock(block *common.Block) error { 45 // track elapsed time to collect block commit time 46 startBlockCommit := time.Now() 47 result := store.fileMgr.addBlock(block) 48 elapsedBlockCommit := time.Since(startBlockCommit) 49 50 store.updateBlockStats(block.Header.Number, elapsedBlockCommit) 51 52 return result 53 } 54 55 // GetBlockchainInfo returns the current info about blockchain 56 func (store *BlockStore) GetBlockchainInfo() (*common.BlockchainInfo, error) { 57 return store.fileMgr.getBlockchainInfo(), nil 58 } 59 60 // RetrieveBlocks returns an iterator that can be used for iterating over a range of blocks 61 func (store *BlockStore) RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error) { 62 return store.fileMgr.retrieveBlocks(startNum) 63 } 64 65 // RetrieveBlockByHash returns the block for given block-hash 66 func (store *BlockStore) RetrieveBlockByHash(blockHash []byte) (*common.Block, error) { 67 return store.fileMgr.retrieveBlockByHash(blockHash) 68 } 69 70 // RetrieveBlockByNumber returns the block at a given blockchain height 71 func (store *BlockStore) RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) { 72 return store.fileMgr.retrieveBlockByNumber(blockNum) 73 } 74 75 // RetrieveTxByID returns a transaction for given transaction id 76 func (store *BlockStore) RetrieveTxByID(txID string) (*common.Envelope, error) { 77 return store.fileMgr.retrieveTransactionByID(txID) 78 } 79 80 // RetrieveTxByBlockNumTranNum returns a transaction for the given <blockNum, tranNum> 81 func (store *BlockStore) RetrieveTxByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error) { 82 return store.fileMgr.retrieveTransactionByBlockNumTranNum(blockNum, tranNum) 83 } 84 85 // RetrieveBlockByTxID returns the block for the specified txID 86 func (store *BlockStore) RetrieveBlockByTxID(txID string) (*common.Block, error) { 87 return store.fileMgr.retrieveBlockByTxID(txID) 88 } 89 90 // RetrieveTxValidationCodeByTxID returns the validation code for the specified txID 91 func (store *BlockStore) RetrieveTxValidationCodeByTxID(txID string) (peer.TxValidationCode, error) { 92 return store.fileMgr.retrieveTxValidationCodeByTxID(txID) 93 } 94 95 // ExportTxIds creates two files in the specified dir and returns a map that contains 96 // the mapping between the names of the files and their hashes. 97 // Technically, the TxIDs appear in the sort order of radix-sort/shortlex. However, 98 // since practically all the TxIDs are of same length, so the sort order would be the lexical sort order 99 func (store *BlockStore) ExportTxIds(dir string, newHashFunc snapshot.NewHashFunc) (map[string][]byte, error) { 100 return store.fileMgr.index.exportUniqueTxIDs(dir, newHashFunc) 101 } 102 103 // Shutdown shuts down the block store 104 func (store *BlockStore) Shutdown() { 105 logger.Debugf("closing fs blockStore:%s", store.id) 106 store.fileMgr.close() 107 } 108 109 func (store *BlockStore) updateBlockStats(blockNum uint64, blockstorageCommitTime time.Duration) { 110 store.stats.updateBlockchainHeight(blockNum + 1) 111 store.stats.updateBlockstorageCommitTime(blockstorageCommitTime) 112 }