Skip to content

Commit 639d0bb

Browse files
committed
[block-store] return ErrNotFound instead of default error type
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(). Signed-off-by: Senthilnathan <cendhu@gmail.com>
1 parent 7df0f34 commit 639d0bb

File tree

7 files changed

+38
-16
lines changed

7 files changed

+38
-16
lines changed

common/ledger/blkstorage/blockindex.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

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

@@ -40,6 +40,7 @@ var (
4040
indexSavePointKey = []byte(indexSavePointKeyStr)
4141
errIndexSavePointKeyNotPresent = errors.New("NoBlockIndexed")
4242
errNilValue = errors.New("")
43+
ErrNotFound = errors.New("not found in index")
4344
importTxIDsBatchSize = uint64(10000) // txID is 64 bytes, so batch size roughly translates to 640KB
4445
)
4546

@@ -157,7 +158,7 @@ func (index *blockIndex) getBlockLocByHash(blockHash []byte) (*fileLocPointer, e
157158
return nil, err
158159
}
159160
if b == nil {
160-
return nil, errors.Errorf("no such block hash [%x] in index", blockHash)
161+
return nil, errors.Wrapf(ErrNotFound, "block hash [%x]", blockHash)
161162
}
162163
blkLoc := &fileLocPointer{}
163164
if err := blkLoc.unmarshal(b); err != nil {
@@ -175,7 +176,7 @@ func (index *blockIndex) getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer
175176
return nil, err
176177
}
177178
if b == nil {
178-
return nil, errors.Errorf("no such block number [%d] in index", blockNum)
179+
return nil, errors.Wrapf(ErrNotFound, "block number [%d]", blockNum)
179180
}
180181
blkLoc := &fileLocPointer{}
181182
if err := blkLoc.unmarshal(b); err != nil {
@@ -250,7 +251,7 @@ func (index *blockIndex) getTxIDVal(txID string) (*TxIDIndexValue, uint64, error
250251
return nil, 0, errors.Wrapf(err, "error while trying to retrieve transaction info by TXID [%s]", txID)
251252
}
252253
if !present {
253-
return nil, 0, errors.Errorf("no such transaction ID [%s] in index", txID)
254+
return nil, 0, errors.Wrapf(ErrNotFound, "transaction ID [%s]", txID)
254255
}
255256
valBytes := itr.Value()
256257
if len(valBytes) == 0 {
@@ -276,7 +277,7 @@ func (index *blockIndex) getTXLocByBlockNumTranNum(blockNum uint64, tranNum uint
276277
return nil, err
277278
}
278279
if b == nil {
279-
return nil, errors.Errorf("no such blockNumber, transactionNumber <%d, %d> in index", blockNum, tranNum)
280+
return nil, errors.Wrapf(ErrNotFound, "blockNumber, transactionNumber <%d, %d>", blockNum, tranNum)
280281
}
281282
txFLP := &fileLocPointer{}
282283
if err := txFLP.unmarshal(b); err != nil {

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(t testing.TB, err error, expectedContext string) {
18+
t.Helper()
19+
require.ErrorIs(t, err, ErrNotFound)
20+
require.Contains(t, err.Error(), expectedContext)
21+
}

0 commit comments

Comments
 (0)