forked from celestiaorg/celestia-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: prototype for gatekeeping messages based on their version (cel…
…estiaorg#3162) Ref: celestiaorg#3134 This PR solves the problem of ensuring the messages belonging to modules not part of the current app version aren't executed. It does this in two ways: - Introducing an antehandler decorator to be predominantly used in CheckTx to immediately reject transactions giving users a useful error message (instead of just failing to execute) - Introduces a `CircuitBreaker` implementation to the `MsgServiceRouter` which prevents the execution of messages not belonging to the current app version. We need this because another module may call a message that is not current supported (think a governance proposal) I had several complications with the wiring of this given the structure of the SDK and tried a few different variants - this one I think being the better. It uses the configurator which is reponsible for registering services to read all the methods a modules grpc Server supports and extracting out the message names and mapping them to one or more versions that they are supported for. --------- Co-authored-by: Rootul P <[email protected]>
- Loading branch information
Showing
22 changed files
with
596 additions
and
158 deletions.
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,57 @@ | ||
package ante | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
var ( | ||
_ sdk.AnteDecorator = MsgVersioningGateKeeper{} | ||
_ baseapp.CircuitBreaker = MsgVersioningGateKeeper{} | ||
) | ||
|
||
// MsgVersioningGateKeeper dictates which transactions are accepted for an app version | ||
type MsgVersioningGateKeeper struct { | ||
// acceptedMsgs is a map from appVersion -> msgTypeURL -> struct{}. | ||
// If a msgTypeURL is present in the map it should be accepted for that appVersion. | ||
acceptedMsgs map[uint64]map[string]struct{} | ||
} | ||
|
||
func NewMsgVersioningGateKeeper(acceptedList map[uint64]map[string]struct{}) *MsgVersioningGateKeeper { | ||
return &MsgVersioningGateKeeper{ | ||
acceptedMsgs: acceptedList, | ||
} | ||
} | ||
|
||
// AnteHandle implements the ante.Decorator interface | ||
func (mgk MsgVersioningGateKeeper) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { | ||
acceptedMsgs, exists := mgk.acceptedMsgs[ctx.BlockHeader().Version.App] | ||
if !exists { | ||
return ctx, sdkerrors.ErrNotSupported.Wrapf("app version %d is not supported", ctx.BlockHeader().Version.App) | ||
} | ||
for _, msg := range tx.GetMsgs() { | ||
msgTypeURL := sdk.MsgTypeURL(msg) | ||
_, exists := acceptedMsgs[msgTypeURL] | ||
if !exists { | ||
return ctx, sdkerrors.ErrNotSupported.Wrapf("message type %s is not supported in version %d", msgTypeURL, ctx.BlockHeader().Version.App) | ||
} | ||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} | ||
|
||
func (mgk MsgVersioningGateKeeper) IsAllowed(ctx context.Context, msgName string) (bool, error) { | ||
appVersion := sdk.UnwrapSDKContext(ctx).BlockHeader().Version.App | ||
acceptedMsgs, exists := mgk.acceptedMsgs[appVersion] | ||
if !exists { | ||
return false, sdkerrors.ErrNotSupported.Wrapf("app version %d is not supported", appVersion) | ||
} | ||
_, exists = acceptedMsgs[msgName] | ||
if !exists { | ||
return false, nil | ||
} | ||
return true, nil | ||
} |
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,70 @@ | ||
package ante_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/celestiaorg/celestia-app/app" | ||
"github.com/celestiaorg/celestia-app/app/ante" | ||
"github.com/celestiaorg/celestia-app/app/encoding" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/stretchr/testify/require" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
version "github.com/tendermint/tendermint/proto/tendermint/version" | ||
) | ||
|
||
func TestMsgGateKeeperAnteHandler(t *testing.T) { | ||
// Define test cases | ||
tests := []struct { | ||
name string | ||
msg sdk.Msg | ||
acceptMsg bool | ||
version uint64 | ||
}{ | ||
{ | ||
name: "Accept MsgSend", | ||
msg: &banktypes.MsgSend{}, | ||
acceptMsg: true, | ||
version: 1, | ||
}, | ||
{ | ||
name: "Reject MsgMultiSend", | ||
msg: &banktypes.MsgMultiSend{}, | ||
acceptMsg: false, | ||
version: 1, | ||
}, | ||
{ | ||
name: "Reject MsgSend with version 2", | ||
msg: &banktypes.MsgSend{}, | ||
acceptMsg: false, | ||
version: 2, | ||
}, | ||
} | ||
|
||
msgGateKeeper := ante.NewMsgVersioningGateKeeper(map[uint64]map[string]struct{}{ | ||
1: { | ||
"/cosmos.bank.v1beta1.MsgSend": {}, | ||
}, | ||
2: {}, | ||
}) | ||
cdc := encoding.MakeConfig(app.ModuleEncodingRegisters...) | ||
anteHandler := sdk.ChainAnteDecorators(msgGateKeeper) | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctx := sdk.NewContext(nil, tmproto.Header{Version: version.Consensus{App: tc.version}}, false, nil) | ||
txBuilder := cdc.TxConfig.NewTxBuilder() | ||
require.NoError(t, txBuilder.SetMsgs(tc.msg)) | ||
_, err := anteHandler(ctx, txBuilder.GetTx(), false) | ||
allowed, err2 := msgGateKeeper.IsAllowed(ctx, sdk.MsgTypeURL(tc.msg)) | ||
require.NoError(t, err2) | ||
if tc.acceptMsg { | ||
require.NoError(t, err, "expected message to be accepted") | ||
require.True(t, allowed) | ||
} else { | ||
require.Error(t, err, "expected message to be rejected") | ||
require.False(t, allowed) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.