Skip to content

Commit

Permalink
fix: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt committed Jul 22, 2023
1 parent c36f801 commit a0b4a96
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 39 deletions.
4 changes: 0 additions & 4 deletions x/epochs/abci.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package epochs

import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
Expand All @@ -22,20 +21,17 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
if !shouldEpochStart(epochInfo, ctx) {
return false
}
logger := k.Logger(ctx)

epochInfo.CurrentEpochStartHeight = ctx.BlockHeight()
epochInfo.CurrentEpochStartTime = ctx.BlockTime()

if !epochInfo.EpochCountingStarted {
epochInfo.EpochCountingStarted = true
epochInfo.CurrentEpoch = 1
logger.Info(fmt.Sprintf("Starting new epoch with identifier %s epoch number %d", epochInfo.Identifier, epochInfo.CurrentEpoch))
} else {
_ = ctx.EventManager().EmitTypedEvent(&types.EventEpochEnd{EpochNumber: epochInfo.CurrentEpoch})
k.AfterEpochEnd(ctx, epochInfo.Identifier, epochInfo.CurrentEpoch)
epochInfo.CurrentEpoch += 1
logger.Info(fmt.Sprintf("Starting epoch with identifier %s epoch number %d", epochInfo.Identifier, epochInfo.CurrentEpoch))
}

// emit new epoch start event, set epoch info, and run BeforeEpochStart hook
Expand Down
41 changes: 35 additions & 6 deletions x/epochs/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestEpochInfoChangesBeginBlockerAndInitGenesis(t *testing.T) {

// insert epoch info that's already begun
ctx = ctx.WithBlockHeight(1).WithBlockTime(now)
_ = app.EpochsKeeper.AddEpochInfo(ctx, types.EpochInfo{
err := app.EpochsKeeper.AddEpochInfo(ctx, types.EpochInfo{
Identifier: "monthly",
StartTime: now,
Duration: time.Hour * 24 * 31,
Expand All @@ -119,13 +119,38 @@ func TestEpochInfoChangesBeginBlockerAndInitGenesis(t *testing.T) {
CurrentEpochStartTime: now,
EpochCountingStarted: true,
})
assert.NoError(t, err)

err = app.EpochsKeeper.AddEpochInfo(ctx, types.EpochInfo{
Identifier: "monthly",
StartTime: now,
Duration: time.Hour * 24 * 31,
CurrentEpoch: 1,
CurrentEpochStartHeight: 1,
CurrentEpochStartTime: now,
EpochCountingStarted: true,
})
assert.Error(t, err)

tc.when()

epochInfo := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
epochInfo, err := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
assert.NoError(t, err)
assert.Equal(t, tc.expectedEpochInfo.CurrentEpoch, epochInfo.CurrentEpoch)
assert.Equal(t, tc.expectedEpochInfo.CurrentEpochStartTime, epochInfo.CurrentEpochStartTime)
assert.Equal(t, tc.expectedEpochInfo.CurrentEpochStartHeight, epochInfo.CurrentEpochStartHeight)

// insert epoch info that's already begun
err = app.EpochsKeeper.DeleteEpochInfo(ctx, "monthly")
assert.NoError(t, err)

err = app.EpochsKeeper.DeleteEpochInfo(ctx, "monthly")
assert.Error(t, err)

tc.when()

epochInfo, err = app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
assert.Error(t, err)
})
}
}
Expand Down Expand Up @@ -161,7 +186,8 @@ func TestEpochStartingOneMonthAfterInitGenesis(t *testing.T) {
})

// epoch not started yet
epochInfo := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
epochInfo, err := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
assert.NoError(t, err)
require.Equal(t, epochInfo.CurrentEpoch, uint64(0))
require.Equal(t, epochInfo.CurrentEpochStartHeight, initialBlockHeight)
require.Equal(t, epochInfo.CurrentEpochStartTime, time.Time{})
Expand All @@ -172,7 +198,8 @@ func TestEpochStartingOneMonthAfterInitGenesis(t *testing.T) {
epochs.BeginBlocker(ctx, app.EpochsKeeper)

// epoch not started yet
epochInfo = app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
epochInfo, err = app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
assert.NoError(t, err)
require.Equal(t, epochInfo.CurrentEpoch, uint64(0))
require.Equal(t, epochInfo.CurrentEpochStartHeight, initialBlockHeight)
require.Equal(t, epochInfo.CurrentEpochStartTime, time.Time{})
Expand All @@ -183,7 +210,8 @@ func TestEpochStartingOneMonthAfterInitGenesis(t *testing.T) {
epochs.BeginBlocker(ctx, app.EpochsKeeper)

// epoch started
epochInfo = app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
epochInfo, err = app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
assert.NoError(t, err)
require.Equal(t, epochInfo.CurrentEpoch, uint64(1))
require.Equal(t, epochInfo.CurrentEpochStartHeight, ctx.BlockHeight())
require.Equal(t, epochInfo.CurrentEpochStartTime.UTC().String(), now.Add(month).UTC().String())
Expand Down Expand Up @@ -225,7 +253,8 @@ func TestLegacyEpochSerialization(t *testing.T) {
// Increment epoch
ctx = ctx.WithBlockHeight(3).WithBlockTime(now.Add(time.Hour * 24 * 32))
epochs.BeginBlocker(ctx, app.EpochsKeeper)
epochInfo := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
epochInfo, err := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
assert.NoError(t, err)

require.NotEqual(t, epochInfo.CurrentEpochStartHeight, int64(0))
}
3 changes: 2 additions & 1 deletion x/epochs/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func TestEpochsInitGenesis(t *testing.T) {
}

epochs.InitGenesis(ctx, app.EpochsKeeper, genesisState)
epochInfo := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
epochInfo, err := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
require.NoError(t, err)
require.Equal(t, epochInfo.Identifier, "monthly")
require.Equal(t, epochInfo.StartTime.UTC().String(), now.UTC().String())
require.Equal(t, epochInfo.Duration, time.Hour*24)
Expand Down
5 changes: 4 additions & 1 deletion x/epochs/integration/action/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ type startEpoch struct {
}

func (s startEpoch) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
epochInfo := app.EpochsKeeper.GetEpochInfo(ctx, s.epochIdentifier)
epochInfo, err := app.EpochsKeeper.GetEpochInfo(ctx, s.epochIdentifier)
if err != nil {
return ctx, err, false
}
epochInfo.EpochCountingStarted = true
epochInfo.CurrentEpoch = 1
epochInfo.CurrentEpochStartHeight = ctx.BlockHeight()
Expand Down
19 changes: 8 additions & 11 deletions x/epochs/keeper/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ import (
)

// GetEpochInfo returns epoch info by identifier.
func (k Keeper) GetEpochInfo(ctx sdk.Context, identifier string) types.EpochInfo {
epoch, err := k.Epochs.Get(ctx, identifier)
func (k Keeper) GetEpochInfo(ctx sdk.Context, identifier string) (epoch types.EpochInfo, err error) {
epoch, err = k.Epochs.Get(ctx, identifier)
if err != nil {
panic(err)
err = fmt.Errorf("epoch with identifier %s not found", identifier)
return
}

return epoch
return
}

// EpochExists checks if the epoch exists
func (k Keeper) EpochExists(ctx sdk.Context, identifier string) bool {
_, err := k.Epochs.Get(ctx, identifier)
if errors.Is(err, collections.ErrNotFound) {
return false
} else if err != nil {
panic(err)
}

return true
Expand Down Expand Up @@ -59,11 +58,9 @@ func (k Keeper) AddEpochInfo(ctx sdk.Context, epoch types.EpochInfo) error {
}

// DeleteEpochInfo delete epoch info.
func (k Keeper) DeleteEpochInfo(ctx sdk.Context, identifier string) {
err := k.Epochs.Delete(ctx, identifier)
if err != nil {
panic(err)
}
func (k Keeper) DeleteEpochInfo(ctx sdk.Context, identifier string) (err error) {
err = k.Epochs.Delete(ctx, identifier)
return
}

// IterateEpochInfo iterate through epochs.
Expand Down
9 changes: 4 additions & 5 deletions x/epochs/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"context"
"errors"

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

Expand Down Expand Up @@ -31,12 +30,12 @@ func (q Querier) EpochInfos(c context.Context, _ *types.QueryEpochsInfoRequest)
}

// CurrentEpoch provides current epoch of specified identifier.
func (q Querier) CurrentEpoch(c context.Context, req *types.QueryCurrentEpochRequest) (*types.QueryCurrentEpochResponse, error) {
func (q Querier) CurrentEpoch(c context.Context, req *types.QueryCurrentEpochRequest) (resp *types.QueryCurrentEpochResponse, err error) {
ctx := sdk.UnwrapSDKContext(c)

info := q.Keeper.GetEpochInfo(ctx, req.Identifier)
if info.Identifier != req.Identifier {
return nil, errors.New("not available identifier")
info, err := q.Keeper.GetEpochInfo(ctx, req.Identifier)
if err != nil {
return
}

return &types.QueryCurrentEpochResponse{
Expand Down
17 changes: 17 additions & 0 deletions x/epochs/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ func TestQueryEpochInfos(t *testing.T) {
require.Equal(t, epochInfosResponse.Epochs[1].CurrentEpochStartTime, chainStartTime)
require.Equal(t, epochInfosResponse.Epochs[1].EpochCountingStarted, false)
}

func TestCurrentEpochQuery(t *testing.T) {
nibiruApp, ctx := testapp.NewNibiruTestAppAndContext(true)

queryHelper := baseapp.NewQueryServerTestHelper(ctx, nibiruApp.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, keeper.NewQuerier(nibiruApp.EpochsKeeper))
queryClient := types.NewQueryClient(queryHelper)

// Valid epoch
epochInfosResponse, err := queryClient.CurrentEpoch(gocontext.Background(), &types.QueryCurrentEpochRequest{Identifier: "15 min"})
require.NoError(t, err)
require.Equal(t, epochInfosResponse.CurrentEpoch, uint64(0))

// Invalid epoch
_, err = queryClient.CurrentEpoch(gocontext.Background(), &types.QueryCurrentEpochRequest{Identifier: "invalid epoch"})
require.Error(t, err)
}
8 changes: 0 additions & 8 deletions x/epochs/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package keeper

import (
"fmt"

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

"github.com/NibiruChain/collections"

"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/x/epochs/types"
)
Expand Down Expand Up @@ -37,7 +33,3 @@ func (k *Keeper) SetHooks(eh types.EpochHooks) *Keeper {

return k
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
3 changes: 2 additions & 1 deletion x/epochs/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func TestUpsertEpochInfo_HappyPath(t *testing.T) {

nibiruApp.EpochsKeeper.Epochs.Insert(ctx, epochInfo.Identifier, epochInfo)

epochInfoSaved := nibiruApp.EpochsKeeper.GetEpochInfo(ctx, "monthly")
epochInfoSaved, err := nibiruApp.EpochsKeeper.GetEpochInfo(ctx, "monthly")
require.NoError(t, err)
require.Equal(t, epochInfo, epochInfoSaved)

allEpochs := nibiruApp.EpochsKeeper.AllEpochInfos(ctx)
Expand Down
6 changes: 5 additions & 1 deletion x/perp/v2/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ uint64)
continue
}

epochInfo := k.EpochKeeper.GetEpochInfo(ctx, epochIdentifier)
epochInfo, err := k.EpochKeeper.GetEpochInfo(ctx, epochIdentifier)
if err != nil {
ctx.Logger().Error("failed to fetch epoch info", "epochIdentifier", epochIdentifier, "error", err)
continue
}
intervalsPerDay := (24 * time.Hour) / epochInfo.Duration
// See https://www.notion.so/nibiru/Funding-Payments-5032d0f8ed164096808354296d43e1fa for an explanation of these terms.
premiumFraction := markTwap.Sub(indexTwap).QuoInt64(int64(intervalsPerDay))
Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ type OracleKeeper interface {

type EpochKeeper interface {
// GetEpochInfo returns epoch info by identifier.
GetEpochInfo(ctx sdk.Context, identifier string) types.EpochInfo
GetEpochInfo(ctx sdk.Context, identifier string) (types.EpochInfo, error)
}

0 comments on commit a0b4a96

Please sign in to comment.