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

[Mempool] Implement custom mempool #587

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
46 changes: 41 additions & 5 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"

"github.com/bandprotocol/chain/v3/app/mempool"
bandtsskeeper "github.com/bandprotocol/chain/v3/x/bandtss/keeper"
feedskeeper "github.com/bandprotocol/chain/v3/x/feeds/keeper"
"github.com/bandprotocol/chain/v3/x/globalfee/feechecker"
Expand All @@ -32,6 +33,7 @@ type HandlerOptions struct {
TSSKeeper *tsskeeper.Keeper
BandtssKeeper *bandtsskeeper.Keeper
FeedsKeeper *feedskeeper.Keeper
Lanes []*mempool.Lane
}

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
Expand Down Expand Up @@ -98,11 +100,14 @@ 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,
options.TxFeeChecker,
NewIgnoreDecorator(
ante.NewDeductFeeDecorator(
options.AccountKeeper,
options.BankKeeper,
options.FeegrantKeeper,
options.TxFeeChecker,
),
options.Lanes...,
),
// SetPubKeyDecorator must be called before all signature verification decorators
ante.NewSetPubKeyDecorator(options.AccountKeeper),
Expand All @@ -115,3 +120,34 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {

return sdk.ChainAnteDecorators(anteDecorators...), nil
}

// IgnoreDecorator is an AnteDecorator that wraps an existing AnteDecorator. It allows
// for the AnteDecorator to be ignored for specified lanes.
type IgnoreDecorator struct {
decorator sdk.AnteDecorator
lanes []*mempool.Lane
}

// NewIgnoreDecorator returns a new IgnoreDecorator instance.
func NewIgnoreDecorator(decorator sdk.AnteDecorator, lanes ...*mempool.Lane) *IgnoreDecorator {
return &IgnoreDecorator{
decorator: decorator,
lanes: lanes,
}
}

// AnteHandle implements the sdk.AnteDecorator interface. If the transaction belongs to
// one of the lanes, the next AnteHandler is called. Otherwise, the decorator's AnteHandler
// is called.
func (sd IgnoreDecorator) AnteHandle(
ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler,
) (sdk.Context, error) {
cacheCtx, _ := ctx.CacheContext()
for _, lane := range sd.lanes {
if lane.Match(cacheCtx, tx) {
return next(ctx, tx, simulate)
}
}

return sd.decorator.AnteHandle(ctx, tx, simulate, next)
}
17 changes: 17 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/bandprotocol/chain/v3/app/keepers"
"github.com/bandprotocol/chain/v3/app/mempool"
"github.com/bandprotocol/chain/v3/app/upgrades"
v3 "github.com/bandprotocol/chain/v3/app/upgrades/v3"
nodeservice "github.com/bandprotocol/chain/v3/client/grpc/node"
Expand Down Expand Up @@ -253,6 +254,14 @@ func NewBandApp(

app.sm.RegisterStoreDecoders()

feedsLane, tssLane, oracleLane, defaultLane := CreateLanes(app)
bandLanes := []*mempool.Lane{feedsLane, tssLane, oracleLane, defaultLane}

// create Band mempool
bandMempool := mempool.NewMempool(app.Logger(), bandLanes)
// set the mempool
app.SetMempool(bandMempool)

// Initialize stores.
app.MountKVStores(app.GetKVStoreKey())
app.MountTransientStores(app.GetTransientStoreKey())
Expand All @@ -276,12 +285,20 @@ func NewBandApp(
IBCKeeper: app.IBCKeeper,
StakingKeeper: app.StakingKeeper,
GlobalfeeKeeper: &app.GlobalFeeKeeper,
Lanes: []*mempool.Lane{feedsLane, tssLane, oracleLane}, // every lane except default lane
},
)
if err != nil {
panic(fmt.Errorf("failed to create ante handler: %s", err))
}

// proposal handler
proposalHandler := mempool.NewProposalHandler(app.Logger(), txConfig.TxDecoder(), bandMempool)

// set the Prepare / ProcessProposal Handlers on the app to be the `LanedMempool`'s
app.SetPrepareProposal(proposalHandler.PrepareProposalHandler())
app.SetProcessProposal(proposalHandler.ProcessProposalHandler())

postHandler, err := NewPostHandler(
PostHandlerOptions{},
)
Expand Down
Loading
Loading