feat(BB): Free Lane (#167)

This commit is contained in:
David Terpay
2023-06-07 16:38:42 +00:00
committed by GitHub
parent b7780a3140
commit cfe2147d52
8 changed files with 432 additions and 39 deletions
+47
View File
@@ -0,0 +1,47 @@
package free
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
type (
// Factory defines the interface for processing free transactions. It is
// a wrapper around all of the functionality that each application chain must implement
// in order for free processing to work.
Factory interface {
// IsFreeTx defines a function that checks if a transaction qualifies as free.
IsFreeTx(tx sdk.Tx) bool
}
// DefaultFreeFactory defines a default implmentation for the free factory interface for processing free transactions.
DefaultFreeFactory struct {
txDecoder sdk.TxDecoder
}
)
var _ Factory = (*DefaultFreeFactory)(nil)
// NewDefaultFreeFactory returns a default free factory interface implementation.
func NewDefaultFreeFactory(txDecoder sdk.TxDecoder) Factory {
return &DefaultFreeFactory{
txDecoder: txDecoder,
}
}
// IsFreeTx defines a default function that checks if a transaction is free. In this case,
// any transaction that is a delegation/redelegation transaction is free.
func (config *DefaultFreeFactory) IsFreeTx(tx sdk.Tx) bool {
for _, msg := range tx.GetMsgs() {
switch msg.(type) {
case *types.MsgDelegate:
return true
case *types.MsgBeginRedelegate:
return true
case *types.MsgCancelUnbondingDelegation:
return true
}
}
return false
}
+42
View File
@@ -0,0 +1,42 @@
package free
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/pob/blockbuster"
"github.com/skip-mev/pob/blockbuster/lanes/base"
)
const (
// LaneName defines the name of the free lane.
LaneName = "free"
)
var _ blockbuster.Lane = (*Lane)(nil)
// FreeLane defines the lane that is responsible for processing free transactions.
type Lane struct {
*base.DefaultLane
Factory
}
// NewFreeLane returns a new free lane.
func NewFreeLane(cfg blockbuster.BaseLaneConfig, factory Factory) *Lane {
if err := cfg.ValidateBasic(); err != nil {
panic(err)
}
return &Lane{
DefaultLane: base.NewDefaultLane(cfg),
Factory: factory,
}
}
// Match returns true if the transaction is a free transaction.
func (l *Lane) Match(tx sdk.Tx) bool {
return l.IsFreeTx(tx)
}
// Name returns the name of the free lane.
func (l *Lane) Name() string {
return LaneName
}
+43
View File
@@ -0,0 +1,43 @@
package utils
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
type (
// Lane defines the required functionality for a lane. The ignore decorator
// will check if a transaction belongs to a lane by calling the Match function.
Lane interface {
Match(tx sdk.Tx) bool
}
// IgnoreDecorator is an AnteDecorator that wraps an existing AnteDecorator. It allows
// for the AnteDecorator to be ignored for specified lanes.
IgnoreDecorator struct {
decorator sdk.AnteDecorator
lanes []Lane
}
)
// NewIgnoreDecorator returns a new IgnoreDecorator instance.
func NewIgnoreDecorator(decorator sdk.AnteDecorator, lanes ...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) {
for _, lane := range sd.lanes {
if lane.Match(tx) {
return next(ctx, tx, simulate)
}
}
return sd.decorator.AnteHandle(ctx, tx, simulate, next)
}