Skip to content

Commit

Permalink
gracefully handle panics in TX decoding (sei-protocol#1762)
Browse files Browse the repository at this point in the history
gracefully handle panics in tx decoding
  • Loading branch information
codchen authored Jul 11, 2024
1 parent cc0d48e commit 8a11b77
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 11 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,12 @@ func (app *App) DecodeTransactionsConcurrently(ctx sdk.Context, txs [][]byte) []
wg.Add(1)
go func(idx int, encodedTx []byte) {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
ctx.Logger().Error(fmt.Sprintf("encountered panic during transaction decoding: %s", err))
typedTxs[idx] = nil
}
}()
typedTx, err := app.txDecoder(encodedTx)
// get txkey from tx
if err != nil {
Expand Down Expand Up @@ -1899,6 +1905,11 @@ func (app *App) BlacklistedAccAddrs() map[string]bool {
return blacklistedAddrs
}

// test-only
func (app *App) SetTxDecoder(txDecoder sdk.TxDecoder) {
app.txDecoder = txDecoder
}

func init() {
// override max wasm size to 2MB
wasmtypes.MaxWasmSize = 2 * 1024 * 1024
Expand Down
16 changes: 12 additions & 4 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"encoding/hex"
"fmt"
"math/big"
"reflect"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server/api"
cosmosConfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"math/big"
"reflect"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -360,6 +361,13 @@ func TestDecodeTransactionsConcurrently(t *testing.T) {
require.NotNil(t, typedTxs[0].GetMsgs()[0].(*evmtypes.MsgEVMTransaction).Derived)
require.Nil(t, typedTxs[1])
require.NotNil(t, typedTxs[2])

// test panic handling
testWrapper.App.SetTxDecoder(func(txBytes []byte) (sdk.Tx, error) { panic("test") })
typedTxs = testWrapper.App.DecodeTransactionsConcurrently(testWrapper.Ctx, [][]byte{evmtxbz, invalidbz, banktxbz})
require.Nil(t, typedTxs[0])
require.Nil(t, typedTxs[1])
require.Nil(t, typedTxs[2])
}

func TestApp_RegisterAPIRoutes(t *testing.T) {
Expand Down

0 comments on commit 8a11b77

Please sign in to comment.