test: use chaintestutil (#296)
* rename * fix * format * workflow * add basic test * add targetss * use commit * workflow * basic setup * add keeper * fix * fix? * update * update * update * fix * fix * go work inti
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
// Package keeper provides methods to initialize SDK keepers with local storage for test purposes
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
|
||||
"cosmossdk.io/log"
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
|
||||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
||||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||
testkeeper "github.com/skip-mev/chaintestutil/keeper"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/skip-mev/block-sdk/lanes/base"
|
||||
"github.com/skip-mev/block-sdk/lanes/free"
|
||||
"github.com/skip-mev/block-sdk/lanes/mev"
|
||||
auctionkeeper "github.com/skip-mev/block-sdk/x/auction/keeper"
|
||||
auctiontypes "github.com/skip-mev/block-sdk/x/auction/types"
|
||||
blocksdkkeeper "github.com/skip-mev/block-sdk/x/blocksdk/keeper"
|
||||
blocksdktypes "github.com/skip-mev/block-sdk/x/blocksdk/types"
|
||||
)
|
||||
|
||||
// TestKeepers holds all keepers used during keeper tests for all modules
|
||||
type TestKeepers struct {
|
||||
testkeeper.TestKeepers
|
||||
AuctionKeeper auctionkeeper.Keeper
|
||||
BlockSDKKeeper blocksdkkeeper.Keeper
|
||||
}
|
||||
|
||||
// TestMsgServers holds all message servers used during keeper tests for all modules
|
||||
type TestMsgServers struct {
|
||||
testkeeper.TestMsgServers
|
||||
AuctionMsgServer auctiontypes.MsgServer
|
||||
}
|
||||
|
||||
var additionalMaccPerms = map[string][]string{
|
||||
auctiontypes.ModuleName: nil,
|
||||
}
|
||||
|
||||
// NewTestSetup returns initialized instances of all the keepers and message servers of the modules
|
||||
func NewTestSetup(t testing.TB, options ...testkeeper.SetupOption) (sdk.Context, TestKeepers, TestMsgServers) {
|
||||
options = append(options, testkeeper.WithAdditionalModuleAccounts(additionalMaccPerms))
|
||||
|
||||
_, tk, tms := testkeeper.NewTestSetup(t, options...)
|
||||
|
||||
// initialize extra keeper
|
||||
auctionKeeper := Auction(tk.Initializer, tk.AccountKeeper, tk.BankKeeper, tk.DistrKeeper, tk.StakingKeeper)
|
||||
blocksdkKeeper := BlockSDK(tk.Initializer)
|
||||
require.NoError(t, tk.Initializer.LoadLatest())
|
||||
|
||||
// initialize msg servers
|
||||
auctionMsgSrv := auctionkeeper.NewMsgServerImpl(auctionKeeper)
|
||||
|
||||
ctx := sdk.NewContext(tk.Initializer.StateStore, tmproto.Header{
|
||||
Time: testkeeper.ExampleTimestamp,
|
||||
Height: testkeeper.ExampleHeight,
|
||||
}, false, log.NewNopLogger())
|
||||
|
||||
err := auctionKeeper.SetParams(ctx, auctiontypes.DefaultParams())
|
||||
require.NoError(t, err)
|
||||
|
||||
err = blocksdkKeeper.AddLane(ctx, blocksdktypes.Lane{
|
||||
Id: mev.LaneName,
|
||||
MaxBlockSpace: math.LegacyMustNewDecFromStr("0.2"),
|
||||
Order: 0,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = blocksdkKeeper.AddLane(ctx, blocksdktypes.Lane{
|
||||
Id: free.LaneName,
|
||||
MaxBlockSpace: math.LegacyMustNewDecFromStr("0.2"),
|
||||
Order: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = blocksdkKeeper.AddLane(ctx, blocksdktypes.Lane{
|
||||
Id: base.LaneName,
|
||||
MaxBlockSpace: math.LegacyMustNewDecFromStr("0.6"),
|
||||
Order: 2,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
testKeepers := TestKeepers{
|
||||
TestKeepers: tk,
|
||||
AuctionKeeper: auctionKeeper,
|
||||
BlockSDKKeeper: blocksdkKeeper,
|
||||
}
|
||||
|
||||
testMsgServers := TestMsgServers{
|
||||
TestMsgServers: tms,
|
||||
AuctionMsgServer: auctionMsgSrv,
|
||||
}
|
||||
|
||||
return ctx, testKeepers, testMsgServers
|
||||
}
|
||||
|
||||
// Auction initializes the auction module using the testkeepers intializer.
|
||||
func Auction(
|
||||
initializer *testkeeper.Initializer,
|
||||
authKeeper authkeeper.AccountKeeper,
|
||||
bankKeeper bankkeeper.Keeper,
|
||||
distrKeeper distrkeeper.Keeper,
|
||||
stakingKeeper *stakingkeeper.Keeper,
|
||||
) auctionkeeper.Keeper {
|
||||
storeKey := storetypes.NewKVStoreKey(auctiontypes.StoreKey)
|
||||
initializer.StateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, initializer.DB)
|
||||
|
||||
return auctionkeeper.NewKeeper(
|
||||
initializer.Codec,
|
||||
storeKey,
|
||||
authKeeper,
|
||||
bankKeeper,
|
||||
distrKeeper,
|
||||
stakingKeeper,
|
||||
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
|
||||
)
|
||||
}
|
||||
|
||||
// BlockSDK initializes the blocksdk module using the testkeepers intializer.
|
||||
func BlockSDK(
|
||||
initializer *testkeeper.Initializer,
|
||||
) blocksdkkeeper.Keeper {
|
||||
storeKey := storetypes.NewKVStoreKey(blocksdktypes.StoreKey)
|
||||
initializer.StateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, initializer.DB)
|
||||
|
||||
return blocksdkkeeper.NewKeeper(
|
||||
initializer.Codec,
|
||||
storeKey,
|
||||
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// Package networksuite provides a base test suite for tests that need a local network instance
|
||||
package networksuite
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
"github.com/skip-mev/chaintestutil/network"
|
||||
"github.com/skip-mev/chaintestutil/sample"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"github.com/skip-mev/block-sdk/lanes/base"
|
||||
"github.com/skip-mev/block-sdk/lanes/free"
|
||||
"github.com/skip-mev/block-sdk/lanes/mev"
|
||||
"github.com/skip-mev/block-sdk/tests/app"
|
||||
auctiontypes "github.com/skip-mev/block-sdk/x/auction/types"
|
||||
blocksdktypes "github.com/skip-mev/block-sdk/x/blocksdk/types"
|
||||
)
|
||||
|
||||
// NetworkTestSuite is a test suite for query tests that initializes a network instance.
|
||||
type NetworkTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
Network *network.Network
|
||||
AuctionState auctiontypes.GenesisState
|
||||
BlockSDKState blocksdktypes.GenesisState
|
||||
}
|
||||
|
||||
// SetupSuite setups the local network with a genesis state.
|
||||
func (nts *NetworkTestSuite) SetupSuite() {
|
||||
var (
|
||||
r = sample.Rand()
|
||||
cfg = network.NewConfig(app.AppConfig)
|
||||
)
|
||||
|
||||
updateGenesisConfigState := func(moduleName string, moduleState proto.Message) {
|
||||
buf, err := cfg.Codec.MarshalJSON(moduleState)
|
||||
require.NoError(nts.T(), err)
|
||||
cfg.GenesisState[moduleName] = buf
|
||||
}
|
||||
|
||||
// initialize genesis
|
||||
require.NoError(nts.T(), cfg.Codec.UnmarshalJSON(cfg.GenesisState[auctiontypes.ModuleName], &nts.AuctionState))
|
||||
nts.AuctionState = populateAuction(r, nts.AuctionState)
|
||||
updateGenesisConfigState(auctiontypes.ModuleName, &nts.AuctionState)
|
||||
|
||||
nts.BlockSDKState = populateBlockSDK(r, nts.BlockSDKState)
|
||||
updateGenesisConfigState(blocksdktypes.ModuleName, &nts.BlockSDKState)
|
||||
|
||||
nts.Network = network.New(nts.T(), cfg)
|
||||
}
|
||||
|
||||
func populateAuction(_ *rand.Rand, auctionState auctiontypes.GenesisState) auctiontypes.GenesisState {
|
||||
// TODO intercept and populate state randomly if desired
|
||||
return auctionState
|
||||
}
|
||||
|
||||
func populateBlockSDK(_ *rand.Rand, bsdkState blocksdktypes.GenesisState) blocksdktypes.GenesisState {
|
||||
bsdkState.Lanes = blocksdktypes.Lanes{
|
||||
{
|
||||
Id: mev.LaneName,
|
||||
MaxBlockSpace: math.LegacyMustNewDecFromStr("0.2"),
|
||||
Order: 0,
|
||||
},
|
||||
{
|
||||
Id: free.LaneName,
|
||||
MaxBlockSpace: math.LegacyMustNewDecFromStr("0.2"),
|
||||
Order: 1,
|
||||
},
|
||||
{
|
||||
Id: base.LaneName,
|
||||
MaxBlockSpace: math.LegacyMustNewDecFromStr("0.6"),
|
||||
Order: 2,
|
||||
},
|
||||
}
|
||||
|
||||
return bsdkState
|
||||
}
|
||||
Reference in New Issue
Block a user