Skip to content

Commit

Permalink
refactor(epochs): use single field to track epoch block height
Browse files Browse the repository at this point in the history
  • Loading branch information
jeebster committed Oct 30, 2021
1 parent 85440b2 commit a821daa
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 116 deletions.
3 changes: 1 addition & 2 deletions proto/osmosis/epochs/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ message EpochInfo {
];
bool epoch_counting_started = 6;
reserved 7;
int64 start_height = 8;
int64 current_epoch_start_height = 9;
int64 current_epoch_start_height = 8;
}

// GenesisState defines the epochs module's genesis state.
Expand Down
4 changes: 2 additions & 2 deletions x/epochs/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
shouldEpochStart := ctx.BlockTime().After(epochEndTime) && !shouldInitialEpochStart && !epochInfo.StartTime.After(ctx.BlockTime())

if shouldInitialEpochStart || shouldEpochStart {
epochInfo.CurrentEpochStartHeight = ctx.BlockHeight()

if shouldInitialEpochStart {
epochInfo.EpochCountingStarted = true
epochInfo.CurrentEpoch = 1
epochInfo.CurrentEpochStartTime = epochInfo.StartTime
epochInfo.CurrentEpochStartHeight = epochInfo.StartHeight
logger.Info(fmt.Sprintf("Starting new epoch with identifier %s", epochInfo.Identifier))
} else {
epochInfo.CurrentEpoch += 1
epochInfo.CurrentEpochStartTime = epochInfo.CurrentEpochStartTime.Add(epochInfo.Duration)
epochInfo.CurrentEpochStartHeight = ctx.BlockHeight()
logger.Info(fmt.Sprintf("Starting epoch with identifier %s", epochInfo.Identifier))
ctx.EventManager().EmitEvent(
sdk.NewEvent(
Expand Down
28 changes: 9 additions & 19 deletions x/epochs/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@ func TestEpochInfoChangesBeginBlockerAndInitGenesis(t *testing.T) {
var epochInfo types.EpochInfo

now := time.Now()
initialBlockHeight := int64(1)

tests := []struct {
expCurrentEpochStartTime time.Time
expCurrentEpochStartHeight int64
expCurrentEpoch int64
expInitialEpochStartHeight int64
expInitialEpochStartTime time.Time
fn func()
}{
{
// Only advance 2 seconds, do not increment epoch
expCurrentEpochStartHeight: 1,
expCurrentEpochStartHeight: 2,
expCurrentEpochStartTime: now,
expCurrentEpoch: 1,
expInitialEpochStartHeight: initialBlockHeight,
expInitialEpochStartTime: now,
fn: func() {
ctx = ctx.WithBlockHeight(2).WithBlockTime(now.Add(time.Second))
Expand All @@ -42,10 +39,9 @@ func TestEpochInfoChangesBeginBlockerAndInitGenesis(t *testing.T) {
},
},
{
expCurrentEpochStartHeight: 1,
expCurrentEpochStartHeight: 2,
expCurrentEpochStartTime: now,
expCurrentEpoch: 1,
expInitialEpochStartHeight: initialBlockHeight,
expInitialEpochStartTime: now,
fn: func() {
ctx = ctx.WithBlockHeight(2).WithBlockTime(now.Add(time.Second))
Expand All @@ -54,10 +50,9 @@ func TestEpochInfoChangesBeginBlockerAndInitGenesis(t *testing.T) {
},
},
{
expCurrentEpochStartHeight: 1,
expCurrentEpochStartHeight: 2,
expCurrentEpochStartTime: now,
expCurrentEpoch: 1,
expInitialEpochStartHeight: initialBlockHeight,
expInitialEpochStartTime: now,
fn: func() {
ctx = ctx.WithBlockHeight(2).WithBlockTime(now.Add(time.Second))
Expand All @@ -72,7 +67,6 @@ func TestEpochInfoChangesBeginBlockerAndInitGenesis(t *testing.T) {
expCurrentEpochStartHeight: 3,
expCurrentEpochStartTime: now.Add(time.Hour * 24 * 31),
expCurrentEpoch: 2,
expInitialEpochStartHeight: initialBlockHeight,
expInitialEpochStartTime: now,
fn: func() {
ctx = ctx.WithBlockHeight(2).WithBlockTime(now.Add(time.Second))
Expand All @@ -86,7 +80,6 @@ func TestEpochInfoChangesBeginBlockerAndInitGenesis(t *testing.T) {
expCurrentEpochStartHeight: 3,
expCurrentEpochStartTime: now.Add(time.Hour * 24 * 31),
expCurrentEpoch: 2,
expInitialEpochStartHeight: initialBlockHeight,
expInitialEpochStartTime: now,
fn: func() {
ctx = ctx.WithBlockHeight(2).WithBlockTime(now.Add(time.Second))
Expand All @@ -102,7 +95,6 @@ func TestEpochInfoChangesBeginBlockerAndInitGenesis(t *testing.T) {
expCurrentEpochStartHeight: 3,
expCurrentEpochStartTime: now.Add(time.Hour * 24 * 31),
expCurrentEpoch: 2,
expInitialEpochStartHeight: initialBlockHeight,
expInitialEpochStartTime: now,
fn: func() {
ctx = ctx.WithBlockHeight(2).WithBlockTime(now.Add(time.Second))
Expand All @@ -127,14 +119,13 @@ func TestEpochInfoChangesBeginBlockerAndInitGenesis(t *testing.T) {
app.EpochsKeeper.DeleteEpochInfo(ctx, epochInfo.Identifier)
}

ctx = ctx.WithBlockHeight(initialBlockHeight).WithBlockTime(now)
ctx = ctx.WithBlockHeight(1).WithBlockTime(now)

// check init genesis
epochs.InitGenesis(ctx, app.EpochsKeeper, types.GenesisState{
Epochs: []types.EpochInfo{
{
Identifier: "monthly",
StartHeight: ctx.BlockHeight(),
StartTime: time.Time{},
Duration: time.Hour * 24 * 31,
CurrentEpoch: 0,
Expand All @@ -148,7 +139,6 @@ func TestEpochInfoChangesBeginBlockerAndInitGenesis(t *testing.T) {
test.fn()

require.Equal(t, epochInfo.Identifier, "monthly")
require.Equal(t, epochInfo.StartHeight, test.expInitialEpochStartHeight)
require.Equal(t, epochInfo.StartTime.UTC().String(), test.expInitialEpochStartTime.UTC().String())
require.Equal(t, epochInfo.Duration, time.Hour*24*31)
require.Equal(t, epochInfo.CurrentEpoch, test.expCurrentEpoch)
Expand All @@ -172,13 +162,13 @@ func TestEpochStartingOneMonthAfterInitGenesis(t *testing.T) {
now := time.Now()
week := time.Hour * 24 * 7
month := time.Hour * 24 * 30
ctx = ctx.WithBlockHeight(1).WithBlockTime(now)
initialBlockHeight := int64(1)
ctx = ctx.WithBlockHeight(initialBlockHeight).WithBlockTime(now)

epochs.InitGenesis(ctx, app.EpochsKeeper, types.GenesisState{
Epochs: []types.EpochInfo{
{
Identifier: "monthly",
StartHeight: ctx.BlockHeight(),
StartTime: now.Add(month),
Duration: time.Hour * 24 * 30,
CurrentEpoch: 0,
Expand All @@ -192,7 +182,7 @@ func TestEpochStartingOneMonthAfterInitGenesis(t *testing.T) {
// epoch not started yet
epochInfo := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
require.Equal(t, epochInfo.CurrentEpoch, int64(0))
require.Equal(t, epochInfo.CurrentEpochStartHeight, epochInfo.StartHeight)
require.Equal(t, epochInfo.CurrentEpochStartHeight, initialBlockHeight)
require.Equal(t, epochInfo.CurrentEpochStartTime, time.Time{})
require.Equal(t, epochInfo.EpochCountingStarted, false)

Expand All @@ -203,7 +193,7 @@ func TestEpochStartingOneMonthAfterInitGenesis(t *testing.T) {
// epoch not started yet
epochInfo = app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
require.Equal(t, epochInfo.CurrentEpoch, int64(0))
require.Equal(t, epochInfo.CurrentEpochStartHeight, epochInfo.StartHeight)
require.Equal(t, epochInfo.CurrentEpochStartHeight, initialBlockHeight)
require.Equal(t, epochInfo.CurrentEpochStartTime, time.Time{})
require.Equal(t, epochInfo.EpochCountingStarted, false)

Expand All @@ -214,7 +204,7 @@ func TestEpochStartingOneMonthAfterInitGenesis(t *testing.T) {
// epoch started
epochInfo = app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
require.Equal(t, epochInfo.CurrentEpoch, int64(1))
require.Equal(t, epochInfo.CurrentEpochStartHeight, epochInfo.StartHeight)
require.Equal(t, epochInfo.CurrentEpochStartHeight, ctx.BlockHeight())
require.Equal(t, epochInfo.CurrentEpochStartTime.UTC().String(), now.Add(month).UTC().String())
require.Equal(t, epochInfo.EpochCountingStarted, true)
}
2 changes: 1 addition & 1 deletion x/epochs/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
epoch.StartTime = ctx.BlockTime()
}

epoch.StartHeight = ctx.BlockHeight()
epoch.CurrentEpochStartHeight = ctx.BlockHeight()

k.SetEpochInfo(ctx, epoch)
}
Expand Down
6 changes: 0 additions & 6 deletions x/epochs/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ func TestEpochsExportGenesis(t *testing.T) {

require.Equal(t, genesis.Epochs[0].Identifier, "day")
require.Equal(t, genesis.Epochs[0].StartTime, chainStartTime)
require.Equal(t, genesis.Epochs[0].StartHeight, chainStartHeight)
require.Equal(t, genesis.Epochs[0].Duration, time.Hour*24)
require.Equal(t, genesis.Epochs[0].CurrentEpoch, int64(0))
require.Equal(t, genesis.Epochs[0].CurrentEpochStartHeight, chainStartHeight)
require.Equal(t, genesis.Epochs[0].CurrentEpochStartTime, chainStartTime)
require.Equal(t, genesis.Epochs[0].EpochCountingStarted, false)
require.Equal(t, genesis.Epochs[1].Identifier, "week")
require.Equal(t, genesis.Epochs[1].StartTime, chainStartTime)
require.Equal(t, genesis.Epochs[1].StartHeight, chainStartHeight)
require.Equal(t, genesis.Epochs[1].Duration, time.Hour*24*7)
require.Equal(t, genesis.Epochs[1].CurrentEpoch, int64(0))
require.Equal(t, genesis.Epochs[1].CurrentEpochStartHeight, chainStartHeight)
Expand Down Expand Up @@ -59,7 +57,6 @@ func TestEpochsInitGenesis(t *testing.T) {
Epochs: []types.EpochInfo{
{
Identifier: "monthly",
StartHeight: ctx.BlockHeight(),
StartTime: time.Time{},
Duration: time.Hour * 24,
CurrentEpoch: 0,
Expand All @@ -69,7 +66,6 @@ func TestEpochsInitGenesis(t *testing.T) {
},
{
Identifier: "monthly",
StartHeight: ctx.BlockHeight(),
StartTime: time.Time{},
Duration: time.Hour * 24,
CurrentEpoch: 0,
Expand All @@ -85,7 +81,6 @@ func TestEpochsInitGenesis(t *testing.T) {
Epochs: []types.EpochInfo{
{
Identifier: "monthly",
StartHeight: ctx.BlockHeight(),
StartTime: time.Time{},
Duration: time.Hour * 24,
CurrentEpoch: 0,
Expand All @@ -99,7 +94,6 @@ func TestEpochsInitGenesis(t *testing.T) {
epochs.InitGenesis(ctx, app.EpochsKeeper, genesisState)
epochInfo := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
require.Equal(t, epochInfo.Identifier, "monthly")
require.Equal(t, epochInfo.StartHeight, ctx.BlockHeight())
require.Equal(t, epochInfo.StartTime.UTC().String(), now.UTC().String())
require.Equal(t, epochInfo.Duration, time.Hour*24)
require.Equal(t, epochInfo.CurrentEpoch, int64(0))
Expand Down
2 changes: 0 additions & 2 deletions x/epochs/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func RandomizedGenState(simState *module.SimulationState) {
epochs := []types.EpochInfo{
{
Identifier: "day",
StartHeight: 0,
StartTime: time.Time{},
Duration: time.Hour * 24,
CurrentEpoch: 0,
Expand All @@ -26,7 +25,6 @@ func RandomizedGenState(simState *module.SimulationState) {
},
{
Identifier: "hour",
StartHeight: 0,
StartTime: time.Time{},
Duration: time.Hour,
CurrentEpoch: 0,
Expand Down
8 changes: 3 additions & 5 deletions x/epochs/spec/02_state.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@ message EpochInfo {
];
bool epoch_counting_started = 6;
reserved 7;
int64 start_height = 8;
int64 current_epoch_start_height = 9;
int64 current_epoch_start_height = 8;
}
```

EpochInfo keeps `identifier`, `start_time`,`duration`, `current_epoch`, `current_epoch_start_time`, `epoch_counting_started`, `start_height`, `current_epoch_start_height`.
EpochInfo keeps `identifier`, `start_time`,`duration`, `current_epoch`, `current_epoch_start_time`, `epoch_counting_started`, `current_epoch_start_height`.

1. `identifier` keeps epoch identification string.
2. `start_time` keeps epoch counting start time, if block time passes `start_time`, `epoch_counting_started` is set.
3. `duration` keeps target epoch duration.
4. `current_epoch` keeps current active epoch number.
5. `current_epoch_start_time` keeps the start time of current epoch.
6. `epoch_number` is counted only when `epoch_counting_started` flag is set.
7. `start_height` keeps epoch start block height.
8. `current_epoch_start_height` keeps the start block height of current epoch.
7. `current_epoch_start_height` keeps the start block height of current epoch.
26 changes: 14 additions & 12 deletions x/epochs/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ func NewGenesisState(epochs []EpochInfo) *GenesisState {
func DefaultGenesis() *GenesisState {
epochs := []EpochInfo{
{
Identifier: "week",
StartTime: time.Time{},
Duration: time.Hour * 24 * 7,
CurrentEpoch: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
Identifier: "week",
StartTime: time.Time{},
Duration: time.Hour * 24 * 7,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
},
{
Identifier: "day",
StartTime: time.Time{},
Duration: time.Hour * 24,
CurrentEpoch: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
Identifier: "day",
StartTime: time.Time{},
Duration: time.Hour * 24,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
},
}
return NewGenesisState(epochs)
Expand Down
Loading

0 comments on commit a821daa

Please sign in to comment.