Skip to content

refactor: unordered transactions ante handling #24573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
10e7310
make unordered nonce checking part of sigverify
technicallyty Apr 24, 2025
c7c9a08
fix tests and remove unordered tx manager thing
technicallyty Apr 25, 2025
ef9a516
upgrade docs, fix apps
technicallyty Apr 25, 2025
971f549
fix test
technicallyty Apr 25, 2025
9ead09b
lint fix
technicallyty Apr 25, 2025
b3be99c
add test for increment sequence
technicallyty Apr 25, 2025
3f25119
comments
technicallyty Apr 25, 2025
39e0768
Merge branch 'main' into technicallyty/SDK-314/unordered-tx-ante-refa…
technicallyty Apr 25, 2025
409fa4f
spelling
technicallyty Apr 25, 2025
1966dbb
rabbit feedback
technicallyty Apr 25, 2025
a81f333
Merge branch 'technicallyty/SDK-314/unordered-tx-ante-refactor' of ss…
technicallyty Apr 25, 2025
cfffb44
remove unused test utils
technicallyty Apr 25, 2025
2fc58be
group vars
technicallyty Apr 25, 2025
14e6284
slim down test copying
technicallyty Apr 25, 2025
1298d61
app di options
technicallyty Apr 25, 2025
d065d69
fix
technicallyty Apr 25, 2025
ce19112
update
technicallyty Apr 25, 2025
ca2e35c
docs and rename
technicallyty Apr 25, 2025
fca3b39
snip -> other decorators
technicallyty Apr 25, 2025
00164ef
rename option
technicallyty Apr 25, 2025
027a5b3
Merge branch 'main' into technicallyty/SDK-314/unordered-tx-ante-refa…
technicallyty Apr 25, 2025
a82c6b0
use module config rather than supplied option
technicallyty Apr 27, 2025
f731600
update api replacements
technicallyty Apr 27, 2025
91d91c5
Update proto/cosmos/auth/module/v1/module.proto
technicallyty Apr 28, 2025
925cb67
rename IsUnorderedTransactionsEnabled
technicallyty Apr 28, 2025
601bc79
Merge branch 'main' into technicallyty/SDK-314/unordered-tx-ante-refa…
technicallyty Apr 28, 2025
ea2ecdb
make mocks
technicallyty Apr 28, 2025
f36f46f
make the test a suite
technicallyty Apr 28, 2025
2cc55cd
mark test as helper
technicallyty Apr 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions UPGRADE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,36 +406,38 @@ Lastly, add an entry for epochs in the ModuleConfig:

## Enable Unordered Transactions **OPTIONAL**

To enable unordered transaction support on an application, the ante handler options must be updated.
To enable unordered transaction support on an application, the `x/auth` keeper must be supplied with the `WithUnorderedTransactions` option.

```go
app.AccountKeeper = authkeeper.NewAccountKeeper(
appCodec,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec(sdk.Bech32MainPrefix),
sdk.Bech32MainPrefix,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authkeeper.WithUnorderedTransactions(true), // new option!
)
```

By default, unordered transactions use a transaction timeout duration of 10 minutes and a default gas charge of 2240 gas units.
To modify these default values, pass in the corresponding options to the new `SigVerifyOptions` field in `x/auth's` `ante.HandlerOptions`.

```go
options := ante.HandlerOptions{
// ...
UnorderedNonceManager: app.AccountKeeper,
// The following options are set by default.
// If you do not want to change these, you may remove the UnorderedTxOptions field entirely.
UnorderedTxOptions: []ante.UnorderedTxDecoratorOptions{
ante.WithUnorderedTxGasCost(2240),
ante.WithTimeoutDuration(10 * time.Minute),
SigVerifyOptions: []ante.SigVerificationDecoratorOption{
// change below as needed.
ante.WithUnorderedTxGasCost(ante.DefaultUnorderedTxGasCost),
ante.WithMaxTxTimeoutDuration(ante.DefaultMaxTimoutDuration),
},
}
```

```go
anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
circuitante.NewCircuitBreakerDecorator(options.CircuitKeeper),
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
// NEW !! NEW !! NEW !!
ante.NewUnorderedTxDecorator(options.UnorderedNonceManager, options.UnorderedTxOptions...)
// ... snip
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler, options.SigVerifyOptions...), // supply new options
}
```

Expand Down
46 changes: 22 additions & 24 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,38 @@ unique to the account; however, the difference may be as small as a nanosecond.

#### Enabling Unordered Transactions

To enable unordered transactions, set the new `UnorderedNonceManager` field in the `x/auth` `ante.HandlerOptions`.
To enable unordered transactions, supply the `WithUnorderedTransactions` option to the `x/auth` keeper:

By default, unordered transactions use a transaction timeout duration of 10 minutes and a default gas charge of 2240 gas.
To modify these default values, pass in the corresponding options to the new `UnorderedTxOptions` field in `x/auth's` `ante.HandlerOptions`.
```go
app.AccountKeeper = authkeeper.NewAccountKeeper(
appCodec,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec(sdk.Bech32MainPrefix),
sdk.Bech32MainPrefix,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authkeeper.WithUnorderedTransactions(true), // new option!
)
```

By default, unordered transactions use a transaction timeout duration of 10 minutes and a default gas charge of 2240 gas units.
To modify these default values, pass in the corresponding options to the new `SigVerifyOptions` field in `x/auth's` `ante.HandlerOptions`.

```go
options := ante.HandlerOptions{
UnorderedNonceManager: app.AccountKeeper,
// The following options are set by default.
// If you do not want to change these, you may remove the UnorderedTxOptions field entirely.
UnorderedTxOptions: []ante.UnorderedTxDecoratorOptions{
ante.WithUnorderedTxGasCost(2240),
ante.WithTimeoutDuration(10 * time.Minute),
SigVerifyOptions: []ante.SigVerificationDecoratorOption{
// change below as needed.
ante.WithUnorderedTxGasCost(ante.DefaultUnorderedTxGasCost),
ante.WithMaxTxTimeoutDuration(ante.DefaultMaxTimoutDuration),
},
}
```

```go
anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
circuitante.NewCircuitBreakerDecorator(options.CircuitKeeper),
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
// NEW !! NEW !! NEW !!
ante.NewUnorderedTxDecorator(options.UnorderedNonceManager, options.UnorderedTxOptions...)
// ... snip ...
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler, options.SigVerifyOptions...), // supply new options
}
```

Expand Down
6 changes: 1 addition & 5 deletions simapp/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,9 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler, options.SigVerifyOptions...),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
}

if options.UnorderedNonceManager != nil {
anteDecorators = append(anteDecorators, ante.NewUnorderedTxDecorator(options.UnorderedNonceManager, options.UnorderedTxOptions...))
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
}
26 changes: 16 additions & 10 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ package simapp
import (
"encoding/json"
"fmt"
"io"
"maps"

abci "github.com/cometbft/cometbft/abci/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/gogoproto/proto"
"github.com/spf13/cast"
"io"
"maps"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
Expand Down Expand Up @@ -306,6 +307,7 @@ func NewSimApp(
authcodec.NewBech32Codec(sdk.Bech32MainPrefix),
sdk.Bech32MainPrefix,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authkeeper.WithUnorderedTransactions(true),
)

app.BankKeeper = bankkeeper.NewBaseKeeper(
Expand Down Expand Up @@ -467,7 +469,7 @@ func NewSimApp(

app.GovKeeper = *govKeeper.SetHooks(
govtypes.NewMultiGovHooks(
// register the governance hooks
// register the governance hooks
),
)

Expand Down Expand Up @@ -497,7 +499,7 @@ func NewSimApp(

app.EpochsKeeper.SetHooks(
epochstypes.NewMultiEpochHooks(
// insert epoch hooks receivers here
// insert epoch hooks receivers here
),
)

Expand Down Expand Up @@ -697,12 +699,16 @@ func (app *SimApp) setAnteHandler(txConfig client.TxConfig) {
anteHandler, err := NewAnteHandler(
HandlerOptions{
ante.HandlerOptions{
UnorderedNonceManager: app.AccountKeeper,
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
SignModeHandler: txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
SignModeHandler: txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
SigVerifyOptions: []ante.SigVerificationDecoratorOption{
// change below as needed.
ante.WithUnorderedTxGasCost(ante.DefaultUnorderedTxGasCost),
ante.WithMaxTxTimeoutDuration(ante.DefaultMaxTimeoutDuration),
},
},
&app.CircuitKeeper,
},
Expand Down
13 changes: 6 additions & 7 deletions simapp/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func NewSimApp(
// with the prefix defined in the auth module configuration.
//
// func() address.Codec { return <- custom address codec type -> }

[]authkeeper.InitOption{authkeeper.WithUnorderedTransactions(true)},
//
// STAKING
//
Expand Down Expand Up @@ -275,12 +275,11 @@ func (app *SimApp) setAnteHandler(txConfig client.TxConfig) {
anteHandler, err := NewAnteHandler(
HandlerOptions{
ante.HandlerOptions{
UnorderedNonceManager: app.AccountKeeper,
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
SignModeHandler: txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
SignModeHandler: txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
&app.CircuitKeeper,
},
Expand Down
14 changes: 5 additions & 9 deletions x/auth/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ type HandlerOptions struct {
SignModeHandler *txsigning.HandlerMap
SigGasConsumer func(meter storetypes.GasMeter, sig signing.SignatureV2, params types.Params) error
TxFeeChecker TxFeeChecker
// UnorderedNonceManager is an opt-in feature for x/auth.
// When set, this application will be able to receive and process unordered transactions.
UnorderedNonceManager UnorderedNonceManager
UnorderedTxOptions []UnorderedTxDecoratorOptions
// SigVerifyOptions are the options for the signature verification decorator.
// This allows for modification of signature verification behavior, such as how long an unordered transaction can
// be valid, or how much gas to charge for unordered transactions.
SigVerifyOptions []SigVerificationDecoratorOption
}

// NewAnteHandler returns an AnteHandler that checks and increments sequence
Expand Down Expand Up @@ -53,13 +53,9 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
NewValidateSigCountDecorator(options.AccountKeeper),
NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler, options.SigVerifyOptions...),
NewIncrementSequenceDecorator(options.AccountKeeper),
}

if options.UnorderedNonceManager != nil {
anteDecorators = append(anteDecorators, NewUnorderedTxDecorator(options.UnorderedNonceManager, options.UnorderedTxOptions...))
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
}
2 changes: 1 addition & 1 deletion x/auth/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ func TestAnteHandlerReCheck(t *testing.T) {
}

func TestAnteHandlerUnorderedTx(t *testing.T) {
suite := SetupTestSuite(t, false)
suite := SetupTestSuiteWithUnordered(t, false, true)
accs := suite.CreateTestAccounts(1)
msg := testdata.NewTestMsg(accs[0].acc.GetAddress())

Expand Down
5 changes: 1 addition & 4 deletions x/auth/ante/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ type AccountKeeper interface {
SetAccount(ctx context.Context, acc sdk.AccountI)
GetModuleAddress(moduleName string) sdk.AccAddress
AddressCodec() address.Codec
}

// UnorderedNonceManager defines the contract needed for UnorderedNonce management.
type UnorderedNonceManager interface {
IsUnorderedTransactionsEnabled() bool
RemoveExpiredUnorderedNonces(ctx sdk.Context) error
TryAddUnorderedNonce(ctx sdk.Context, sender []byte, timestamp time.Time) error
}
Expand Down
Loading
Loading