Skip to content

Commit

Permalink
chore(FS): Moving Types to Base Subdirectory (#19)
Browse files Browse the repository at this point in the history
* init

* updating readme
  • Loading branch information
davidterpay authored Aug 22, 2023
1 parent cd8ce95 commit c956e6c
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 84 deletions.
8 changes: 4 additions & 4 deletions abci/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,11 @@ func (s *ProposalsTestSuite) TestProcessProposal() {

// Set up the default lane
defaultLane := s.setUpStandardLane(math.LegacyMustNewDecFromStr("0.5"), nil)
defaultLane.SetProcessLaneHandler(block.NoOpProcessLaneHandler())
defaultLane.SetProcessLaneHandler(base.NoOpProcessLaneHandler())

// Set up the TOB lane
mevLane := s.setUpTOBLane(math.LegacyMustNewDecFromStr("0.5"), nil)
mevLane.SetProcessLaneHandler(block.NoOpProcessLaneHandler())
mevLane.SetProcessLaneHandler(base.NoOpProcessLaneHandler())

proposalHandler := s.setUpProposalHandlers([]block.Lane{mevLane, defaultLane}).ProcessProposalHandler()
resp, err := proposalHandler(s.ctx, &cometabci.RequestProcessProposal{Txs: s.getTxBytes(bidTx, bundle[0], bundle[1], normalTx, normalTx2)})
Expand Down Expand Up @@ -783,8 +783,8 @@ func (s *ProposalsTestSuite) setUpPanicLane(maxBlockSpace math.LegacyDec) *base.
base.DefaultMatchHandler(),
)

lane.SetPrepareLaneHandler(block.PanicPrepareLaneHandler())
lane.SetProcessLaneHandler(block.PanicProcessLaneHandler())
lane.SetPrepareLaneHandler(base.PanicPrepareLaneHandler())
lane.SetProcessLaneHandler(base.PanicProcessLaneHandler())

return lane
}
Expand Down
8 changes: 4 additions & 4 deletions block/base/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// selects all transactions in the mempool that are valid and not already in the partial
// proposal. It will continue to reap transactions until the maximum block space for this
// lane has been reached. Additionally, any transactions that are invalid will be returned.
func (l *BaseLane) DefaultPrepareLaneHandler() block.PrepareLaneHandler {
func (l *BaseLane) DefaultPrepareLaneHandler() PrepareLaneHandler {
return func(ctx sdk.Context, proposal block.BlockProposal, maxTxBytes int64) ([][]byte, []sdk.Tx, error) {
var (
totalSize int64
Expand Down Expand Up @@ -96,7 +96,7 @@ func (l *BaseLane) DefaultPrepareLaneHandler() block.PrepareLaneHandler {
// fails to verify, the entire proposal is rejected. If the handler comes across a transaction
// that does not match the lane's matcher, it will return the remaining transactions in the
// proposal.
func (l *BaseLane) DefaultProcessLaneHandler() block.ProcessLaneHandler {
func (l *BaseLane) DefaultProcessLaneHandler() ProcessLaneHandler {
return func(ctx sdk.Context, txs []sdk.Tx) ([]sdk.Tx, error) {
var err error

Expand All @@ -123,7 +123,7 @@ func (l *BaseLane) DefaultProcessLaneHandler() block.ProcessLaneHandler {
// lane.
// 2. Transactions that belong to other lanes cannot be interleaved with transactions that
// belong to this lane.
func (l *BaseLane) DefaultCheckOrderHandler() block.CheckOrderHandler {
func (l *BaseLane) DefaultCheckOrderHandler() CheckOrderHandler {
return func(ctx sdk.Context, txs []sdk.Tx) error {
seenOtherLaneTx := false

Expand All @@ -149,7 +149,7 @@ func (l *BaseLane) DefaultCheckOrderHandler() block.CheckOrderHandler {

// DefaultMatchHandler returns a default implementation of the MatchHandler. It matches all
// transactions.
func DefaultMatchHandler() block.MatchHandler {
func DefaultMatchHandler() MatchHandler {
return func(ctx sdk.Context, tx sdk.Tx) bool {
return true
}
Expand Down
16 changes: 8 additions & 8 deletions block/base/lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ type BaseLane struct { //nolint

// matchHandler is the function that determines whether or not a transaction
// should be processed by this lane.
matchHandler block.MatchHandler
matchHandler MatchHandler

// prepareLaneHandler is the function that is called when a new proposal is being
// requested and the lane needs to submit transactions it wants included in the block.
prepareLaneHandler block.PrepareLaneHandler
prepareLaneHandler PrepareLaneHandler

// checkOrderHandler is the function that is called when a new proposal is being
// verified and the lane needs to verify that the transactions included in the proposal
// respect the ordering rules of the lane and does not interleave transactions from other lanes.
checkOrderHandler block.CheckOrderHandler
checkOrderHandler CheckOrderHandler

// processLaneHandler is the function that is called when a new proposal is being
// verified and the lane needs to verify that the transactions included in the proposal
// are valid respecting the verification logic of the lane.
processLaneHandler block.ProcessLaneHandler
processLaneHandler ProcessLaneHandler
}

// NewBaseLane returns a new lane base. When creating this lane, the type
Expand All @@ -55,7 +55,7 @@ func NewBaseLane(
cfg LaneConfig,
laneName string,
laneMempool block.LaneMempool,
matchHandlerFn block.MatchHandler,
matchHandlerFn MatchHandler,
) *BaseLane {
lane := &BaseLane{
cfg: cfg,
Expand Down Expand Up @@ -104,7 +104,7 @@ func (l *BaseLane) ValidateBasic() error {
// SetPrepareLaneHandler sets the prepare lane handler for the lane. This handler
// is called when a new proposal is being requested and the lane needs to submit
// transactions it wants included in the block.
func (l *BaseLane) SetPrepareLaneHandler(prepareLaneHandler block.PrepareLaneHandler) {
func (l *BaseLane) SetPrepareLaneHandler(prepareLaneHandler PrepareLaneHandler) {
if prepareLaneHandler == nil {
panic("prepare lane handler cannot be nil")
}
Expand All @@ -116,7 +116,7 @@ func (l *BaseLane) SetPrepareLaneHandler(prepareLaneHandler block.PrepareLaneHan
// is called when a new proposal is being verified and the lane needs to verify
// that the transactions included in the proposal are valid respecting the verification
// logic of the lane.
func (l *BaseLane) SetProcessLaneHandler(processLaneHandler block.ProcessLaneHandler) {
func (l *BaseLane) SetProcessLaneHandler(processLaneHandler ProcessLaneHandler) {
if processLaneHandler == nil {
panic("process lane handler cannot be nil")
}
Expand All @@ -128,7 +128,7 @@ func (l *BaseLane) SetProcessLaneHandler(processLaneHandler block.ProcessLaneHan
// is called when a new proposal is being verified and the lane needs to verify
// that the transactions included in the proposal respect the ordering rules of
// the lane and does not include transactions from other lanes.
func (l *BaseLane) SetCheckOrderHandler(checkOrderHandler block.CheckOrderHandler) {
func (l *BaseLane) SetCheckOrderHandler(checkOrderHandler CheckOrderHandler) {
if checkOrderHandler == nil {
panic("check order handler cannot be nil")
}
Expand Down
66 changes: 66 additions & 0 deletions block/base/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package base

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/block-sdk/block"
)

type (
// MatchHandler is utilized to determine if a transaction should be included in the lane. This
// function can be a stateless or stateful check on the transaction.
MatchHandler func(ctx sdk.Context, tx sdk.Tx) bool

// PrepareLaneHandler is responsible for preparing transactions to be included in the block from a
// given lane. Given a lane, this function should return the transactions to include in the block,
// the transactions that must be removed from the lane, and an error if one occurred.
PrepareLaneHandler func(
ctx sdk.Context,
proposal block.BlockProposal,
maxTxBytes int64,
) (txsToInclude [][]byte, txsToRemove []sdk.Tx, err error)

// ProcessLaneHandler is responsible for processing transactions that are included in a block and
// belong to a given lane. ProcessLaneHandler is executed after CheckOrderHandler so the transactions
// passed into this function SHOULD already be in order respecting the ordering rules of the lane and
// respecting the ordering rules of mempool relative to the lanes it has.
ProcessLaneHandler func(ctx sdk.Context, txs []sdk.Tx) ([]sdk.Tx, error)

// CheckOrderHandler is responsible for checking the order of transactions that belong to a given
// lane. This handler should be used to verify that the ordering of transactions passed into the
// function respect the ordering logic of the lane (if any transactions from the lane are included).
// This function should also ensure that transactions that belong to this lane are contiguous and do
// not have any transactions from other lanes in between them.
CheckOrderHandler func(ctx sdk.Context, txs []sdk.Tx) error
)

// NoOpPrepareLaneHandler returns a no-op prepare lane handler.
// This should only be used for testing.
func NoOpPrepareLaneHandler() PrepareLaneHandler {
return func(ctx sdk.Context, proposal block.BlockProposal, maxTxBytes int64) (txsToInclude [][]byte, txsToRemove []sdk.Tx, err error) {
return nil, nil, nil
}
}

// PanicPrepareLaneHandler returns a prepare lane handler that panics.
// This should only be used for testing.
func PanicPrepareLaneHandler() PrepareLaneHandler {
return func(sdk.Context, block.BlockProposal, int64) (txsToInclude [][]byte, txsToRemove []sdk.Tx, err error) {
panic("panic prepare lanes handler")
}
}

// NoOpProcessLaneHandler returns a no-op process lane handler.
// This should only be used for testing.
func NoOpProcessLaneHandler() ProcessLaneHandler {
return func(ctx sdk.Context, txs []sdk.Tx) ([]sdk.Tx, error) {
return txs, nil
}
}

// PanicProcessLanesHandler returns a process lanes handler that panics.
// This should only be used for testing.
func PanicProcessLaneHandler() ProcessLaneHandler {
return func(sdk.Context, []sdk.Tx) ([]sdk.Tx, error) {
panic("panic process lanes handler")
}
}
58 changes: 0 additions & 58 deletions block/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,6 @@ import (
)

type (
// MatchHandler is utilized to determine if a transaction should be included in the lane. This
// function can be a stateless or stateful check on the transaction.
MatchHandler func(ctx sdk.Context, tx sdk.Tx) bool

// PrepareLaneHandler is responsible for preparing transactions to be included in the block from a
// given lane. Given a lane, this function should return the transactions to include in the block,
// the transactions that must be removed from the lane, and an error if one occurred.
PrepareLaneHandler func(
ctx sdk.Context,
proposal BlockProposal,
maxTxBytes int64,
) (txsToInclude [][]byte, txsToRemove []sdk.Tx, err error)

// ProcessLaneHandler is responsible for processing transactions that are included in a block and
// belong to a given lane. ProcessLaneHandler is executed after CheckOrderHandler so the transactions
// passed into this function SHOULD already be in order respecting the ordering rules of the lane and
// respecting the ordering rules of mempool relative to the lanes it has.
ProcessLaneHandler func(ctx sdk.Context, txs []sdk.Tx) ([]sdk.Tx, error)

// CheckOrderHandler is responsible for checking the order of transactions that belong to a given
// lane. This handler should be used to verify that the ordering of transactions passed into the
// function respect the ordering logic of the lane (if any transactions from the lane are included).
// This function should also ensure that transactions that belong to this lane are contiguous and do
// not have any transactions from other lanes in between them.
CheckOrderHandler func(ctx sdk.Context, txs []sdk.Tx) error

// PrepareLanesHandler wraps all of the lanes' PrepareLane function into a single chained
// function. You can think of it like an AnteHandler, but for preparing proposals in the
// context of lanes instead of modules.
Expand All @@ -50,42 +24,10 @@ func NoOpPrepareLanesHandler() PrepareLanesHandler {
}
}

// NoOpPrepareLaneHandler returns a no-op prepare lane handler.
// This should only be used for testing.
func NoOpPrepareLaneHandler() PrepareLaneHandler {
return func(ctx sdk.Context, proposal BlockProposal, maxTxBytes int64) (txsToInclude [][]byte, txsToRemove []sdk.Tx, err error) {
return nil, nil, nil
}
}

// PanicPrepareLaneHandler returns a prepare lane handler that panics.
// This should only be used for testing.
func PanicPrepareLaneHandler() PrepareLaneHandler {
return func(sdk.Context, BlockProposal, int64) (txsToInclude [][]byte, txsToRemove []sdk.Tx, err error) {
panic("panic prepare lanes handler")
}
}

// NoOpProcessLanesHandler returns a no-op process lanes handler.
// This should only be used for testing.
func NoOpProcessLanesHandler() ProcessLanesHandler {
return func(ctx sdk.Context, txs []sdk.Tx) (sdk.Context, error) {
return ctx, nil
}
}

// NoOpProcessLaneHandler returns a no-op process lane handler.
// This should only be used for testing.
func NoOpProcessLaneHandler() ProcessLaneHandler {
return func(ctx sdk.Context, txs []sdk.Tx) ([]sdk.Tx, error) {
return txs, nil
}
}

// PanicProcessLanesHandler returns a process lanes handler that panics.
// This should only be used for testing.
func PanicProcessLaneHandler() ProcessLaneHandler {
return func(sdk.Context, []sdk.Tx) ([]sdk.Tx, error) {
panic("panic process lanes handler")
}
}
4 changes: 2 additions & 2 deletions lanes/build-your-own/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ amount staked, we could do the following:
// NOTE: This is a stateful check on the transaction. The details of how to
// implement this are abstracted away in the example, but you can implement
// this using the staking keeper.
func (h *Handler) CustomMatchHandler() block.MatchHandler {
func (h *Handler) CustomMatchHandler() base.MatchHandler {
return func(ctx sdk.Context, tx sdk.Tx) bool {
if !h.IsStakingTx(tx) {
return false
Expand Down Expand Up @@ -386,7 +386,7 @@ pass in all of the base apps configurations (txDecoder, logger, etc.). A sample
`LaneConfig` might look like the following:

```golang
config := block.LaneConfig{
config := base.LaneConfig{
Logger: app.Logger(),
TxDecoder: app.TxDecoder(),
TxEncoder: app.TxEncoder(),
Expand Down
4 changes: 2 additions & 2 deletions lanes/free/lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type FreeLane struct { //nolint
func NewFreeLane(
cfg base.LaneConfig,
txPriority base.TxPriority[string],
matchFn block.MatchHandler,
matchFn base.MatchHandler,
) *FreeLane {
lane := base.NewBaseLane(
cfg,
Expand All @@ -49,7 +49,7 @@ func NewFreeLane(
// DefaultMatchHandler returns the default match handler for the free lane. The
// default implementation matches transactions that are staking related. In particular,
// any transaction that is a MsgDelegate, MsgBeginRedelegate, or MsgCancelUnbondingDelegation.
func DefaultMatchHandler() block.MatchHandler {
func DefaultMatchHandler() base.MatchHandler {
return func(ctx sdk.Context, tx sdk.Tx) bool {
for _, msg := range tx.GetMsgs() {
switch msg.(type) {
Expand Down
7 changes: 4 additions & 3 deletions lanes/mev/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/block-sdk/block"
"github.com/skip-mev/block-sdk/block/base"
"github.com/skip-mev/block-sdk/block/utils"
"github.com/skip-mev/block-sdk/x/builder/types"
)
Expand All @@ -14,7 +15,7 @@ import (
// and whose bundled transactions are valid and include them in the proposal. It
// will return no transactions if no valid bids are found. If any of the bids are invalid,
// it will return them and will only remove the bids and not the bundled transactions.
func (l *MEVLane) PrepareLaneHandler() block.PrepareLaneHandler {
func (l *MEVLane) PrepareLaneHandler() base.PrepareLaneHandler {
return func(ctx sdk.Context, proposal block.BlockProposal, maxTxBytes int64) ([][]byte, []sdk.Tx, error) {
// Define all of the info we need to select transactions for the partial proposal.
var (
Expand Down Expand Up @@ -147,7 +148,7 @@ func (l *MEVLane) PrepareLaneHandler() block.PrepareLaneHandler {

// ProcessLaneHandler will ensure that block proposals that include transactions from
// the mev lane are valid.
func (l *MEVLane) ProcessLaneHandler() block.ProcessLaneHandler {
func (l *MEVLane) ProcessLaneHandler() base.ProcessLaneHandler {
return func(ctx sdk.Context, txs []sdk.Tx) ([]sdk.Tx, error) {
if len(txs) == 0 {
return txs, nil
Expand Down Expand Up @@ -178,7 +179,7 @@ func (l *MEVLane) ProcessLaneHandler() block.ProcessLaneHandler {
// - there are no other bid transactions in the proposal
// - transactions from other lanes are not interleaved with transactions from the bid
// transaction.
func (l *MEVLane) CheckOrderHandler() block.CheckOrderHandler {
func (l *MEVLane) CheckOrderHandler() base.CheckOrderHandler {
return func(ctx sdk.Context, txs []sdk.Tx) error {
if len(txs) == 0 {
return nil
Expand Down
6 changes: 3 additions & 3 deletions lanes/mev/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/skip-mev/block-sdk/block"
"github.com/skip-mev/block-sdk/block/base"
"github.com/skip-mev/block-sdk/x/builder/types"
)

Expand All @@ -24,7 +24,7 @@ type (
GetAuctionBidInfo(tx sdk.Tx) (*types.BidInfo, error)

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

// DefaultAuctionFactory defines a default implmentation for the auction factory interface for processing auction transactions.
Expand Down Expand Up @@ -95,7 +95,7 @@ func (config *DefaultAuctionFactory) GetAuctionBidInfo(tx sdk.Tx) (*types.BidInf
}, nil
}

func (config *DefaultAuctionFactory) MatchHandler() block.MatchHandler {
func (config *DefaultAuctionFactory) MatchHandler() base.MatchHandler {
return func(ctx sdk.Context, tx sdk.Tx) bool {
bidInfo, err := config.GetAuctionBidInfo(tx)
return bidInfo != nil && err == nil
Expand Down

0 comments on commit c956e6c

Please sign in to comment.