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
+26 -25
View File
@@ -10,6 +10,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/skip-mev/pob/mempool"
testutils "github.com/skip-mev/pob/testutils"
"github.com/skip-mev/pob/x/auction/ante"
"github.com/skip-mev/pob/x/auction/keeper"
auctiontypes "github.com/skip-mev/pob/x/auction/types"
@@ -21,15 +22,15 @@ type AnteTestSuite struct {
ctx sdk.Context
// mempool setup
encodingConfig encodingConfig
encodingConfig testutils.EncodingConfig
random *rand.Rand
// auction setup
auctionKeeper keeper.Keeper
bankKeeper *MockBankKeeper
accountKeeper *MockAccountKeeper
distrKeeper *MockDistributionKeeper
stakingKeeper *MockStakingKeeper
bankKeeper *testutils.MockBankKeeper
accountKeeper *testutils.MockAccountKeeper
distrKeeper *testutils.MockDistributionKeeper
stakingKeeper *testutils.MockStakingKeeper
auctionDecorator ante.AuctionDecorator
key *storetypes.KVStoreKey
authorityAccount sdk.AccAddress
@@ -41,19 +42,19 @@ func TestAnteTestSuite(t *testing.T) {
func (suite *AnteTestSuite) SetupTest() {
// General config
suite.encodingConfig = createTestEncodingConfig()
suite.encodingConfig = testutils.CreateTestEncodingConfig()
suite.random = rand.New(rand.NewSource(time.Now().Unix()))
suite.key = sdk.NewKVStoreKey(auctiontypes.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), suite.key, sdk.NewTransientStoreKey("transient_test"))
suite.key = storetypes.NewKVStoreKey(auctiontypes.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), suite.key, storetypes.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx
// Keepers set up
ctrl := gomock.NewController(suite.T())
suite.accountKeeper = NewMockAccountKeeper(ctrl)
suite.accountKeeper = testutils.NewMockAccountKeeper(ctrl)
suite.accountKeeper.EXPECT().GetModuleAddress(auctiontypes.ModuleName).Return(sdk.AccAddress{}).AnyTimes()
suite.bankKeeper = NewMockBankKeeper(ctrl)
suite.distrKeeper = NewMockDistributionKeeper(ctrl)
suite.stakingKeeper = NewMockStakingKeeper(ctrl)
suite.bankKeeper = testutils.NewMockBankKeeper(ctrl)
suite.distrKeeper = testutils.NewMockDistributionKeeper(ctrl)
suite.stakingKeeper = testutils.NewMockStakingKeeper(ctrl)
suite.authorityAccount = sdk.AccAddress([]byte("authority"))
suite.auctionKeeper = keeper.NewKeeper(
suite.encodingConfig.Codec,
@@ -82,13 +83,13 @@ func (suite *AnteTestSuite) executeAnteHandler(tx sdk.Tx, balance sdk.Coins) (sd
func (suite *AnteTestSuite) TestAnteHandler() {
var (
// Bid set up
bidder = RandomAccounts(suite.random, 1)[0]
bidder = testutils.RandomAccounts(suite.random, 1)[0]
bid = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(1000)))
balance = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(10000)))
signers = []Account{bidder}
signers = []testutils.Account{bidder}
// Top bidding auction tx set up
topBidder = RandomAccounts(suite.random, 1)[0]
topBidder = testutils.RandomAccounts(suite.random, 1)[0]
topBid = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(100)))
insertTopBid = true
@@ -168,30 +169,30 @@ func (suite *AnteTestSuite) TestAnteHandler() {
insertTopBid = true
topBidder = bidder
topBid = bid
signers = []Account{}
signers = []testutils.Account{}
},
true,
},
{
"invalid frontrunning auction bid tx",
func() {
randomAccount := RandomAccounts(suite.random, 2)
randomAccount := testutils.RandomAccounts(suite.random, 2)
bidder := randomAccount[0]
otherUser := randomAccount[1]
insertTopBid = false
signers = []Account{bidder, otherUser}
signers = []testutils.Account{bidder, otherUser}
},
false,
},
{
"valid frontrunning auction bid tx",
func() {
randomAccount := RandomAccounts(suite.random, 2)
randomAccount := testutils.RandomAccounts(suite.random, 2)
bidder := randomAccount[0]
otherUser := randomAccount[1]
signers = []Account{bidder, otherUser}
signers = []testutils.Account{bidder, otherUser}
frontRunningProtection = false
},
true,
@@ -199,11 +200,11 @@ func (suite *AnteTestSuite) TestAnteHandler() {
{
"invalid sandwiching auction bid tx",
func() {
randomAccount := RandomAccounts(suite.random, 2)
randomAccount := testutils.RandomAccounts(suite.random, 2)
bidder := randomAccount[0]
otherUser := randomAccount[1]
signers = []Account{bidder, otherUser, bidder}
signers = []testutils.Account{bidder, otherUser, bidder}
frontRunningProtection = true
},
false,
@@ -211,7 +212,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
{
"invalid auction bid tx with many signers",
func() {
signers = RandomAccounts(suite.random, 10)
signers = testutils.RandomAccounts(suite.random, 10)
frontRunningProtection = true
},
false,
@@ -236,7 +237,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
// Insert the top bid into the mempool
mempool := mempool.NewAuctionMempool(suite.encodingConfig.TxConfig.TxDecoder(), 0)
if insertTopBid {
topAuctionTx, err := createAuctionTxWithSigners(suite.encodingConfig.TxConfig, topBidder, topBid, 0, []Account{})
topAuctionTx, err := testutils.CreateAuctionTxWithSigners(suite.encodingConfig.TxConfig, topBidder, topBid, 0, []testutils.Account{})
suite.Require().NoError(err)
suite.Require().Equal(0, mempool.CountTx())
suite.Require().Equal(0, mempool.CountAuctionTx())
@@ -246,7 +247,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
}
// Create the actual auction tx and insert into the mempool
auctionTx, err := createAuctionTxWithSigners(suite.encodingConfig.TxConfig, bidder, bid, 0, signers)
auctionTx, err := testutils.CreateAuctionTxWithSigners(suite.encodingConfig.TxConfig, bidder, bid, 0, signers)
suite.Require().NoError(err)
// Execute the ante handler
-144
View File
@@ -1,144 +0,0 @@
package ante_test
import (
"reflect"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/golang/mock/gomock"
)
type MockAccountKeeper struct {
ctrl *gomock.Controller
recorder *MockAccountKeeperMockRecorder
}
type MockAccountKeeperMockRecorder struct {
mock *MockAccountKeeper
}
func NewMockAccountKeeper(ctrl *gomock.Controller) *MockAccountKeeper {
mock := &MockAccountKeeper{ctrl: ctrl}
mock.recorder = &MockAccountKeeperMockRecorder{mock}
return mock
}
func (m *MockAccountKeeper) EXPECT() *MockAccountKeeperMockRecorder {
return m.recorder
}
func (m *MockAccountKeeper) GetModuleAddress(name string) sdk.AccAddress {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetModuleAddress", name)
ret0, _ := ret[0].(sdk.AccAddress)
return ret0
}
func (mr *MockAccountKeeperMockRecorder) GetModuleAddress(name interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAddress", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAddress), name)
}
type MockBankKeeper struct {
ctrl *gomock.Controller
recorder *MockBankKeeperMockRecorder
}
type MockBankKeeperMockRecorder struct {
mock *MockBankKeeper
}
func NewMockBankKeeper(ctrl *gomock.Controller) *MockBankKeeper {
mock := &MockBankKeeper{ctrl: ctrl}
mock.recorder = &MockBankKeeperMockRecorder{mock}
return mock
}
func (m *MockBankKeeper) EXPECT() *MockBankKeeperMockRecorder {
return m.recorder
}
func (m *MockBankKeeper) GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetAllBalances", ctx, addr)
ret0 := ret[0].(sdk.Coins)
return ret0
}
func (mr *MockBankKeeperMockRecorder) GetAllBalances(ctx, addr interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAllBalances", reflect.TypeOf((*MockBankKeeper)(nil).GetAllBalances), ctx, addr)
}
func (m *MockBankKeeper) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error {
m.ctrl.T.Helper()
m.ctrl.Call(m, "SendCoins", ctx, fromAddr, toAddr, amt)
return nil
}
func (mr *MockBankKeeperMockRecorder) SendCoins(ctx, fromAddr, toAddr, amt interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendCoins", reflect.TypeOf((*MockBankKeeper)(nil).SendCoins), ctx, fromAddr, toAddr, amt)
}
type MockDistributionKeeperRecorder struct {
mock *MockDistributionKeeper
}
type MockDistributionKeeper struct {
ctrl *gomock.Controller
recorder *MockDistributionKeeperRecorder
}
func NewMockDistributionKeeper(ctrl *gomock.Controller) *MockDistributionKeeper {
mock := &MockDistributionKeeper{ctrl: ctrl}
mock.recorder = &MockDistributionKeeperRecorder{mock}
return mock
}
func (m *MockDistributionKeeper) EXPECT() *MockDistributionKeeperRecorder {
return m.recorder
}
func (m *MockDistributionKeeper) GetPreviousProposerConsAddr(ctx sdk.Context) sdk.ConsAddress {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetPreviousProposerConsAddr", ctx)
ret0 := ret[0].(sdk.ConsAddress)
return ret0
}
func (mr *MockDistributionKeeperRecorder) GetPreviousProposerConsAddr(ctx any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPreviousProposerConsAddr", reflect.TypeOf((*MockDistributionKeeper)(nil).GetPreviousProposerConsAddr), ctx)
}
type MockStakingKeeperRecorder struct {
mock *MockStakingKeeper
}
type MockStakingKeeper struct {
ctrl *gomock.Controller
recorder *MockStakingKeeperRecorder
}
func NewMockStakingKeeper(ctrl *gomock.Controller) *MockStakingKeeper {
mock := &MockStakingKeeper{ctrl: ctrl}
mock.recorder = &MockStakingKeeperRecorder{mock}
return mock
}
func (m *MockStakingKeeper) EXPECT() *MockStakingKeeperRecorder {
return m.recorder
}
func (m *MockStakingKeeper) ValidatorByConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) stakingtypes.ValidatorI {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ValidatorByConsAddr", ctx, consAddr)
ret0 := ret[0].(stakingtypes.ValidatorI)
return ret0
}
func (mr *MockStakingKeeperRecorder) ValidatorByConsAddr(ctx, consAddr any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidatorByConsAddr", reflect.TypeOf((*MockStakingKeeper)(nil).ValidatorByConsAddr), ctx, consAddr)
}
-147
View File
@@ -1,147 +0,0 @@
package ante_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)
auctiontypes.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(acc sdk.AccAddress, numberMsgs int) []sdk.Msg {
msgs := make([]sdk.Msg, numberMsgs)
for i := 0; i < numberMsgs; i++ {
msgs[i] = &banktypes.MsgSend{
FromAddress: acc.String(),
ToAddress: acc.String(),
}
}
return msgs
}
func createAuctionTxWithSigners(txCfg client.TxConfig, bidder Account, bid sdk.Coins, nonce uint64, signers []Account) (authsigning.Tx, error) {
bidMsg := &auctiontypes.MsgAuctionBid{
Bidder: bidder.Address.String(),
Bid: bid,
Transactions: make([][]byte, len(signers)),
}
for i := 0; i < len(signers); i++ {
randomMsg := createRandomMsgs(signers[i].Address, 1)
randomTx, err := createTx(txCfg, signers[i], 0, randomMsg)
if err != nil {
return nil, err
}
bz, err := txCfg.TxEncoder()(randomTx)
if err != nil {
return nil, err
}
bidMsg.Transactions[i] = bz
}
txBuilder := txCfg.NewTxBuilder()
if err := txBuilder.SetMsgs(bidMsg); err != nil {
return nil, err
}
sigV2 := signing.SignatureV2{
PubKey: bidder.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
}
+35 -34
View File
@@ -5,6 +5,7 @@ import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
testutils "github.com/skip-mev/pob/testutils"
"github.com/skip-mev/pob/x/auction/keeper"
auctiontypes "github.com/skip-mev/pob/x/auction/types"
)
@@ -12,7 +13,7 @@ import (
func (suite *KeeperTestSuite) TestValidateAuctionMsg() {
var (
// Tx building variables
accounts = []Account{} // tracks the order of signers in the bundle
accounts = []testutils.Account{} // tracks the order of signers in the bundle
balance = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(10000)))
bid = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(1000)))
@@ -29,7 +30,7 @@ func (suite *KeeperTestSuite) TestValidateAuctionMsg() {
)
rnd := rand.New(rand.NewSource(time.Now().Unix()))
bidder := RandomAccounts(rnd, 1)[0]
bidder := testutils.RandomAccounts(rnd, 1)[0]
cases := []struct {
name string
@@ -65,60 +66,60 @@ func (suite *KeeperTestSuite) TestValidateAuctionMsg() {
// reset the balance and bid to their original values
bid = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(1000)))
balance = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(10000)))
accounts = RandomAccounts(rnd, int(maxBundleSize+1))
accounts = testutils.RandomAccounts(rnd, int(maxBundleSize+1))
},
false,
},
{
"frontrunning bundle",
func() {
randomAccount := RandomAccounts(rnd, 1)[0]
accounts = []Account{bidder, randomAccount}
randomAccount := testutils.RandomAccounts(rnd, 1)[0]
accounts = []testutils.Account{bidder, randomAccount}
},
false,
},
{
"sandwiching bundle",
func() {
randomAccount := RandomAccounts(rnd, 1)[0]
accounts = []Account{bidder, randomAccount, bidder}
randomAccount := testutils.RandomAccounts(rnd, 1)[0]
accounts = []testutils.Account{bidder, randomAccount, bidder}
},
false,
},
{
"valid bundle",
func() {
randomAccount := RandomAccounts(rnd, 1)[0]
accounts = []Account{randomAccount, randomAccount, bidder, bidder, bidder}
randomAccount := testutils.RandomAccounts(rnd, 1)[0]
accounts = []testutils.Account{randomAccount, randomAccount, bidder, bidder, bidder}
},
true,
},
{
"valid bundle with only bidder txs",
func() {
accounts = []Account{bidder, bidder, bidder, bidder}
accounts = []testutils.Account{bidder, bidder, bidder, bidder}
},
true,
},
{
"valid bundle with only random txs from single same user",
func() {
randomAccount := RandomAccounts(rnd, 1)[0]
accounts = []Account{randomAccount, randomAccount, randomAccount, randomAccount}
randomAccount := testutils.RandomAccounts(rnd, 1)[0]
accounts = []testutils.Account{randomAccount, randomAccount, randomAccount, randomAccount}
},
true,
},
{
"invalid bundle with random accounts",
func() {
accounts = RandomAccounts(rnd, 2)
accounts = testutils.RandomAccounts(rnd, 2)
},
false,
},
{
"disabled front-running protection",
func() {
accounts = RandomAccounts(rnd, 10)
accounts = testutils.RandomAccounts(rnd, 10)
frontRunningProtection = false
},
true,
@@ -126,7 +127,7 @@ func (suite *KeeperTestSuite) TestValidateAuctionMsg() {
{
"invalid bundle that does not outbid the highest bid",
func() {
accounts = []Account{bidder, bidder, bidder}
accounts = []testutils.Account{bidder, bidder, bidder}
highestBid = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(500)))
bid = sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(500)))
},
@@ -174,7 +175,7 @@ func (suite *KeeperTestSuite) TestValidateAuctionMsg() {
// Create the bundle of transactions ordered by accounts
bundle := make([]sdk.Tx, 0)
for _, acc := range accounts {
tx, err := createRandomTx(suite.encCfg.TxConfig, acc, 0, 1)
tx, err := testutils.CreateRandomTx(suite.encCfg.TxConfig, acc, 0, 1)
suite.Require().NoError(err)
bundle = append(bundle, tx)
}
@@ -192,10 +193,10 @@ func (suite *KeeperTestSuite) TestValidateAuctionMsg() {
func (suite *KeeperTestSuite) TestValidateBundle() {
// TODO: Update this to be multi-dimensional to test multi-sig
// https://github.com/skip-mev/pob/issues/14
var accounts []Account // tracks the order of signers in the bundle
var accounts []testutils.Account // tracks the order of signers in the bundle
rng := rand.New(rand.NewSource(time.Now().Unix()))
bidder := RandomAccounts(rng, 1)[0]
bidder := testutils.RandomAccounts(rng, 1)[0]
cases := []struct {
name string
@@ -205,69 +206,69 @@ func (suite *KeeperTestSuite) TestValidateBundle() {
{
"valid empty bundle",
func() {
accounts = make([]Account, 0)
accounts = make([]testutils.Account, 0)
},
true,
},
{
"valid single tx bundle",
func() {
accounts = []Account{bidder}
accounts = []testutils.Account{bidder}
},
true,
},
{
"valid multi-tx bundle by same account",
func() {
accounts = []Account{bidder, bidder, bidder, bidder}
accounts = []testutils.Account{bidder, bidder, bidder, bidder}
},
true,
},
{
"valid single-tx bundle by a different account",
func() {
randomAccount := RandomAccounts(rng, 1)[0]
accounts = []Account{randomAccount}
randomAccount := testutils.RandomAccounts(rng, 1)[0]
accounts = []testutils.Account{randomAccount}
},
true,
},
{
"valid multi-tx bundle by a different accounts",
func() {
randomAccount := RandomAccounts(rng, 1)[0]
accounts = []Account{randomAccount, bidder}
randomAccount := testutils.RandomAccounts(rng, 1)[0]
accounts = []testutils.Account{randomAccount, bidder}
},
true,
},
{
"invalid frontrunning bundle",
func() {
randomAccount := RandomAccounts(rng, 1)[0]
accounts = []Account{bidder, randomAccount}
randomAccount := testutils.RandomAccounts(rng, 1)[0]
accounts = []testutils.Account{bidder, randomAccount}
},
false,
},
{
"invalid sandwiching bundle",
func() {
randomAccount := RandomAccounts(rng, 1)[0]
accounts = []Account{bidder, randomAccount, bidder}
randomAccount := testutils.RandomAccounts(rng, 1)[0]
accounts = []testutils.Account{bidder, randomAccount, bidder}
},
false,
},
{
"invalid multi account bundle",
func() {
accounts = RandomAccounts(rng, 3)
accounts = testutils.RandomAccounts(rng, 3)
},
false,
},
{
"invalid multi account bundle without bidder",
func() {
randomAccount1 := RandomAccounts(rng, 1)[0]
randomAccount2 := RandomAccounts(rng, 1)[0]
accounts = []Account{randomAccount1, randomAccount2}
randomAccount1 := testutils.RandomAccounts(rng, 1)[0]
randomAccount2 := testutils.RandomAccounts(rng, 1)[0]
accounts = []testutils.Account{randomAccount1, randomAccount2}
},
false,
},
@@ -284,7 +285,7 @@ func (suite *KeeperTestSuite) TestValidateBundle() {
bundle := make([]sdk.Tx, 0)
for _, acc := range accounts {
// Create a random tx
tx, err := createRandomTx(suite.encCfg.TxConfig, acc, 0, 1)
tx, err := testutils.CreateRandomTx(suite.encCfg.TxConfig, acc, 0, 1)
suite.Require().NoError(err)
bundle = append(bundle, tx)
}
+13 -12
View File
@@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/skip-mev/pob/mempool"
testutils "github.com/skip-mev/pob/testutils"
"github.com/skip-mev/pob/x/auction/keeper"
"github.com/skip-mev/pob/x/auction/types"
@@ -18,11 +19,11 @@ type KeeperTestSuite struct {
suite.Suite
auctionKeeper keeper.Keeper
bankKeeper *MockBankKeeper
accountKeeper *MockAccountKeeper
distrKeeper *MockDistributionKeeper
stakingKeeper *MockStakingKeeper
encCfg encodingConfig
bankKeeper *testutils.MockBankKeeper
accountKeeper *testutils.MockAccountKeeper
distrKeeper *testutils.MockDistributionKeeper
stakingKeeper *testutils.MockStakingKeeper
encCfg testutils.EncodingConfig
ctx sdk.Context
msgServer types.MsgServer
key *storetypes.KVStoreKey
@@ -36,19 +37,19 @@ func TestKeeperTestSuite(t *testing.T) {
}
func (suite *KeeperTestSuite) SetupTest() {
suite.encCfg = createTestEncodingConfig()
suite.key = sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), suite.key, sdk.NewTransientStoreKey("transient_test"))
suite.encCfg = testutils.CreateTestEncodingConfig()
suite.key = storetypes.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), suite.key, storetypes.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx
ctrl := gomock.NewController(suite.T())
suite.accountKeeper = NewMockAccountKeeper(ctrl)
suite.accountKeeper = testutils.NewMockAccountKeeper(ctrl)
suite.accountKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(sdk.AccAddress{}).AnyTimes()
suite.bankKeeper = NewMockBankKeeper(ctrl)
suite.distrKeeper = NewMockDistributionKeeper(ctrl)
suite.stakingKeeper = NewMockStakingKeeper(ctrl)
suite.bankKeeper = testutils.NewMockBankKeeper(ctrl)
suite.distrKeeper = testutils.NewMockDistributionKeeper(ctrl)
suite.stakingKeeper = testutils.NewMockStakingKeeper(ctrl)
suite.authorityAccount = sdk.AccAddress([]byte("authority"))
suite.auctionKeeper = keeper.NewKeeper(
suite.encCfg.Codec,
-144
View File
@@ -1,144 +0,0 @@
package keeper_test
import (
"reflect"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/golang/mock/gomock"
)
type MockAccountKeeper struct {
ctrl *gomock.Controller
recorder *MockAccountKeeperMockRecorder
}
type MockAccountKeeperMockRecorder struct {
mock *MockAccountKeeper
}
func NewMockAccountKeeper(ctrl *gomock.Controller) *MockAccountKeeper {
mock := &MockAccountKeeper{ctrl: ctrl}
mock.recorder = &MockAccountKeeperMockRecorder{mock}
return mock
}
func (m *MockAccountKeeper) EXPECT() *MockAccountKeeperMockRecorder {
return m.recorder
}
func (m *MockAccountKeeper) GetModuleAddress(name string) sdk.AccAddress {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetModuleAddress", name)
ret0, _ := ret[0].(sdk.AccAddress)
return ret0
}
func (mr *MockAccountKeeperMockRecorder) GetModuleAddress(name interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAddress", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAddress), name)
}
type MockBankKeeper struct {
ctrl *gomock.Controller
recorder *MockBankKeeperMockRecorder
}
type MockBankKeeperMockRecorder struct {
mock *MockBankKeeper
}
func NewMockBankKeeper(ctrl *gomock.Controller) *MockBankKeeper {
mock := &MockBankKeeper{ctrl: ctrl}
mock.recorder = &MockBankKeeperMockRecorder{mock}
return mock
}
func (m *MockBankKeeper) EXPECT() *MockBankKeeperMockRecorder {
return m.recorder
}
func (m *MockBankKeeper) GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetAllBalances", ctx, addr)
ret0 := ret[0].(sdk.Coins)
return ret0
}
func (mr *MockBankKeeperMockRecorder) GetAllBalances(ctx, addr interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAllBalances", reflect.TypeOf((*MockBankKeeper)(nil).GetAllBalances), ctx, addr)
}
func (m *MockBankKeeper) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error {
m.ctrl.T.Helper()
m.ctrl.Call(m, "SendCoins", ctx, fromAddr, toAddr, amt)
return nil
}
func (mr *MockBankKeeperMockRecorder) SendCoins(ctx, fromAddr, toAddr, amt interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendCoins", reflect.TypeOf((*MockBankKeeper)(nil).SendCoins), ctx, fromAddr, toAddr, amt)
}
type MockDistributionKeeperRecorder struct {
mock *MockDistributionKeeper
}
type MockDistributionKeeper struct {
ctrl *gomock.Controller
recorder *MockDistributionKeeperRecorder
}
func NewMockDistributionKeeper(ctrl *gomock.Controller) *MockDistributionKeeper {
mock := &MockDistributionKeeper{ctrl: ctrl}
mock.recorder = &MockDistributionKeeperRecorder{mock}
return mock
}
func (m *MockDistributionKeeper) EXPECT() *MockDistributionKeeperRecorder {
return m.recorder
}
func (m *MockDistributionKeeper) GetPreviousProposerConsAddr(ctx sdk.Context) sdk.ConsAddress {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetPreviousProposerConsAddr", ctx)
ret0 := ret[0].(sdk.ConsAddress)
return ret0
}
func (mr *MockDistributionKeeperRecorder) GetPreviousProposerConsAddr(ctx any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPreviousProposerConsAddr", reflect.TypeOf((*MockDistributionKeeper)(nil).GetPreviousProposerConsAddr), ctx)
}
type MockStakingKeeperRecorder struct {
mock *MockStakingKeeper
}
type MockStakingKeeper struct {
ctrl *gomock.Controller
recorder *MockStakingKeeperRecorder
}
func NewMockStakingKeeper(ctrl *gomock.Controller) *MockStakingKeeper {
mock := &MockStakingKeeper{ctrl: ctrl}
mock.recorder = &MockStakingKeeperRecorder{mock}
return mock
}
func (m *MockStakingKeeper) EXPECT() *MockStakingKeeperRecorder {
return m.recorder
}
func (m *MockStakingKeeper) ValidatorByConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) stakingtypes.ValidatorI {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ValidatorByConsAddr", ctx, consAddr)
ret0 := ret[0].(stakingtypes.ValidatorI)
return ret0
}
func (mr *MockStakingKeeperRecorder) ValidatorByConsAddr(ctx, consAddr any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidatorByConsAddr", reflect.TypeOf((*MockStakingKeeper)(nil).ValidatorByConsAddr), ctx, consAddr)
}
+2 -1
View File
@@ -6,12 +6,13 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
testutils "github.com/skip-mev/pob/testutils"
"github.com/skip-mev/pob/x/auction/types"
)
func (suite *KeeperTestSuite) TestMsgAuctionBid() {
rng := rand.New(rand.NewSource(time.Now().Unix()))
accounts := RandomAccounts(rng, 4)
accounts := testutils.RandomAccounts(rng, 4)
bidder := accounts[0]
escrow := accounts[1]
-99
View File
@@ -1,99 +0,0 @@
package keeper_test
import (
"math/rand"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "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"
)
type encodingConfig struct {
InterfaceRegistry codectypes.InterfaceRegistry
Codec codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
}
func createTestEncodingConfig() encodingConfig {
cdc := codec.NewLegacyAmino()
interfaceRegistry := codectypes.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 createRandomTx(txCfg client.TxConfig, account Account, nonce, numberMsgs uint64) (authsigning.Tx, error) {
msgs := make([]sdk.Msg, numberMsgs)
for i := 0; i < int(numberMsgs); i++ {
msgs[i] = &banktypes.MsgSend{
FromAddress: account.Address.String(),
ToAddress: account.Address.String(),
}
}
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
}