Skip to content

Commit

Permalink
Merge pull request #18 from initia-labs/feat/pob-to-block-sdk
Browse files Browse the repository at this point in the history
feat: change pob to block-sdk
  • Loading branch information
beer-1 authored Nov 22, 2023
2 parents c06ada3 + 0ece600 commit 0087c4f
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 146 deletions.
30 changes: 24 additions & 6 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (

moveante "github.com/initia-labs/initia/x/move/ante"
movetypes "github.com/initia-labs/initia/x/move/types"
builderante "github.com/skip-mev/pob/x/builder/ante"
builderkeeper "github.com/skip-mev/pob/x/builder/keeper"

"github.com/skip-mev/block-sdk/block"
auctionante "github.com/skip-mev/block-sdk/x/auction/ante"
auctionkeeper "github.com/skip-mev/block-sdk/x/auction/keeper"
)

// HandlerOptions extends the SDK's AnteHandler options by requiring the IBC
Expand All @@ -23,9 +25,10 @@ type HandlerOptions struct {
Codec codec.BinaryCodec
MoveKeeper movetypes.AnteKeeper
IBCkeeper *ibckeeper.Keeper
BuilderKeeper builderkeeper.Keeper
AuctionKeeper auctionkeeper.Keeper
TxEncoder sdk.TxEncoder
Mempool builderante.Mempool
MevLane auctionante.MEVLane
FreeLane block.Lane
}

// NewAnteHandler returns an AnteHandler that checks and increments sequence
Expand Down Expand Up @@ -54,6 +57,21 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
txFeeChecker = moveante.NewMempoolFeeChecker(options.MoveKeeper).CheckTxFeeWithMinGasPrices
}

freeLaneFeeChecker := func(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error) {
// skip fee checker if the tx is free lane tx.
if !options.FreeLane.Match(ctx, tx) {
return txFeeChecker(ctx, tx)
}

// return fee without fee check
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return nil, 0, errors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}

return feeTx.GetFee(), 1 /* FIFO */, nil
}

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
Expand All @@ -63,15 +81,15 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, txFeeChecker),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, freeLaneFeeChecker),
// SetPubKeyDecorator must be called before all signature verification decorators
ante.NewSetPubKeyDecorator(options.AccountKeeper),
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCkeeper),
builderante.NewBuilderDecorator(options.BuilderKeeper, options.TxEncoder, options.Mempool),
auctionante.NewAuctionDecorator(options.AuctionKeeper, options.TxEncoder, options.MevLane),
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
Expand Down
144 changes: 99 additions & 45 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/math"

dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -119,8 +120,10 @@ import (

// this line is used by starport scaffolding # stargate/app/moduleImport

"github.com/initia-labs/initia/app/ante"
"github.com/initia-labs/initia/app/hook"
appante "github.com/initia-labs/initia/app/ante"
apphook "github.com/initia-labs/initia/app/hook"
applanes "github.com/initia-labs/initia/app/lanes"
initiatx "github.com/initia-labs/initia/tx"
authzmodule "github.com/initia-labs/initia/x/authz/module"
"github.com/initia-labs/initia/x/bank"
bankkeeper "github.com/initia-labs/initia/x/bank/keeper"
Expand All @@ -145,12 +148,17 @@ import (
"github.com/initia-labs/initia/x/slashing"
slashingkeeper "github.com/initia-labs/initia/x/slashing/keeper"

builderabci "github.com/skip-mev/pob/abci"
pobabci "github.com/skip-mev/pob/abci"
buildermempool "github.com/skip-mev/pob/mempool"
"github.com/skip-mev/pob/x/builder"
builderkeeper "github.com/skip-mev/pob/x/builder/keeper"
buildertypes "github.com/skip-mev/pob/x/builder/types"
blockabci "github.com/skip-mev/block-sdk/abci"
signer_extraction "github.com/skip-mev/block-sdk/adapters/signer_extraction_adapter"
"github.com/skip-mev/block-sdk/block"
blockbase "github.com/skip-mev/block-sdk/block/base"
baselane "github.com/skip-mev/block-sdk/lanes/base"
freelane "github.com/skip-mev/block-sdk/lanes/free"
"github.com/skip-mev/block-sdk/lanes/mev"
"github.com/skip-mev/block-sdk/x/auction"
auctionante "github.com/skip-mev/block-sdk/x/auction/ante"
auctionkeeper "github.com/skip-mev/block-sdk/x/auction/keeper"
auctiontypes "github.com/skip-mev/block-sdk/x/auction/types"

"github.com/initia-labs/OPinit/x/ophost"
ophostkeeper "github.com/initia-labs/OPinit/x/ophost/keeper"
Expand Down Expand Up @@ -210,7 +218,7 @@ var (
router.AppModuleBasic{},
ibcperm.AppModuleBasic{},
move.AppModuleBasic{},
builder.AppModuleBasic{},
auction.AppModuleBasic{},
ophost.AppModuleBasic{},
)

Expand All @@ -226,9 +234,9 @@ var (
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
movetypes.MoveStakingModuleName: nil,
// x/builder's module account must be instantiated upon genesis to accrue auction rewards not
// x/auction's module account must be instantiated upon genesis to accrue auction rewards not
// distributed to proposers
buildertypes.ModuleName: nil,
auctiontypes.ModuleName: nil,

// this is only for testing
authtypes.Minter: {authtypes.Minter},
Expand Down Expand Up @@ -293,7 +301,7 @@ type InitiaApp struct {
RouterKeeper *routerkeeper.Keeper // Router Keeper must be a pointer in the app, so we can SetTransferKeeper on it correctly
IBCPermKeeper *ibcpermkeeper.Keeper
MoveKeeper *movekeeper.Keeper
BuilderKeeper *builderkeeper.Keeper // x/builder keeper used to process bids for TOB auctions
AuctionKeeper *auctionkeeper.Keeper // x/auction keeper used to process bids for TOB auctions
OPHostKeeper *ophostkeeper.Keeper

// make scoped keepers public for test purposes
Expand All @@ -311,7 +319,7 @@ type InitiaApp struct {
configurator module.Configurator

// Override of BaseApp's CheckTx
checkTxHandler pobabci.CheckTx
checkTxHandler mev.CheckTx
}

// NewInitiaApp returns a reference to an initialized Initia.
Expand Down Expand Up @@ -345,7 +353,7 @@ func NewInitiaApp(
ibctransfertypes.StoreKey, ibcnfttransfertypes.StoreKey, capabilitytypes.StoreKey,
authzkeeper.StoreKey, feegrant.StoreKey, icahosttypes.StoreKey,
icacontrollertypes.StoreKey, icaauthtypes.StoreKey, ibcfeetypes.StoreKey,
routertypes.StoreKey, ibcpermtypes.StoreKey, movetypes.StoreKey, buildertypes.StoreKey,
routertypes.StoreKey, ibcpermtypes.StoreKey, movetypes.StoreKey, auctiontypes.StoreKey,
ophosttypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand Down Expand Up @@ -749,25 +757,25 @@ func NewInitiaApp(
),
)

// x/builder module keeper initialization
// x/auction module keeper initialization

// initialize the keeper
builderKeeper := builderkeeper.NewKeeperWithRewardsAddressProvider(
auctionKeeper := auctionkeeper.NewKeeperWithRewardsAddressProvider(
app.appCodec,
app.keys[buildertypes.StoreKey],
app.keys[auctiontypes.StoreKey],
app.AccountKeeper,
app.BankKeeper,
NewRewardsAddressProvider(*app.StakingKeeper, *app.DistrKeeper),
applanes.NewRewardsAddressProvider(*app.StakingKeeper, *app.DistrKeeper),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
app.BuilderKeeper = &builderKeeper
app.AuctionKeeper = &auctionKeeper

opHostKeeper := ophostkeeper.NewKeeper(
app.appCodec,
app.keys[ophosttypes.StoreKey],
app.AccountKeeper,
app.BankKeeper,
ophosttypes.NewBridgeHooks(hook.NewBridgeHook(app.IBCKeeper.ChannelKeeper, app.IBCPermKeeper)),
ophosttypes.NewBridgeHooks(apphook.NewBridgeHook(app.IBCKeeper.ChannelKeeper, app.IBCPermKeeper)),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
app.OPHostKeeper = &opHostKeeper
Expand Down Expand Up @@ -824,7 +832,7 @@ func NewInitiaApp(
groupmodule.NewAppModule(appCodec, *app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
consensus.NewAppModule(appCodec, *app.ConsensusParamsKeeper),
move.NewAppModule(app.AccountKeeper, *app.MoveKeeper),
builder.NewAppModule(app.appCodec, *app.BuilderKeeper),
auction.NewAppModule(app.appCodec, *app.AuctionKeeper),
ibctransfer.NewAppModule(*app.TransferKeeper),
ibcnfttransfer.NewAppModule(*app.NftTransferKeeper),
ica.NewAppModule(app.ICAControllerKeeper, app.ICAHostKeeper),
Expand Down Expand Up @@ -867,7 +875,7 @@ func NewInitiaApp(
routertypes.ModuleName,
ibcpermtypes.ModuleName,
consensusparamtypes.ModuleName,
buildertypes.ModuleName,
auctiontypes.ModuleName,
ophosttypes.ModuleName,
)

Expand Down Expand Up @@ -899,7 +907,7 @@ func NewInitiaApp(
routertypes.ModuleName,
ibcpermtypes.ModuleName,
consensusparamtypes.ModuleName,
buildertypes.ModuleName,
auctiontypes.ModuleName,
ophosttypes.ModuleName,
)

Expand Down Expand Up @@ -937,7 +945,7 @@ func NewInitiaApp(
routertypes.ModuleName,
ibcpermtypes.ModuleName,
consensusparamtypes.ModuleName,
buildertypes.ModuleName,
auctiontypes.ModuleName,
ophosttypes.ModuleName,
)

Expand Down Expand Up @@ -969,35 +977,74 @@ func NewInitiaApp(
app.SetEndBlocker(app.EndBlocker)

// initialize and set the InitiaApp mempool. The current mempool will be the
// x/builder module's mempool which will extract the top bid from the current block's auction
// x/auction module's mempool which will extract the top bid from the current block's auction
// and insert the txs at the top of the block spots.
factory := buildermempool.NewDefaultAuctionFactory(app.txConfig.TxDecoder())
mempool := buildermempool.NewAuctionMempool(
app.txConfig.TxDecoder(),
app.txConfig.TxEncoder(),
0,
factory,
signerExtractor := signer_extraction.NewDefaultAdapter()

mevConfig := blockbase.LaneConfig{
Logger: app.Logger(),
TxEncoder: app.txConfig.TxEncoder(),
TxDecoder: app.txConfig.TxDecoder(),
MaxBlockSpace: math.LegacyZeroDec(),
MaxTxs: 100,
SignerExtractor: signerExtractor,
}
mevLane := mev.NewMEVLane(
mevConfig,
mev.NewDefaultAuctionFactory(app.txConfig.TxDecoder(), signerExtractor),
)

freeConfig := blockbase.LaneConfig{
Logger: app.Logger(),
TxEncoder: app.txConfig.TxEncoder(),
TxDecoder: app.txConfig.TxDecoder(),
MaxBlockSpace: math.LegacyZeroDec(),
MaxTxs: 10,
SignerExtractor: signerExtractor,
}

freeLane := freelane.NewFreeLane(
freeConfig,
blockbase.DefaultTxPriority(),
applanes.FreeLaneMatchHandler(),
)

defaultLaneConfig := blockbase.LaneConfig{
Logger: app.Logger(),
TxEncoder: app.txConfig.TxEncoder(),
TxDecoder: app.txConfig.TxDecoder(),
MaxBlockSpace: math.LegacyZeroDec(),
MaxTxs: 0,
SignerExtractor: signerExtractor,
}
defaultLane := baselane.NewDefaultLane(defaultLaneConfig)

lanes := []block.Lane{mevLane, freeLane, defaultLane}
mempool := block.NewLanedMempool(app.Logger(), true, lanes...)
app.SetMempool(mempool)
anteHandler := app.setAnteHandler(mempool)

anteHandler := app.setAnteHandler(mevLane, freeLane)
for _, lane := range lanes {
lane.SetAnteHandler(anteHandler)
}

// override the base-app's ABCI methods (CheckTx, PrepareProposal, ProcessProposal)
proposalHandlers := builderabci.NewProposalHandler(
mempool,
proposalHandlers := blockabci.NewProposalHandler(
app.Logger(),
anteHandler,
app.txConfig.TxEncoder(),
app.txConfig.TxDecoder(),
app.txConfig.TxEncoder(),
mempool,
)

// override base-app's ProcessProposal + PrepareProposal
app.SetPrepareProposal(proposalHandlers.PrepareProposalHandler())
app.SetProcessProposal(proposalHandlers.ProcessProposalHandler())

// overrde base-app's CheckTx
checkTxHandler := builderabci.NewCheckTxHandler(
checkTxHandler := mev.NewCheckTxHandler(
app.BaseApp,
app.txConfig.TxDecoder(),
mempool,
mevLane,
anteHandler,
app.ChainID(),
)
Expand Down Expand Up @@ -1030,13 +1077,16 @@ func (app *InitiaApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
}

// SetCheckTx sets the checkTxHandler for the app.
func (app *InitiaApp) SetCheckTx(handler pobabci.CheckTx) {
func (app *InitiaApp) SetCheckTx(handler mev.CheckTx) {
app.checkTxHandler = handler
}

func (app *InitiaApp) setAnteHandler(mempl buildermempool.Mempool) sdk.AnteHandler {
anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
func (app *InitiaApp) setAnteHandler(
mevLane auctionante.MEVLane,
freeLane block.Lane,
) sdk.AnteHandler {
anteHandler, err := appante.NewAnteHandler(
appante.HandlerOptions{
HandlerOptions: cosmosante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
Expand All @@ -1048,8 +1098,9 @@ func (app *InitiaApp) setAnteHandler(mempl buildermempool.Mempool) sdk.AnteHandl
MoveKeeper: movekeeper.NewDexKeeper(app.MoveKeeper),
Codec: app.appCodec,
TxEncoder: app.txConfig.TxEncoder(),
BuilderKeeper: *app.BuilderKeeper,
Mempool: mempl,
AuctionKeeper: *app.AuctionKeeper,
MevLane: mevLane,
FreeLane: freeLane,
},
)
if err != nil {
Expand Down Expand Up @@ -1191,7 +1242,10 @@ func (app *InitiaApp) Simulate(txBytes []byte) (sdk.GasInfo, *sdk.Result, error)

// RegisterTxService implements the Application.RegisterTxService method.
func (app *InitiaApp) RegisterTxService(clientCtx client.Context) {
authtx.RegisterTxService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.Simulate, app.interfaceRegistry)
initiatx.RegisterTxService(
app.BaseApp.GRPCQueryRouter(), clientCtx,
authtx.NewTxServer(clientCtx, app.Simulate, app.interfaceRegistry),
)
}

// RegisterTendermintService implements the Application.RegisterTendermintService method.
Expand Down
13 changes: 10 additions & 3 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,21 @@ func (app *InitiaApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
app.DistrKeeper.SetFeePool(ctx, feePool)

app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
if err := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator()); err != nil {
panic(err)
}

return false
})

// reinitialize all delegations
for _, del := range dels {
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
if err := app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr()); err != nil {
panic(err)
}
if err := app.DistrKeeper.Hooks().AfterDelegationModified(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr()); err != nil {
panic(err)
}
}

// reset context height
Expand Down
Loading

0 comments on commit 0087c4f

Please sign in to comment.