[ENG-680]: Enforcing auction txs to have timeouts (#52)

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
David Terpay
2023-04-10 12:19:25 -04:00
committed by GitHub
co-authored by Aleksandr Bezobchuk
parent 89d111aa43
commit fe35a9eeb2
6 changed files with 54 additions and 20 deletions
+25 -6
View File
@@ -2,6 +2,7 @@ package ante
import (
"bytes"
"fmt"
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -11,12 +12,20 @@ import (
var _ sdk.AnteDecorator = BuilderDecorator{}
type BuilderDecorator struct {
builderKeeper keeper.Keeper
txDecoder sdk.TxDecoder
txEncoder sdk.TxEncoder
mempool *mempool.AuctionMempool
}
type (
BuilderDecorator struct {
builderKeeper keeper.Keeper
txDecoder sdk.TxDecoder
txEncoder sdk.TxEncoder
mempool *mempool.AuctionMempool
}
TxWithTimeoutHeight interface {
sdk.Tx
GetTimeoutHeight() uint64
}
)
func NewBuilderDecorator(ak keeper.Keeper, txDecoder sdk.TxDecoder, txEncoder sdk.TxEncoder, mempool *mempool.AuctionMempool) BuilderDecorator {
return BuilderDecorator{
@@ -37,6 +46,16 @@ func (ad BuilderDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
// Validate the auction bid if one exists.
if auctionMsg != nil {
auctionTx, ok := tx.(TxWithTimeoutHeight)
if !ok {
return ctx, fmt.Errorf("transaction does not implement TxWithTimeoutHeight")
}
timeout := auctionTx.GetTimeoutHeight()
if timeout == 0 {
return ctx, fmt.Errorf("timeout height cannot be zero")
}
bidder, err := sdk.AccAddressFromBech32(auctionMsg.Bidder)
if err != nil {
return ctx, errors.Wrapf(err, "invalid bidder address (%s)", auctionMsg.Bidder)
+11 -2
View File
@@ -92,6 +92,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
topBidder = testutils.RandomAccounts(suite.random, 1)[0]
topBid = sdk.NewCoin("foo", sdk.NewInt(100))
insertTopBid = true
timeout = uint64(1000)
// Auction setup
maxBundleSize uint32 = 5
@@ -158,9 +159,17 @@ func (suite *AnteTestSuite) TestAnteHandler() {
},
true,
},
{
"invalid auction bid tx with no timeout",
func() {
timeout = 0
},
false,
},
{
"auction tx is the top bidding tx",
func() {
timeout = 1000
balance = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(10000)))
bid = sdk.NewCoin("foo", sdk.NewInt(1000))
reserveFee = sdk.NewCoin("foo", sdk.NewInt(100))
@@ -237,7 +246,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
// Insert the top bid into the mempool
mempool := mempool.NewAuctionMempool(suite.encodingConfig.TxConfig.TxDecoder(), 0)
if insertTopBid {
topAuctionTx, err := testutils.CreateAuctionTxWithSigners(suite.encodingConfig.TxConfig, topBidder, topBid, 0, []testutils.Account{})
topAuctionTx, err := testutils.CreateAuctionTxWithSigners(suite.encodingConfig.TxConfig, topBidder, topBid, 0, timeout, []testutils.Account{})
suite.Require().NoError(err)
suite.Require().Equal(0, mempool.CountTx())
suite.Require().Equal(0, mempool.CountAuctionTx())
@@ -247,7 +256,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
}
// Create the actual auction tx and insert into the mempool
auctionTx, err := testutils.CreateAuctionTxWithSigners(suite.encodingConfig.TxConfig, bidder, bid, 0, signers)
auctionTx, err := testutils.CreateAuctionTxWithSigners(suite.encodingConfig.TxConfig, bidder, bid, 0, timeout, signers)
suite.Require().NoError(err)
// Execute the ante handler
+2 -2
View File
@@ -183,7 +183,7 @@ func (suite *KeeperTestSuite) TestValidateAuctionMsg() {
// Create the bundle of transactions ordered by accounts
bundle := make([]sdk.Tx, 0)
for _, acc := range accounts {
tx, err := testutils.CreateRandomTx(suite.encCfg.TxConfig, acc, 0, 1)
tx, err := testutils.CreateRandomTx(suite.encCfg.TxConfig, acc, 0, 1, 100)
suite.Require().NoError(err)
bundle = append(bundle, tx)
}
@@ -293,7 +293,7 @@ func (suite *KeeperTestSuite) TestValidateBundle() {
bundle := make([]sdk.Tx, 0)
for _, acc := range accounts {
// Create a random tx
tx, err := testutils.CreateRandomTx(suite.encCfg.TxConfig, acc, 0, 1)
tx, err := testutils.CreateRandomTx(suite.encCfg.TxConfig, acc, 0, 1, 1000)
suite.Require().NoError(err)
bundle = append(bundle, tx)
}