From a2be39ff4102e134aa39dc2f169e05e2e2ecfbec Mon Sep 17 00:00:00 2001 From: Alex Gartner Date: Thu, 12 Sep 2024 13:00:21 -0700 Subject: [PATCH 1/3] fix(zetacore): add default cctx list pagination size (#2871) * fix(zetacore): add default cctx list pagination size * add unit tests --- x/crosschain/keeper/grpc_query_cctx.go | 9 +++++ x/crosschain/keeper/grpc_query_cctx_test.go | 38 +++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/x/crosschain/keeper/grpc_query_cctx.go b/x/crosschain/keeper/grpc_query_cctx.go index 15c4420246..a361c943fb 100644 --- a/x/crosschain/keeper/grpc_query_cctx.go +++ b/x/crosschain/keeper/grpc_query_cctx.go @@ -20,6 +20,8 @@ const ( // MaxLookbackNonce is the maximum number of nonces to look back to find missed pending cctxs MaxLookbackNonce = 1000 + + DefaultPageSize = 100 ) func (k Keeper) ZetaAccounting( @@ -46,6 +48,13 @@ func (k Keeper) CctxAll(c context.Context, req *types.QueryAllCctxRequest) (*typ store := ctx.KVStore(k.storeKey) sendStore := prefix.NewStore(store, types.KeyPrefix(types.CCTXKey)) + if req.Pagination == nil { + req.Pagination = &query.PageRequest{} + } + if req.Pagination.Limit == 0 { + req.Pagination.Limit = DefaultPageSize + } + pageRes, err := query.Paginate(sendStore, req.Pagination, func(_ []byte, value []byte) error { var send types.CrossChainTx if err := k.cdc.Unmarshal(value, &send); err != nil { diff --git a/x/crosschain/keeper/grpc_query_cctx_test.go b/x/crosschain/keeper/grpc_query_cctx_test.go index 6112d5e58c..42f87ef7e4 100644 --- a/x/crosschain/keeper/grpc_query_cctx_test.go +++ b/x/crosschain/keeper/grpc_query_cctx_test.go @@ -5,6 +5,7 @@ import ( "testing" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/query" "github.com/stretchr/testify/require" keepertest "github.com/zeta-chain/zetacore/testutil/keeper" @@ -288,3 +289,40 @@ func TestKeeper_CctxByNonce(t *testing.T) { require.Equal(t, res.CrossChainTx.CctxStatus.LastUpdateTimestamp, ctx.BlockTime().Unix()) }) } + +func TestKeeper_CctxAll(t *testing.T) { + t.Run("empty request", func(t *testing.T) { + k, ctx, _, _ := keepertest.CrosschainKeeper(t) + _, err := k.CctxAll(ctx, &types.QueryAllCctxRequest{}) + require.NoError(t, err) + }) + + t.Run("default page size", func(t *testing.T) { + k, ctx, _, zk := keepertest.CrosschainKeeper(t) + chainID := getValidEthChainID() + tss := sample.Tss() + zk.ObserverKeeper.SetTSS(ctx, tss) + _ = createCctxWithNonceRange(t, ctx, *k, 1000, 2000, chainID, tss, zk) + + res, err := k.CctxAll(ctx, &types.QueryAllCctxRequest{}) + require.NoError(t, err) + require.Len(t, res.CrossChainTx, keeper.DefaultPageSize) + }) + + t.Run("page size provided", func(t *testing.T) { + k, ctx, _, zk := keepertest.CrosschainKeeper(t) + chainID := getValidEthChainID() + tss := sample.Tss() + zk.ObserverKeeper.SetTSS(ctx, tss) + _ = createCctxWithNonceRange(t, ctx, *k, 1000, 2000, chainID, tss, zk) + testPageSize := 200 + + res, err := k.CctxAll(ctx, &types.QueryAllCctxRequest{ + Pagination: &query.PageRequest{ + Limit: uint64(testPageSize), + }, + }) + require.NoError(t, err) + require.Len(t, res.CrossChainTx, testPageSize) + }) +} From 929b6619358c6c6ae535e80d63fab20d8e9e8303 Mon Sep 17 00:00:00 2001 From: Alex Gartner Date: Sat, 14 Sep 2024 15:37:22 -0700 Subject: [PATCH 2/3] fix(zetaclient): add block by number result nil check (#2882) --- zetaclient/chains/evm/observer/observer.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/zetaclient/chains/evm/observer/observer.go b/zetaclient/chains/evm/observer/observer.go index 82fc1e4d6d..06ca06c843 100644 --- a/zetaclient/chains/evm/observer/observer.go +++ b/zetaclient/chains/evm/observer/observer.go @@ -357,6 +357,9 @@ func (ob *Observer) BlockByNumber(blockNumber int) (*ethrpc.Block, error) { if err != nil { return nil, err } + if block == nil { + return nil, fmt.Errorf("block not found: %d", blockNumber) + } for i := range block.Transactions { err := evm.ValidateEvmTransaction(&block.Transactions[i]) if err != nil { From 0fdbfffdc72870afc1028ef9398851dd0cd68d01 Mon Sep 17 00:00:00 2001 From: Alex Gartner Date: Mon, 16 Sep 2024 08:48:26 -0700 Subject: [PATCH 3/3] chore: log panic line in ticker error (#2881) --- pkg/ticker/ticker.go | 11 ++++++++++- pkg/ticker/ticker_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/pkg/ticker/ticker.go b/pkg/ticker/ticker.go index 566fc03f8b..94fc9ab2fb 100644 --- a/pkg/ticker/ticker.go +++ b/pkg/ticker/ticker.go @@ -29,6 +29,8 @@ package ticker import ( "context" "fmt" + "runtime/debug" + "strings" "sync" "time" @@ -78,7 +80,14 @@ func SecondsFromUint64(d uint64) time.Duration { func (t *Ticker) Run(ctx context.Context) (err error) { defer func() { if r := recover(); r != nil { - err = fmt.Errorf("panic during ticker run: %v", r) + stack := string(debug.Stack()) + lines := strings.Split(stack, "\n") + line := "" + // 8th line should be the actual line, see the unit tests + if len(lines) > 8 { + line = strings.TrimSpace(lines[8]) + } + err = fmt.Errorf("panic during ticker run: %v at %s", r, line) } }() diff --git a/pkg/ticker/ticker_test.go b/pkg/ticker/ticker_test.go index 671091c71f..4d890bf051 100644 --- a/pkg/ticker/ticker_test.go +++ b/pkg/ticker/ticker_test.go @@ -147,6 +147,33 @@ func TestTicker(t *testing.T) { // ASSERT assert.ErrorContains(t, err, "panic during ticker run: oops") + // assert that we get error with the correct line number + assert.ErrorContains(t, err, "ticker_test.go:142") + }) + + t.Run("Nil panic", func(t *testing.T) { + // ARRANGE + // Given a context + ctx := context.Background() + + // And a ticker + ticker := New(durSmall, func(_ context.Context, _ *Ticker) error { + var a func() + a() + return nil + }) + + // ACT + err := ticker.Run(ctx) + + // ASSERT + assert.ErrorContains( + t, + err, + "panic during ticker run: runtime error: invalid memory address or nil pointer dereference", + ) + // assert that we get error with the correct line number + assert.ErrorContains(t, err, "ticker_test.go:162") }) t.Run("Run as a single call", func(t *testing.T) {