Skip to content
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

feat: [ADR-070] Unordered Transactions (2/2) #18739

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
28 changes: 28 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"cosmossdk.io/x/accounts/testing/counter"
"cosmossdk.io/x/auth"
"cosmossdk.io/x/auth/ante"
"cosmossdk.io/x/auth/ante/unorderedtx"
authcodec "cosmossdk.io/x/auth/codec"
authkeeper "cosmossdk.io/x/auth/keeper"
"cosmossdk.io/x/auth/posthandler"
Expand Down Expand Up @@ -169,6 +170,8 @@ type SimApp struct {
ModuleManager *module.Manager
BasicModuleManager module.BasicManager

UnorderedTxManager *unorderedtx.Manager

// simulation manager
sm *module.SimulationManager

Expand Down Expand Up @@ -519,6 +522,25 @@ func NewSimApp(
}
app.sm = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, overrideModules)

// create, start, and load the unordered tx manager
utxDataDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data")
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
app.UnorderedTxManager = unorderedtx.NewManager(utxDataDir)
app.UnorderedTxManager.Start()

if err := app.UnorderedTxManager.OnInit(); err != nil {
panic(fmt.Errorf("failed to initialize unordered tx manager: %w", err))
}

// register custom snapshot extensions (if any)
// if manager := app.SnapshotManager(); manager != nil {
// err := manager.RegisterExtensions(
// unorderedtx.NewSnapshotter(app.UnorderedTxManager),
// )
// if err != nil {
// panic(fmt.Errorf("failed to register snapshot extension: %s", err))
// }
// }
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

app.sm.RegisterStoreDecoders()

// initialize stores
Expand Down Expand Up @@ -600,6 +622,12 @@ func (app *SimApp) setPostHandler() {
app.SetPostHandler(postHandler)
}

// Close implements the Application interface and closes all necessary application
// resources.
func (app *SimApp) Close() error {
return app.UnorderedTxManager.Close()
}

// Name returns the name of the App
func (app *SimApp) Name() string { return app.BaseApp.Name() }

Expand Down
31 changes: 31 additions & 0 deletions simapp/app_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
package simapp

import (
"fmt"
"io"
"os"
"path/filepath"

dbm "github.com/cosmos/cosmos-db"
"github.com/spf13/cast"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/auth"
"cosmossdk.io/x/auth/ante/unorderedtx"
authkeeper "cosmossdk.io/x/auth/keeper"
authsims "cosmossdk.io/x/auth/simulation"
authtypes "cosmossdk.io/x/auth/types"
Expand All @@ -34,6 +37,7 @@ import (

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
Expand Down Expand Up @@ -64,6 +68,8 @@ type SimApp struct {
txConfig client.TxConfig
interfaceRegistry codectypes.InterfaceRegistry

UnorderedTxManager *unorderedtx.Manager

// keepers
AuthKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
Expand Down Expand Up @@ -256,13 +262,38 @@ func NewSimApp(
// return app.App.InitChainer(ctx, req)
// })

// create, start, and load the unordered tx manager
utxDataDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data")
app.UnorderedTxManager = unorderedtx.NewManager(utxDataDir)
app.UnorderedTxManager.Start()

if err := app.UnorderedTxManager.OnInit(); err != nil {
panic(fmt.Errorf("failed to initialize unordered tx manager: %w", err))
}

// register custom snapshot extensions (if any)
// if manager := app.SnapshotManager(); manager != nil {
// err := manager.RegisterExtensions(
// unorderedtx.NewSnapshotter(app.UnorderedTxManager),
// )
// if err != nil {
// panic(fmt.Errorf("failed to register snapshot extension: %s", err))
// }
// }

if err := app.Load(loadLatest); err != nil {
panic(err)
}

return app
}

// Close implements the Application interface and closes all necessary application
// resources.
func (app *SimApp) Close() error {
return app.UnorderedTxManager.Close()
}

// LegacyAmino returns SimApp's amino codec.
//
// NOTE: This is solely to be used for testing purposes as it may be desirable
Expand Down
10 changes: 6 additions & 4 deletions x/auth/ante/unordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ func (d *UnorderedTxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b
return next(ctx, tx, simulate)
}

if unorderedTx.GetTimeoutHeight() == 0 {
// TTL is defined as a specific block height at which this tx is no longer valid
ttl := unorderedTx.GetTimeoutHeight()

if ttl == 0 {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "unordered transaction must have timeout_height set")
}

if unorderedTx.GetTimeoutHeight() > uint64(ctx.BlockHeight())+d.maxUnOrderedTTL {
if ttl > uint64(ctx.BlockHeight())+d.maxUnOrderedTTL {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "unordered tx ttl exceeds %d", d.maxUnOrderedTTL)
}

Expand All @@ -62,7 +64,7 @@ func (d *UnorderedTxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b

if ctx.ExecMode() == sdk.ExecModeFinalize {
// a new tx included in the block, add the hash to the unordered tx manager
d.txManager.Add(txHash, unorderedTx.GetTimeoutHeight())
d.txManager.Add(txHash, ttl)
}

return next(ctx, tx, simulate)
Expand Down
36 changes: 24 additions & 12 deletions x/auth/ante/unordered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
)

func TestUnorderedTxDecorator_OrderedTx(t *testing.T) {
txm := unorderedtx.NewManager()
defer txm.Close()
txm := unorderedtx.NewManager(t.TempDir())
defer func() {
require.NoError(t, txm.Close())
}()

txm.Start()

Expand All @@ -31,8 +33,10 @@ func TestUnorderedTxDecorator_OrderedTx(t *testing.T) {
}

func TestUnorderedTxDecorator_UnorderedTx_NoTTL(t *testing.T) {
txm := unorderedtx.NewManager()
defer txm.Close()
txm := unorderedtx.NewManager(t.TempDir())
defer func() {
require.NoError(t, txm.Close())
}()

txm.Start()

Expand All @@ -46,8 +50,10 @@ func TestUnorderedTxDecorator_UnorderedTx_NoTTL(t *testing.T) {
}

func TestUnorderedTxDecorator_UnorderedTx_InvalidTTL(t *testing.T) {
txm := unorderedtx.NewManager()
defer txm.Close()
txm := unorderedtx.NewManager(t.TempDir())
defer func() {
require.NoError(t, txm.Close())
}()

txm.Start()

Expand All @@ -61,8 +67,10 @@ func TestUnorderedTxDecorator_UnorderedTx_InvalidTTL(t *testing.T) {
}

func TestUnorderedTxDecorator_UnorderedTx_AlreadyExists(t *testing.T) {
txm := unorderedtx.NewManager()
defer txm.Close()
txm := unorderedtx.NewManager(t.TempDir())
defer func() {
require.NoError(t, txm.Close())
}()

txm.Start()

Expand All @@ -79,8 +87,10 @@ func TestUnorderedTxDecorator_UnorderedTx_AlreadyExists(t *testing.T) {
}

func TestUnorderedTxDecorator_UnorderedTx_ValidCheckTx(t *testing.T) {
txm := unorderedtx.NewManager()
defer txm.Close()
txm := unorderedtx.NewManager(t.TempDir())
defer func() {
require.NoError(t, txm.Close())
}()

txm.Start()

Expand All @@ -94,8 +104,10 @@ func TestUnorderedTxDecorator_UnorderedTx_ValidCheckTx(t *testing.T) {
}

func TestUnorderedTxDecorator_UnorderedTx_ValidDeliverTx(t *testing.T) {
txm := unorderedtx.NewManager()
defer txm.Close()
txm := unorderedtx.NewManager(t.TempDir())
defer func() {
require.NoError(t, txm.Close())
}()

txm.Start()

Expand Down
Loading
Loading