2022-12-09 21:24:35 +00:00
|
|
|
package community_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-09-22 16:05:12 +00:00
|
|
|
"time"
|
2022-12-09 21:24:35 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2023-09-22 16:05:12 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2022-12-09 21:24:35 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2023-09-22 16:05:12 +00:00
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/community"
|
|
|
|
"github.com/0glabs/0g-chain/x/community/testutil"
|
|
|
|
"github.com/0glabs/0g-chain/x/community/types"
|
2022-12-09 21:24:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type genesisTestSuite struct {
|
|
|
|
testutil.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *genesisTestSuite) SetupTest() {
|
|
|
|
suite.Suite.SetupTest()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenesisTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(genesisTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *genesisTestSuite) TestInitGenesis() {
|
|
|
|
|
|
|
|
accountKeeper := suite.App.GetAccountKeeper()
|
|
|
|
|
2023-09-22 16:05:12 +00:00
|
|
|
genesisState := types.NewGenesisState(
|
|
|
|
types.NewParams(
|
|
|
|
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
|
2023-09-26 17:08:26 +00:00
|
|
|
sdkmath.LegacyNewDec(1000),
|
2023-10-03 15:41:54 +00:00
|
|
|
sdkmath.LegacyNewDec(1000),
|
|
|
|
),
|
|
|
|
types.NewStakingRewardsState(
|
|
|
|
time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
sdkmath.LegacyMustNewDecFromStr("0.100000000000000000"),
|
2023-09-22 16:05:12 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-12-09 21:24:35 +00:00
|
|
|
suite.NotPanics(func() {
|
2023-09-22 16:05:12 +00:00
|
|
|
community.InitGenesis(suite.Ctx, suite.Keeper, accountKeeper, genesisState)
|
2022-12-09 21:24:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// check for module account this way b/c GetModuleAccount creates if not existing.
|
2022-12-13 00:38:27 +00:00
|
|
|
acc := accountKeeper.GetAccount(suite.Ctx, suite.MaccAddress)
|
2022-12-09 21:24:35 +00:00
|
|
|
suite.NotNil(acc)
|
|
|
|
_, ok := acc.(authtypes.ModuleAccountI)
|
|
|
|
suite.True(ok)
|
2023-09-22 16:05:12 +00:00
|
|
|
|
2023-10-03 15:41:54 +00:00
|
|
|
keeper := suite.App.GetCommunityKeeper()
|
|
|
|
storedParams, found := keeper.GetParams(suite.Ctx)
|
2023-09-22 16:05:12 +00:00
|
|
|
suite.True(found)
|
|
|
|
suite.Equal(genesisState.Params, storedParams)
|
2023-10-03 15:41:54 +00:00
|
|
|
|
|
|
|
stakingRewardsState := keeper.GetStakingRewardsState(suite.Ctx)
|
|
|
|
suite.Equal(genesisState.StakingRewardsState, stakingRewardsState)
|
2023-09-22 16:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *genesisTestSuite) TestExportGenesis() {
|
|
|
|
params := types.NewParams(
|
|
|
|
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
|
2023-09-26 17:08:26 +00:00
|
|
|
sdkmath.LegacyNewDec(1000),
|
2023-10-03 15:41:54 +00:00
|
|
|
sdkmath.LegacyNewDec(1000),
|
2023-09-22 16:05:12 +00:00
|
|
|
)
|
|
|
|
suite.Keeper.SetParams(suite.Ctx, params)
|
|
|
|
|
2023-10-03 15:41:54 +00:00
|
|
|
stakingRewardsState := types.NewStakingRewardsState(
|
|
|
|
time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
sdkmath.LegacyMustNewDecFromStr("0.100000000000000000"),
|
|
|
|
)
|
|
|
|
suite.Keeper.SetStakingRewardsState(suite.Ctx, stakingRewardsState)
|
|
|
|
|
2023-09-22 16:05:12 +00:00
|
|
|
genesisState := community.ExportGenesis(suite.Ctx, suite.Keeper)
|
|
|
|
|
|
|
|
suite.Equal(params, genesisState.Params)
|
2023-10-03 15:41:54 +00:00
|
|
|
suite.Equal(stakingRewardsState, genesisState.StakingRewardsState)
|
2023-09-22 16:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *genesisTestSuite) TestInitExportIsLossless() {
|
|
|
|
genesisState := types.NewGenesisState(
|
|
|
|
types.NewParams(
|
|
|
|
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
|
2023-09-26 17:08:26 +00:00
|
|
|
sdkmath.LegacyNewDec(1000),
|
2023-10-03 15:41:54 +00:00
|
|
|
sdkmath.LegacyNewDec(1000),
|
|
|
|
),
|
|
|
|
types.NewStakingRewardsState(
|
|
|
|
time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
sdkmath.LegacyMustNewDecFromStr("0.100000000000000000"),
|
2023-09-22 16:05:12 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
community.InitGenesis(suite.Ctx, suite.Keeper, suite.App.GetAccountKeeper(), genesisState)
|
|
|
|
exportedState := community.ExportGenesis(suite.Ctx, suite.Keeper)
|
|
|
|
|
|
|
|
suite.Equal(genesisState, exportedState)
|
2022-12-09 21:24:35 +00:00
|
|
|
}
|