-
Notifications
You must be signed in to change notification settings - Fork 197
/
abci.go
80 lines (65 loc) · 2.53 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package keeper
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/Stride-Labs/stride/v15/utils"
"github.com/Stride-Labs/stride/v15/x/epochs/types"
)
// BeginBlocker of epochs module
func (k Keeper) BeginBlocker(ctx sdk.Context) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
logger := k.Logger(ctx)
k.IterateEpochInfo(ctx, func(_ int64, epochInfo types.EpochInfo) (stop bool) {
// 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())
epochInfo.CurrentEpochStartHeight = ctx.BlockHeight()
switch {
case shouldInitialEpochStart:
epochInfo = startInitialEpoch(epochInfo)
logger.Info(fmt.Sprintf("initial %s epoch", epochInfo.Identifier))
case shouldEpochStart:
epochInfo = endEpoch(epochInfo)
// Capitalize the epoch identifier for the logs
epochAlias := strings.ToUpper(strings.ReplaceAll(epochInfo.Identifier, "_epoch", ""))
logger.Info(utils.LogHeader("%s EPOCH %d", epochAlias, epochInfo.CurrentEpoch))
logger.Info(utils.LogHeader("Epoch Start Time: %s", epochInfo.CurrentEpochStartTime))
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeEpochEnd,
sdk.NewAttribute(types.AttributeEpochNumber, strconv.FormatInt(epochInfo.CurrentEpoch, 10)),
),
)
k.AfterEpochEnd(ctx, epochInfo)
default:
// continue
return false
}
k.SetEpochInfo(ctx, epochInfo)
k.BeforeEpochStart(ctx, epochInfo)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeEpochStart,
sdk.NewAttribute(types.AttributeEpochNumber, strconv.FormatInt(epochInfo.CurrentEpoch, 10)),
sdk.NewAttribute(types.AttributeEpochStartTime, strconv.FormatInt(epochInfo.CurrentEpochStartTime.Unix(), 10)),
),
)
return false
})
}
func startInitialEpoch(epochInfo types.EpochInfo) types.EpochInfo {
epochInfo.EpochCountingStarted = true
epochInfo.CurrentEpoch = 1
epochInfo.CurrentEpochStartTime = epochInfo.StartTime
return epochInfo
}
func endEpoch(epochInfo types.EpochInfo) types.EpochInfo {
epochInfo.CurrentEpoch++
epochInfo.CurrentEpochStartTime = epochInfo.CurrentEpochStartTime.Add(epochInfo.Duration)
return epochInfo
}