/
genesis.go
107 lines (96 loc) · 2.73 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package keeper
import (
"bytes"
"fmt"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/strangelove-ventures/hyperlane-cosmos/x/ism/types"
)
// InitGenesis initializes the hyperlane ISM module's state from a provided genesis
// state.
func (k Keeper) InitGenesis(ctx sdk.Context, gs types.GenesisState) error {
for _, originIsm := range gs.DefaultIsm {
ism, err := types.UnpackAbstractIsm(originIsm.AbstractIsm)
if err != nil {
return err
}
err = k.storeDefaultIsm(ctx, originIsm.Origin, ism)
if err != nil {
return err
}
}
for _, customIsm := range gs.CustomIsm {
ism, err := types.UnpackAbstractIsm(customIsm.AbstractIsm)
if err != nil {
return err
}
err = k.storeCustomIsm(ctx, customIsm.Index, ism)
if err != nil {
return err
}
}
return nil
}
// ExportGenesis returns the hyperlane ISM module's exported genesis.
func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
return types.GenesisState{
DefaultIsm: k.ExportDefaultIsms(ctx),
CustomIsm: k.ExportCustomIsms(ctx),
}
}
// ExportDefaultIsms return the default ISMs
func (k Keeper) ExportDefaultIsms(ctx sdk.Context) []types.DefaultIsm {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.KeyOriginsDefaultIsm))
defer iterator.Close()
var defaultIsms []types.DefaultIsm
for ; iterator.Valid(); iterator.Next() {
originBz := bytes.TrimPrefix(iterator.Key(), []byte(fmt.Sprintf("%s/", types.KeyOriginsDefaultIsm)))
origin, err := strconv.ParseUint(string(originBz), 10, 32)
if err != nil {
panic(err)
}
var ism types.AbstractIsm
err = k.cdc.UnmarshalInterface(iterator.Value(), &ism)
if err != nil {
panic(err)
}
ismAny, err := types.PackAbstractIsm(ism)
if err != nil {
panic(err)
}
defaultIsms = append(defaultIsms, types.DefaultIsm{
Origin: uint32(origin),
AbstractIsm: ismAny,
})
}
return defaultIsms
}
// ExportCustomIsms return the custom ISMs
func (k Keeper) ExportCustomIsms(ctx sdk.Context) []types.CustomIsm {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.KeyCustomIsm))
defer iterator.Close()
var customIsms []types.CustomIsm
for ; iterator.Valid(); iterator.Next() {
indexBz := bytes.TrimPrefix(iterator.Key(), []byte(fmt.Sprintf("%s/", types.KeyCustomIsm)))
index, err := strconv.ParseUint(string(indexBz), 10, 32)
if err != nil {
panic(err)
}
var ism types.AbstractIsm
err = k.cdc.UnmarshalInterface(iterator.Value(), &ism)
if err != nil {
panic(err)
}
ismAny, err := types.PackAbstractIsm(ism)
if err != nil {
panic(err)
}
customIsms = append(customIsms, types.CustomIsm{
Index: uint32(index),
AbstractIsm: ismAny,
})
}
return customIsms
}