Skip to content

Commit 12a17e4

Browse files
authored
[block-store] return ErrNotFound instead of default error type (#90)
#### Type of change - Improvement (improvement to code, performance, etc) #### Description Currently, the committer sends NotFound grpc error code for block APIs by parsing the error message. As this is fragile, this commit adds ErrNotFound error so that the committer can use errors.Is(). #### Related issues - resolves #87 --------- Signed-off-by: Senthilnathan <cendhu@gmail.com>
1 parent 897c999 commit 12a17e4

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

common/ledger/blkstorage/blockindex.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
"path/filepath"
1313
"unicode/utf8"
1414

15+
"github.com/cockroachdb/errors"
1516
"github.com/hyperledger/fabric-protos-go-apiv2/common"
1617
"github.com/hyperledger/fabric-protos-go-apiv2/peer"
17-
"github.com/pkg/errors"
1818
"google.golang.org/protobuf/encoding/protowire"
1919
"google.golang.org/protobuf/proto"
2020

@@ -36,6 +36,9 @@ const (
3636
snapshotMetadataFileName = "txids.metadata"
3737
)
3838

39+
// ErrNotFound is returned when a requested item is not found in the block index.
40+
var ErrNotFound = errors.New("not found in index")
41+
3942
var (
4043
indexSavePointKey = []byte(indexSavePointKeyStr)
4144
errIndexSavePointKeyNotPresent = errors.New("NoBlockIndexed")
@@ -157,7 +160,7 @@ func (index *blockIndex) getBlockLocByHash(blockHash []byte) (*fileLocPointer, e
157160
return nil, err
158161
}
159162
if b == nil {
160-
return nil, errors.Errorf("no such block hash [%x] in index", blockHash)
163+
return nil, errors.Wrapf(ErrNotFound, "block hash [%x]", blockHash)
161164
}
162165
blkLoc := &fileLocPointer{}
163166
if err := blkLoc.unmarshal(b); err != nil {
@@ -175,7 +178,7 @@ func (index *blockIndex) getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer
175178
return nil, err
176179
}
177180
if b == nil {
178-
return nil, errors.Errorf("no such block number [%d] in index", blockNum)
181+
return nil, errors.Wrapf(ErrNotFound, "block number [%d]", blockNum)
179182
}
180183
blkLoc := &fileLocPointer{}
181184
if err := blkLoc.unmarshal(b); err != nil {
@@ -250,7 +253,7 @@ func (index *blockIndex) getTxIDVal(txID string) (*TxIDIndexValue, uint64, error
250253
return nil, 0, errors.Wrapf(err, "error while trying to retrieve transaction info by TXID [%s]", txID)
251254
}
252255
if !present {
253-
return nil, 0, errors.Errorf("no such transaction ID [%s] in index", txID)
256+
return nil, 0, errors.Wrapf(ErrNotFound, "transaction ID [%s]", txID)
254257
}
255258
valBytes := itr.Value()
256259
if len(valBytes) == 0 {
@@ -276,7 +279,7 @@ func (index *blockIndex) getTXLocByBlockNumTranNum(blockNum uint64, tranNum uint
276279
return nil, err
277280
}
278281
if b == nil {
279-
return nil, errors.Errorf("no such blockNumber, transactionNumber <%d, %d> in index", blockNum, tranNum)
282+
return nil, errors.Wrapf(ErrNotFound, "blockNumber, transactionNumber <%d, %d>", blockNum, tranNum)
280283
}
281284
txFLP := &fileLocPointer{}
282285
if err := txFLP.unmarshal(b); err != nil {
@@ -358,7 +361,8 @@ func (index *blockIndex) exportUniqueTxIDs(dir string, newHashFunc snapshot.NewH
358361
func importTxIDsFromSnapshot(
359362
snapshotDir string,
360363
lastBlockNumInSnapshot uint64,
361-
db *leveldbhelper.DBHandle) error {
364+
db *leveldbhelper.DBHandle,
365+
) error {
362366
txIDsMetadata, err := snapshot.OpenFile(filepath.Join(snapshotDir, snapshotMetadataFileName), snapshotFileFormat)
363367
if err != nil {
364368
return err

common/ledger/blkstorage/blockindex_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func testBlockIndexSync(t *testing.T, numBlocks int, numBlocksToIndex int, syncB
6767
// Before, we test for index sync-up, verify that the last set of blocks not indexed in the original index
6868
for i := numBlocksToIndex + 1; i <= numBlocks; i++ {
6969
_, err := blkfileMgr.retrieveBlockByNumber(uint64(i))
70-
require.EqualError(t, err, fmt.Sprintf("no such block number [%d] in index", i))
70+
requireNotFoundError(t, err, fmt.Sprintf("block number [%d]", i))
7171
}
7272

7373
// perform index sync

common/ledger/blkstorage/blockstore_provider_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,24 @@ func checkBlocks(t *testing.T, expectedBlocks []*common.Block, store *BlockStore
143143
func checkWithWrongInputs(t *testing.T, store *BlockStore, numBlocks int) {
144144
block, err := store.RetrieveBlockByHash([]byte("non-existent-hash"))
145145
require.Nil(t, block)
146-
require.EqualError(t, err, fmt.Sprintf("no such block hash [%x] in index", []byte("non-existent-hash")))
146+
requireNotFoundError(t, err, "block hash")
147147

148148
block, err = store.RetrieveBlockByTxID("non-existent-txid")
149149
require.Nil(t, block)
150-
require.EqualError(t, err, "no such transaction ID [non-existent-txid] in index")
150+
requireNotFoundError(t, err, "transaction ID [non-existent-txid]")
151151

152152
tx, err := store.RetrieveTxByID("non-existent-txid")
153153
require.Nil(t, tx)
154-
require.EqualError(t, err, "no such transaction ID [non-existent-txid] in index")
154+
requireNotFoundError(t, err, "transaction ID [non-existent-txid]")
155155

156156
tx, err = store.RetrieveTxByBlockNumTranNum(uint64(numBlocks+1), uint64(0))
157157
require.Nil(t, tx)
158-
require.EqualError(t, err, fmt.Sprintf("no such blockNumber, transactionNumber <%d, 0> in index", numBlocks+1))
158+
requireNotFoundError(t, err, fmt.Sprintf("blockNumber, transactionNumber <%d, 0>", numBlocks+1))
159159

160160
txCode, blkNum, err := store.RetrieveTxValidationCodeByTxID("non-existent-txid")
161161
require.Equal(t, peer.TxValidationCode(-1), txCode)
162162
require.Equal(t, uint64(0), blkNum)
163-
require.EqualError(t, err, "no such transaction ID [non-existent-txid] in index")
163+
requireNotFoundError(t, err, "transaction ID [non-existent-txid]")
164164
}
165165

166166
func TestBlockStoreProvider(t *testing.T) {

common/ledger/blkstorage/pkg_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (w *testBlockfileMgrWrapper) testGetBlockByHashNotIndexed(blocks []*common.
113113
for _, block := range blocks {
114114
hash := protoutil.BlockHeaderHash(block.Header)
115115
_, err := w.blockfileMgr.retrieveBlockByHash(hash)
116-
require.EqualError(w.t, err, fmt.Sprintf("no such block hash [%x] in index", hash))
116+
requireNotFoundError(w.t, err, "block hash")
117117
}
118118
}
119119

@@ -123,7 +123,7 @@ func (w *testBlockfileMgrWrapper) testGetBlockByTxIDNotIndexed(blocks []*common.
123123
txID, err := protoutil.GetOrComputeTxIDFromEnvelope(txEnv)
124124
require.NoError(w.t, err)
125125
_, err = w.blockfileMgr.retrieveBlockByTxID(txID)
126-
require.EqualError(w.t, err, fmt.Sprintf("no such transaction ID [%s] in index", txID))
126+
requireNotFoundError(w.t, err, fmt.Sprintf("transaction ID [%s]", txID))
127127
}
128128
}
129129
}

common/ledger/blkstorage/reset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func assertBlockStorePostReset(t *testing.T, store *BlockStore, originallyCommit
236236
require.Equal(t, originallyCommittedBlocks[0], blk)
237237

238238
_, err = store.RetrieveBlockByNumber(1)
239-
require.EqualError(t, err, "no such block number [1] in index")
239+
requireNotFoundError(t, err, "block number [1]")
240240

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

common/ledger/blkstorage/snapshot_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func TestImportFromSnapshot(t *testing.T) {
285285

286286
// before, we test for index sync-up, verify that the last set of blocks not indexed in the original index
287287
_, err := blkfileMgr.retrieveBlockByNumber(block.Header.Number)
288-
require.EqualError(t, err, fmt.Sprintf("no such block number [%d] in index", block.Header.Number))
288+
requireNotFoundError(t, err, fmt.Sprintf("block number [%d]", block.Header.Number))
289289

290290
// close and open should be able to sync-up the index
291291
closeBlockStore()
@@ -472,7 +472,7 @@ func verifyQueriesOnBlocksPriorToSnapshot(
472472
require.EqualError(t, err, expectedErrStr)
473473

474474
_, err = bootstrappedBlockStore.RetrieveBlockByHash(blockHash)
475-
require.EqualError(t, err, fmt.Sprintf("no such block hash [%x] in index", blockHash))
475+
requireNotFoundError(t, err, "block hash")
476476
}
477477

478478
bootstrappingSnapshotHeight := uint64(len(blocksDetailsBeforeSnapshot))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
"testing"
11+
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
// requireNotFoundError is a test helper that verifies an error is ErrNotFound
16+
// and contains the expected context string.
17+
func requireNotFoundError(tb testing.TB, err error, expectedContext string) {
18+
tb.Helper()
19+
require.ErrorIs(tb, err, ErrNotFound)
20+
require.ErrorContains(tb, err, expectedContext)
21+
}

0 commit comments

Comments
 (0)