-
Notifications
You must be signed in to change notification settings - Fork 889
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
[ENG-214] Add new epoch types: year, hour #559
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a2d1a20
Store epoch information by duration and by identifier
loredanacirstea 0a0e29b
Merge branch 'main' into loredana/ENG-215-epoch-key-duration
loredanacirstea d27fcaa
Revert to ascending order
loredanacirstea 77d9157
Fix epochs unit tests
loredanacirstea 000034b
Add migration logic for epoch change
loredanacirstea c314ec8
Add change log
loredanacirstea 6e56725
uncomment check
loredanacirstea 602836d
Merge branch 'main' into loredana/ENG-215-epoch-key-duration
loredanacirstea 6df81c2
Update evmos v3 -> v4 after latest changes
loredanacirstea 31335b9
Add year and hour epochs
loredanacirstea 5caaeb9
Update evmos v3 -> v4 after latest changes
loredanacirstea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
|
@@ -9,9 +12,28 @@ import ( | |
|
||
// GetEpochInfo returns epoch info by identifier | ||
func (k Keeper) GetEpochInfo(ctx sdk.Context, identifier string) (types.EpochInfo, bool) { | ||
duration, found := k.GetEpochDuration(ctx, identifier) | ||
if !found { | ||
return types.EpochInfo{}, false | ||
} | ||
return k.GetEpoch(ctx, duration) | ||
} | ||
|
||
// GetEpochDuration returns epoch duration by identifier | ||
func (k Keeper) GetEpochDuration(ctx sdk.Context, identifier string) ([]byte, bool) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpochDuration) | ||
bz := store.Get([]byte(identifier)) | ||
if len(bz) == 0 { | ||
return make([]byte, 0), false | ||
} | ||
return bz, true | ||
} | ||
|
||
// GetEpochInfo returns epoch info by duration | ||
func (k Keeper) GetEpoch(ctx sdk.Context, duration []byte) (types.EpochInfo, bool) { | ||
epoch := types.EpochInfo{} | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpoch) | ||
bz := store.Get([]byte(identifier)) | ||
bz := store.Get(duration) | ||
if len(bz) == 0 { | ||
return epoch, false | ||
} | ||
|
@@ -22,18 +44,45 @@ func (k Keeper) GetEpochInfo(ctx sdk.Context, identifier string) (types.EpochInf | |
|
||
// SetEpochInfo set epoch info | ||
func (k Keeper) SetEpochInfo(ctx sdk.Context, epoch types.EpochInfo) { | ||
k.setEpochDuration(ctx, epoch) | ||
k.setEpoch(ctx, epoch) | ||
} | ||
|
||
// SetEpochDuration set epoch duration by identifier | ||
func (k Keeper) setEpochDuration(ctx sdk.Context, epoch types.EpochInfo) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpochDuration) | ||
store.Set([]byte(epoch.Identifier), durationToBz(epoch.Duration)) | ||
} | ||
|
||
// SetEpochInfo set epoch duration by identifier | ||
func (k Keeper) setEpoch(ctx sdk.Context, epoch types.EpochInfo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpoch) | ||
bz := k.cdc.MustMarshal(&epoch) | ||
store.Set([]byte(epoch.Identifier), bz) | ||
store.Set(durationToBz(epoch.Duration), bz) | ||
} | ||
|
||
// DeleteEpochInfo delete epoch info | ||
func (k Keeper) DeleteEpochInfo(ctx sdk.Context, identifier string) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpoch) | ||
duration, found := k.GetEpochDuration(ctx, identifier) | ||
if found { | ||
k.deleteEpochDuration(ctx, identifier) | ||
k.deleteEpoch(ctx, duration) | ||
} | ||
} | ||
|
||
// DeleteEpochDuration delete epoch duration by identifier | ||
func (k Keeper) deleteEpochDuration(ctx sdk.Context, identifier string) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpochDuration) | ||
store.Delete([]byte(identifier)) | ||
} | ||
|
||
// IterateEpochInfo iterate through epochs | ||
// DeleteEpoch delete epoch info | ||
func (k Keeper) deleteEpoch(ctx sdk.Context, duration []byte) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpoch) | ||
store.Delete(duration) | ||
} | ||
|
||
// IterateEpochInfo iterate through epochs in ascending numerical order, by duration | ||
func (k Keeper) IterateEpochInfo(ctx sdk.Context, fn func(index int64, epochInfo types.EpochInfo) (stop bool)) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpoch) | ||
|
||
|
@@ -64,3 +113,10 @@ func (k Keeper) AllEpochInfos(ctx sdk.Context) []types.EpochInfo { | |
}) | ||
return epochs | ||
} | ||
|
||
// durationToBz parses time duration to maintain number-compatible ordering | ||
func durationToBz(duration time.Duration) []byte { | ||
// 13 digits left padded with zero, allows for 300 year durations | ||
s := fmt.Sprintf("%013d", duration.Milliseconds()) | ||
return []byte(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
|
||
v2 "github.com/tharsis/evmos/v4/x/epochs/migrations/v2" | ||
) | ||
|
||
var _ module.MigrationHandler = Migrator{}.Migrate1to2 | ||
|
||
// Migrator is a struct for handling in-place store migrations. | ||
type Migrator struct { | ||
keeper Keeper | ||
} | ||
|
||
// NewMigrator returns a new Migrator. | ||
func NewMigrator(keeper Keeper) Migrator { | ||
return Migrator{ | ||
keeper: keeper, | ||
} | ||
} | ||
|
||
// Migrate1to2 migrates from consensus version 1 to 2. | ||
func (m Migrator) Migrate1to2(ctx sdk.Context) error { | ||
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package v2 | ||
|
||
const ( | ||
// YearEpochID defines the identifier for yealy epochs | ||
YearEpochID = "year" | ||
// WeekEpochID defines the identifier for weekly epochs | ||
WeekEpochID = "week" | ||
// DayEpochID defines the identifier for daily epochs | ||
DayEpochID = "day" | ||
// HourEpochID defines the identifier for hourly epochs | ||
HourEpochID = "hour" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package v2 | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/tharsis/evmos/v4/x/epochs/types" | ||
) | ||
|
||
var NewEpochs = []types.EpochInfo{ | ||
{ | ||
Identifier: YearEpochID, | ||
StartTime: time.Time{}, | ||
Duration: time.Hour * 24 * 365, | ||
CurrentEpoch: 0, | ||
CurrentEpochStartHeight: 0, | ||
CurrentEpochStartTime: time.Time{}, | ||
EpochCountingStarted: false, | ||
}, | ||
{ | ||
Identifier: HourEpochID, | ||
StartTime: time.Time{}, | ||
Duration: time.Hour, | ||
CurrentEpoch: 0, | ||
CurrentEpochStartHeight: 0, | ||
CurrentEpochStartTime: time.Time{}, | ||
EpochCountingStarted: false, | ||
}, | ||
} | ||
|
||
// MigrateJSON accepts exported 1 x/epochs genesis state and migrates it | ||
// to 2 x/epochs genesis state. Hourly and yearly epochs are added. | ||
func MigrateJSON(state types.GenesisState) types.GenesisState { | ||
state.Epochs = append(state.Epochs, NewEpochs[:]...) | ||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package v2 | ||
|
||
// prefix bytes for the epochs persistent store | ||
const ( | ||
prefixEpoch = iota + 1 | ||
prefixEpochDuration | ||
) | ||
|
||
// KeyPrefixEpoch defines prefix key for storing epochs | ||
var KeyPrefixEpoch = []byte{prefixEpoch} | ||
|
||
// KeyPrefixEpochDuration defines prefix key for storing epochs durations | ||
var KeyPrefixEpochDuration = []byte{prefixEpochDuration} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would merge
GetEpoch
withGetEpochInfo
, as right now it's confusing to have both andGetEpoch
is only called byGetEpochInfo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@loredanacirstea what do you think of renaming
EpochInfo
toEpoch
in the proto files? This will make the code less wordy and I don't see the benefit in haveInfo
added.