refactor(x/auth/ante): don't use simulate bool in ante handler (#19586)

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
Julien Robert 2024-02-29 09:39:03 +01:00 committed by GitHub
parent 0a801e1c03
commit 975ddc1d31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 38 additions and 38 deletions

View File

@ -6,23 +6,23 @@ package types
// is required for the remainder of the AnteHandler execution, a new Context should
// be created off of the provided Context and returned as <newCtx>.
//
// The simulate argument is provided to indicate if the AnteHandler is being executed
// in simulation mode, which attempts to estimate a gas cost for the tx. Any state
// modifications made will be discarded if simulate is true.
type AnteHandler func(ctx Context, tx Tx, simulate bool) (newCtx Context, err error)
// When exec module is in simulation mode (ctx.ExecMode() == ExecModeSimulate), it indicates if the AnteHandler is
// being executed in simulation mode, which attempts to estimate a gas cost for the tx.
// Any state modifications made will be discarded in simulation mode.
type AnteHandler func(ctx Context, tx Tx, _ bool) (newCtx Context, err error)
// PostHandler like AnteHandler but it executes after RunMsgs. Runs on success
// or failure and enables use cases like gas refunding.
type PostHandler func(ctx Context, tx Tx, simulate, success bool) (newCtx Context, err error)
type PostHandler func(ctx Context, tx Tx, _, success bool) (newCtx Context, err error)
// AnteDecorator wraps the next AnteHandler to perform custom pre-processing.
type AnteDecorator interface {
AnteHandle(ctx Context, tx Tx, simulate bool, next AnteHandler) (newCtx Context, err error)
AnteHandle(ctx Context, tx Tx, _ bool, next AnteHandler) (newCtx Context, err error)
}
// PostDecorator wraps the next PostHandler to perform custom post-processing.
type PostDecorator interface {
PostHandle(ctx Context, tx Tx, simulate, success bool, next PostHandler) (newCtx Context, err error)
PostHandle(ctx Context, tx Tx, _, success bool, next PostHandler) (newCtx Context, err error)
}
// ChainAnteDecorators ChainDecorator chains AnteDecorators together with each AnteDecorator
@ -46,13 +46,13 @@ func ChainAnteDecorators(chain ...AnteDecorator) AnteHandler {
handlerChain := make([]AnteHandler, len(chain)+1)
// set the terminal AnteHandler decorator
handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate bool) (Context, error) {
handlerChain[len(chain)] = func(ctx Context, tx Tx, _ bool) (Context, error) {
return ctx, nil
}
for i := 0; i < len(chain); i++ {
ii := i
handlerChain[ii] = func(ctx Context, tx Tx, simulate bool) (Context, error) {
return chain[ii].AnteHandle(ctx, tx, simulate, handlerChain[ii+1])
handlerChain[ii] = func(ctx Context, tx Tx, _ bool) (Context, error) {
return chain[ii].AnteHandle(ctx, tx, ctx.ExecMode() == ExecModeSimulate, handlerChain[ii+1])
}
}
@ -74,13 +74,13 @@ func ChainPostDecorators(chain ...PostDecorator) PostHandler {
handlerChain := make([]PostHandler, len(chain)+1)
// set the terminal PostHandler decorator
handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) {
handlerChain[len(chain)] = func(ctx Context, tx Tx, _, success bool) (Context, error) {
return ctx, nil
}
for i := 0; i < len(chain); i++ {
ii := i
handlerChain[ii] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) {
return chain[ii].PostHandle(ctx, tx, simulate, success, handlerChain[ii+1])
handlerChain[ii] = func(ctx Context, tx Tx, _, success bool) (Context, error) {
return chain[ii].PostHandle(ctx, tx, ctx.ExecMode() == ExecModeSimulate, success, handlerChain[ii+1])
}
}
return handlerChain[0]

View File

@ -14,7 +14,7 @@ func TestChainAnteDecorators(t *testing.T) {
// test panic
require.Nil(t, sdk.ChainAnteDecorators([]sdk.AnteDecorator{}...))
ctx, tx := sdk.Context{}, sdk.Tx(nil)
ctx, tx := sdk.Context{}.WithExecMode(sdk.ExecModeSimulate), sdk.Tx(nil)
mockCtrl := gomock.NewController(t)
mockAnteDecorator1 := mock.NewMockAnteDecorator(mockCtrl)
mockAnteDecorator1.EXPECT().AnteHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Any()).Times(1)
@ -39,7 +39,7 @@ func TestChainPostDecorators(t *testing.T) {
require.Nil(t, sdk.ChainPostDecorators([]sdk.PostDecorator{}...))
// Create empty context as well as transaction
ctx := sdk.Context{}
ctx := sdk.Context{}.WithExecMode(sdk.ExecModeSimulate)
tx := sdk.Tx(nil)
// Create mocks

View File

@ -24,10 +24,10 @@ func NewValidateBasicDecorator() ValidateBasicDecorator {
return ValidateBasicDecorator{}
}
func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
// no need to validate basic on recheck tx, call next antehandler
if ctx.ExecMode() == sdk.ExecModeReCheck {
return next(ctx, tx, simulate)
return next(ctx, tx, false)
}
if validateBasic, ok := tx.(sdk.HasValidateBasic); ok {
@ -36,7 +36,7 @@ func (vbd ValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulat
}
}
return next(ctx, tx, simulate)
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}
// ValidateMemoDecorator will validate memo given the parameters passed in
@ -52,7 +52,7 @@ func NewValidateMemoDecorator(ak AccountKeeper) ValidateMemoDecorator {
}
}
func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
memoTx, ok := tx.(sdk.TxWithMemo)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type")
@ -69,7 +69,7 @@ func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
}
}
return next(ctx, tx, simulate)
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}
// ConsumeTxSizeGasDecorator will take in parameters and consume gas proportional
@ -77,7 +77,7 @@ func (vmd ValidateMemoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
// slightly over estimated due to the fact that any given signing account may need
// to be retrieved from state.
//
// CONTRACT: If simulate=true, then signatures must either be completely filled
// CONTRACT: If exec mode = simulate, then signatures must either be completely filled
// in or empty.
// CONTRACT: To use this decorator, signatures of transaction must be represented
// as legacytx.StdSignature otherwise simulate mode will incorrectly estimate gas cost.
@ -91,7 +91,7 @@ func NewConsumeGasForTxSizeDecorator(ak AccountKeeper) ConsumeTxSizeGasDecorator
}
}
func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
sigTx, ok := tx.(authsigning.SigVerifiableTx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid tx type")
@ -143,7 +143,7 @@ func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim
}
}
return next(ctx, tx, simulate)
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}
// isIncompleteSignature tests whether SignatureData is fully filled in for simulation purposes
@ -193,7 +193,7 @@ func NewTxTimeoutHeightDecorator() TxTimeoutHeightDecorator {
// type where the current block height is checked against the tx's height timeout.
// If a height timeout is provided (non-zero) and is less than the current block
// height, then an error is returned.
func (txh TxTimeoutHeightDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
func (txh TxTimeoutHeightDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
timeoutTx, ok := tx.(TxWithTimeoutHeight)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "expected tx to implement TxWithTimeoutHeight")
@ -206,5 +206,5 @@ func (txh TxTimeoutHeightDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
)
}
return next(ctx, tx, simulate)
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}

View File

@ -43,13 +43,13 @@ func NewExtensionOptionsDecorator(checker ExtensionOptionChecker) sdk.AnteDecora
var _ sdk.AnteDecorator = RejectExtensionOptionsDecorator{}
// AnteHandle implements the AnteDecorator.AnteHandle method
func (r RejectExtensionOptionsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
func (r RejectExtensionOptionsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
err = checkExtOpts(tx, r.checker)
if err != nil {
return ctx, err
}
return next(ctx, tx, simulate)
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}
func checkExtOpts(tx sdk.Tx, checker ExtensionOptionChecker) error {

View File

@ -39,7 +39,7 @@ func NewDeductFeeDecorator(ak AccountKeeper, bk types.BankKeeper, fk FeegrantKee
}
}
func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
@ -67,7 +67,7 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
newCtx := ctx.WithPriority(priority)
return next(newCtx, tx, simulate)
return next(newCtx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}
func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee sdk.Coins) error {

View File

@ -27,7 +27,7 @@ func NewSetUpContextDecorator() SetUpContextDecorator {
return SetUpContextDecorator{}
}
func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
// all transactions must implement GasTx
gasTx, ok := tx.(GasTx)
if !ok {
@ -67,7 +67,7 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
}
}()
return next(newCtx, tx, simulate)
return next(newCtx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}
// SetGasMeter returns a new context with a gas meter set from a given context.

View File

@ -149,7 +149,7 @@ func verifyIsOnCurve(pubKey cryptotypes.PubKey) (err error) {
return nil
}
func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
sigTx, ok := tx.(authsigning.Tx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type")
@ -215,7 +215,7 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
ctx.EventManager().EmitEvents(events)
return next(ctx, tx, simulate)
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}
// authenticate the authentication of the TX for a specific tx signer.
@ -459,7 +459,7 @@ func NewValidateSigCountDecorator(ak AccountKeeper) ValidateSigCountDecorator {
}
}
func (vscd ValidateSigCountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
func (vscd ValidateSigCountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
sigTx, ok := tx.(authsigning.SigVerifiableTx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a sigTx")
@ -479,7 +479,7 @@ func (vscd ValidateSigCountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim
}
}
return next(ctx, tx, simulate)
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}
// DefaultSigVerificationGasConsumer is the default implementation of SignatureVerificationGasConsumer. It consumes gas

View File

@ -37,12 +37,12 @@ func NewUnorderedTxDecorator(maxTTL uint64, m *unorderedtx.Manager) *UnorderedTx
}
}
func (d *UnorderedTxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
func (d *UnorderedTxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
unorderedTx, ok := tx.(sdk.TxWithUnordered)
if !ok || !unorderedTx.GetUnordered() {
// If the transaction does not implement unordered capabilities or has the
// unordered value as false, we bypass.
return next(ctx, tx, simulate)
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}
// TTL is defined as a specific block height at which this tx is no longer valid
@ -70,5 +70,5 @@ func (d *UnorderedTxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b
d.txManager.Add(txHash, ttl)
}
return next(ctx, tx, simulate)
return next(ctx, tx, ctx.ExecMode() == sdk.ExecModeSimulate)
}