Skip to content

Commit

Permalink
introduce mapping for block not found errors
Browse files Browse the repository at this point in the history
Instead of returning empty block info for an unknown/unvalidated block,
some evm blockchains will return an error. Now, the ethereum connector
can keep track of these different errors and treat them as if
`blockInfo` is nil

Signed-off-by: Alex Shorsher <[email protected]>
  • Loading branch information
shorsher committed Aug 23, 2022
1 parent 03acd05 commit 1c39f8f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions internal/ethereum/error_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
filterRPCMethods ethRPCMethodCategory = iota
sendRPCMethods
callRPCMethods
blockRPCMethods
)

// mapErrorToReason provides a common place for mapping Ethereum client
Expand Down Expand Up @@ -58,6 +59,11 @@ func mapError(methodType ethRPCMethodCategory, err error) ffcapi.ErrorReason {
case strings.Contains(errString, "known transaction"):
return ffcapi.ErrorKnownTransaction
}
case blockRPCMethods:
// https://docs.avax.network/quickstart/integrate-exchange-with-avalanche#determining-finality
if strings.Contains(errString, "cannot query unfinalized data") {
return ffcapi.ErrorReasonNotFound
}
}

// Best default in FFCAPI is to provide no mapping
Expand Down
9 changes: 8 additions & 1 deletion internal/ethereum/get_block_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ func (c *ethConnector) getBlockInfoByNumber(ctx context.Context, blockNumber int

if blockInfo == nil {
err := c.backend.Invoke(ctx, &blockInfo, "eth_getBlockByNumber", ethtypes.NewHexInteger64(blockNumber), false /* only the txn hashes */)
if err != nil || blockInfo == nil {
if err != nil {
if mapError(blockRPCMethods, err) == ffcapi.ErrorReasonNotFound {
log.L(ctx).Debugf("Received error signifying 'block not found': '%s'", err)
return nil, nil
}
return nil, err
}
if blockInfo == nil {
return nil, err
}
c.addToBlockCache(blockInfo)
Expand Down
16 changes: 16 additions & 0 deletions internal/ethereum/get_block_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ func TestGetBlockInfoByNumberOK(t *testing.T) {

}

func TestGetBlockInfoByNumberBlockNotFoundError(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("Invoke", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.Anything, false).
Return(fmt.Errorf("cannot query unfinalized data"))

var req ffcapi.BlockInfoByNumberRequest
err := json.Unmarshal([]byte(sampleGetBlockInfoByNumber), &req)
assert.NoError(t, err)
res, reason, err := c.BlockInfoByNumber(ctx, &req)
assert.Regexp(t, "FF23011", err)
assert.Equal(t, ffcapi.ErrorReasonNotFound, reason)
assert.Nil(t, res)
}

func TestGetBlockInfoByNumberNotFound(t *testing.T) {

ctx, c, mRPC, done := newTestConnector(t)
Expand Down

0 comments on commit 1c39f8f

Please sign in to comment.