-
Notifications
You must be signed in to change notification settings - Fork 208
/
abci.go
63 lines (50 loc) · 1.92 KB
/
abci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package epochs
import (
"time"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/NibiruChain/nibiru/x/epochs/keeper"
"github.com/NibiruChain/nibiru/x/epochs/types"
)
// BeginBlocker of epochs module.
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
k.IterateEpochInfo(ctx, func(index int64, epochInfo types.EpochInfo) (stop bool) {
if ctx.BlockTime().Before(epochInfo.StartTime) {
return false
}
if !shouldEpochStart(epochInfo, ctx) {
return false
}
epochInfo.CurrentEpochStartHeight = ctx.BlockHeight()
epochInfo.CurrentEpochStartTime = ctx.BlockTime()
if !epochInfo.EpochCountingStarted {
epochInfo.EpochCountingStarted = true
epochInfo.CurrentEpoch = 1
} else {
_ = ctx.EventManager().EmitTypedEvent(&types.EventEpochEnd{EpochNumber: epochInfo.CurrentEpoch})
k.AfterEpochEnd(ctx, epochInfo.Identifier, epochInfo.CurrentEpoch)
epochInfo.CurrentEpoch += 1
}
// emit new epoch start event, set epoch info, and run BeforeEpochStart hook
_ = ctx.EventManager().EmitTypedEvent(&types.EventEpochStart{
EpochNumber: epochInfo.CurrentEpoch,
EpochStartTime: epochInfo.CurrentEpochStartTime,
})
k.Epochs.Insert(ctx, epochInfo.Identifier, epochInfo)
k.BeforeEpochStart(ctx, epochInfo.Identifier, epochInfo.CurrentEpoch)
return false
})
}
// shouldEpochStart checks if the epoch should start.
// an epoch is ready to start if:
// - it has not yet been initialized.
// - the current epoch end time is before the current block time
func shouldEpochStart(epochInfo types.EpochInfo, ctx sdk.Context) bool {
// Epoch has not started yet
if !epochInfo.EpochCountingStarted {
return true
}
epochEndTime := epochInfo.CurrentEpochStartTime.Add(epochInfo.Duration)
return ctx.BlockTime().After(epochEndTime) || ctx.BlockTime().Equal(epochEndTime)
}