Skip to content

Commit

Permalink
Merge branch 'main' into terpay/readmes-everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
davidterpay authored Nov 29, 2023
2 parents d3cb0a6 + b91cfb6 commit d011f07
Show file tree
Hide file tree
Showing 17 changed files with 358 additions and 374 deletions.
12 changes: 0 additions & 12 deletions abci/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,6 @@ func (s *ProposalsTestSuite) TestPrepareProposalEdgeCases() {
)
s.Require().NoError(err)

defaultLane.SetIgnoreList(nil)

proposalHandler := abci.NewProposalHandler(
log.NewNopLogger(),
s.encodingConfig.TxConfig.TxDecoder(),
Expand Down Expand Up @@ -598,8 +596,6 @@ func (s *ProposalsTestSuite) TestPrepareProposalEdgeCases() {
)
s.Require().NoError(err)

defaultLane.SetIgnoreList(nil)

proposalHandler := abci.NewProposalHandler(
log.NewNopLogger(),
s.encodingConfig.TxConfig.TxDecoder(),
Expand Down Expand Up @@ -671,10 +667,6 @@ func (s *ProposalsTestSuite) TestPrepareProposalEdgeCases() {
)
s.Require().NoError(err)

panicLane.SetIgnoreList(nil)
panicLane2.SetIgnoreList(nil)
defaultLane.SetIgnoreList(nil)

proposalHandler := abci.NewProposalHandler(
log.NewNopLogger(),
s.encodingConfig.TxConfig.TxDecoder(),
Expand Down Expand Up @@ -746,10 +738,6 @@ func (s *ProposalsTestSuite) TestPrepareProposalEdgeCases() {
)
s.Require().NoError(err)

panicLane.SetIgnoreList(nil)
panicLane2.SetIgnoreList(nil)
defaultLane.SetIgnoreList(nil)

proposalHandler := abci.NewProposalHandler(
log.NewNopLogger(),
s.encodingConfig.TxConfig.TxDecoder(),
Expand Down
6 changes: 3 additions & 3 deletions abci/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ func (s *ProposalsTestSuite) setUpStandardLane(maxBlockSpace math.LegacyDec, exp
TxDecoder: s.encodingConfig.TxConfig.TxDecoder(),
AnteHandler: s.setUpAnteHandler(expectedExecution),
MaxBlockSpace: maxBlockSpace,
IgnoreList: make([]block.Lane, 0),
SignerExtractor: signeradaptors.NewDefaultAdapter(),
}

return defaultlane.NewDefaultLane(cfg)
return defaultlane.NewDefaultLane(cfg, base.DefaultMatchHandler())
}

func (s *ProposalsTestSuite) setUpTOBLane(maxBlockSpace math.LegacyDec, expectedExecution map[sdk.Tx]bool) *mev.MEVLane {
Expand All @@ -102,7 +101,8 @@ func (s *ProposalsTestSuite) setUpTOBLane(maxBlockSpace math.LegacyDec, expected
SignerExtractor: signeradaptors.NewDefaultAdapter(),
}

return mev.NewMEVLane(cfg, mev.NewDefaultAuctionFactory(cfg.TxDecoder, signeradaptors.NewDefaultAdapter()))
factory := mev.NewDefaultAuctionFactory(cfg.TxDecoder, signeradaptors.NewDefaultAdapter())
return mev.NewMEVLane(cfg, factory, factory.MatchHandler())
}

func (s *ProposalsTestSuite) setUpFreeLane(maxBlockSpace math.LegacyDec, expectedExecution map[sdk.Tx]bool) *free.FreeLane {
Expand Down
9 changes: 0 additions & 9 deletions block/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

signer_extraction "github.com/skip-mev/block-sdk/adapters/signer_extraction_adapter"
"github.com/skip-mev/block-sdk/block"
)

// LaneConfig defines the basic configurations needed for a lane.
Expand All @@ -28,14 +27,6 @@ type LaneConfig struct {
// lane (up to maxTxBytes as provided by the request). This is useful for the default lane.
MaxBlockSpace math.LegacyDec

// IgnoreList defines the list of lanes to ignore when processing transactions. This
// is useful for when you want lanes to exist after the default lane. For example,
// say there are two lanes: default and free. The free lane should be processed after
// the default lane. In this case, the free lane should be added to the ignore list
// of the default lane. Otherwise, the transactions that belong to the free lane
// will be processed by the default lane (which accepts all transactions by default).
IgnoreList []block.Lane

// MaxTxs sets the maximum number of transactions allowed in the mempool with
// the semantics:
// - if MaxTx == 0, there is no cap on the number of transactions in the mempool
Expand Down
16 changes: 16 additions & 0 deletions block/base/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,19 @@ func DefaultMatchHandler() MatchHandler {
return true
}
}

// NewMatchHandler returns a match handler that matches transactions
// that match the lane and do not match with any of the provided match handlers.
// In the context of building an application, you would want to use this to
// ignore the match handlers of other lanes in the application.
func NewMatchHandler(mh MatchHandler, ignoreMHs ...MatchHandler) MatchHandler {
return func(ctx sdk.Context, tx sdk.Tx) bool {
for _, ignoreMH := range ignoreMHs {
if ignoreMH(ctx, tx) {
return false
}
}

return mh(ctx, tx)
}
}
31 changes: 1 addition & 30 deletions block/base/lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,43 +121,14 @@ func (l *BaseLane) SetProcessLaneHandler(processLaneHandler ProcessLaneHandler)
// if the transaction is on the ignore list. If the transaction is on the ignore
// list, it returns false.
func (l *BaseLane) Match(ctx sdk.Context, tx sdk.Tx) bool {
return l.matchHandler(ctx, tx) && !l.CheckIgnoreList(ctx, tx)
}

// CheckIgnoreList returns true if the transaction is on the ignore list. The ignore
// list is utilized to prevent transactions that should be considered in other lanes
// from being considered from this lane.
func (l *BaseLane) CheckIgnoreList(ctx sdk.Context, tx sdk.Tx) bool {
if l.cfg.IgnoreList == nil {
return false
}

for _, lane := range l.cfg.IgnoreList {
if lane.Match(ctx, tx) {
return true
}
}

return false
return l.matchHandler(ctx, tx)
}

// Name returns the name of the lane.
func (l *BaseLane) Name() string {
return l.laneName
}

// SetIgnoreList sets the ignore list for the lane. The ignore list is a list
// of lanes that the lane should ignore when processing transactions.
func (l *BaseLane) SetIgnoreList(lanes []block.Lane) {
l.cfg.IgnoreList = lanes
}

// GetIgnoreList returns the ignore list for the lane. The ignore list is a list
// of lanes that the lane should ignore when processing transactions.
func (l *BaseLane) GetIgnoreList() []block.Lane {
return l.cfg.IgnoreList
}

// SetAnteHandler sets the ante handler for the lane.
func (l *BaseLane) SetAnteHandler(anteHandler sdk.AnteHandler) {
l.cfg.AnteHandler = anteHandler
Expand Down
6 changes: 0 additions & 6 deletions block/lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ type Lane interface {
// SetAnteHandler sets the lane's antehandler.
SetAnteHandler(antehander sdk.AnteHandler)

// SetIgnoreList sets the lanes that should be ignored by this lane.
SetIgnoreList(ignoreList []Lane)

// GetIgnoreList returns the lanes that should be ignored by this lane.
GetIgnoreList() []Lane

// Match determines if a transaction belongs to this lane.
Match(ctx sdk.Context, tx sdk.Tx) bool

Expand Down
56 changes: 0 additions & 56 deletions block/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import (
blocksdkmoduletypes "github.com/skip-mev/block-sdk/x/blocksdk/types"
)

const (
// DefaultLaneName is the default lane name. We enforce that a lane with the name
// "default" is provided when constructing the mempool.
DefaultLaneName = "default"
)

var _ Mempool = (*LanedMempool)(nil)

// LaneFetcher defines the interface to get a lane stored in the x/blocksdk module.
Expand Down Expand Up @@ -62,61 +56,11 @@ type (
// to its own selection logic. The lanes are ordered according to their priority. The
// first lane in the registry has the highest priority. Proposals are verified according
// to the order of the lanes in the registry. Each transaction SHOULD only belong in one lane.
// To enforce that transactions only belong to one lane, each lane has an ignore list.
//
// For example, say we have three lanes, MEV, default, and free. The ignore list of each
// lane will look like the following:
// - MEV: free
// - default: MEV, free
// - free: MEV.
//
// Note that a name with the value "default" MUST be provided.
func NewLanedMempool(
logger log.Logger,
lanes []Lane,
laneFetcher LaneFetcher,
) (*LanedMempool, error) {
laneCache := make(map[Lane]struct{})
seenDefault := false

// Ensure that each of the lanes are mutually exclusive. The default lane should
// ignore all other lanes, while all other lanes should ignore every lane except
// the default lane.
for index, lane := range lanes {
if lane.Name() == DefaultLaneName {
lowerIgnoreList := make([]Lane, index)
copy(lowerIgnoreList, lanes[:index])

upperIgnoreList := make([]Lane, len(lanes)-index-1)
copy(upperIgnoreList, lanes[index+1:])

lane.SetIgnoreList(append(lowerIgnoreList, upperIgnoreList...))
seenDefault = true
} else {
laneCache[lane] = struct{}{}
}
}

if !seenDefault {
return nil, fmt.Errorf("default lane not found. a lane with the name %s must be provided", DefaultLaneName)
}

for _, lane := range lanes {
if lane.Name() == DefaultLaneName {
continue
}

delete(laneCache, lane)

ignoreList := make([]Lane, 0)
for otherLane := range laneCache {
ignoreList = append(ignoreList, otherLane)
}

lane.SetIgnoreList(ignoreList)
laneCache[lane] = struct{}{}
}

mempool := &LanedMempool{
logger: logger,
registry: lanes,
Expand Down
72 changes: 9 additions & 63 deletions block/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ func (suite *BlockBusterTestSuite) SetupTest() {
AnteHandler: nil,
MaxBlockSpace: math.LegacyZeroDec(),
}
factory := mev.NewDefaultAuctionFactory(suite.encodingConfig.TxConfig.TxDecoder(), signer_extraction.NewDefaultAdapter())
suite.mevLane = mev.NewMEVLane(
mevConfig,
mev.NewDefaultAuctionFactory(suite.encodingConfig.TxConfig.TxDecoder(), signer_extraction.NewDefaultAdapter()),
factory,
factory.MatchHandler(),
)

suite.mevSDKLane = blocksdkmoduletypes.Lane{
Expand Down Expand Up @@ -121,6 +123,7 @@ func (suite *BlockBusterTestSuite) SetupTest() {
}
suite.baseLane = defaultlane.NewDefaultLane(
baseConfig,
base.DefaultMatchHandler(),
)

suite.baseSDKLane = blocksdkmoduletypes.Lane{
Expand Down Expand Up @@ -169,10 +172,12 @@ func (suite *BlockBusterTestSuite) TestNewMempool() {
MaxBlockSpace: math.LegacyZeroDec(),
}

defaultLane := defaultlane.NewDefaultLane(baseConfig)
defaultLane := defaultlane.NewDefaultLane(baseConfig, base.DefaultMatchHandler())
factory := mev.NewDefaultAuctionFactory(suite.encodingConfig.TxConfig.TxDecoder(), signer_extraction.NewDefaultAdapter())
mevLane := mev.NewMEVLane(
baseConfig,
mev.NewDefaultAuctionFactory(suite.encodingConfig.TxConfig.TxDecoder(), signer_extraction.NewDefaultAdapter()),
factory,
factory.MatchHandler(),
)
freeLane := free.NewFreeLane(
baseConfig,
Expand All @@ -189,9 +194,6 @@ func (suite *BlockBusterTestSuite) TestNewMempool() {
fetcher,
)
suite.Require().NoError(err)

ignoreList := defaultLane.GetIgnoreList()
suite.Require().Equal(0, len(ignoreList))
})

suite.Run("works mev and default lane", func() {
Expand All @@ -203,12 +205,6 @@ func (suite *BlockBusterTestSuite) TestNewMempool() {
fetcher,
)
suite.Require().NoError(err)

ignoreList := defaultLane.GetIgnoreList()
suite.Require().Equal(1, len(ignoreList))

ignoreList = mevLane.GetIgnoreList()
suite.Require().Equal(0, len(ignoreList))
})

suite.Run("works mev and default lane in reverse order", func() {
Expand All @@ -220,12 +216,6 @@ func (suite *BlockBusterTestSuite) TestNewMempool() {
fetcher,
)
suite.Require().NoError(err)

ignoreList := defaultLane.GetIgnoreList()
suite.Require().Equal(1, len(ignoreList))

ignoreList = mevLane.GetIgnoreList()
suite.Require().Equal(0, len(ignoreList))
})

suite.Run("works with mev, free, and default lane", func() {
Expand All @@ -237,17 +227,6 @@ func (suite *BlockBusterTestSuite) TestNewMempool() {
fetcher,
)
suite.Require().NoError(err)

ignoreList := defaultLane.GetIgnoreList()
suite.Require().Equal(2, len(ignoreList))

ignoreList = mevLane.GetIgnoreList()
suite.Require().Equal(1, len(ignoreList))
suite.Require().Equal(freeLane, ignoreList[0])

ignoreList = freeLane.GetIgnoreList()
suite.Require().Equal(1, len(ignoreList))
suite.Require().Equal(mevLane, ignoreList[0])
})

suite.Run("works with mev, default, free lane", func() {
Expand All @@ -259,17 +238,6 @@ func (suite *BlockBusterTestSuite) TestNewMempool() {
fetcher,
)
suite.Require().NoError(err)

ignoreList := defaultLane.GetIgnoreList()
suite.Require().Equal(2, len(ignoreList))

ignoreList = mevLane.GetIgnoreList()
suite.Require().Equal(1, len(ignoreList))
suite.Require().Equal(freeLane, ignoreList[0])

ignoreList = freeLane.GetIgnoreList()
suite.Require().Equal(1, len(ignoreList))
suite.Require().Equal(mevLane, ignoreList[0])
})

suite.Run("works with free, mev, and default lane", func() {
Expand All @@ -281,17 +249,6 @@ func (suite *BlockBusterTestSuite) TestNewMempool() {
fetcher,
)
suite.Require().NoError(err)

ignoreList := defaultLane.GetIgnoreList()
suite.Require().Equal(2, len(ignoreList))

ignoreList = mevLane.GetIgnoreList()
suite.Require().Equal(1, len(ignoreList))
suite.Require().Equal(freeLane, ignoreList[0])

ignoreList = freeLane.GetIgnoreList()
suite.Require().Equal(1, len(ignoreList))
suite.Require().Equal(mevLane, ignoreList[0])
})

suite.Run("works with default, free, mev lanes", func() {
Expand All @@ -303,17 +260,6 @@ func (suite *BlockBusterTestSuite) TestNewMempool() {
fetcher,
)
suite.Require().NoError(err)

ignoreList := defaultLane.GetIgnoreList()
suite.Require().Equal(2, len(ignoreList))

ignoreList = mevLane.GetIgnoreList()
suite.Require().Equal(1, len(ignoreList))
suite.Require().Equal(freeLane, ignoreList[0])

ignoreList = freeLane.GetIgnoreList()
suite.Require().Equal(1, len(ignoreList))
suite.Require().Equal(mevLane, ignoreList[0])
})

suite.Run("default lane not included", func() {
Expand All @@ -324,7 +270,7 @@ func (suite *BlockBusterTestSuite) TestNewMempool() {
lanes,
fetcher,
)
suite.Require().Error(err)
suite.Require().NoError(err)
})

suite.Run("duplicate lanes", func() {
Expand Down
Loading

0 comments on commit d011f07

Please sign in to comment.