feat(BB): Main BB testing (Mempool + ABCI) (#169)

This commit is contained in:
David Terpay 2023-06-08 11:46:24 -04:00 committed by GitHub
parent cfe2147d52
commit 38f2ae20f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 1571 additions and 24 deletions

View File

@ -132,8 +132,6 @@ func ChainPrepareLanes(chain ...blockbuster.Lane) blockbuster.PrepareLanesHandle
// Write the cache to the context since we know that the lane successfully prepared
// the partial proposal.
write()
lane.Logger().Info("prepared lane", "lane", lane.Name())
}
}()

File diff suppressed because it is too large Load Diff

View File

@ -102,11 +102,10 @@ selectBidTxLoop:
break selectBidTxLoop
}
txsToRemove[tmpBidTx] = struct{}{}
l.Cfg.Logger.Info(
"failed to select auction bid tx; tx size is too large",
"failed to select auction bid tx for lane; tx size is too large",
"tx_size", bidTxSize,
"max_size", proposal.MaxTxBytes,
"max_size", maxTxBytes,
)
}

View File

@ -2,6 +2,7 @@ package blockbuster
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkmempool "github.com/cosmos/cosmos-sdk/types/mempool"
@ -22,6 +23,12 @@ type (
// GetTxDistribution returns the number of transactions in each lane.
GetTxDistribution() map[string]int
// Match will return the lane that the transaction belongs to.
Match(tx sdk.Tx) (Lane, error)
// GetLane returns the lane with the given name.
GetLane(name string) (Lane, error)
}
// Mempool defines the Blockbuster mempool implement. It contains a registry
@ -58,16 +65,27 @@ func (m *BBMempool) GetTxDistribution() map[string]int {
return counts
}
// Insert inserts a transaction into every lane that it matches. Insertion will
// be attempted on all lanes, even if an error is encountered.
func (m *BBMempool) Insert(ctx context.Context, tx sdk.Tx) error {
// Match will return the lane that the transaction belongs to. It matches to
// the first lane where lane.Match(tx) is true.
func (m *BBMempool) Match(tx sdk.Tx) (Lane, error) {
for _, lane := range m.registry {
if lane.Match(tx) {
return lane.Insert(ctx, tx)
return lane, nil
}
}
return nil
return nil, fmt.Errorf("no lane matched transaction")
}
// Insert will insert a transaction into the mempool. It inserts the transaction
// into the first lane that it matches.
func (m *BBMempool) Insert(ctx context.Context, tx sdk.Tx) error {
lane, err := m.Match(tx)
if err != nil {
return err
}
return lane.Insert(ctx, tx)
}
// Insert returns a nil iterator.
@ -80,30 +98,40 @@ func (m *BBMempool) Select(_ context.Context, _ [][]byte) sdkmempool.Iterator {
return nil
}
// Remove removes a transaction from the mempool. It removes the transaction
// from the first lane that it matches.
// Remove removes a transaction from the mempool based on the first lane that
// it matches.
func (m *BBMempool) Remove(tx sdk.Tx) error {
for _, lane := range m.registry {
if lane.Match(tx) {
return lane.Remove(tx)
}
lane, err := m.Match(tx)
if err != nil {
return err
}
return nil
return lane.Remove(tx)
}
// Contains returns true if the transaction is contained in the mempool.
// Contains returns true if the transaction is contained in the mempool. It
// checks the first lane that it matches to.
func (m *BBMempool) Contains(tx sdk.Tx) (bool, error) {
for _, lane := range m.registry {
if lane.Match(tx) {
return lane.Contains(tx)
}
lane, err := m.Match(tx)
if err != nil {
return false, err
}
return false, nil
return lane.Contains(tx)
}
// Registry returns the mempool's lane registry.
func (m *BBMempool) Registry() []Lane {
return m.registry
}
// GetLane returns the lane with the given name.
func (m *BBMempool) GetLane(name string) (Lane, error) {
for _, lane := range m.registry {
if lane.Name() == name {
return lane, nil
}
}
return nil, fmt.Errorf("lane %s not found", name)
}

361
blockbuster/mempool_test.go Normal file
View File

@ -0,0 +1,361 @@
package blockbuster_test
import (
"math/rand"
"testing"
"time"
"github.com/cometbft/cometbft/libs/log"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/pob/blockbuster"
"github.com/skip-mev/pob/blockbuster/lanes/auction"
"github.com/skip-mev/pob/blockbuster/lanes/base"
"github.com/skip-mev/pob/blockbuster/lanes/free"
testutils "github.com/skip-mev/pob/testutils"
buildertypes "github.com/skip-mev/pob/x/builder/types"
"github.com/stretchr/testify/suite"
)
type BlockBusterTestSuite struct {
suite.Suite
ctx sdk.Context
// Define basic tx configuration
encodingConfig testutils.EncodingConfig
// Define all of the lanes utilized in the test suite
tobLane *auction.TOBLane
baseLane *base.DefaultLane
freeLane *free.Lane
lanes []blockbuster.Lane
mempool blockbuster.Mempool
// account set up
accounts []testutils.Account
random *rand.Rand
nonces map[string]uint64
}
func TestBlockBusterTestSuite(t *testing.T) {
suite.Run(t, new(BlockBusterTestSuite))
}
func (suite *BlockBusterTestSuite) SetupTest() {
// General config for transactions and randomness for the test suite
suite.encodingConfig = testutils.CreateTestEncodingConfig()
suite.random = rand.New(rand.NewSource(time.Now().Unix()))
key := sdk.NewKVStoreKey(buildertypes.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), key, storetypes.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx.WithBlockHeight(1)
// Lanes configuration
//
// TOB lane set up
config := blockbuster.BaseLaneConfig{
Logger: log.NewNopLogger(),
TxEncoder: suite.encodingConfig.TxConfig.TxEncoder(),
TxDecoder: suite.encodingConfig.TxConfig.TxDecoder(),
AnteHandler: nil,
MaxBlockSpace: sdk.ZeroDec(),
}
// Top of block lane set up
suite.tobLane = auction.NewTOBLane(
config,
0, // No bound on the number of transactions in the lane
auction.NewDefaultAuctionFactory(suite.encodingConfig.TxConfig.TxDecoder()),
)
// Free lane set up
suite.freeLane = free.NewFreeLane(
config,
free.NewDefaultFreeFactory(suite.encodingConfig.TxConfig.TxDecoder()),
)
// Base lane set up
suite.baseLane = base.NewDefaultLane(
config,
)
// Mempool set up
suite.lanes = []blockbuster.Lane{suite.tobLane, suite.freeLane, suite.baseLane}
suite.mempool = blockbuster.NewMempool(suite.lanes...)
// Accounts set up
suite.accounts = testutils.RandomAccounts(suite.random, 10)
suite.nonces = make(map[string]uint64)
for _, acc := range suite.accounts {
suite.nonces[acc.Address.String()] = 0
}
}
func (suite *BlockBusterTestSuite) TestInsert() {
cases := []struct {
name string
insertDistribution map[string]int
}{
{
"insert 1 tob tx",
map[string]int{
suite.tobLane.Name(): 1,
},
},
{
"insert 10 tob txs",
map[string]int{
suite.tobLane.Name(): 10,
},
},
{
"insert 1 base tx",
map[string]int{
suite.baseLane.Name(): 1,
},
},
{
"insert 10 base txs and 10 tob txs",
map[string]int{
suite.baseLane.Name(): 10,
suite.tobLane.Name(): 10,
},
},
{
"insert 100 base txs and 100 tob txs",
map[string]int{
suite.baseLane.Name(): 100,
suite.tobLane.Name(): 100,
},
},
{
"insert 100 base txs, 100 tob txs, and 100 free txs",
map[string]int{
suite.baseLane.Name(): 100,
suite.tobLane.Name(): 100,
suite.freeLane.Name(): 100,
},
},
{
"insert 10 free txs",
map[string]int{
suite.freeLane.Name(): 10,
},
},
{
"insert 10 free txs and 10 base txs",
map[string]int{
suite.freeLane.Name(): 10,
suite.baseLane.Name(): 10,
},
},
{
"insert 10 tob txs and 10 free txs",
map[string]int{
suite.tobLane.Name(): 10,
suite.freeLane.Name(): 10,
},
},
}
for _, tc := range cases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset
// Fill the base lane with numBaseTxs transactions
suite.fillBaseLane(tc.insertDistribution[suite.baseLane.Name()])
// Fill the TOB lane with numTobTxs transactions
suite.fillTOBLane(tc.insertDistribution[suite.tobLane.Name()])
// Fill the Free lane with numFreeTxs transactions
suite.fillFreeLane(tc.insertDistribution[suite.freeLane.Name()])
sum := 0
for _, v := range tc.insertDistribution {
sum += v
}
// Validate the mempool
suite.Require().Equal(suite.mempool.CountTx(), sum)
// Validate the lanes
suite.Require().Equal(suite.baseLane.CountTx(), tc.insertDistribution[suite.baseLane.Name()])
suite.Require().Equal(suite.tobLane.CountTx(), tc.insertDistribution[suite.tobLane.Name()])
suite.Require().Equal(suite.freeLane.CountTx(), tc.insertDistribution[suite.freeLane.Name()])
// Validate the lane counts
laneCounts := suite.mempool.GetTxDistribution()
// Ensure that the lane counts are correct
suite.Require().Equal(laneCounts[suite.tobLane.Name()], tc.insertDistribution[suite.tobLane.Name()])
suite.Require().Equal(laneCounts[suite.baseLane.Name()], tc.insertDistribution[suite.baseLane.Name()])
suite.Require().Equal(laneCounts[suite.freeLane.Name()], tc.insertDistribution[suite.freeLane.Name()])
})
}
}
func (suite *BlockBusterTestSuite) TestRemove() {
cases := []struct {
name string
numTobTxs int
numBaseTxs int
}{
{
"insert 1 tob tx",
1,
0,
},
{
"insert 10 tob txs",
10,
0,
},
{
"insert 1 base tx",
0,
1,
},
{
"insert 10 base txs and 10 tob txs",
10,
10,
},
{
"insert 100 base txs and 100 tob txs",
100,
100,
},
}
for _, tc := range cases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset
// Fill the base lane with numBaseTxs transactions
suite.fillBaseLane(tc.numBaseTxs)
// Fill the TOB lane with numTobTxs transactions
suite.fillTOBLane(tc.numTobTxs)
// Remove all transactions from the lanes
tobCount := tc.numTobTxs
baseCount := tc.numBaseTxs
for iterator := suite.baseLane.Select(suite.ctx, nil); iterator != nil; {
tx := iterator.Tx()
// Remove the transaction from the mempool
suite.Require().NoError(suite.mempool.Remove(tx))
// Ensure that the transaction is no longer in the mempool
contains, err := suite.mempool.Contains(tx)
suite.Require().NoError(err)
suite.Require().False(contains)
// Ensure the number of transactions in the lane is correct
baseCount--
suite.Require().Equal(suite.baseLane.CountTx(), baseCount)
distribution := suite.mempool.GetTxDistribution()
suite.Require().Equal(distribution[suite.baseLane.Name()], baseCount)
iterator = suite.baseLane.Select(suite.ctx, nil)
}
suite.Require().Equal(0, suite.baseLane.CountTx())
suite.Require().Equal(tobCount, suite.tobLane.CountTx())
// Remove all transactions from the lanes
for iterator := suite.tobLane.Select(suite.ctx, nil); iterator != nil; {
tx := iterator.Tx()
// Remove the transaction from the mempool
suite.Require().NoError(suite.mempool.Remove(tx))
// Ensure that the transaction is no longer in the mempool
contains, err := suite.mempool.Contains(tx)
suite.Require().NoError(err)
suite.Require().False(contains)
// Ensure the number of transactions in the lane is correct
tobCount--
suite.Require().Equal(suite.tobLane.CountTx(), tobCount)
distribution := suite.mempool.GetTxDistribution()
suite.Require().Equal(distribution[suite.tobLane.Name()], tobCount)
iterator = suite.tobLane.Select(suite.ctx, nil)
}
suite.Require().Equal(0, suite.tobLane.CountTx())
suite.Require().Equal(0, suite.baseLane.CountTx())
suite.Require().Equal(0, suite.mempool.CountTx())
// Validate the lane counts
distribution := suite.mempool.GetTxDistribution()
// Ensure that the lane counts are correct
suite.Require().Equal(distribution[suite.tobLane.Name()], 0)
suite.Require().Equal(distribution[suite.baseLane.Name()], 0)
})
}
}
// fillBaseLane fills the base lane with numTxs transactions that are randomly created.
func (suite *BlockBusterTestSuite) fillBaseLane(numTxs int) {
for i := 0; i < numTxs; i++ {
// randomly select an account to create the tx
randomIndex := suite.random.Intn(len(suite.accounts))
acc := suite.accounts[randomIndex]
// create a few random msgs and construct the tx
nonce := suite.nonces[acc.Address.String()]
randomMsgs := testutils.CreateRandomMsgs(acc.Address, 3)
tx, err := testutils.CreateTx(suite.encodingConfig.TxConfig, acc, nonce, 1000, randomMsgs)
suite.Require().NoError(err)
// insert the tx into the lane and update the account
suite.nonces[acc.Address.String()]++
priority := suite.random.Int63n(100) + 1
suite.Require().NoError(suite.mempool.Insert(suite.ctx.WithPriority(priority), tx))
}
}
// fillTOBLane fills the TOB lane with numTxs transactions that are randomly created.
func (suite *BlockBusterTestSuite) fillTOBLane(numTxs int) {
for i := 0; i < numTxs; i++ {
// randomly select a bidder to create the tx
randomIndex := suite.random.Intn(len(suite.accounts))
acc := suite.accounts[randomIndex]
// create a randomized auction transaction
nonce := suite.nonces[acc.Address.String()]
bidAmount := sdk.NewInt(int64(suite.random.Intn(1000) + 1))
bid := sdk.NewCoin("foo", bidAmount)
tx, err := testutils.CreateAuctionTxWithSigners(suite.encodingConfig.TxConfig, acc, bid, nonce, 1000, nil)
suite.Require().NoError(err)
// insert the auction tx into the global mempool
suite.Require().NoError(suite.mempool.Insert(suite.ctx, tx))
suite.nonces[acc.Address.String()]++
}
}
// filleFreeLane fills the free lane with numTxs transactions that are randomly created.
func (suite *BlockBusterTestSuite) fillFreeLane(numTxs int) {
for i := 0; i < numTxs; i++ {
// randomly select an account to create the tx
randomIndex := suite.random.Intn(len(suite.accounts))
acc := suite.accounts[randomIndex]
// create a few random msgs and construct the tx
nonce := suite.nonces[acc.Address.String()]
tx, err := testutils.CreateFreeTx(suite.encodingConfig.TxConfig, acc, nonce, 1000, "val1", sdk.NewCoin("foo", sdk.NewInt(100)))
suite.Require().NoError(err)
// insert the tx into the lane and update the account
suite.nonces[acc.Address.String()]++
suite.Require().NoError(suite.mempool.Insert(suite.ctx, tx))
}
}

View File

@ -0,0 +1,90 @@
package utils
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/pob/blockbuster"
)
func TestGetMaxTxBytesForLane(t *testing.T) {
testCases := []struct {
name string
proposal *blockbuster.Proposal
ratio sdk.Dec
expected int64
}{
{
"ratio is zero",
&blockbuster.Proposal{
MaxTxBytes: 100,
TotalTxBytes: 50,
},
sdk.ZeroDec(),
50,
},
{
"ratio is zero",
&blockbuster.Proposal{
MaxTxBytes: 100,
TotalTxBytes: 100,
},
sdk.ZeroDec(),
0,
},
{
"ratio is zero",
&blockbuster.Proposal{
MaxTxBytes: 100,
TotalTxBytes: 150,
},
sdk.ZeroDec(),
0,
},
{
"ratio is 1",
&blockbuster.Proposal{
MaxTxBytes: 100,
TotalTxBytes: 50,
},
sdk.OneDec(),
100,
},
{
"ratio is 10%",
&blockbuster.Proposal{
MaxTxBytes: 100,
TotalTxBytes: 50,
},
sdk.MustNewDecFromStr("0.1"),
10,
},
{
"ratio is 25%",
&blockbuster.Proposal{
MaxTxBytes: 100,
TotalTxBytes: 50,
},
sdk.MustNewDecFromStr("0.25"),
25,
},
{
"ratio is 50%",
&blockbuster.Proposal{
MaxTxBytes: 101,
TotalTxBytes: 50,
},
sdk.MustNewDecFromStr("0.5"),
50,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := GetMaxTxBytesForLane(tc.proposal, tc.ratio)
if actual != tc.expected {
t.Errorf("expected %d, got %d", tc.expected, actual)
}
})
}
}

View File

@ -1032,11 +1032,11 @@ func (s *IntegrationTestSuite) TestFreeLane() {
tx := s.createMsgDelegateTx(accounts[0], validator.OperatorAddress, defaultStakeAmount, 0, 1000)
// Broadcast the transaction
s.waitForABlock()
s.broadcastTx(tx, 0)
// Wait for a block to be created
s.waitForABlock()
s.waitForABlock()
// Ensure that the transaction was executed correctly
balanceAfterFreeTx := s.queryBalanceOf(accounts[0].Address.String(), app.BondDenom)
@ -1058,6 +1058,7 @@ func (s *IntegrationTestSuite) TestFreeLane() {
normalTx := s.createMsgSendTx(accounts[1], accounts[2].Address.String(), defaultSendAmountCoins, 0, 1000)
// Broadcast the transactions
s.waitForABlock()
s.broadcastTx(freeTx, 0)
s.broadcastTx(normalTx, 0)
@ -1098,6 +1099,7 @@ func (s *IntegrationTestSuite) TestFreeLane() {
freeTx2 := s.createMsgDelegateTx(accounts[1], validator.OperatorAddress, defaultStakeAmount, 0, 1000)
// Broadcast the transactions
s.waitForABlock()
s.broadcastTx(freeTx, 0)
s.broadcastTx(freeTx2, 0)

View File

@ -15,6 +15,7 @@ import (
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
buildertypes "github.com/skip-mev/pob/x/builder/types"
)
@ -32,6 +33,7 @@ func CreateTestEncodingConfig() EncodingConfig {
banktypes.RegisterInterfaces(interfaceRegistry)
cryptocodec.RegisterInterfaces(interfaceRegistry)
buildertypes.RegisterInterfaces(interfaceRegistry)
stakingtypes.RegisterInterfaces(interfaceRegistry)
codec := codec.NewProtoCodec(interfaceRegistry)
@ -94,6 +96,18 @@ func CreateTx(txCfg client.TxConfig, account Account, nonce, timeout uint64, msg
return txBuilder.GetTx(), nil
}
func CreateFreeTx(txCfg client.TxConfig, account Account, nonce, timeout uint64, validator string, amount sdk.Coin) (authsigning.Tx, error) {
msgs := []sdk.Msg{
&stakingtypes.MsgDelegate{
DelegatorAddress: account.Address.String(),
ValidatorAddress: validator,
Amount: amount,
},
}
return CreateTx(txCfg, account, nonce, timeout, msgs)
}
func CreateRandomTx(txCfg client.TxConfig, account Account, nonce, numberMsgs, timeout uint64) (authsigning.Tx, error) {
msgs := make([]sdk.Msg, numberMsgs)
for i := 0; i < int(numberMsgs); i++ {