[ENG-682]: Refactoring auction mempool to ingress normal sdk txs (#50)

This commit is contained in:
David Terpay 2023-04-06 11:13:44 -04:00 committed by GitHub
parent d3a198a710
commit cc3012a129
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 62 deletions

View File

@ -57,7 +57,7 @@ func (h *ProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHandler {
// bundled transactions are valid.
selectBidTxLoop:
for ; bidTxIterator != nil; bidTxIterator = bidTxIterator.Next() {
tmpBidTx := mempool.UnwrapBidTx(bidTxIterator.Tx())
tmpBidTx := bidTxIterator.Tx()
bidTxBz, err := h.txVerifier.PrepareProposalVerifyTx(tmpBidTx)
if err != nil {

View File

@ -234,7 +234,7 @@ func (suite *ABCITestSuite) exportMempool(exportRefTxs bool) [][]byte {
auctionIterator := suite.mempool.AuctionBidSelect(suite.ctx)
for ; auctionIterator != nil; auctionIterator = auctionIterator.Next() {
auctionTx := auctionIterator.Tx().(*mempool.WrappedBidTx).Tx
auctionTx := auctionIterator.Tx()
txBz, err := suite.encodingConfig.TxConfig.TxEncoder()(auctionTx)
suite.Require().NoError(err)
@ -771,6 +771,6 @@ func (suite *ABCITestSuite) isTopBidValid() bool {
}
// check if the top bid is valid
_, err := suite.executeAnteHandler(iterator.Tx().(*mempool.WrappedBidTx).Tx)
_, err := suite.executeAnteHandler(iterator.Tx())
return err == nil
}

View File

@ -34,7 +34,12 @@ type AuctionMempool struct {
func AuctionTxPriority() TxPriority[string] {
return TxPriority[string]{
GetTxPriority: func(goCtx context.Context, tx sdk.Tx) string {
return tx.(*WrappedBidTx).GetBid().String()
msgAuctionBid, err := GetMsgAuctionBidFromTx(tx)
if err != nil {
panic(err)
}
return msgAuctionBid.Bid.String()
},
Compare: func(a, b string) int {
aCoins, _ := sdk.ParseCoinsNormalized(a)
@ -99,7 +104,7 @@ func (am *AuctionMempool) Insert(ctx context.Context, tx sdk.Tx) error {
}
if msg != nil {
if err := am.auctionIndex.Insert(ctx, NewWrappedBidTx(tx, msg.GetBid())); err != nil {
if err := am.auctionIndex.Insert(ctx, tx); err != nil {
removeTx(am.globalIndex, tx)
return fmt.Errorf("failed to insert tx into auction index: %w", err)
}
@ -123,7 +128,7 @@ func (am *AuctionMempool) Remove(tx sdk.Tx) error {
// 2. Remove the bid from the auction index (if applicable). In addition, we
// remove all referenced transactions from the global mempool.
if msg != nil {
removeTx(am.auctionIndex, NewWrappedBidTx(tx, msg.GetBid()))
removeTx(am.auctionIndex, tx)
for _, refRawTx := range msg.GetTransactions() {
refTx, err := am.txDecoder(refRawTx)
@ -152,7 +157,7 @@ func (am *AuctionMempool) RemoveWithoutRefTx(tx sdk.Tx) error {
}
if msg != nil {
removeTx(am.auctionIndex, NewWrappedBidTx(tx, msg.GetBid()))
removeTx(am.auctionIndex, tx)
}
return nil

View File

@ -4,27 +4,9 @@ import (
"errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
buildertypes "github.com/skip-mev/pob/x/builder/types"
)
// WrappedBidTx defines a wrapper around an sdk.Tx that contains a single
// MsgAuctionBid message with additional metadata.
type WrappedBidTx struct {
signing.Tx
bid sdk.Coin
}
func NewWrappedBidTx(tx sdk.Tx, bid sdk.Coin) *WrappedBidTx {
return &WrappedBidTx{
Tx: tx.(signing.Tx),
bid: bid,
}
}
func (wbtx *WrappedBidTx) GetBid() sdk.Coin { return wbtx.bid }
// GetMsgAuctionBidFromTx attempts to retrieve a MsgAuctionBid from an sdk.Tx if
// one exists. If a MsgAuctionBid does exist and other messages are also present,
// an error is returned. If no MsgAuctionBid is present, <nil, nil> is returned.
@ -51,17 +33,3 @@ func GetMsgAuctionBidFromTx(tx sdk.Tx) (*buildertypes.MsgAuctionBid, error) {
return nil, errors.New("invalid MsgAuctionBid transaction")
}
}
// UnwrapBidTx attempts to unwrap a WrappedBidTx from an sdk.Tx if one exists.
func UnwrapBidTx(tx sdk.Tx) sdk.Tx {
if tx == nil {
return nil
}
wTx, ok := tx.(*WrappedBidTx)
if ok {
return wTx.Tx
}
return tx
}

View File

@ -3,7 +3,6 @@ package mempool_test
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
pobcodec "github.com/skip-mev/pob/codec"
"github.com/skip-mev/pob/mempool"
@ -47,22 +46,3 @@ func TestGetMsgAuctionBidFromTx_NoBid(t *testing.T) {
require.NoError(t, err)
require.Nil(t, msg)
}
func TestGetUnwrappedTx(t *testing.T) {
encCfg := pobcodec.CreateEncodingConfig()
txBuilder := encCfg.TxConfig.NewTxBuilder()
txBuilder.SetMsgs(&buildertypes.MsgAuctionBid{})
tx := txBuilder.GetTx()
bid := sdk.NewCoin("foo", sdk.NewInt(1000000))
wrappedTx := mempool.NewWrappedBidTx(tx, bid)
unWrappedTx := mempool.UnwrapBidTx(wrappedTx)
unwrappedBz, err := encCfg.TxConfig.TxEncoder()(unWrappedTx)
require.NoError(t, err)
txBz, err := encCfg.TxConfig.TxEncoder()(tx)
require.NoError(t, err)
require.Equal(t, txBz, unwrappedBz)
}

View File

@ -83,7 +83,12 @@ func (ad BuilderDecorator) GetTopAuctionBid(ctx sdk.Context) (sdk.Coin, error) {
return sdk.Coin{}, nil
}
return auctionTx.(*mempool.WrappedBidTx).GetBid(), nil
msgAuctionBid, err := mempool.GetMsgAuctionBidFromTx(auctionTx)
if err != nil {
return sdk.Coin{}, err
}
return msgAuctionBid.Bid, nil
}
// IsTopBidTx returns true if the transaction inputted is the highest bidding auction transaction in the mempool.
@ -93,8 +98,7 @@ func (ad BuilderDecorator) IsTopBidTx(ctx sdk.Context, tx sdk.Tx) (bool, error)
return false, nil
}
topBidTx := mempool.UnwrapBidTx(auctionTx)
topBidBz, err := ad.txEncoder(topBidTx)
topBidBz, err := ad.txEncoder(auctionTx)
if err != nil {
return false, err
}