-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
genesis.go
45 lines (38 loc) · 1.19 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
package capability
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/capability/keeper"
"github.com/cosmos/cosmos-sdk/x/capability/types"
)
// InitGenesis initializes the capability module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
if err := k.InitializeIndex(ctx, genState.Index); err != nil {
panic(err)
}
// set owners for each index and initialize capability
for _, genOwner := range genState.Owners {
k.SetOwners(ctx, genOwner.Index, genOwner.IndexOwners)
k.InitializeCapability(ctx, genOwner.Index, genOwner.IndexOwners)
}
}
// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
index := k.GetLatestIndex(ctx)
owners := []types.GenesisOwners{}
for i := uint64(1); i < index; i++ {
capabilityOwners, ok := k.GetOwners(ctx, i)
if !ok || len(capabilityOwners.Owners) == 0 {
continue
}
genOwner := types.GenesisOwners{
Index: i,
IndexOwners: capabilityOwners,
}
owners = append(owners, genOwner)
}
return &types.GenesisState{
Index: index,
Owners: owners,
}
}