Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add tests for module epochs #351

Merged
merged 7 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions x/epochs/genesis_test.go

This file was deleted.

26 changes: 26 additions & 0 deletions x/epochs/keeper/epoch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package keeper

import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand All @@ -20,6 +23,29 @@ func (k Keeper) GetEpochInfo(ctx sdk.Context, identifier string) (types.EpochInf
return epoch, true
}

// AddEpochInfo adds a new epoch info. Will return an error if the epoch fails validation,
// or re-uses an existing identifier.
// This method also sets the start time if left unset, and sets the epoch start height.
func (k Keeper) AddEpochInfo(ctx sdk.Context, epoch types.EpochInfo) error {
mtsitrin marked this conversation as resolved.
Show resolved Hide resolved
err := epoch.Validate()
if err != nil {
return err
}
// Check if identifier already exists
_, found := k.GetEpochInfo(ctx, epoch.Identifier)
if !found {
return fmt.Errorf("epoch with identifier %s already exists", epoch.Identifier)
}

// Initialize empty and default epoch values
if epoch.StartTime.Equal(time.Time{}) {
epoch.StartTime = ctx.BlockTime()
}
epoch.CurrentEpochStartHeight = ctx.BlockHeight()
k.SetEpochInfo(ctx, epoch)
return nil
}

// SetEpochInfo set epoch info
func (k Keeper) SetEpochInfo(ctx sdk.Context, epoch types.EpochInfo) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpoch)
Expand Down
13 changes: 6 additions & 7 deletions x/epochs/genesis.go → x/epochs/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package epochs
package keeper

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/dymensionxyz/dymension-rdk/x/epochs/keeper"
"github.com/dymensionxyz/dymension-rdk/x/epochs/types"
)

// InitGenesis initializes the capability module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
// set epoch info from genesis
for _, epoch := range genState.Epochs {
// Initialize empty epoch values via Cosmos SDK
Expand All @@ -34,8 +33,8 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
}

// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
return &types.GenesisState{
Epochs: k.AllEpochInfos(ctx),
}
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Epochs = k.AllEpochInfos(ctx)
return genesis
}
24 changes: 24 additions & 0 deletions x/epochs/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package keeper_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/dymensionxyz/dymension-rdk/testutil/nullify"
"github.com/dymensionxyz/dymension-rdk/x/epochs/types"
)

// TODO: add specific test and scenario
func TestEpochsInitAndExportGenesis(t *testing.T) {
genesisState := types.GenesisState{}
ctx, epochsKeeper := Setup(t)

epochsKeeper.InitGenesis(ctx, genesisState)
genesis := epochsKeeper.ExportGenesis(ctx)
require.NotNil(t, genesis)
require.Len(t, genesis.Epochs, 5)
nullify.Fill(&genesis)
nullify.Fill(genesis)

}
11 changes: 11 additions & 0 deletions x/epochs/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ import (

var _ types.QueryServer = Keeper{}

// Querier defines a wrapper around the x/epochs keeper providing gRPC method
// handlers.
type Querier struct {
Keeper
}

// NewQuerier initializes new querier.
func NewQuerier(k Keeper) Querier {
return Querier{Keeper: k}
}

// EpochInfos provide running epochInfos
func (k Keeper) EpochInfos(c context.Context, req *types.QueryEpochsInfoRequest) (*types.QueryEpochsInfoResponse, error) {
if req == nil {
Expand Down
36 changes: 36 additions & 0 deletions x/epochs/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package keeper_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/dymension-rdk/testutil/utils"
"github.com/stretchr/testify/suite"

testkeepers "github.com/dymensionxyz/dymension-rdk/testutil/keepers"
epochskeeper "github.com/dymensionxyz/dymension-rdk/x/epochs/keeper"
"github.com/dymensionxyz/dymension-rdk/x/epochs/types"
)

type KeeperTestSuite struct {
suite.Suite
Ctx sdk.Context
EpochsKeeper *epochskeeper.Keeper
queryClient types.QueryClient
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func Setup(t *testing.T) (sdk sdk.Context, k *epochskeeper.Keeper) {
app := utils.Setup(t, false)
k, ctx := testkeepers.NewTestEpochKeeperFromApp(app)
return ctx, k
}

func SetupTestWithHooks(t *testing.T, hooks *types.MultiEpochHooks) (sdk.Context, *epochskeeper.Keeper) {
ctx, k := Setup(t)
k.SetHooks(hooks)
return ctx, k
}
5 changes: 3 additions & 2 deletions x/epochs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.Ra
// Initialize global index to index in genesis state
cdc.MustUnmarshalJSON(gs, &genState)

InitGenesis(ctx, am.keeper, genState)
am.keeper.InitGenesis(ctx, genState)

return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the capability module's exported genesis state as raw JSON bytes.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
genState := ExportGenesis(ctx, am.keeper)
genState := am.keeper.ExportGenesis(ctx)
return cdc.MustMarshalJSON(genState)
}

Expand Down
17 changes: 17 additions & 0 deletions x/epochs/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,20 @@ func (gs GenesisState) Validate() error {
}
return nil
}

// Validate also validates epoch info.
func (epoch EpochInfo) Validate() error {
hoangdv2429 marked this conversation as resolved.
Show resolved Hide resolved
if epoch.Identifier == "" {
return errors.New("epoch identifier should NOT be empty")
}
if epoch.Duration == 0 {
return errors.New("epoch duration should NOT be 0")
}
if epoch.CurrentEpoch < 0 {
return errors.New("epoch CurrentEpoch must be non-negative")
}
if epoch.CurrentEpochStartHeight < 0 {
return errors.New("epoch CurrentEpochStartHeight must be non-negative")
}
return nil
}
1 change: 1 addition & 0 deletions x/mint/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// BeforeEpochStart is a hook which is executed before the start of an epoch. It is a no-op for mint module.
func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochInfo epochstypes.EpochInfo) {
}

Expand Down
Loading