/
genesis.go
65 lines (54 loc) · 1.85 KB
/
genesis.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
package checkpoint
import (
"errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/maticnetwork/heimdall/checkpoint/types"
hmTypes "github.com/maticnetwork/heimdall/types"
)
// InitGenesis sets distribution information for genesis.
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {
keeper.SetParams(ctx, data.Params)
// Set last no-ack
if data.LastNoACK > 0 {
keeper.SetLastNoAck(ctx, data.LastNoACK)
}
// Add finalised checkpoints to state
if len(data.Checkpoints) != 0 {
// check if we are provided all the headers
if int(data.AckCount) != len(data.Checkpoints) {
panic(errors.New("Incorrect state in state-dump , Please Check "))
}
// sort headers before loading to state
data.Checkpoints = hmTypes.SortHeaders(data.Checkpoints)
// load checkpoints to state
for i, checkpoint := range data.Checkpoints {
checkpointIndex := uint64(i) + 1
if err := keeper.AddCheckpoint(ctx, checkpointIndex, checkpoint); err != nil {
keeper.Logger(ctx).Error("InitGenesis | AddCheckpoint",
"checkpointIndex", checkpointIndex,
"checkpoint", checkpoint.String(),
"error", err)
}
}
}
// Add checkpoint in buffer
if data.BufferedCheckpoint != nil {
if err := keeper.SetCheckpointBuffer(ctx, *data.BufferedCheckpoint); err != nil {
keeper.Logger(ctx).Error("InitGenesis | SetCheckpointBuffer", "error", err)
}
}
// Set initial ack count
keeper.UpdateACKCountWithValue(ctx, data.AckCount)
}
// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
params := keeper.GetParams(ctx)
bufferedCheckpoint, _ := keeper.GetCheckpointFromBuffer(ctx)
return types.NewGenesisState(
params,
bufferedCheckpoint,
keeper.GetLastNoAck(ctx),
keeper.GetACKCount(ctx),
hmTypes.SortHeaders(keeper.GetCheckpoints(ctx)),
)
}