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(app): Integrate the block-sdk #413

Merged
15 changes: 15 additions & 0 deletions app/ante_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
consumerante "github.com/cosmos/interchain-security/v3/app/consumer/ante"
ibcconsumerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper"
auctionante "github.com/skip-mev/block-sdk/x/auction/ante"
auctionkeeper "github.com/skip-mev/block-sdk/x/auction/keeper"
)

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
Expand All @@ -27,6 +29,11 @@ type HandlerOptions struct {
WasmConfig *wasmTypes.WasmConfig
TXCounterStoreKey storetypes.StoreKey

// dependencies for the x/auction ante-handler
AuctionKeeper auctionkeeper.Keeper
TxEncoder sdk.TxEncoder
MEVLane auctionante.MEVLane

// globalFee
GlobalFeeSubspace paramtypes.Subspace
}
Expand All @@ -50,6 +57,9 @@ func NewAnteHandler(options HandlerOptions, logger log.Logger) (sdk.AnteHandler,
if options.GlobalFeeSubspace.Name() == "" {
return nil, errors.Wrap(gaiaerrors.ErrNotFound, "globalfee param store is required for AnteHandler")
}
if options.MEVLane == nil {
return nil, errors.Wrap(gaiaerrors.ErrLogic, "mev lane is required for AnteHandler")
}

sigGasConsumer := options.SigGasConsumer
if sigGasConsumer == nil {
Expand Down Expand Up @@ -80,6 +90,11 @@ func NewAnteHandler(options HandlerOptions, logger log.Logger) (sdk.AnteHandler,
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
auctionante.NewAuctionDecorator(
options.AuctionKeeper,
options.TxEncoder,
options.MEVLane,
),
}

// Don't delete it even if IDE tells you so.
Expand Down
126 changes: 118 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path/filepath"
"reflect"

globalfeetypes "github.com/cosmos/gaia/v11/x/globalfee/types"
"github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/neutron-org/neutron/v2/docs"

"github.com/neutron-org/neutron/v2/app/upgrades"
"github.com/neutron-org/neutron/v2/app/upgrades/nextupgrade"
v030 "github.com/neutron-org/neutron/v2/app/upgrades/v0.3.0"
v044 "github.com/neutron-org/neutron/v2/app/upgrades/v0.4.4"
v200 "github.com/neutron-org/neutron/v2/app/upgrades/v2.0.0"
Expand Down Expand Up @@ -161,14 +163,25 @@ import (
ibcswaptypes "github.com/neutron-org/neutron/v2/x/ibcswap/types"

gmpmiddleware "github.com/neutron-org/neutron/v2/x/gmp"

// Block-sdk imports
blocksdkabci "github.com/skip-mev/block-sdk/abci"
blocksdk "github.com/skip-mev/block-sdk/block"
"github.com/skip-mev/block-sdk/x/auction"
auctionkeeper "github.com/skip-mev/block-sdk/x/auction/keeper"
rewardsaddressprovider "github.com/skip-mev/block-sdk/x/auction/rewards"
auctiontypes "github.com/skip-mev/block-sdk/x/auction/types"

"github.com/skip-mev/block-sdk/abci/checktx"
"github.com/skip-mev/block-sdk/block/base"
)

const (
Name = "neutrond"
)

var (
Upgrades = []upgrades.Upgrade{v030.Upgrade, v044.Upgrade, v200.Upgrade, v202.Upgrade}
Upgrades = []upgrades.Upgrade{v030.Upgrade, v044.Upgrade, v200.Upgrade, v202.Upgrade, nextupgrade.Upgrade}

// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string
Expand Down Expand Up @@ -215,6 +228,7 @@ var (
),
ibchooks.AppModuleBasic{},
packetforward.AppModuleBasic{},
auction.AppModuleBasic{},
globalfee.AppModule{},
dex.AppModuleBasic{},
ibcswap.AppModuleBasic{},
Expand All @@ -223,6 +237,7 @@ var (
// module account permissions
maccPerms = map[string][]string{
authtypes.FeeCollectorName: nil,
auctiontypes.ModuleName: nil,
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
icatypes.ModuleName: nil,
wasmtypes.ModuleName: {},
Expand Down Expand Up @@ -275,10 +290,12 @@ type App struct {
memKeys map[string]*storetypes.MemoryStoreKey

// keepers
AccountKeeper authkeeper.AccountKeeper
AdminmoduleKeeper adminmodulekeeper.Keeper
AuthzKeeper authzkeeper.Keeper
BankKeeper bankkeeper.BaseKeeper
AccountKeeper authkeeper.AccountKeeper
AdminmoduleKeeper adminmodulekeeper.Keeper
AuthzKeeper authzkeeper.Keeper
BankKeeper bankkeeper.BaseKeeper
// AuctionKeeper handles the processing of bid-txs, the selection of winners per height, and the distribution of rewards.
AuctionKeeper auctionkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
CrisisKeeper crisiskeeper.Keeper
Expand Down Expand Up @@ -324,6 +341,10 @@ type App struct {

// sm is the simulation manager
sm *module.SimulationManager

// Custom checkTx handler -> this check-tx is used to simulate txs that are
// wrapped in a bid-tx
checkTxHandler checktx.CheckTx
}

func (app *App) GetTestBankKeeper() integration.TestBankKeeper {
Expand Down Expand Up @@ -374,7 +395,7 @@ func New(
icahosttypes.StoreKey, capabilitytypes.StoreKey,
interchainqueriesmoduletypes.StoreKey, contractmanagermoduletypes.StoreKey, interchaintxstypes.StoreKey, wasmtypes.StoreKey, feetypes.StoreKey,
feeburnertypes.StoreKey, adminmoduletypes.StoreKey, ccvconsumertypes.StoreKey, tokenfactorytypes.StoreKey, pfmtypes.StoreKey,
crontypes.StoreKey, ibchookstypes.StoreKey, consensusparamtypes.StoreKey, crisistypes.StoreKey, dextypes.StoreKey,
crontypes.StoreKey, ibchookstypes.StoreKey, consensusparamtypes.StoreKey, crisistypes.StoreKey, dextypes.StoreKey, auctiontypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey, feetypes.MemStoreKey)
Expand Down Expand Up @@ -595,6 +616,16 @@ func New(
authtypes.NewModuleAddress(adminmoduletypes.ModuleName).String(),
)

app.AuctionKeeper = auctionkeeper.NewKeeperWithRewardsAddressProvider(
appCodec,
keys[auctiontypes.StoreKey],
app.AccountKeeper,
&app.BankKeeper,
// 25% of rewards should be sent to the redistribute address
rewardsaddressprovider.NewFixedAddressRewardsAddressProvider(app.AccountKeeper.GetModuleAddress(ccvconsumertypes.ConsumerRedistributeName)),
authtypes.NewModuleAddress(adminmoduletypes.ModuleName).String(),
)

dexModule := dex.NewAppModule(appCodec, app.DexKeeper, app.BankKeeper)

app.SwapKeeper = ibcswapkeeper.NewKeeper(
Expand Down Expand Up @@ -779,6 +810,7 @@ func New(
globalfee.NewAppModule(app.GetSubspace(globalfee.ModuleName)),
swapModule,
dexModule,
auction.NewAppModule(appCodec, app.AuctionKeeper),
crisis.NewAppModule(&app.CrisisKeeper, skipGenesisInvariants, app.GetSubspace(crisistypes.ModuleName)), // always be last to make sure that it checks for all invariants and not only part of them
)

Expand All @@ -787,6 +819,7 @@ func New(
// CanWithdrawInvariant invariant.
// NOTE: staking module is required if HistoricalEntries param > 0
app.mm.SetOrderBeginBlockers(
auctiontypes.ModuleName,
upgradetypes.ModuleName,
capabilitytypes.ModuleName,
slashingtypes.ModuleName,
Expand Down Expand Up @@ -819,6 +852,7 @@ func New(
)

app.mm.SetOrderEndBlockers(
auctiontypes.ModuleName,
crisistypes.ModuleName,
capabilitytypes.ModuleName,
authtypes.ModuleName,
Expand Down Expand Up @@ -858,6 +892,7 @@ func New(
// so that other modules that want to create or claim capabilities afterwards in InitChain
// can do so safely.
app.mm.SetOrderInitGenesis(
auctiontypes.ModuleName,
capabilitytypes.ModuleName,
authtypes.ModuleName,
ibctransfertypes.ModuleName,
Expand Down Expand Up @@ -928,6 +963,19 @@ func New(
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)

app.SetEndBlocker(app.EndBlocker)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same method called on L1016, pls remove one of


// create the lanes
mevLane, baseLane := app.CreateLanes()
mempool, err := blocksdk.NewLanedMempool(app.Logger(), []blocksdk.Lane{mevLane, baseLane})
if err != nil {
panic(err)
}

// set the mempool first
app.SetMempool(mempool)

// then create the ante-handler
anteHandler, err := NewAnteHandler(
HandlerOptions{
HandlerOptions: ante.HandlerOptions{
Expand All @@ -942,16 +990,57 @@ func New(
TXCounterStoreKey: keys[wasmtypes.StoreKey],
ConsumerKeeper: app.ConsumerKeeper,
GlobalFeeSubspace: app.GetSubspace(globalfee.ModuleName),
AuctionKeeper: app.AuctionKeeper,
TxEncoder: app.GetTxConfig().TxEncoder(),
MEVLane: mevLane,
},
app.Logger(),
)
if err != nil {
panic(err)
}

app.SetAnteHandler(anteHandler)

app.SetEndBlocker(app.EndBlocker)
// set ante-handlers
opts := []base.LaneOption{
base.WithAnteHandler(anteHandler),
}
baseLane.WithOptions(opts...)
mevLane.WithOptions(opts...)

// set the block-sdk prepare / process-proposal handlers
handler := blocksdkabci.NewProposalHandler(
app.Logger(),
app.GetTxConfig().TxDecoder(),
app.GetTxConfig().TxEncoder(),
mempool,
)
app.SetPrepareProposal(handler.PrepareProposalHandler())

// we use a no-op ProcessProposal, this way, we accept all proposals in avoidance
// of liveness failures due to Prepare / Process inconsistency. In other words,
// this ProcessProposal always returns ACCEPT.
app.SetProcessProposal(baseapp.NoOpProcessProposal())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting ProcessProposal to a nop


// block-sdk CheckTx handler
mevCheckTxHandler := checktx.NewMEVCheckTxHandler(
app,
app.GetTxConfig().TxDecoder(),
mevLane,
anteHandler,
app.BaseApp.CheckTx,
app.ChainID(),
)

// wrap checkTxHandler with mempool parity handler
parityCheckTx := checktx.NewMempoolParityCheckTx(
app.Logger(),
mempool,
app.GetTxConfig().TxDecoder(),
mevCheckTxHandler.CheckTx(),
)

app.SetCheckTx(parityCheckTx.CheckTx())

// must be before Loading version
// requires the snapshot store to be created and registered as a BaseAppOption
Expand Down Expand Up @@ -1021,6 +1110,7 @@ func (app *App) setupUpgradeHandlers() {
SlashingKeeper: app.SlashingKeeper,
ParamsKeeper: app.ParamsKeeper,
CapabilityKeeper: app.CapabilityKeeper,
AuctionKeeper: app.AuctionKeeper,
ContractManager: app.ContractManagerKeeper,
AdminModule: app.AdminmoduleKeeper,
ConsensusKeeper: &app.ConsensusParamsKeeper,
Expand All @@ -1034,6 +1124,26 @@ func (app *App) setupUpgradeHandlers() {
}
}

// ChainID gets chainID from private fields of BaseApp
// Should be removed once SDK 0.50.x will be adopted
func (app *App) ChainID() string {
field := reflect.ValueOf(app.BaseApp).Elem().FieldByName("chainID")
return field.String()
}

// CheckTx will check the transaction with the provided checkTxHandler. We override the default
// handler so that we can verify bid transactions before they are inserted into the mempool.
// With the Block-SDK CheckTx, we can verify the bid transaction and all of the bundled transactions
// before inserting the bid transaction into the mempool.
func (app *App) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
return app.checkTxHandler(req)
}

// SetCheckTx sets the checkTxHandler for the app.
func (app *App) SetCheckTx(handler checktx.CheckTx) {
app.checkTxHandler = handler
}

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

Expand Down
55 changes: 55 additions & 0 deletions app/lane.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package app

import (
"cosmossdk.io/math"
signer_extraction_adapter "github.com/skip-mev/block-sdk/adapters/signer_extraction_adapter"
blocksdkbase "github.com/skip-mev/block-sdk/block/base"
base_lane "github.com/skip-mev/block-sdk/lanes/base"
mev_lane "github.com/skip-mev/block-sdk/lanes/mev"
)

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

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

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

// BaseLane (DefaultLane) is intended to hold all txs that are not matched by any lanes ordered before this
// lane.
baseLane := base_lane.NewDefaultLane(basecfg, blocksdkbase.DefaultMatchHandler())

// initialize the MEV lane, this lane is intended to hold all bid txs
factory := mev_lane.NewDefaultAuctionFactory(app.GetTxConfig().TxDecoder(), signer_extraction_adapter.NewDefaultAdapter())

mevcfg := blocksdkbase.LaneConfig{
Logger: app.Logger(),
TxDecoder: app.GetTxConfig().TxDecoder(),
TxEncoder: app.GetTxConfig().TxEncoder(),
SignerExtractor: signer_extraction_adapter.NewDefaultAdapter(),
MaxBlockSpace: MaxBlockspaceForMEVLane,
MaxTxs: MaxTxsForMEVLane,
}
mevLane := mev_lane.NewMEVLane(
mevcfg,
factory,
factory.MatchHandler(),
)

return mevLane, baseLane
}
32 changes: 32 additions & 0 deletions app/upgrades/nextupgrade/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nextupgrade

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
auctiontypes "github.com/skip-mev/block-sdk/x/auction/types"

"github.com/neutron-org/neutron/v2/app/upgrades"
)

const (
// UpgradeName defines the on-chain upgrade name.
UpgradeName = "nextupgrade"

AuctionParamsMaxBundleSize = 2
AuctionParamsFrontRunningProtection = true
)

var (
Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
StoreUpgrades: storetypes.StoreUpgrades{
Added: []string{auctiontypes.ModuleName},
},
}
AuctionParamsReserveFee = sdk.Coin{Denom: "untrn", Amount: sdk.NewInt(500_000)}
AuctionParamsMinBidIncrement = sdk.Coin{Denom: "untrn", Amount: sdk.NewInt(100_000)}
AuctionParamsProposerFee = math.LegacyNewDecWithPrec(25, 2)
)
Loading
Loading