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

Updating epochs module. #375

Merged
merged 2 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func NewSimApp(
app.CrisisKeeper = crisiskeeper.NewKeeper(
app.GetSubspace(crisistypes.ModuleName), invCheckPeriod, app.BankKeeper, authtypes.FeeCollectorName,
)
epochKeeper := epochsKeeper.NewKeeper(appCodec, keys[epochsTypes.StoreKey])
epochKeeper := epochsKeeper.NewKeeper(keys[epochsTypes.StoreKey])
app.EpochsKeeper = epochKeeper.SetHooks(
epochsTypes.NewMultiEpochHooks(),
)
Expand Down
35 changes: 18 additions & 17 deletions x/epochs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ they can easily be signalled upon such events.
1. **[Concept](#concepts)**
2. **[State](#state)**
3. **[Events](#events)**
4. **[Keeper](#keeper)**
4. **[Keeper](#keepers)**
5. **[Hooks](#hooks)**
6. **[Queries](#queries)**
7. **[Downtime Recovery](#downtime-recovery)**

## Concepts

Expand All @@ -27,7 +26,7 @@ We refer to the period in between two timer ticks as an "epoch".

Every timer has a unique identifier.
Every epoch will have a start time, and an end time, where `end time = start time + timer interval`.
On Osmosis mainnet, we only utilize one identifier, with a time interval of `one day`.
On Persistence mainnet, we only utilize one identifier, with a time interval of `one day`.

The timer will tick at the first block whose blocktime is greater than the timer end time,
and set the start as the prior timer end time. (Notably, its not set to the block time!)
Expand All @@ -36,9 +35,9 @@ until the timer has caught up.

## State

The Epochs module keeps a single [`EpochInfo`](https://github.com/osmosis-labs/osmosis/blob/b4befe4f3eb97ebb477323234b910c4afafab9b7/proto/osmosis/epochs/genesis.proto#L12) per identifier.
The Epochs module keeps a single `EpochInfo` per identifier.
This contains the current state of the timer with the corresponding identifier.
Its fields are modified at every timer tick.
Its fields are modified at every timer tick.
EpochInfos are initialized as part of genesis initialization or upgrade logic,
and are only modified on begin blockers.

Expand All @@ -48,16 +47,16 @@ The `epochs` module emits the following events:

### BeginBlocker

| Type | Attribute Key | Attribute Value |
| --------------| ---------------| -----------------|
| epoch\_start | epoch\_number | {epoch\_number} |
| epoch\_start | start\_time | {start\_time} |
| Type | Attribute Key | Attribute Value |
| ----------- | ------------- | --------------- |
| epoch_start | epoch_number | {epoch_number} |
| epoch_start | start_time | {start_time} |

### EndBlocker

| Type | Attribute Key | Attribute Value |
| ------------| ---------------| -----------------|
| epoch\_end | epoch\_number | {epoch\_number} |
| Type | Attribute Key | Attribute Value |
| --------- | ------------- | --------------- |
| epoch_end | epoch_number | {epoch_number} |

## Keepers

Expand All @@ -71,7 +70,7 @@ type Keeper interface {
// GetEpochInfo returns epoch info by identifier
GetEpochInfo(ctx sdk.Context, identifier string) types.EpochInfo
// SetEpochInfo set epoch info
SetEpochInfo(ctx sdk.Context, epoch types.EpochInfo)
SetEpochInfo(ctx sdk.Context, epoch types.EpochInfo)
// DeleteEpochInfo delete epoch info
DeleteEpochInfo(ctx sdk.Context, identifier string)
// IterateEpochInfo iterate through epochs
Expand All @@ -98,6 +97,7 @@ epochIdentifier. Filtering epochIdentifier could be in `Params` of other
modules so that they can be modified by governance.

This is the standard dev UX of this:

```golang
func (k MyModuleKeeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) {
params := k.GetParams(ctx)
Expand Down Expand Up @@ -137,8 +137,9 @@ service Query {
Query the currently running epochInfos

```sh
osmosisd query epochs epoch-infos
persistenceCore query epochs epoch-infos
```

::: details Example

An example output:
Expand All @@ -160,23 +161,23 @@ epochs:
identifier: week
start_time: "2021-06-18T17:00:00Z"
```

:::

### Current Epoch


Query the current epoch by the specified identifier

```sh
osmosisd query epochs current-epoch [identifier]
persistenceCore query epochs current-epoch [identifier]
```

::: details Example

Query the current `day` epoch:

```sh
osmosisd query epochs current-epoch day
persistenceCore query epochs current-epoch day
```

Which in this example outputs:
Expand Down
88 changes: 88 additions & 0 deletions x/epochs/client/cli/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cli_test

import (
"context"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/persistenceOne/persistence-sdk/simapp"
"github.com/persistenceOne/persistence-sdk/x/epochs/types"
)

type QueryTestSuite struct {
suite.Suite

app *simapp.SimApp
ctx sdk.Context

queryHelper *baseapp.QueryServiceTestHelper
queryClient types.QueryClient
}

func (s *QueryTestSuite) SetupSuite() {
s.app = simapp.Setup(false)
s.ctx = s.app.NewContext(false, tmproto.Header{})
s.queryHelper = &baseapp.QueryServiceTestHelper{
GRPCQueryRouter: s.app.GRPCQueryRouter(),
Ctx: s.ctx,
}
s.queryClient = types.NewQueryClient(s.queryHelper)

// add new epoch
epoch := types.EpochInfo{
Identifier: "weekly",
StartTime: time.Time{},
Duration: time.Hour,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
}

err := s.app.EpochsKeeper.AddEpochInfo(s.ctx, epoch)
require.NoError(s.T(), err)

s.app.Commit()
}

func (s *QueryTestSuite) TestQueriesNeverAlterState() {
testCases := []struct {
name string
query string
input interface{}
output interface{}
}{
{
"Query current epoch",
"/persistence.epochs.v1beta1.Query/CurrentEpoch",
&types.QueryCurrentEpochRequest{Identifier: "weekly"},
&types.QueryCurrentEpochResponse{},
},
{
"Query epochs info",
"/persistence.epochs.v1beta1.Query/EpochInfos",
&types.QueryEpochsInfoRequest{},
&types.QueryEpochsInfoResponse{},
},
}

for _, tc := range testCases {
tc := tc

s.Run(tc.name, func() {
s.SetupSuite()
err := s.queryHelper.Invoke(context.Background(), tc.query, tc.input, tc.output)
s.Require().NoError(err)
})
}
}

func TestQueryTestSuite(t *testing.T) {
suite.Run(t, new(QueryTestSuite))
}
4 changes: 4 additions & 0 deletions x/epochs/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func (q Querier) CurrentEpoch(c context.Context, req *types.QueryCurrentEpochReq
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if req.Identifier == "" {
return nil, status.Error(codes.InvalidArgument, "identifier is empty")
}

ctx := sdk.UnwrapSDKContext(c)

info := q.Keeper.GetEpochInfo(ctx, req.Identifier)
Expand Down
3 changes: 1 addition & 2 deletions x/epochs/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/persistenceOne/persistence-sdk/x/epochs/types"

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

Expand All @@ -19,7 +18,7 @@ type (
)

// NewKeeper returns a new keeper by codec and storeKey inputs.
func NewKeeper(cdc codec.Codec, storeKey sdk.StoreKey) *Keeper {
func NewKeeper(storeKey sdk.StoreKey) *Keeper {
return &Keeper{
storeKey: storeKey,
}
Expand Down
3 changes: 1 addition & 2 deletions x/epochs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ var (
// ----------------------------------------------------------------------------

// AppModuleBasic implements the AppModuleBasic interface for the capability module.
type AppModuleBasic struct {
}
type AppModuleBasic struct{}

func NewAppModuleBasic() AppModuleBasic {
return AppModuleBasic{}
Expand Down