Skip to content

Commit

Permalink
fix(x/crisis): do not recover panic
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Nov 6, 2023
1 parent fb9dadc commit c91f5a9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
9 changes: 9 additions & 0 deletions baseapp/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"

"github.com/cockroachdb/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

var IrrecoverablePanic = errors.New("irrecoverable panic")

// RecoveryHandler handles recovery() object.
// Return a non-nil error if recoveryObj was processed.
// Return nil if recoveryObj was not processed.
Expand All @@ -24,6 +27,12 @@ type recoveryMiddleware func(recoveryObj interface{}) (recoveryMiddleware, error
// processRecovery processes recoveryMiddleware chain for recovery() object.
// Chain processing stops on non-nil error or when chain is processed.
func processRecovery(recoveryObj interface{}, middleware recoveryMiddleware) error {
if err, ok := recoveryObj.(error); ok {
if errors.Is(err, IrrecoverablePanic) {
panic(err)
}
}

if middleware == nil {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions x/crisis/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"cosmossdk.io/collections"
"cosmossdk.io/core/address"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/errors"
"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
Expand Down Expand Up @@ -107,11 +109,9 @@ func (k *Keeper) AssertInvariants(ctx context.Context) {

invCtx, _ := sdkCtx.CacheContext()
if res, stop := ir.Invar(invCtx); stop {
// TODO: Include app name as part of context to allow for this to be
// variable.
panic(fmt.Errorf("invariant broken: %s\n"+
panic(errors.Wrap(baseapp.IrrecoverablePanic, fmt.Sprintf("invariant broken: %s\n"+
"\tCRITICAL please submit the following transaction:\n"+
"\t\t tx crisis invariant-broken %s %s", res, ir.ModuleName, ir.Route))
"\t\t tx crisis invariant-broken %s %s", res, ir.ModuleName, ir.Route)))
}
}

Expand Down
13 changes: 7 additions & 6 deletions x/crisis/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"cosmossdk.io/errors"
govtypes "cosmossdk.io/x/gov/types"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
Expand All @@ -15,7 +16,7 @@ var _ types.MsgServer = &Keeper{}

// VerifyInvariant implements MsgServer.VerifyInvariant method.
// It defines a method to verify a particular invariant.
func (k *Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInvariant) (*types.MsgVerifyInvariantResponse, error) {
func (k *Keeper) VerifyInvariant(ctx context.Context, msg *types.MsgVerifyInvariant) (*types.MsgVerifyInvariantResponse, error) {
if msg.Sender == "" {
return nil, sdkerrors.ErrInvalidAddress.Wrap("empty address string is not allowed")
}
Expand All @@ -24,8 +25,7 @@ func (k *Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInva
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid sender address: %s", err)
}

ctx := sdk.UnwrapSDKContext(goCtx)
params, err := k.ConstantFee.Get(goCtx)
params, err := k.ConstantFee.Get(ctx)
if err != nil {
return nil, err
}
Expand All @@ -36,7 +36,8 @@ func (k *Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInva
}

// use a cached context to avoid gas costs during invariants
cacheCtx, _ := ctx.CacheContext()
sdkCtx := sdk.UnwrapSDKContext(ctx)
cacheCtx, _ := sdkCtx.CacheContext()

found := false
msgFullRoute := msg.FullInvariantRoute()
Expand All @@ -61,10 +62,10 @@ func (k *Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInva
// blockchain thus the constant fee will have never been deducted. Thus no refund is required.

// TODO replace with circuit breaker
panic(res)
panic(errors.Wrap(baseapp.IrrecoverablePanic, res))
}

ctx.EventManager().EmitEvents(sdk.Events{
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeInvariant,
sdk.NewAttribute(types.AttributeKeyRoute, msg.InvariantRoute),
Expand Down

0 comments on commit c91f5a9

Please sign in to comment.