-
Notifications
You must be signed in to change notification settings - Fork 39
/
genesis.go
58 lines (46 loc) · 1.2 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
package plan
import (
sdk "github.com/cosmos/cosmos-sdk/types"
hubtypes "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/hub/x/plan/keeper"
"github.com/sentinel-official/hub/x/plan/types"
)
func InitGenesis(ctx sdk.Context, k keeper.Keeper, state types.GenesisState) {
for _, item := range state {
addr := item.Plan.GetProviderAddress()
k.SetPlan(ctx, item.Plan)
k.SetPlanForProvider(ctx, addr, item.Plan.ID)
for _, node := range item.Nodes {
addr, err := hubtypes.NodeAddressFromBech32(node)
if err != nil {
panic(err)
}
k.SetNodeForPlan(ctx, item.Plan.ID, addr)
}
}
count := uint64(0)
for _, item := range state {
if item.Plan.ID > count {
count = item.Plan.ID
}
}
k.SetCount(ctx, count)
}
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
var (
plans = k.GetPlans(ctx)
items = make(types.GenesisPlans, 0, len(plans))
)
for _, plan := range plans {
item := types.GenesisPlan{
Plan: plan,
Nodes: []string{},
}
nodes := k.GetNodesForPlan(ctx, plan.ID)
for _, node := range nodes {
item.Nodes = append(item.Nodes, node.Address)
}
items = append(items, item)
}
return types.NewGenesisState(items)
}