Aggregating all testing functionality into one directory (#38)

This commit is contained in:
David Terpay
2023-03-29 14:30:53 -04:00
committed by GitHub
parent bd5104ffc7
commit c368f4cb00
14 changed files with 188 additions and 835 deletions
+10 -9
View File
@@ -9,6 +9,7 @@ import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/pob/mempool"
testutils "github.com/skip-mev/pob/testutils"
auctiontypes "github.com/skip-mev/pob/x/auction/types"
"github.com/stretchr/testify/suite"
)
@@ -16,11 +17,11 @@ import (
type IntegrationTestSuite struct {
suite.Suite
encCfg encodingConfig
encCfg testutils.EncodingConfig
mempool *mempool.AuctionMempool
ctx sdk.Context
random *rand.Rand
accounts []Account
accounts []testutils.Account
nonces map[string]uint64
}
@@ -30,13 +31,13 @@ func TestMempoolTestSuite(t *testing.T) {
func (suite *IntegrationTestSuite) SetupTest() {
// Mempool setup
suite.encCfg = createTestEncodingConfig()
suite.encCfg = testutils.CreateTestEncodingConfig()
suite.mempool = mempool.NewAuctionMempool(suite.encCfg.TxConfig.TxDecoder(), 0)
suite.ctx = sdk.NewContext(nil, cmtproto.Header{}, false, log.NewNopLogger())
// Init accounts
suite.random = rand.New(rand.NewSource(time.Now().Unix()))
suite.accounts = RandomAccounts(suite.random, 5)
suite.accounts = testutils.RandomAccounts(suite.random, 5)
suite.nonces = make(map[string]uint64)
for _, acc := range suite.accounts {
@@ -51,13 +52,13 @@ func (suite *IntegrationTestSuite) CreateFilledMempool(numNormalTxs, numAuctionT
// Insert a bunch of normal transactions into the global mempool
for i := 0; i < numNormalTxs; i++ {
// create a few random msgs
randomMsgs := createRandomMsgs(3)
// randomly select an account to create the tx
randomIndex := suite.random.Intn(len(suite.accounts))
acc := suite.accounts[randomIndex]
nonce := suite.nonces[acc.Address.String()]
randomTx, err := createTx(suite.encCfg.TxConfig, acc, nonce, randomMsgs)
randomMsgs := testutils.CreateRandomMsgs(acc.Address, 3)
randomTx, err := testutils.CreateTx(suite.encCfg.TxConfig, acc, nonce, randomMsgs)
suite.Require().NoError(err)
suite.nonces[acc.Address.String()]++
@@ -71,19 +72,19 @@ func (suite *IntegrationTestSuite) CreateFilledMempool(numNormalTxs, numAuctionT
// Insert a bunch of auction transactions into the global mempool and auction mempool
for i := 0; i < numAuctionTxs; i++ {
// randomly select a bidder to create the tx
acc := RandomAccounts(suite.random, 1)[0]
acc := testutils.RandomAccounts(suite.random, 1)[0]
// create a new auction bid msg with numBundledTxs bundled transactions
priority := suite.random.Int63n(100) + 1
bid := sdk.NewCoins(sdk.NewInt64Coin("foo", priority))
nonce := suite.nonces[acc.Address.String()]
bidMsg, err := createMsgAuctionBid(suite.encCfg.TxConfig, acc, bid, nonce, numBundledTxs)
bidMsg, err := testutils.CreateMsgAuctionBid(suite.encCfg.TxConfig, acc, bid, nonce, numBundledTxs)
suite.nonces[acc.Address.String()] += uint64(numBundledTxs)
suite.Require().NoError(err)
// create the auction tx
nonce = suite.nonces[acc.Address.String()]
auctionTx, err := createTx(suite.encCfg.TxConfig, acc, nonce, []sdk.Msg{bidMsg})
auctionTx, err := testutils.CreateTx(suite.encCfg.TxConfig, acc, nonce, []sdk.Msg{bidMsg})
suite.Require().NoError(err)
// insert the auction tx into the global mempool
-148
View File
@@ -1,148 +0,0 @@
package mempool_test
import (
"math/rand"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
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"
auctiontypes "github.com/skip-mev/pob/x/auction/types"
)
type encodingConfig struct {
InterfaceRegistry types.InterfaceRegistry
Codec codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
}
func createTestEncodingConfig() encodingConfig {
cdc := codec.NewLegacyAmino()
interfaceRegistry := types.NewInterfaceRegistry()
banktypes.RegisterInterfaces(interfaceRegistry)
cryptocodec.RegisterInterfaces(interfaceRegistry)
codec := codec.NewProtoCodec(interfaceRegistry)
return encodingConfig{
InterfaceRegistry: interfaceRegistry,
Codec: codec,
TxConfig: tx.NewTxConfig(codec, tx.DefaultSignModes),
Amino: cdc,
}
}
type Account struct {
PrivKey cryptotypes.PrivKey
PubKey cryptotypes.PubKey
Address sdk.AccAddress
ConsKey cryptotypes.PrivKey
}
func (acc Account) Equals(acc2 Account) bool {
return acc.Address.Equals(acc2.Address)
}
func RandomAccounts(r *rand.Rand, n int) []Account {
accs := make([]Account, n)
for i := 0; i < n; i++ {
pkSeed := make([]byte, 15)
r.Read(pkSeed)
accs[i].PrivKey = secp256k1.GenPrivKeyFromSecret(pkSeed)
accs[i].PubKey = accs[i].PrivKey.PubKey()
accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address())
accs[i].ConsKey = ed25519.GenPrivKeyFromSecret(pkSeed)
}
return accs
}
func createTx(txCfg client.TxConfig, account Account, nonce uint64, msgs []sdk.Msg) (authsigning.Tx, error) {
txBuilder := txCfg.NewTxBuilder()
if err := txBuilder.SetMsgs(msgs...); err != nil {
return nil, err
}
sigV2 := signing.SignatureV2{
PubKey: account.PrivKey.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: txCfg.SignModeHandler().DefaultMode(),
Signature: nil,
},
Sequence: nonce,
}
if err := txBuilder.SetSignatures(sigV2); err != nil {
return nil, err
}
return txBuilder.GetTx(), nil
}
func createRandomMsgs(numberMsgs int) []sdk.Msg {
msgs := make([]sdk.Msg, numberMsgs)
for i := 0; i < numberMsgs; i++ {
msgs[i] = &banktypes.MsgSend{
FromAddress: sdk.AccAddress([]byte("addr1_______________")).String(),
ToAddress: sdk.AccAddress([]byte("addr2_______________")).String(),
}
}
return msgs
}
// createMsgAuctionBid creates a new MsgAuctionBid with numberMsgs of referenced transactions embedded into the message and the inputted bid/bidder.
func createMsgAuctionBid(txCfg client.TxConfig, bidder Account, bid sdk.Coins, nonce uint64, numberMsgs int) (*auctiontypes.MsgAuctionBid, error) {
bidMsg := &auctiontypes.MsgAuctionBid{
Bidder: bidder.Address.String(),
Bid: bid,
Transactions: make([][]byte, numberMsgs),
}
for i := 0; i < numberMsgs; i++ {
txBuilder := txCfg.NewTxBuilder()
msgs := []sdk.Msg{
&banktypes.MsgSend{
FromAddress: bidder.Address.String(),
ToAddress: bidder.Address.String(),
},
}
if err := txBuilder.SetMsgs(msgs...); err != nil {
return nil, err
}
sigV2 := signing.SignatureV2{
PubKey: bidder.PrivKey.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: txCfg.SignModeHandler().DefaultMode(),
Signature: nil,
},
Sequence: nonce + uint64(i),
}
if err := txBuilder.SetSignatures(sigV2); err != nil {
return nil, err
}
bz, err := txCfg.TxEncoder()(txBuilder.GetTx())
if err != nil {
return nil, err
}
bidMsg.Transactions[i] = bz
}
return bidMsg, nil
}