Skip to content

Commit

Permalink
Remove custom error types
Browse files Browse the repository at this point in the history
This commit removes the custom error types that are not used by the consumers

Signed-off-by: manish <manish.sethi@gmail.com>
  • Loading branch information
manish-sethi committed Nov 12, 2020
1 parent 4f1e093 commit ded8734
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 83 deletions.
12 changes: 6 additions & 6 deletions common/ledger/blkstorage/blockfile_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func TestBlockfileMgrBlockReadWrite(t *testing.T) {
defer blkfileMgrWrapper.close()
blocks := testutil.ConstructTestBlocks(t, 10)
blkfileMgrWrapper.addBlocks(blocks)
blkfileMgrWrapper.testGetBlockByHash(blocks, nil)
blkfileMgrWrapper.testGetBlockByNumber(blocks, 0, nil)
blkfileMgrWrapper.testGetBlockByHash(blocks)
blkfileMgrWrapper.testGetBlockByNumber(blocks)
}

func TestAddBlockWithWrongHash(t *testing.T) {
Expand Down Expand Up @@ -384,7 +384,7 @@ func TestBlockfileMgrRestart(t *testing.T) {
blkfileMgrWrapper = newTestBlockfileWrapper(env, ledgerid)
defer blkfileMgrWrapper.close()
require.Equal(t, 9, int(blkfileMgrWrapper.blockfileMgr.blockfilesInfo.lastPersistedBlock))
blkfileMgrWrapper.testGetBlockByHash(blocks, nil)
blkfileMgrWrapper.testGetBlockByHash(blocks)
require.Equal(t, expectedHeight, blkfileMgrWrapper.blockfileMgr.getBlockchainInfo().Height)
}

Expand All @@ -406,14 +406,14 @@ func TestBlockfileMgrFileRolling(t *testing.T) {
blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
blkfileMgrWrapper.addBlocks(blocks[:100])
require.Equal(t, 1, blkfileMgrWrapper.blockfileMgr.blockfilesInfo.latestFileNumber)
blkfileMgrWrapper.testGetBlockByHash(blocks[:100], nil)
blkfileMgrWrapper.testGetBlockByHash(blocks[:100])
blkfileMgrWrapper.close()

blkfileMgrWrapper = newTestBlockfileWrapper(env, ledgerid)
defer blkfileMgrWrapper.close()
blkfileMgrWrapper.addBlocks(blocks[100:])
require.Equal(t, 2, blkfileMgrWrapper.blockfileMgr.blockfilesInfo.latestFileNumber)
blkfileMgrWrapper.testGetBlockByHash(blocks[100:], nil)
blkfileMgrWrapper.testGetBlockByHash(blocks[100:])
}

func TestBlockfileMgrGetBlockByTxID(t *testing.T) {
Expand Down Expand Up @@ -500,7 +500,7 @@ func testBlockfileMgrSimulateCrashAtFirstBlockInFile(t *testing.T, deleteBlkfile
blkfileMgrWrapper.addBlocks(blocks[5:])
require.True(t, testutilGetFileSize(t, lastFilePath) > 0)
require.Equal(t, firstBlkFileSize, testutilGetFileSize(t, firstFilePath))
blkfileMgrWrapper.testGetBlockByNumber(blocks, 0, nil)
blkfileMgrWrapper.testGetBlockByNumber(blocks)
testBlockfileMgrBlockIterator(t, blkfileMgrWrapper.blockfileMgr, 0, len(blocks)-1, blocks)
}

Expand Down
32 changes: 19 additions & 13 deletions common/ledger/blkstorage/blockindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,33 +161,37 @@ func (index *blockIndex) isAttributeIndexed(attribute IndexableAttr) bool {

func (index *blockIndex) getBlockLocByHash(blockHash []byte) (*fileLocPointer, error) {
if !index.isAttributeIndexed(IndexableAttrBlockHash) {
return nil, ErrAttrNotIndexed
return nil, errors.New("block hashes not maintained in index")
}
b, err := index.db.Get(constructBlockHashKey(blockHash))
if err != nil {
return nil, err
}
if b == nil {
return nil, ErrNotFoundInIndex
return nil, errors.Errorf("no such block hash [%x] in index", blockHash)
}
blkLoc := &fileLocPointer{}
blkLoc.unmarshal(b)
if err := blkLoc.unmarshal(b); err != nil {
return nil, err
}
return blkLoc, nil
}

func (index *blockIndex) getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer, error) {
if !index.isAttributeIndexed(IndexableAttrBlockNum) {
return nil, ErrAttrNotIndexed
return nil, errors.New("block numbers not maintained in index")
}
b, err := index.db.Get(constructBlockNumKey(blockNum))
if err != nil {
return nil, err
}
if b == nil {
return nil, ErrNotFoundInIndex
return nil, errors.Errorf("no such block number [%d] in index", blockNum)
}
blkLoc := &fileLocPointer{}
blkLoc.unmarshal(b)
if err := blkLoc.unmarshal(b); err != nil {
return nil, err
}
return blkLoc, nil
}

Expand Down Expand Up @@ -225,7 +229,7 @@ func (index *blockIndex) getTxValidationCodeByTxID(txID string) (peer.TxValidati

func (index *blockIndex) txIDExists(txID string) (bool, error) {
if !index.isAttributeIndexed(IndexableAttrTxID) {
return false, ErrAttrNotIndexed
return false, errors.New("transaction IDs not maintained in index")
}
rangeScan := constructTxIDRangeScan(txID)
itr, err := index.db.GetIterator(rangeScan.startKey, rangeScan.stopKey)
Expand All @@ -243,7 +247,7 @@ func (index *blockIndex) txIDExists(txID string) (bool, error) {

func (index *blockIndex) getTxIDVal(txID string) (*TxIDIndexValue, error) {
if !index.isAttributeIndexed(IndexableAttrTxID) {
return nil, ErrAttrNotIndexed
return nil, errors.New("transaction IDs not maintained in index")
}
rangeScan := constructTxIDRangeScan(txID)
itr, err := index.db.GetIterator(rangeScan.startKey, rangeScan.stopKey)
Expand All @@ -257,7 +261,7 @@ func (index *blockIndex) getTxIDVal(txID string) (*TxIDIndexValue, error) {
return nil, errors.Wrapf(err, "error while trying to retrieve transaction info by TXID [%s]", txID)
}
if !present {
return nil, ErrNotFoundInIndex
return nil, errors.Errorf("no such transaction ID [%s] in index", txID)
}
valBytes := itr.Value()
if len(valBytes) == 0 {
Expand All @@ -272,23 +276,25 @@ func (index *blockIndex) getTxIDVal(txID string) (*TxIDIndexValue, error) {

func (index *blockIndex) getTXLocByBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error) {
if !index.isAttributeIndexed(IndexableAttrBlockNumTranNum) {
return nil, ErrAttrNotIndexed
return nil, errors.New("<blockNumber, transactionNumber> tuple not maintained in index")
}
b, err := index.db.Get(constructBlockNumTranNumKey(blockNum, tranNum))
if err != nil {
return nil, err
}
if b == nil {
return nil, ErrNotFoundInIndex
return nil, errors.Errorf("no such blockNumber, transactionNumber <%d, %d> in index", blockNum, tranNum)
}
txFLP := &fileLocPointer{}
txFLP.unmarshal(b)
if err := txFLP.unmarshal(b); err != nil {
return nil, err
}
return txFLP, nil
}

func (index *blockIndex) exportUniqueTxIDs(dir string, newHashFunc snapshot.NewHashFunc) (map[string][]byte, error) {
if !index.isAttributeIndexed(IndexableAttrTxID) {
return nil, ErrAttrNotIndexed
return nil, errors.New("transaction IDs not maintained in index")
}

dbItr, err := index.db.GetIterator([]byte{txIDIdxKeyPrefix}, []byte{txIDIdxKeyPrefix + 1})
Expand Down
22 changes: 10 additions & 12 deletions common/ledger/blkstorage/blockindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func testBlockIndexSync(t *testing.T, numBlocks int, numBlocksToIndex int, syncB
// Before, we test for index sync-up, verify that the last set of blocks not indexed in the original index
for i := numBlocksToIndex + 1; i <= numBlocks; i++ {
_, err := blkfileMgr.retrieveBlockByNumber(uint64(i))
require.Exactly(t, ErrNotFoundInIndex, err)
require.EqualError(t, err, fmt.Sprintf("no such block number [%d] in index", i))
}

// perform index sync
Expand Down Expand Up @@ -125,7 +125,7 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []IndexableAttr) {
require.NoError(t, err, "Error while retrieving block by hash")
require.Equal(t, blocks[0], block)
} else {
require.Exactly(t, ErrAttrNotIndexed, err)
require.EqualError(t, err, "block hashes not maintained in index")
}

// test 'retrieveBlockByNumber'
Expand All @@ -134,7 +134,7 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []IndexableAttr) {
require.NoError(t, err, "Error while retrieving block by number")
require.Equal(t, blocks[0], block)
} else {
require.Exactly(t, ErrAttrNotIndexed, err)
require.EqualError(t, err, "block numbers not maintained in index")
}

// test 'retrieveTransactionByID'
Expand All @@ -148,7 +148,7 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []IndexableAttr) {
require.NoError(t, err)
require.Equal(t, txEnvelopeOrig, txEnvelope)
} else {
require.Exactly(t, ErrAttrNotIndexed, err)
require.EqualError(t, err, "transaction IDs not maintained in index")
}

// test txIDExists
Expand All @@ -159,7 +159,7 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []IndexableAttr) {
require.NoError(t, err)
require.True(t, exists)
} else {
require.Exactly(t, ErrAttrNotIndexed, err)
require.EqualError(t, err, "transaction IDs not maintained in index")
}

//test 'retrieveTrasnactionsByBlockNumTranNum
Expand All @@ -171,7 +171,7 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []IndexableAttr) {
require.NoError(t, err2)
require.Equal(t, txEnvelopeOrig2, txEnvelope2)
} else {
require.Exactly(t, ErrAttrNotIndexed, err)
require.EqualError(t, err, "<blockNumber, transactionNumber> tuple not maintained in index")
}

// test 'retrieveBlockByTxID'
Expand All @@ -182,7 +182,7 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []IndexableAttr) {
require.NoError(t, err, "Error while retrieving block by txID")
require.Equal(t, block, blocks[0])
} else {
require.Exactly(t, ErrAttrNotIndexed, err)
require.EqualError(t, err, "transaction IDs not maintained in index")
}

for _, block := range blocks {
Expand All @@ -195,13 +195,11 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []IndexableAttr) {
reason, err := blockfileMgr.retrieveTxValidationCodeByTxID(txid)

if containsAttr(indexItems, IndexableAttrTxID) {
require.NoError(t, err, "Error while retrieving tx validation code by txID")

require.NoError(t, err)
reasonFromFlags := flags.Flag(idx)

require.Equal(t, reasonFromFlags, reason)
} else {
require.Exactly(t, ErrAttrNotIndexed, err)
require.EqualError(t, err, "transaction IDs not maintained in index")
}
}
}
Expand Down Expand Up @@ -349,7 +347,7 @@ func TestExportUniqueTxIDsWhenTxIDsNotIndexed(t *testing.T) {
testSnapshotDir := testPath()
defer os.RemoveAll(testSnapshotDir)
_, err := blkfileMgrWrapper.blockfileMgr.index.exportUniqueTxIDs(testSnapshotDir, testNewHashFunc)
require.Equal(t, err, ErrAttrNotIndexed)
require.EqualError(t, err, "transaction IDs not maintained in index")
}

func TestExportUniqueTxIDsErrorCases(t *testing.T) {
Expand Down
9 changes: 0 additions & 9 deletions common/ledger/blkstorage/blockstore_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/hyperledger/fabric/common/ledger/dataformat"
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/common/metrics"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/internal/fileutil"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -53,14 +52,6 @@ func (c *IndexConfig) Contains(indexableAttr IndexableAttr) bool {
return false
}

var (
// ErrNotFoundInIndex is used to indicate missing entry in the index
ErrNotFoundInIndex = ledger.NotFoundInIndexErr("")

// ErrAttrNotIndexed is used to indicate that an attribute is not indexed
ErrAttrNotIndexed = errors.New("attribute not indexed")
)

// BlockStoreProvider provides handle to block storage - this is not thread-safe
type BlockStoreProvider struct {
conf *Conf
Expand Down
10 changes: 5 additions & 5 deletions common/ledger/blkstorage/blockstore_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,23 @@ func checkBlocks(t *testing.T, expectedBlocks []*common.Block, store *BlockStore
func checkWithWrongInputs(t *testing.T, store *BlockStore, numBlocks int) {
block, err := store.RetrieveBlockByHash([]byte("non-existent-hash"))
require.Nil(t, block)
require.Equal(t, ErrNotFoundInIndex, err)
require.EqualError(t, err, fmt.Sprintf("no such block hash [%x] in index", []byte("non-existent-hash")))

block, err = store.RetrieveBlockByTxID("non-existent-txid")
require.Nil(t, block)
require.Equal(t, ErrNotFoundInIndex, err)
require.EqualError(t, err, "no such transaction ID [non-existent-txid] in index")

tx, err := store.RetrieveTxByID("non-existent-txid")
require.Nil(t, tx)
require.Equal(t, ErrNotFoundInIndex, err)
require.EqualError(t, err, "no such transaction ID [non-existent-txid] in index")

tx, err = store.RetrieveTxByBlockNumTranNum(uint64(numBlocks+1), uint64(0))
require.Nil(t, tx)
require.Equal(t, ErrNotFoundInIndex, err)
require.EqualError(t, err, fmt.Sprintf("no such blockNumber, transactionNumber <%d, 0> in index", numBlocks+1))

txCode, err := store.RetrieveTxValidationCodeByTxID("non-existent-txid")
require.Equal(t, peer.TxValidationCode(-1), txCode)
require.Equal(t, ErrNotFoundInIndex, err)
require.EqualError(t, err, "no such transaction ID [non-existent-txid] in index")
}

func TestBlockStoreProvider(t *testing.T) {
Expand Down
40 changes: 24 additions & 16 deletions common/ledger/blkstorage/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package blkstorage

import (
"fmt"
"io/ioutil"
"math"
"os"
Expand Down Expand Up @@ -90,26 +91,18 @@ func (w *testBlockfileMgrWrapper) addBlocks(blocks []*common.Block) {
}
}

func (w *testBlockfileMgrWrapper) testGetBlockByHash(blocks []*common.Block, expectedErr error) {
func (w *testBlockfileMgrWrapper) testGetBlockByHash(blocks []*common.Block) {
for i, block := range blocks {
hash := protoutil.BlockHeaderHash(block.Header)
b, err := w.blockfileMgr.retrieveBlockByHash(hash)
if expectedErr != nil {
require.Error(w.t, err, expectedErr.Error())
continue
}
require.NoError(w.t, err, "Error while retrieving [%d]th block from blockfileMgr", i)
require.Equal(w.t, block, b)
}
}

func (w *testBlockfileMgrWrapper) testGetBlockByNumber(blocks []*common.Block, startingNum uint64, expectedErr error) {
func (w *testBlockfileMgrWrapper) testGetBlockByNumber(blocks []*common.Block) {
for i := 0; i < len(blocks); i++ {
b, err := w.blockfileMgr.retrieveBlockByNumber(startingNum + uint64(i))
if expectedErr != nil {
require.Equal(w.t, err.Error(), expectedErr.Error())
continue
}
b, err := w.blockfileMgr.retrieveBlockByNumber(blocks[0].Header.Number + uint64(i))
require.NoError(w.t, err, "Error while retrieving [%d]th block from blockfileMgr", i)
require.Equal(w.t, blocks[i], b)
}
Expand All @@ -120,22 +113,37 @@ func (w *testBlockfileMgrWrapper) testGetBlockByNumber(blocks []*common.Block, s
require.Equal(w.t, blocks[iLastBlock], b)
}

func (w *testBlockfileMgrWrapper) testGetBlockByTxID(blocks []*common.Block, expectedErr error) {
func (w *testBlockfileMgrWrapper) testGetBlockByTxID(blocks []*common.Block) {
for i, block := range blocks {
for _, txEnv := range block.Data.Data {
txID, err := protoutil.GetOrComputeTxIDFromEnvelope(txEnv)
require.NoError(w.t, err)
b, err := w.blockfileMgr.retrieveBlockByTxID(txID)
if expectedErr != nil {
require.Equal(w.t, err.Error(), expectedErr.Error())
continue
}
require.NoError(w.t, err, "Error while retrieving [%d]th block from blockfileMgr", i)
require.Equal(w.t, block, b)
}
}
}

func (w *testBlockfileMgrWrapper) testGetBlockByHashNotIndexed(blocks []*common.Block) {
for _, block := range blocks {
hash := protoutil.BlockHeaderHash(block.Header)
_, err := w.blockfileMgr.retrieveBlockByHash(hash)
require.EqualError(w.t, err, fmt.Sprintf("no such block hash [%x] in index", hash))
}
}

func (w *testBlockfileMgrWrapper) testGetBlockByTxIDNotIndexed(blocks []*common.Block) {
for _, block := range blocks {
for _, txEnv := range block.Data.Data {
txID, err := protoutil.GetOrComputeTxIDFromEnvelope(txEnv)
require.NoError(w.t, err)
_, err = w.blockfileMgr.retrieveBlockByTxID(txID)
require.EqualError(w.t, err, fmt.Sprintf("no such transaction ID [%s] in index", txID))
}
}
}

func (w *testBlockfileMgrWrapper) testGetTransactionByTxID(txID string, expectedEnvelope []byte, expectedErr error) {
envelope, err := w.blockfileMgr.retrieveTransactionByID(txID)
if expectedErr != nil {
Expand Down
3 changes: 1 addition & 2 deletions common/ledger/blkstorage/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ func assertBlockStorePostReset(t *testing.T, store *BlockStore, originallyCommit
require.Equal(t, originallyCommittedBlocks[0], blk)

_, err = store.RetrieveBlockByNumber(1)
require.Error(t, err)
require.Equal(t, err, ErrNotFoundInIndex)
require.EqualError(t, err, "no such block number [1] in index")

err = store.AddBlock(originallyCommittedBlocks[0])
require.EqualError(t, err, "block number should have been 1 but was 0")
Expand Down
Loading

0 comments on commit ded8734

Please sign in to comment.