-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathhistorical_info.go
56 lines (46 loc) · 1.61 KB
/
historical_info.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
package keeper
import (
"context"
"cosmossdk.io/x/staking/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// TrackHistoricalInfo saves the latest historical-info and deletes the oldest
// heights that are below pruning height
func (k Keeper) TrackHistoricalInfo(ctx context.Context) error {
entryNum, err := k.HistoricalEntries(ctx)
if err != nil {
return err
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
// Prune store to ensure we only have parameter-defined historical entries.
// In most cases, this will involve removing a single historical entry.
// In the rare scenario when the historical entries gets reduced to a lower value k'
// from the original value k. k - k' entries must be deleted from the store.
// Since the entries to be deleted are always in a continuous range, we can iterate
// over the historical entries starting from the most recent version to be pruned
// and then return at the first empty entry.
for i := sdkCtx.HeaderInfo().Height - int64(entryNum); i >= 0; i-- {
has, err := k.HistoricalInfo.Has(ctx, uint64(i))
if err != nil {
return err
}
if !has {
break
}
if err = k.HistoricalInfo.Remove(ctx, uint64(i)); err != nil {
return err
}
}
// if there is no need to persist historicalInfo, return
if entryNum == 0 {
return nil
}
time := sdkCtx.HeaderInfo().Time
historicalEntry := types.HistoricalRecord{
Time: &time,
ValidatorsHash: sdkCtx.CometInfo().ValidatorsHash,
Apphash: sdkCtx.HeaderInfo().AppHash,
}
// Set latest HistoricalInfo at current height
return k.HistoricalInfo.Set(ctx, uint64(sdkCtx.HeaderInfo().Height), historicalEntry)
}