keyshare

package
v0.10.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README ΒΆ

πŸ—οΈ 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.

πŸ“₯ 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.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const (
	// LaneName defines the name of the keyshare lane.
	LaneName = "keyshare"
)

Variables ΒΆ

This section is empty.

Functions ΒΆ

func GetDecodedTxs ΒΆ

func GetDecodedTxs(txDecoder sdk.TxDecoder, txs [][]byte) ([]sdk.Tx, error)

GetDecodedTxs returns the decoded transactions from the given bytes.

func GetMaxTxBytesForLane ΒΆ

func GetMaxTxBytesForLane(maxTxBytes, totalTxBytes int64, ratio sdkmath.LegacyDec) int64

GetMaxTxBytesForLane returns the maximum number of bytes that can be included in the proposal for the given lane.

func GetSubmitDecryptionKeyMsgFromTx ΒΆ added in v0.10.0

func GetSubmitDecryptionKeyMsgFromTx(tx sdk.Tx) (*peptypes.MsgSubmitDecryptionKey, error)

func GetTxHashStr ΒΆ

func GetTxHashStr(txEncoder sdk.TxEncoder, tx sdk.Tx) ([]byte, string, error)

GetTxHashStr returns the hex-encoded hash of the transaction alongside the transaction bytes.

func RemoveTxsFromLane ΒΆ

func RemoveTxsFromLane(txs map[sdk.Tx]struct{}, mempool sdkmempool.Mempool) error

RemoveTxsFromLane removes the transactions from the given lane's mempool.

func TxPriority ΒΆ

func TxPriority(config Factory) base.TxPriority[string]

TxPriority returns a TxPriority over Keyshare transactions only. It is to be used in the Keyshare index only.

Types ΒΆ

type DefaultKeyshareFactory ΒΆ

type DefaultKeyshareFactory struct {
	// contains filtered or unexported fields
}

DefaultKeyshareFactory defines a default implmentation for the keyshare factory interface for processing keyshare transactions.

func (*DefaultKeyshareFactory) GetDecryptionKeyInfo ΒΆ added in v0.10.0

func (config *DefaultKeyshareFactory) GetDecryptionKeyInfo(tx sdk.Tx) (*peptypes.DecryptionKey, error)

func (*DefaultKeyshareFactory) GetTimeoutHeight ΒΆ

func (config *DefaultKeyshareFactory) GetTimeoutHeight(tx sdk.Tx) (uint64, error)

GetTimeoutHeight returns the timeout height of the transaction.

func (*DefaultKeyshareFactory) IsKeyshareTx ΒΆ

func (config *DefaultKeyshareFactory) IsKeyshareTx(tx sdk.Tx) bool

func (*DefaultKeyshareFactory) MatchHandler ΒΆ

func (config *DefaultKeyshareFactory) MatchHandler() base.MatchHandler

MatchHandler defines a default function that checks if a transaction matches the keyshare lane.

type Factory ΒΆ

type Factory interface {
	// IsKeyshareTx defines a function that checks if a transaction qualifies as Keyshare Tx.
	IsKeyshareTx(tx sdk.Tx) bool

	// GetDecryptionKeyInfo defines a function that returns the Keyshare info from the Tx
	GetDecryptionKeyInfo(tx sdk.Tx) (*peptypes.DecryptionKey, error)

	// MatchHandler defines a function that checks if a transaction matches the keyshare lane.
	MatchHandler() base.MatchHandler
}

Factory defines the interface for processing Keyshare transactions. It is a wrapper around all of the functionality that each application chain must implement in order for keyshare processing to work.

func NewDefaultKeyshareFactory ΒΆ

func NewDefaultKeyshareFactory(txDecoder sdk.TxDecoder, extractor signer_extraction.Adapter) Factory

NewDefaultKeyshareFactory returns a default keyshare factory interface implementation.

type KeyshareLane ΒΆ added in v0.10.0

type KeyshareLane struct {
	*base.BaseLane

	// Factory defines the API/functionality which is responsible for determining
	// if a transaction is a Keyshare transaction and how to extract relevant
	// information from the transaction (creator Address).
	Factory
}

KeyshareLane defines the lane that is responsible for processing Keyshare transactions.

func NewKeyshareLane ΒΆ added in v0.10.0

func NewKeyshareLane(
	cfg base.LaneConfig,
	factory Factory,
	matchHandler base.MatchHandler,
) *KeyshareLane

NewKeyshareLane returns a new Keyshare lane.

type ProposalHandler ΒΆ

type ProposalHandler struct {
	// contains filtered or unexported fields
}

Implements the Keyshare lane's PrepareLaneHandler and ProcessLaneHandler.

func NewProposalHandler ΒΆ

func NewProposalHandler(lane *base.BaseLane, factory Factory) *ProposalHandler

NewProposalHandler returns a new keyshare proposal handler.

func (*ProposalHandler) PrepareLaneHandler ΒΆ

func (h *ProposalHandler) PrepareLaneHandler() base.PrepareLaneHandler

PrepareLaneHandler will attempt to select the keyshare transactions that are valid and include them in the proposal. It will return an empty partial proposal if no valid keyshare transactions are found.

func (*ProposalHandler) ProcessLaneHandler ΒΆ

func (h *ProposalHandler) ProcessLaneHandler() base.ProcessLaneHandler

ProcessLaneHandler ensures that if keyshare transactions are present in a proposal,

  • they are the first transaction in the partial proposal
  • there are no other keyshare transactions in the proposal
  • block proposals that include transactions from the keyshare lane are valid

func (*ProposalHandler) VerifyTx ΒΆ

func (h *ProposalHandler) VerifyTx(ctx sdk.Context, keyshareTx sdk.Tx) error

VerifyTx will verify that the keyshare transaction is valid. It will return an error if the transaction is invalid.

type TxWithTimeoutHeight ΒΆ

type TxWithTimeoutHeight interface {
	sdk.Tx

	GetTimeoutHeight() uint64
}

TxWithTimeoutHeight is used to extract timeouts from sdk.Tx transactions. In the case where, timeouts are explicitly set on the sdk.Tx, we can use this interface to extract the timeout.

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL