-
Notifications
You must be signed in to change notification settings - Fork 10
/
genesisio.go
125 lines (107 loc) · 3.95 KB
/
genesisio.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cli
import (
"encoding/json"
"fmt"
wasmcli "github.com/CosmWasm/wasmd/x/wasm/client/cli"
"github.com/cosmos/cosmos-sdk/server"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/confio/tgrade/x/twasm/types"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/client"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/spf13/cobra"
)
var _ wasmcli.GenesisMutator = GenesisIO{}
// GenesisIO to alter the genesis state for this module. To be used with the wasm cli extension point.
type GenesisIO struct {
GenesisReader
}
func NewGenesisIO() *GenesisIO {
return &GenesisIO{GenesisReader: GenesisReader{}}
}
// AlterWasmModuleState loads the genesis from the default or set home dir,
// unmarshalls the wasm module section into the object representation
// calls the callback function to modify it
// and marshals the modified state back into the genesis file
func (x GenesisIO) AlterWasmModuleState(cmd *cobra.Command, callback func(state *wasmtypes.GenesisState, appState map[string]json.RawMessage) error) error {
return x.AlterTWasmModuleState(cmd, func(state *types.GenesisState, appState map[string]json.RawMessage) error {
wasmState := state.RawWasmState()
if err := callback(&wasmState, appState); err != nil {
return err
}
// update genesis messages as they can be modified
state.GenMsgs = wasmState.GenMsgs
return nil
})
}
// AlterTWasmModuleState loads the genesis from the default or set home dir,
// unmarshalls the twasm module section into the object representation
// calls the callback function to modify it
// and marshals the modified state back into the genesis file
func (x GenesisIO) AlterTWasmModuleState(cmd *cobra.Command, callback func(state *types.GenesisState, appState map[string]json.RawMessage) error) error {
g, err := x.ReadTWasmGenesis(cmd)
if err != nil {
return err
}
if err := callback(&g.twasmModuleState, g.AppState); err != nil {
return err
}
// and store update
if err := g.twasmModuleState.ValidateBasic(); err != nil {
return err
}
clientCtx := client.GetClientContextFromCmd(cmd)
twasmGenStateBz, err := clientCtx.Codec.MarshalJSON(&g.twasmModuleState)
if err != nil {
return sdkerrors.Wrap(err, "marshal twasm genesis state")
}
g.AppState[types.ModuleName] = twasmGenStateBz
appStateJSON, err := json.Marshal(g.AppState)
if err != nil {
return sdkerrors.Wrap(err, "marshal application genesis state")
}
g.GenDoc.AppState = appStateJSON
return genutil.ExportGenesisFile(g.GenDoc, g.GenesisFile)
}
// TWasmGenesisData extends the wasmcli GenesisData for this module state.
type TWasmGenesisData struct {
*wasmcli.GenesisData
twasmModuleState types.GenesisState
}
var _ wasmcli.GenesisReader = GenesisReader{}
// GenesisReader reads the genesis data for this module. To be used with the wasm cli extension point
type GenesisReader struct{}
func (d GenesisReader) ReadTWasmGenesis(cmd *cobra.Command) (*TWasmGenesisData, error) {
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
config.SetRoot(clientCtx.HomeDir)
genFile := config.GenesisFile()
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal genesis state: %w", err)
}
var twasmGenesisState types.GenesisState
if appState[types.ModuleName] != nil {
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx.Codec.MustUnmarshalJSON(appState[types.ModuleName], &twasmGenesisState)
}
wasmState := twasmGenesisState.RawWasmState()
return &TWasmGenesisData{
GenesisData: wasmcli.NewGenesisData(
genFile,
genDoc,
appState,
&wasmState,
),
twasmModuleState: twasmGenesisState,
}, nil
}
func (d GenesisReader) ReadWasmGenesis(cmd *cobra.Command) (*wasmcli.GenesisData, error) {
r, err := d.ReadTWasmGenesis(cmd)
if err != nil {
return nil, err
}
return r.GenesisData, nil
}