-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add decorator to wrap panic messages with the transaction that …
…caused them (#2559) Closes: #2557 This will allow us to understand when a panic happens, which transaction caused it. As an example here is a mock panic: ``` mock panic caused by transaction: *types.MsgSend{from_address:"celestia15v4r83er48zfrm7u6uuqs3lytwxc63c34ltdgn" to_address:"celestia1t5m2f2m000twkmsgaa777rty36wd254292am6c" amount:<denom:"utia" amount:"10" > } ``` (cherry picked from commit a3d2453)
- Loading branch information
1 parent
c33a188
commit 877558f
Showing
3 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ante | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// HandlePanicDecorator that catches panics and wraps them in the transaction that caused the panic | ||
type HandlePanicDecorator struct{} | ||
|
||
func NewHandlePanicDecorator() HandlePanicDecorator { | ||
return HandlePanicDecorator{} | ||
} | ||
|
||
func (d HandlePanicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
panic(fmt.Sprint(r, FormatTx(tx))) | ||
} | ||
}() | ||
|
||
return next(ctx, tx, simulate) | ||
} | ||
|
||
func FormatTx(tx sdk.Tx) string { | ||
output := "\ncaused by transaction:\n" | ||
for _, msg := range tx.GetMsgs() { | ||
output += fmt.Sprintf("%T{%s}\n", msg, msg) | ||
} | ||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ante_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/celestiaorg/celestia-app/app" | ||
"github.com/celestiaorg/celestia-app/app/ante" | ||
"github.com/celestiaorg/celestia-app/app/encoding" | ||
"github.com/celestiaorg/celestia-app/test/util/testnode" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPanicHandlerDecorator(t *testing.T) { | ||
decorator := ante.NewHandlePanicDecorator() | ||
anteHandler := sdk.ChainAnteDecorators(decorator, mockPanicDecorator{}) | ||
ctx := sdk.Context{} | ||
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) | ||
builder := encCfg.TxConfig.NewTxBuilder() | ||
err := builder.SetMsgs(banktypes.NewMsgSend(testnode.RandomAddress().(sdk.AccAddress), testnode.RandomAddress().(sdk.AccAddress), sdk.NewCoins(sdk.NewInt64Coin(app.BondDenom, 10)))) | ||
Check failure on line 22 in app/ante/panic_test.go GitHub Actions / test / test
Check failure on line 22 in app/ante/panic_test.go GitHub Actions / test / test-coverage
|
||
require.NoError(t, err) | ||
tx := builder.GetTx() | ||
defer func() { | ||
r := recover() | ||
require.NotNil(t, r) | ||
require.Equal(t, fmt.Sprint("mock panic", ante.FormatTx(tx)), r) | ||
}() | ||
_, _ = anteHandler(ctx, tx, false) | ||
} | ||
|
||
type mockPanicDecorator struct{} | ||
|
||
func (d mockPanicDecorator) AnteHandle(_ sdk.Context, _ sdk.Tx, _ bool, _ sdk.AnteHandler) (newCtx sdk.Context, err error) { | ||
panic("mock panic") | ||
} |