[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
6 changed files with 19 additions and 62 deletions
+9 -4
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
-32
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
}
@@ -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)
}