-
Notifications
You must be signed in to change notification settings - Fork 44
/
abci.go
64 lines (48 loc) · 1.87 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
package keeper
import (
"strconv"
"time"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/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)
shouldEpochEnd := ctx.BlockTime().After(epochEndTime) && !shouldInitialEpochStart && !epochInfo.StartTime.After(ctx.BlockTime())
epochInfo.CurrentEpochStartHeight = ctx.BlockHeight()
switch {
case shouldInitialEpochStart:
epochInfo.StartInitialEpoch()
logger.Debug("starting epoch", "identifier", epochInfo.Identifier)
case shouldEpochEnd:
epochInfo.EndEpoch()
logger.Debug("ending epoch", "identifier", epochInfo.Identifier)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeEpochEnd,
sdk.NewAttribute(types.AttributeEpochNumber, strconv.FormatInt(epochInfo.CurrentEpoch, 10)),
),
)
k.AfterEpochEnd(ctx, epochInfo.Identifier, epochInfo.CurrentEpoch)
default:
// continue
return false
}
k.SetEpochInfo(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)),
),
)
k.BeforeEpochStart(ctx, epochInfo.Identifier, epochInfo.CurrentEpoch)
return false
})
}