Skip to content

Commit

Permalink
add gassless lane
Browse files Browse the repository at this point in the history
  • Loading branch information
joldie777 committed Jan 28, 2025
1 parent 954660e commit 9797c4c
Show file tree
Hide file tree
Showing 36 changed files with 2,914 additions and 12 deletions.
18 changes: 12 additions & 6 deletions app/ante_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
consumerante "github.com/cosmos/interchain-security/v5/app/consumer/ante"
ibcconsumerkeeper "github.com/cosmos/interchain-security/v5/x/ccv/consumer/keeper"
blocksdk "github.com/skip-mev/block-sdk/v2/block"
feemarketante "github.com/skip-mev/feemarket/x/feemarket/ante"

globalfeeante "github.com/neutron-org/neutron/v5/x/globalfee/ante"
Expand All @@ -33,6 +34,8 @@ type HandlerOptions struct {
WasmConfig *wasmTypes.WasmConfig
TXCounterStoreService corestoretypes.KVStoreService
FeeMarketKeeper feemarketante.FeeMarketKeeper

FreeLane blocksdk.Lane
}

func NewAnteHandler(options HandlerOptions, logger log.Logger) (sdk.AnteHandler, error) {
Expand Down Expand Up @@ -71,12 +74,15 @@ func NewAnteHandler(options HandlerOptions, logger log.Logger) (sdk.AnteHandler,
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
feemarketante.NewFeeMarketCheckDecorator(
options.AccountKeeper,
options.BankKeeper,
options.FeegrantKeeper,
options.FeeMarketKeeper,
NewFeeDecoratorWithSwitch(options),
blocksdk.NewIgnoreDecorator(
feemarketante.NewFeeMarketCheckDecorator(
options.AccountKeeper,
options.BankKeeper,
options.FeegrantKeeper,
options.FeeMarketKeeper,
NewFeeDecoratorWithSwitch(options),
),
options.FreeLane,
),
// SetPubKeyDecorator must be called before all signature verification decorators
ante.NewSetPubKeyDecorator(options.AccountKeeper),
Expand Down
22 changes: 19 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"

dynamicfeestypes "github.com/neutron-org/neutron/v5/x/dynamicfees/types"
"github.com/neutron-org/neutron/v5/x/freelane"
freelanetypes "github.com/neutron-org/neutron/v5/x/freelane/types"

"github.com/skip-mev/feemarket/x/feemarket"
feemarketkeeper "github.com/skip-mev/feemarket/x/feemarket/keeper"
Expand Down Expand Up @@ -170,6 +172,7 @@ import (
feeburnertypes "github.com/neutron-org/neutron/v5/x/feeburner/types"
"github.com/neutron-org/neutron/v5/x/feerefunder"
feekeeper "github.com/neutron-org/neutron/v5/x/feerefunder/keeper"
freelanekeeper "github.com/neutron-org/neutron/v5/x/freelane/keeper"
ibchooks "github.com/neutron-org/neutron/v5/x/ibc-hooks"
ibchookstypes "github.com/neutron-org/neutron/v5/x/ibc-hooks/types"
"github.com/neutron-org/neutron/v5/x/interchainqueries"
Expand Down Expand Up @@ -283,6 +286,7 @@ var (
oracle.AppModuleBasic{},
marketmap.AppModuleBasic{},
dynamicfees.AppModuleBasic{},
freelane.AppModuleBasic{},
consensus.AppModuleBasic{},
)

Expand Down Expand Up @@ -365,6 +369,7 @@ type App struct {
FeeGrantKeeper feegrantkeeper.Keeper
FeeMarkerKeeper *feemarketkeeper.Keeper
DynamicFeesKeeper *dynamicfeeskeeper.Keeper
FreeLaneKeeper *freelanekeeper.Keeper
FeeKeeper *feekeeper.Keeper
FeeBurnerKeeper *feeburnerkeeper.Keeper
ConsumerKeeper ccvconsumerkeeper.Keeper
Expand Down Expand Up @@ -487,7 +492,7 @@ func New(
interchainqueriesmoduletypes.StoreKey, contractmanagermoduletypes.StoreKey, interchaintxstypes.StoreKey, wasmtypes.StoreKey, feetypes.StoreKey,
feeburnertypes.StoreKey, adminmoduletypes.StoreKey, ccvconsumertypes.StoreKey, tokenfactorytypes.StoreKey, pfmtypes.StoreKey,
crontypes.StoreKey, ibcratelimittypes.ModuleName, ibchookstypes.StoreKey, consensusparamtypes.StoreKey, crisistypes.StoreKey, dextypes.StoreKey, auctiontypes.StoreKey,
oracletypes.StoreKey, marketmaptypes.StoreKey, feemarkettypes.StoreKey, dynamicfeestypes.StoreKey, globalfeetypes.StoreKey,
oracletypes.StoreKey, marketmaptypes.StoreKey, feemarkettypes.StoreKey, dynamicfeestypes.StoreKey, freelanetypes.StoreKey, globalfeetypes.StoreKey,
)
tkeys := storetypes.NewTransientStoreKeys(paramstypes.TStoreKey, dextypes.TStoreKey)
memKeys := storetypes.NewMemoryStoreKeys(capabilitytypes.MemStoreKey, feetypes.MemStoreKey)
Expand Down Expand Up @@ -584,6 +589,12 @@ func New(
authtypes.NewModuleAddress(adminmoduletypes.ModuleName).String(),
)

app.FreeLaneKeeper = freelanekeeper.NewKeeper(
appCodec,
keys[freelanetypes.StoreKey],
authtypes.NewModuleAddress(adminmoduletypes.ModuleName).String(),
)

// ... other modules keepers
// pre-initialize ConsumerKeeper to satisfy ibckeeper.NewKeeper
// which would panic on nil or zero keeper
Expand Down Expand Up @@ -922,6 +933,7 @@ func New(
globalfee.NewAppModule(app.GlobalFeeKeeper, app.GetSubspace(globalfee.ModuleName), app.AppCodec(), app.keys[globalfee.ModuleName]),
feemarket.NewAppModule(appCodec, *app.FeeMarkerKeeper),
dynamicfees.NewAppModule(appCodec, *app.DynamicFeesKeeper),
freelane.NewAppModule(appCodec, *app.FreeLaneKeeper),
dexModule,
marketmapModule,
oracleModule,
Expand Down Expand Up @@ -1053,6 +1065,7 @@ func New(
marketmaptypes.ModuleName,
dextypes.ModuleName,
dynamicfeestypes.ModuleName,
freelanetypes.ModuleName,
consensusparamtypes.ModuleName,
)

Expand Down Expand Up @@ -1101,8 +1114,8 @@ func New(
app.SetEndBlocker(app.EndBlocker)

// create the lanes
baseLane := app.CreateLanes()
mempool, err := blocksdk.NewLanedMempool(app.Logger(), []blocksdk.Lane{baseLane})
freeLane, baseLane := app.CreateLanes()
mempool, err := blocksdk.NewLanedMempool(app.Logger(), []blocksdk.Lane{freeLane, baseLane})
if err != nil {
panic(err)
}
Expand All @@ -1126,6 +1139,7 @@ func New(
TXCounterStoreService: runtime.NewKVStoreService(keys[wasmtypes.StoreKey]),
ConsumerKeeper: app.ConsumerKeeper,
FeeMarketKeeper: app.FeeMarkerKeeper,
FreeLane: freeLane,
},
app.Logger(),
)
Expand All @@ -1149,6 +1163,7 @@ func New(
opts := []base.LaneOption{
base.WithAnteHandler(anteHandler),
}
freeLane.WithOptions(opts...)
baseLane.WithOptions(opts...)

// set the block-sdk prepare / process-proposal handlers
Expand Down Expand Up @@ -1367,6 +1382,7 @@ func (app *App) setupUpgradeHandlers() {
MarketmapKeeper: app.MarketMapKeeper,
FeeMarketKeeper: app.FeeMarkerKeeper,
DynamicfeesKeeper: app.DynamicFeesKeeper,
FreeLaneKeeper: app.FreeLaneKeeper,
DexKeeper: &app.DexKeeper,
IbcRateLimitKeeper: app.RateLimitingICS4Wrapper.IbcratelimitKeeper,
GlobalFeeSubspace: app.GetSubspace(globalfee.ModuleName),
Expand Down
38 changes: 35 additions & 3 deletions app/lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@ package app

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
signer_extraction_adapter "github.com/skip-mev/block-sdk/v2/adapters/signer_extraction_adapter"
blocksdkbase "github.com/skip-mev/block-sdk/v2/block/base"
base_lane "github.com/skip-mev/block-sdk/v2/lanes/base"
free_lane "github.com/skip-mev/block-sdk/v2/lanes/free"
)

const (
MaxTxsForDefaultLane = 3000 // maximal number of txs that can be stored in this lane at any point in time
)

var MaxBlockspaceForDefaultLane = math.LegacyMustNewDecFromStr("1") // maximal fraction of blockMaxBytes / gas that can be used by this lane at any point in time (100%)
var (
MaxBlockSpaceForFreeLane = math.LegacyMustNewDecFromStr("0.1") // (10%)
MaxBlockspaceForDefaultLane = math.LegacyMustNewDecFromStr("0.9") // maximal fraction of blockMaxBytes / gas that can be used by this lane at any point in time (90%)
)

// CreateLanes creates a LaneMempool containing MEV, default lanes (in that order)
func (app *App) CreateLanes() *blocksdkbase.BaseLane {
func (app *App) CreateLanes() (*blocksdkbase.BaseLane, *blocksdkbase.BaseLane) {
// initialize the free lane
freeConfig := blocksdkbase.LaneConfig{
Logger: app.Logger(),
TxEncoder: app.GetTxConfig().TxEncoder(),
TxDecoder: app.GetTxConfig().TxDecoder(),
SignerExtractor: signer_extraction_adapter.NewDefaultAdapter(),
MaxBlockSpace: MaxBlockSpaceForFreeLane,
MaxTxs: 0,
}

freeLane := free_lane.NewFreeLane(freeConfig, blocksdkbase.NewDefaultTxPriority(), app.freeLaneMatchHandler())

// initialize the default lane
basecfg := blocksdkbase.LaneConfig{
Logger: app.Logger(),
Expand All @@ -34,5 +51,20 @@ func (app *App) CreateLanes() *blocksdkbase.BaseLane {
basecfg.MaxTxs,
)

return baseLane
return freeLane, baseLane
}

func (app *App) freeLaneMatchHandler() blocksdkbase.MatchHandler {
return func(ctx sdk.Context, tx sdk.Tx) bool {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return false
}

acc := app.AccountKeeper.GetAccount(ctx, feeTx.FeePayer())

freeLaneParams := app.FreeLaneKeeper.GetParams(ctx)

return acc.GetSequence() < freeLaneParams.SequenceNumber
}
}
2 changes: 2 additions & 0 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
ibcratelimitkeeper "github.com/neutron-org/neutron/v5/x/ibc-rate-limit/keeper"

dynamicfeeskeeper "github.com/neutron-org/neutron/v5/x/dynamicfees/keeper"
freelanekeeper "github.com/neutron-org/neutron/v5/x/freelane/keeper"

contractmanagerkeeper "github.com/neutron-org/neutron/v5/x/contractmanager/keeper"
cronkeeper "github.com/neutron-org/neutron/v5/x/cron/keeper"
Expand Down Expand Up @@ -67,6 +68,7 @@ type UpgradeKeepers struct {
MarketmapKeeper *marketmapkeeper.Keeper
FeeMarketKeeper *feemarketkeeper.Keeper
DynamicfeesKeeper *dynamicfeeskeeper.Keeper
FreeLaneKeeper *freelanekeeper.Keeper
DexKeeper *dexkeeper.Keeper
IbcRateLimitKeeper *ibcratelimitkeeper.Keeper
// subspaces
Expand Down
12 changes: 12 additions & 0 deletions proto/neutron/freelane/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";
package neutron.freelane;

import "gogoproto/gogo.proto";
import "neutron/freelane/params.proto";

option go_package = "github.com/neutron-org/neutron/v5/x/freelane/types";

// Defines the freelane module's genesis state.
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
}
15 changes: 15 additions & 0 deletions proto/neutron/freelane/params.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";
package neutron.freelane;

import "gogoproto/gogo.proto";

option go_package = "github.com/neutron-org/neutron/v5/x/freelane/types";

// Defines the parameters for the module.
message Params {
option (gogoproto.goproto_stringer) = false;
// Relative percentage of block space that can be used by the lane
double block_space = 1;
// Limit of gasless transactions
uint64 sequence_number = 2;
}
25 changes: 25 additions & 0 deletions proto/neutron/freelane/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";
package neutron.freelane;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "neutron/freelane/params.proto";

option go_package = "github.com/neutron-org/neutron/v5/x/freelane/types";

// Defines the gRPC querier service.
service Query {
// Queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/neutron/freelane/params";
}
}

// The request type for the Query/Params RPC method.
message QueryParamsRequest {}

// The response type for the Query/Params RPC method.
message QueryParamsResponse {
// Holds all the parameters of this module.
Params params = 1 [(gogoproto.nullable) = false];
}
38 changes: 38 additions & 0 deletions proto/neutron/freelane/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
syntax = "proto3";
package neutron.freelane;

import "amino/amino.proto";
import "cosmos/msg/v1/msg.proto";
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "neutron/freelane/params.proto";

option go_package = "github.com/neutron-org/neutron/v5/x/freelane/types";

// Defines the Msg service.
service Msg {
option (cosmos.msg.v1.service) = true;

// Updates the module parameters.
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}

// The MsgUpdateParams request type.
message MsgUpdateParams {
option (amino.name) = "freelane/MsgUpdateParams";
option (cosmos.msg.v1.signer) = "authority";

// The address of the governance account.
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// Defines the x/freelane parameters to update.
//
// NOTE: All parameters must be supplied.
Params params = 2 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
}

// Defines the response structure for executing a MsgUpdateParams message.
message MsgUpdateParamsResponse {}
48 changes: 48 additions & 0 deletions testutil/freelane/keeper/freelane.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package keeper

import (
"testing"

"cosmossdk.io/log"
"cosmossdk.io/store/metrics"
adminmoduletypes "github.com/cosmos/admin-module/v2/x/adminmodule/types"
cosmosdb "github.com/cosmos/cosmos-db"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"cosmossdk.io/store"
storetypes "cosmossdk.io/store/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/neutron-org/neutron/v5/x/freelane/keeper"
"github.com/neutron-org/neutron/v5/x/freelane/types"
)

func FreeLaneKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)

db := cosmosdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

k := keeper.NewKeeper(
cdc,
storeKey,
authtypes.NewModuleAddress(adminmoduletypes.ModuleName).String(),
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

// Initialize params
err := k.SetParams(ctx, types.DefaultParams())
require.NoError(t, err)

return k, ctx
}
27 changes: 27 additions & 0 deletions x/freelane/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"

"github.com/neutron-org/neutron/v5/x/freelane/types"
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(_ string) *cobra.Command {
// Group freelane queries under a subcommand
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdQueryParams())

return cmd
}
Loading

0 comments on commit 9797c4c

Please sign in to comment.