0g-chain/x/auction/genesis_test.go

160 lines
4.8 KiB
Go
Raw Normal View History

package auction_test
import (
"sort"
"testing"
"time"
"github.com/stretchr/testify/require"
sdkmath "cosmossdk.io/math"
Update cosmos-sdk to v0.47.7 (#1811) * Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2024-02-06 22:54:10 +00:00
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
2020-05-11 19:45:00 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
2020-05-11 19:45:00 +00:00
2024-08-03 09:51:56 +00:00
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/x/auction"
"github.com/0glabs/0g-chain/x/auction/types"
)
var (
_, testAddrs = app.GeneratePrivKeyAddressPairs(2)
testTime = time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC)
testAuction = types.NewCollateralAuction(
"seller",
c("lotdenom", 10),
testTime,
c("biddenom", 1000),
types.WeightedAddresses{Addresses: testAddrs, Weights: []sdkmath.Int{sdk.OneInt(), sdk.OneInt()}},
c("debt", 1000),
).WithID(3).(types.GenesisAuction)
)
func TestInitGenesis(t *testing.T) {
t.Run("valid", func(t *testing.T) {
// setup keepers
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
// setup module account
modBaseAcc := authtypes.NewBaseAccount(authtypes.NewModuleAddress(types.ModuleName), nil, 0, 0)
modAcc := authtypes.NewModuleAccount(modBaseAcc, types.ModuleName, []string{authtypes.Minter, authtypes.Burner}...)
tApp.GetAccountKeeper().SetModuleAccount(ctx, modAcc)
tApp.GetBankKeeper().MintCoins(ctx, types.ModuleName, testAuction.GetModuleAccountCoins())
// set up auction genesis state with module account
auctionGS, err := types.NewGenesisState(
10,
types.DefaultParams(),
[]types.GenesisAuction{testAuction},
)
require.NoError(t, err)
// run init
keeper := tApp.GetAuctionKeeper()
require.NotPanics(t, func() {
auction.InitGenesis(ctx, keeper, tApp.GetBankKeeper(), tApp.GetAccountKeeper(), auctionGS)
})
// check state is as expected
actualID, err := keeper.GetNextAuctionID(ctx)
require.NoError(t, err)
require.Equal(t, auctionGS.NextAuctionId, actualID)
require.Equal(t, auctionGS.Params, keeper.GetParams(ctx))
genesisAuctions, err := types.UnpackGenesisAuctions(auctionGS.Auctions)
if err != nil {
panic(err)
}
sort.Slice(genesisAuctions, func(i, j int) bool {
return genesisAuctions[i].GetID() > genesisAuctions[j].GetID()
})
i := 0
keeper.IterateAuctions(ctx, func(a types.Auction) bool {
require.Equal(t, genesisAuctions[i], a)
i++
return false
})
})
t.Run("invalid (invalid nextAuctionID)", func(t *testing.T) {
// setup keepers
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
// setup module account
modBaseAcc := authtypes.NewBaseAccount(authtypes.NewModuleAddress(types.ModuleName), nil, 0, 0)
modAcc := authtypes.NewModuleAccount(modBaseAcc, types.ModuleName, []string{authtypes.Minter, authtypes.Burner}...)
tApp.GetAccountKeeper().SetModuleAccount(ctx, modAcc)
tApp.GetBankKeeper().MintCoins(ctx, types.ModuleName, testAuction.GetModuleAccountCoins())
// create invalid genesis
auctionGS, err := types.NewGenesisState(
0, // next id < testAuction ID
types.DefaultParams(),
[]types.GenesisAuction{testAuction},
)
require.NoError(t, err)
// check init fails
require.Panics(t, func() {
auction.InitGenesis(ctx, tApp.GetAuctionKeeper(), tApp.GetBankKeeper(), tApp.GetAccountKeeper(), auctionGS)
})
})
t.Run("invalid (missing mod account coins)", func(t *testing.T) {
// setup keepers
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
// invalid as there is no module account setup
// create invalid genesis
auctionGS, err := types.NewGenesisState(
10,
types.DefaultParams(),
[]types.GenesisAuction{testAuction},
)
require.NoError(t, err)
// check init fails
require.Panics(t, func() {
auction.InitGenesis(ctx, tApp.GetAuctionKeeper(), tApp.GetBankKeeper(), tApp.GetAccountKeeper(), auctionGS)
})
})
}
func TestExportGenesis(t *testing.T) {
t.Run("default", func(t *testing.T) {
// setup state
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
tApp.InitializeFromGenesisStates()
// export
gs := auction.ExportGenesis(ctx, tApp.GetAuctionKeeper())
// check state matches
defaultGS := types.DefaultGenesisState()
require.Equal(t, defaultGS, gs)
})
t.Run("one auction", func(t *testing.T) {
// setup state
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
tApp.InitializeFromGenesisStates()
tApp.GetAuctionKeeper().SetAuction(ctx, testAuction)
// export
gs := auction.ExportGenesis(ctx, tApp.GetAuctionKeeper())
// check state matches
expectedGenesisState := types.DefaultGenesisState()
packedGenesisAuctions, err := types.PackGenesisAuctions([]types.GenesisAuction{testAuction})
require.NoError(t, err)
expectedGenesisState.Auctions = append(expectedGenesisState.Auctions, packedGenesisAuctions...)
require.Equal(t, expectedGenesisState, gs)
})
}