forked from cosmos/cosmos-sdk
-
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.
Add RejectExtensionOptionsDecorator (cosmos#6988)
* Add RejectExtensionOptionsDecorator * Fix error code * Fix lint * Add ExtensionOptionsTxBuilder * Add tests * Add tests * Update tests * Docs Co-authored-by: sahith-narahari <[email protected]> Co-authored-by: Alexander Bezobchuk <[email protected]>
- Loading branch information
1 parent
7f0c3f0
commit 94b3cc5
Showing
6 changed files
with
113 additions
and
2 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
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,36 @@ | ||
package ante | ||
|
||
import ( | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
type HasExtensionOptionsTx interface { | ||
GetExtensionOptions() []*codectypes.Any | ||
GetNonCriticalExtensionOptions() []*codectypes.Any | ||
} | ||
|
||
// RejectExtensionOptionsDecorator is an AnteDecorator that rejects all extension | ||
// options which can optionally be included in protobuf transactions. Users that | ||
// need extension options should create a custom AnteHandler chain that handles | ||
// needed extension options properly and rejects unknown ones. | ||
type RejectExtensionOptionsDecorator struct{} | ||
|
||
// NewRejectExtensionOptionsDecorator creates a new RejectExtensionOptionsDecorator | ||
func NewRejectExtensionOptionsDecorator() RejectExtensionOptionsDecorator { | ||
return RejectExtensionOptionsDecorator{} | ||
} | ||
|
||
var _ types.AnteDecorator = RejectExtensionOptionsDecorator{} | ||
|
||
// AnteHandle implements the AnteDecorator.AnteHandle method | ||
func (r RejectExtensionOptionsDecorator) AnteHandle(ctx types.Context, tx types.Tx, simulate bool, next types.AnteHandler) (newCtx types.Context, err error) { | ||
if hasExtOptsTx, ok := tx.(HasExtensionOptionsTx); ok { | ||
if len(hasExtOptsTx.GetExtensionOptions()) != 0 { | ||
return ctx, sdkerrors.ErrUnknownExtensionOptions | ||
} | ||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} |
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,36 @@ | ||
package ante_test | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/testutil/testdata" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/ante" | ||
"github.com/cosmos/cosmos-sdk/x/auth/tx" | ||
) | ||
|
||
func (suite *AnteTestSuite) TestRejectExtensionOptionsDecorator() { | ||
suite.SetupTest(true) // setup | ||
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() | ||
|
||
reod := ante.NewRejectExtensionOptionsDecorator() | ||
antehandler := sdk.ChainAnteDecorators(reod) | ||
|
||
// no extension options should not trigger an error | ||
theTx := suite.txBuilder.GetTx() | ||
_, err := antehandler(suite.ctx, theTx, false) | ||
suite.Require().NoError(err) | ||
|
||
extOptsTxBldr, ok := suite.txBuilder.(tx.ExtensionOptionsTxBuilder) | ||
if !ok { | ||
// if we can't set extension options, this decorator doesn't apply and we're done | ||
return | ||
} | ||
|
||
// setting any extension option should cause an error | ||
any, err := types.NewAnyWithValue(testdata.NewTestMsg()) | ||
suite.Require().NoError(err) | ||
extOptsTxBldr.SetExtensionOptions(any) | ||
theTx = suite.txBuilder.GetTx() | ||
_, err = antehandler(suite.ctx, theTx, false) | ||
suite.Require().EqualError(err, "unknown extension options") | ||
} |
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