Skip to content

Commit

Permalink
Improve error handling during TX querying when unpack errors occur, s…
Browse files Browse the repository at this point in the history
…how warning when an unpack fails
  • Loading branch information
pharr117 committed Jun 26, 2024
1 parent 169febb commit 7ba9d98
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
6 changes: 5 additions & 1 deletion cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func processBlock(cl *client.ChainClient, dbConn *gorm.DB, failedBlockHandler fu
var err error
errTypeURL := false

txsEventResp, err := rpc.GetTxsByBlockHeight(cl, newBlock.Height)
txsEventResp, unpackError, err := rpc.GetTxsByBlockHeight(cl, newBlock.Height)
if err != nil {
if strings.Contains(err.Error(), "unable to resolve type URL") {
errTypeURL = true
Expand All @@ -338,6 +338,10 @@ func processBlock(cl *client.ChainClient, dbConn *gorm.DB, failedBlockHandler fu
}
}

if unpackError != nil {
config.Log.Warn("There was an error unpacking interfaces during RPC querying, check your codecs", unpackError)
}

// There are two reasons this block would be hit
// 1) The node might have pruned history resulting in a failed lookup. Recheck to see if the block was supposed to have TX results.
// 2) The RPC endpoint (node we queried) doesn't recognize the type URL anymore, for an older type (e.g. on an archive node).
Expand Down
2 changes: 1 addition & 1 deletion cosmos/modules/ibc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,5 @@ func (w *WrapperMsgAcknowledgement) String() string {
return "MsgAcknowledgement: IBC transfer was not a FungibleTokenTransfer"
}

return fmt.Sprintf("MsgAcknowledgement: IBC transfer of %s%s from %s to %s\n", w.Amount, w.Denom, w.SenderAddress, w.ReceiverAddress)
return fmt.Sprintf("MsgAcknowledgement: IBC transfer of %s%s from %s to %s", w.Amount, w.Denom, w.SenderAddress, w.ReceiverAddress)
}
2 changes: 1 addition & 1 deletion cosmos/modules/tx/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ type MessageLogFormatError struct {
}

func (e *MessageLogFormatError) Error() string {
return fmt.Sprintf("Type: %s could not handle message log %s\n", e.MessageType, e.Log)
return fmt.Sprintf("Type: %s could not handle message log %s", e.MessageType, e.Log)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/BurntSushi/toml v1.3.2
// FYI, you can do go get github.com/DefiantLabs/lens@1f6f34841280df179c6e098f040bd584ced43a4c
// (using the commit hash from github) to pin to a specific commit.
github.com/DefiantLabs/lens v0.3.1-0.20240530040544-3ad0f04acd32
github.com/DefiantLabs/lens v0.3.1-0.20240626064943-499ef68c78a4
github.com/cosmos/cosmos-sdk v0.47.8
github.com/gin-gonic/gin v1.9.1
github.com/go-co-op/gocron v1.13.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,8 @@ github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/zstd v1.5.0 h1:+K/VEwIAaPcHiMtQvpLD4lqW7f0Gk3xdYZmI1hD+CXo=
github.com/DataDog/zstd v1.5.0/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/DefiantLabs/lens v0.3.1-0.20240530040544-3ad0f04acd32 h1:wl/7DINIB9ekq3/Hm9Ck0T0y9oA49YdLvAUh8AsTRXE=
github.com/DefiantLabs/lens v0.3.1-0.20240530040544-3ad0f04acd32/go.mod h1:+PbjDba4Lz2mDEywmIv3DKuWH2G/asWovif+Dph6zd8=
github.com/DefiantLabs/lens v0.3.1-0.20240626064943-499ef68c78a4 h1:ReN2eDugPg51M/c2v7dLeYm/CA2gFT47qnJxikGE6zI=
github.com/DefiantLabs/lens v0.3.1-0.20240626064943-499ef68c78a4/go.mod h1:+PbjDba4Lz2mDEywmIv3DKuWH2G/asWovif+Dph6zd8=
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
Expand Down
26 changes: 20 additions & 6 deletions rpc/requests.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rpc

import (
"fmt"
"time"

coretypes "github.com/cometbft/cometbft/rpc/core/types"
Expand Down Expand Up @@ -50,30 +51,43 @@ func GetBlock(cl *lensClient.ChainClient, height int64) (*coretypes.ResultBlock,
}

// GetTxsByBlockHeight makes a request to the Cosmos RPC API and returns all the transactions for a specific block
func GetTxsByBlockHeight(cl *lensClient.ChainClient, height int64) (*txTypes.GetTxsEventResponse, error) {
func GetTxsByBlockHeight(cl *lensClient.ChainClient, height int64) (resp *txTypes.GetTxsEventResponse, unpackError error, queryError error) {
pg := query.PageRequest{Limit: 100}
options := lensQuery.QueryOptions{Height: height, Pagination: &pg}
query := lensQuery.Query{Client: cl, Options: &options}
resp, err := query.TxByHeight(cl.Codec)
resp, unpackError, err := query.TxByHeight(cl.Codec)

if err != nil {
return nil, err
return nil, unpackError, err
}

unpackErrors := ""
hadUnpackErrors := false

if unpackError != nil {
unpackErrors = unpackError.Error()
hadUnpackErrors = true
}

// handle pagination if needed
if resp != nil && resp.Pagination != nil {
// if there are more total objects than we have so far, keep going
for resp.Pagination.Total > uint64(len(resp.Txs)) {
query.Options.Pagination.Offset = uint64(len(resp.Txs))
chunkResp, err := query.TxByHeight(cl.Codec)
chunkResp, chunkUnpackError, err := query.TxByHeight(cl.Codec)
if err != nil {
return nil, err
return nil, err, chunkUnpackError
}
resp.Txs = append(resp.Txs, chunkResp.Txs...)
resp.TxResponses = append(resp.TxResponses, chunkResp.TxResponses...)
}
}

return resp, nil
if hadUnpackErrors {
return resp, fmt.Errorf("error unpacking the TX response: %s", unpackErrors), nil
}

return resp, nil, nil
}

// IsCatchingUp true if the node is catching up to the chain, false otherwise
Expand Down
4 changes: 2 additions & 2 deletions rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestRPC(t *testing.T) {

func TestDecodeIBCTypes(t *testing.T) {
cl := GetOsmosisTestClient(t)
resp, err := GetTxsByBlockHeight(cl, 2620000)
resp, _, err := GetTxsByBlockHeight(cl, 2620000)
assert.Empty(t, err)
hasIbcType := false

Expand Down Expand Up @@ -146,7 +146,7 @@ func rpcQueryTx(t *testing.T, height int64) error {
// requestEndpoint := fmt.Sprintf(rest.GetEndpoint("txs_by_block_height_endpoint"), height)
options := lensQuery.QueryOptions{Height: height}
query := lensQuery.Query{Client: cl, Options: &options}
resp, err := query.TxByHeight(cl.Codec)
resp, _, err := query.TxByHeight(cl.Codec)
if err != nil {
return err
}
Expand Down

0 comments on commit 7ba9d98

Please sign in to comment.