block-sdk/lanes/mev
Alex Johnson f1cde2acec
fix: mempool lane size check on CheckTx (#561)
* push

* init

* fix setup

* format

* fix test

* use lane

* ok

* finalize

* fix everything

* lint fix:

* Update abci/checktx/mempool_parity_check_tx.go

Co-authored-by: David Terpay <35130517+davidterpay@users.noreply.github.com>

* lint fix

* tidy

* remove

* cleanup

---------

Co-authored-by: David Terpay <david.terpay@gmail.com>
Co-authored-by: David Terpay <35130517+davidterpay@users.noreply.github.com>
2024-07-02 19:19:22 -04:00
..
testutils fix: mempool lane size check on CheckTx (#561) 2024-07-02 19:19:22 -04:00
abci_test.go fix: mempool lane size check on CheckTx (#561) 2024-07-02 19:19:22 -04:00
abci.go fix: mempool lane size check on CheckTx (#561) 2024-07-02 19:19:22 -04:00
factory_test.go chore: Upgrade module path for v2 (#383) 2024-01-24 09:06:20 -08:00
factory.go fix: mempool lane size check on CheckTx (#561) 2024-07-02 19:19:22 -04:00
lane.go chore: Upgrade module path for v2 (#383) 2024-01-24 09:06:20 -08:00
mempool.go fix: mempool lane size check on CheckTx (#561) 2024-07-02 19:19:22 -04:00
mev_test.go chore: Upgrade module path for v2 (#383) 2024-01-24 09:06:20 -08:00
README.md chore: Update Lane Specific Readmes (#254) 2023-12-01 16:13:57 -05:00
utils_test.go chore: Upgrade module path for v2 (#383) 2024-01-24 09:06:20 -08:00
utils.go chore: Upgrade module path for v2 (#383) 2024-01-24 09:06:20 -08:00

🏗️ MEV Lane Setup

📦 Dependencies

The Block SDK is built on top of the Cosmos SDK. The Block SDK is currently compatible with Cosmos SDK versions greater than or equal to v0.47.0.

Release Compatibility Matrix

Block SDK Version Cosmos SDK
v1.x.x v0.47.x
v2.x.x v0.50.x

📥 Installation

To install the Block SDK, run the following command:

$ go install github.com/skip-mev/block-sdk

📚 Usage

Note: Please visit app.go to see a sample base app set up.

  1. This guide assumes you have already set up the Block SDK (and the default lane)
  2. You will need to instantiate the x/auction module into your application. This module is responsible for processing auction transactions and distributing revenue to the auction house. The x/auction module is also responsible for ensuring the validity of auction transactions. The x/auction module should not exist on its own. This is the most intensive part of the set up process.
  3. Next, add the MEV lane into the lane object on your app.go. The first lane is the highest priority lane and the last lane is the lowest priority lane. Since the MEV lane is meant to auction off the top of the block, it should be the highest priority lane. The default lane should follow.
  4. You will also need to create a PrepareProposalHandler and a ProcessProposalHandler that will be responsible for preparing and processing proposals respectively. Configure the order of the lanes in the PrepareProposalHandler and ProcessProposalHandler to match the order of the lanes in the LanedMempool.

NOTE: This example walks through setting up the MEV and Default lanes.

  1. Import the necessary dependencies into your application. This includes the Block SDK proposal handlers + mempool, keeper, auction types, and auction module. This tutorial will go into more detail into each of the dependencies.

    import (
     ...
     "github.com/skip-mev/block-sdk/abci"
     "github.com/skip-mev/block-sdk/lanes/mev"
     "github.com/skip-mev/block-sdk/lanes/base"
     auctionmodule "github.com/skip-mev/block-sdk/x/auction"
     auctionkeeper "github.com/skip-mev/block-sdk/x/auction/keeper"
     auctiontypes "github.com/skip-mev/block-sdk/x/auction/types"
     auctionante "github.com/skip-mev/block-sdk/x/auction/ante"
      ...
    )
    
  2. Add your module to the the AppModuleBasic manager. This manager is in charge of setting up basic, non-dependent module elements such as codec registration and genesis verification. This will register the special MsgAuctionBid message. When users want to bid for top of block execution, they will submit a transaction - which we call an auction transaction - that includes a single MsgAuctionBid. We prevent any other messages from being included in auction transaction to prevent malicious behavior - such as front running or sandwiching.

    var (
      ModuleBasics = module.NewBasicManager(
        ...
        auctionmodule.AppModuleBasic{},
      )
      ...
    )
    
  3. The auction Keeper is MEV lane's gateway to processing special MsgAuctionBid messages that allow users to participate in the top of block auction, distribute revenue to the auction house, and ensure the validity of auction transactions.

    a. First add the keeper to the app's struct definition. We also want to add MEV lane's custom checkTx handler to the app's struct definition. This will allow us to override the default checkTx handler to process bid transactions before they are inserted into the LanedMempool. NOTE: The custom handler is required as otherwise the auction can be held hostage by a malicious users.

    type App struct {
    ...
    // auctionkeeper is the keeper that handles processing auction transactions
    auctionkeeper         auctionkeeper.Keeper
    
    // Custom checkTx handler
    checkTxHandler mev.CheckTx
    }
    

    b. Add the auction module to the list of module account permissions. This will instantiate the auction module account on genesis.

    maccPerms = map[string][]string{
    auction.ModuleName: nil,
    ...
    }
    

    c. Instantiate the Block SDK's LanedMempool with the application's desired lanes.

    // 1. Create the lanes.
    //
    // NOTE: The lanes are ordered by priority. The first lane is the
    // highest priority
    // lane and the last lane is the lowest priority lane. Top of block 
    // lane allows transactions to bid for inclusion at the top of the next block.
    //
    // For more information on how to utilize the LaneConfig please
    // visit the README in docs.skip.money/chains/lanes/build-your-own-lane#-lane-config.
    //
    // MEV lane hosts an auction at the top of the block.
    mevConfig := base.LaneConfig{
        Logger:        app.Logger(),
        TxEncoder:     app.txConfig.TxEncoder(),
        TxDecoder:     app.txConfig.TxDecoder(),
        MaxBlockSpace: math.LegacyZeroDec(), 
        MaxTxs:        0,
    }
    mevLane := mev.NewMEVLane(
        mevConfig,
        mev.NewDefaultAuctionFactory(app.txConfig.TxDecoder()),
    )
    
    // default lane accepts all other transactions.
    defaultConfig := base.LaneConfig{
        Logger:        app.Logger(),
        TxEncoder:     app.txConfig.TxEncoder(),
        TxDecoder:     app.txConfig.TxDecoder(),
        MaxBlockSpace: math.LegacyZeroDec(),
        MaxTxs:        0,
    }
    defaultLane := base.NewStandardLane(defaultConfig, base.DefaultMatchHandler())
    
    // 2. Set up the relative priority of lanes
    lanes := []block.Lane{
        mevLane,
        defaultLane,
    }
    mempool := block.NewLanedMempool(app.Logger(), true, lanes...)
    app.App.SetMempool(mempool)
    

    d. Add the x/auction module's AuctionDecorator to the ante-handler chain. The AuctionDecorator is an AnteHandler decorator that enforces various chain configurable MEV rules.

    anteDecorators := []sdk.AnteDecorator{
        ante.NewSetUpContextDecorator(), 
        ...
        auctionante.NewAuctionDecorator(
        options.auctionkeeper, 
        options.TxEncoder, 
        options.TOBLane, 
        options.Mempool,
        ),
    }
    
    anteHandler := sdk.ChainAnteDecorators(anteDecorators...)
    app.SetAnteHandler(anteHandler)
    
    // Set the antehandlers on the lanes.
    //
    // NOTE: This step is required as otherwise the lanes will not be able to
    // process auction transactions.
    for _, lane := range lanes {
        lane.SetAnteHandler(anteHandler)
    }
    app.App.SetAnteHandler(anteHandler)
    

    e. Instantiate the auction keeper, store keys, and module manager. Note, be sure to do this after all the required keeper dependencies have been instantiated.

    keys := storetypes.NewKVStoreKeys(
        auctiontypes.StoreKey,
        ...
    )
    
    ...
    app.auctionkeeper := auctionkeeper.NewKeeper(
        appCodec,
        keys[auctiontypes.StoreKey],
        app.AccountKeeper,
        app.BankKeeper,
        app.DistrKeeper,
        app.StakingKeeper,
        authtypes.NewModuleAddress(govv1.ModuleName).String(),
    )
    
    
    app.ModuleManager = module.NewManager(
        auction.NewAppModule(appCodec, app.auctionkeeper),
        ...
    )
    

    f. Configure the proposal/checkTx handlers on base app.

    // Create the proposal handler that will be used to build and validate blocks.
    proposalHandler := abci.NewProposalHandler(
        app.Logger(),
        app.txConfig.TxDecoder(),
        mempool,
    )
    app.App.SetPrepareProposal(proposalHandler.PrepareProposalHandler())
    app.App.SetProcessProposal(proposalHandler.ProcessProposalHandler())
    
    // Set the custom CheckTx handler on BaseApp.
    checkTxHandler := mev.NewCheckTxHandler(
        app.App,
        app.txConfig.TxDecoder(),
        mevLane,
        anteHandler,
    )
    app.SetCheckTx(checkTxHandler.CheckTx())
    
    // CheckTx will check the transaction with the provided checkTxHandler. 
    // We override the default handler so that we can verify transactions 
    // before they are inserted into the mempool. With the CheckTx, we can 
    // verify the bid transaction and all of the bundled transactions
    // before inserting the bid transaction into the mempool.
    func (app *TestApp) CheckTx(req *cometabci.RequestCheckTx) 
        (*cometabci.ResponseCheckTx, error) {
        return app.checkTxHandler(req)
    }
    
    // SetCheckTx sets the checkTxHandler for the app.
    func (app *TestApp) SetCheckTx(handler mev.CheckTx) {
        app.checkTxHandler = handler
    }
    

    g. Finally, update the app's InitGenesis order.

    genesisModuleOrder := []string{
        auctiontypes.ModuleName,
        ...,
    }
    

Params

Note, before building or upgrading the application, make sure to initialize the escrow address in the parameters of the module. The default parameters initialize the escrow address to be the module account address.