Skip to content

Commit a27a1d5

Browse files
Return value consistency
Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
1 parent e3db303 commit a27a1d5

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

internal/ethereum/get_block_info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828

2929
func (c *ethConnector) BlockInfoByNumber(ctx context.Context, req *ffcapi.BlockInfoByNumberRequest) (*ffcapi.BlockInfoByNumberResponse, ffcapi.ErrorReason, error) {
3030

31-
blockInfo, reason, err := c.blockListener.GetBlockInfoByNumber(ctx, req.BlockNumber.Uint64(), req.AllowCache, req.ExpectedParentHash, "")
31+
blockInfo, err := c.blockListener.GetBlockInfoByNumber(ctx, req.BlockNumber.Uint64(), req.AllowCache, req.ExpectedParentHash, "")
3232
if err != nil {
33-
return nil, reason, err
33+
return nil, "", err
3434
}
3535
if blockInfo == nil {
3636
return nil, ffcapi.ErrorReasonNotFound, i18n.NewError(ctx, msgs.MsgBlockNotAvailable)

internal/msgs/en_error_messages.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package msgs
1818

1919
import (
20+
"net/http"
21+
2022
"github.com/hyperledger/firefly-common/pkg/i18n"
2123
"golang.org/x/text/language"
2224
)
@@ -79,4 +81,5 @@ var (
7981
MsgTransactionNotFound = ffe("FF23061", "Transaction not found: %s")
8082
MsgInMemoryPartialChainNotCaughtUp = ffe("FF23062", "In-memory partial chain is waiting for the transaction block %d (%s) to be indexed")
8183
MsgFailedToBuildExistingConfirmationInvalid = ffe("FF23063", "Failed to build confirmations, existing confirmations are not valid")
84+
MsgFromBlockInvalid = ffe("FF23064", "From block invalid. Must be 'earliest', 'latest' or a decimal: %s", http.StatusBadRequest)
8285
)

pkg/ethblocklistener/blocklistener.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type BlockListener interface {
4949
ReconcileConfirmationsForTransaction(ctx context.Context, txHash string, existingConfirmations []*ffcapi.MinimalBlockInfo, targetConfirmationCount uint64) (*ffcapi.ConfirmationUpdateResult, *ethrpc.TxReceiptJSONRPC, error)
5050
AddConsumer(ctx context.Context, c *BlockUpdateConsumer)
5151
GetHighestBlock(ctx context.Context) (uint64, bool)
52-
GetBlockInfoByNumber(ctx context.Context, blockNumber uint64, allowCache bool, expectedParentHashStr string, expectedBlockHashStr string) (*ethrpc.BlockInfoJSONRPC, ffcapi.ErrorReason, error)
52+
GetBlockInfoByNumber(ctx context.Context, blockNumber uint64, allowCache bool, expectedParentHashStr string, expectedBlockHashStr string) (*ethrpc.BlockInfoJSONRPC, error)
5353
GetBlockInfoByHash(ctx context.Context, hash0xString string) (*ethrpc.BlockInfoJSONRPC, error)
5454
WaitClosed()
5555
UTSetBackend(rpcbackend.RPC)
@@ -426,8 +426,8 @@ func (bl *blockListener) rebuildCanonicalChain() *list.Element {
426426
var bi *ethrpc.BlockInfoJSONRPC
427427
var reason ffcapi.ErrorReason
428428
err := bl.retry.Do(bl.ctx, "rebuild listener canonical chain", func(_ int) (retry bool, err error) {
429-
bi, reason, err = bl.GetBlockInfoByNumber(bl.ctx, nextBlockNumber, false, "", "")
430-
return reason != ffcapi.ErrorReasonNotFound, err
429+
bi, err = bl.GetBlockInfoByNumber(bl.ctx, nextBlockNumber, false, "", "")
430+
return true, err
431431
})
432432
if err != nil {
433433
if reason != ffcapi.ErrorReasonNotFound {
@@ -480,16 +480,13 @@ func (bl *blockListener) trimToLastValidBlock() (lastValidBlock *ffcapi.MinimalB
480480
log.L(bl.ctx).Debugf("Canonical chain checking from last block: %d", startingNumber)
481481
}
482482
var freshBlockInfo *ethrpc.BlockInfoJSONRPC
483-
var reason ffcapi.ErrorReason
484483
err := bl.retry.Do(bl.ctx, "rebuild listener canonical chain", func(_ int) (retry bool, err error) {
485484
log.L(bl.ctx).Debugf("Canonical chain validating block: %d", currentViewBlock.BlockNumber.Uint64())
486-
freshBlockInfo, reason, err = bl.GetBlockInfoByNumber(bl.ctx, currentViewBlock.BlockNumber.Uint64(), false, "", "")
487-
return reason != ffcapi.ErrorReasonNotFound, err
485+
freshBlockInfo, err = bl.GetBlockInfoByNumber(bl.ctx, currentViewBlock.BlockNumber.Uint64(), false, "", "")
486+
return true, err
488487
})
489488
if err != nil {
490-
if reason != ffcapi.ErrorReasonNotFound {
491-
return nil // Context must have been cancelled
492-
}
489+
return nil // Context must have been cancelled
493490
}
494491

495492
if freshBlockInfo != nil && freshBlockInfo.Hash.String() == currentViewBlock.BlockHash {

pkg/ethblocklistener/blocklistener_blockquery.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ func (bl *blockListener) getBlockInfoContainsTxHash(ctx context.Context, txHash
4848
txBlockHash := receipt.BlockHash
4949
txBlockNumber := receipt.BlockNumber.Uint64()
5050
// get the parent hash of the transaction block
51-
bi, reason, err := bl.GetBlockInfoByNumber(ctx, txBlockNumber, true, "", txBlockHash.String())
52-
if err != nil && reason != ffcapi.ErrorReasonNotFound { // if the block info is not found, then there could be a fork, twe don't throw error in this case and treating it as block not found
51+
bi, err := bl.GetBlockInfoByNumber(ctx, txBlockNumber, true, "", txBlockHash.String())
52+
if err != nil {
5353
return nil, nil, i18n.WrapError(ctx, err, msgs.MsgFailedToQueryBlockInfo, txHash)
5454
}
5555
if bi == nil {
56+
// if the block info is not found, then there could be a fork, twe don't throw error in this case and treating it as block not found
5657
return nil, nil, nil
5758
}
5859

@@ -69,7 +70,7 @@ func (bl *blockListener) getTransactionReceipt(ctx context.Context, txHash strin
6970
return
7071
}
7172

72-
func (bl *blockListener) GetBlockInfoByNumber(ctx context.Context, blockNumber uint64, allowCache bool, expectedParentHashStr string, expectedBlockHashStr string) (*ethrpc.BlockInfoJSONRPC, ffcapi.ErrorReason, error) {
73+
func (bl *blockListener) GetBlockInfoByNumber(ctx context.Context, blockNumber uint64, allowCache bool, expectedParentHashStr string, expectedBlockHashStr string) (*ethrpc.BlockInfoJSONRPC, error) {
7374
var blockInfo *ethrpc.BlockInfoJSONRPC
7475
if allowCache {
7576
cached, ok := bl.blockCache.Get(strconv.FormatUint(blockNumber, 10))
@@ -85,15 +86,15 @@ func (bl *blockListener) GetBlockInfoByNumber(ctx context.Context, blockNumber u
8586
if blockInfo == nil {
8687
rpcErr := bl.backend.CallRPC(ctx, &blockInfo, "eth_getBlockByNumber", ethtypes.NewHexIntegerU64(blockNumber), false /* only the txn hashes */)
8788
if rpcErr != nil {
88-
return nil, ffcapi.ErrorReason(""), rpcErr.Error()
89+
return nil, rpcErr.Error()
8990
}
9091
if blockInfo == nil {
91-
return nil, ffcapi.ErrorReasonNotFound, i18n.NewError(ctx, msgs.MsgBlockNotAvailable)
92+
return nil, nil
9293
}
9394
bl.addToBlockCache(blockInfo)
9495
}
9596

96-
return blockInfo, "", nil
97+
return blockInfo, nil
9798
}
9899

99100
func (bl *blockListener) GetBlockInfoByHash(ctx context.Context, hash0xString string) (*ethrpc.BlockInfoJSONRPC, error) {

pkg/ethblocklistener/confirmation_reconciler.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (s *splice) fillOneGap(ctx context.Context, blockListener *blockListener) e
181181
// always fill from the end of the gap ( i.e. the block before the start of the late list) because
182182
// the late list is our best view of the current canonical chain so working backwards from there will increase the number of blocks that we have a high confidence in
183183

184-
freshBlockInfo, _, err := blockListener.GetBlockInfoByNumber(ctx, s.lateList[0].BlockNumber.Uint64()-1, false, "", "")
184+
freshBlockInfo, err := blockListener.GetBlockInfoByNumber(ctx, s.lateList[0].BlockNumber.Uint64()-1, false, "", "")
185185
if err != nil {
186186
return err
187187
}
@@ -258,13 +258,16 @@ func createLateList(ctx context.Context, txBlockInfo *ffcapi.MinimalBlockInfo, t
258258
// If the late list is empty, it may be because the chain has moved on so far and the transaction is so old that
259259
// we no longer have the target block in memory. Lets try to grab the target block from the blockchain and work backwards from there.
260260
if len(lateList) == 0 {
261-
targetBlockInfo, _, err := blockListener.GetBlockInfoByNumber(ctx, txBlockInfo.BlockNumber.Uint64()+targetConfirmationCount, false, "", "")
261+
targetBlockInfo, err := blockListener.GetBlockInfoByNumber(ctx, txBlockInfo.BlockNumber.Uint64()+targetConfirmationCount, false, "", "")
262262
if err != nil {
263263
return nil, err
264264
}
265+
if targetBlockInfo == nil {
266+
return nil, i18n.NewError(ctx, msgs.MsgBlockNotAvailable)
267+
}
265268
lateList = []*ffcapi.MinimalBlockInfo{
266269
{
267-
BlockNumber: fftypes.FFuint64(targetBlockInfo.Number.BigInt().Uint64()),
270+
BlockNumber: fftypes.FFuint64(targetBlockInfo.Number.Uint64()),
268271
BlockHash: targetBlockInfo.Hash.String(),
269272
ParentHash: targetBlockInfo.ParentHash.String(),
270273
},

0 commit comments

Comments
 (0)