forked from osmosis-labs/osmosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abci.go
58 lines (50 loc) · 2.16 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
package epochs
import (
"fmt"
"time"
"github.com/MonikaCat/osmosis/v6/x/epochs/keeper"
"github.com/MonikaCat/osmosis/v6/x/epochs/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/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) {
logger := k.Logger(ctx)
// Has it not started, and is the block time > initial epoch start time
shouldInitialEpochStart := !epochInfo.EpochCountingStarted && !epochInfo.StartTime.After(ctx.BlockTime())
epochEndTime := epochInfo.CurrentEpochStartTime.Add(epochInfo.Duration)
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
logger.Info(fmt.Sprintf("Starting new epoch with identifier %s", epochInfo.Identifier))
} else {
epochInfo.CurrentEpoch += 1
epochInfo.CurrentEpochStartTime = epochInfo.CurrentEpochStartTime.Add(epochInfo.Duration)
logger.Info(fmt.Sprintf("Starting epoch with identifier %s", epochInfo.Identifier))
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeEpochEnd,
sdk.NewAttribute(types.AttributeEpochNumber, fmt.Sprintf("%d", epochInfo.CurrentEpoch)),
),
)
k.AfterEpochEnd(ctx, epochInfo.Identifier, epochInfo.CurrentEpoch)
}
k.SetEpochInfo(ctx, epochInfo)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeEpochStart,
sdk.NewAttribute(types.AttributeEpochNumber, fmt.Sprintf("%d", epochInfo.CurrentEpoch)),
sdk.NewAttribute(types.AttributeEpochStartTime, fmt.Sprintf("%d", epochInfo.CurrentEpochStartTime.Unix())),
),
)
k.BeforeEpochStart(ctx, epochInfo.Identifier, epochInfo.CurrentEpoch)
}
return false
})
}