-
Notifications
You must be signed in to change notification settings - Fork 197
/
consumer.go
143 lines (120 loc) · 4.6 KB
/
consumer.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cmd
import (
"encoding/json"
"fmt"
errorsmod "cosmossdk.io/errors"
types1 "github.com/cometbft/cometbft/abci/types"
pvm "github.com/cometbft/cometbft/privval"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
ccvconsumertypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
"github.com/spf13/cobra"
"github.com/Stride-Labs/stride/v12/testutil"
)
func AddConsumerSectionCmd(defaultNodeHome string) *cobra.Command {
genesisMutator := NewDefaultGenesisIO()
txCmd := &cobra.Command{
Use: "add-consumer-section",
Short: "ONLY FOR TESTING PURPOSES! Modifies genesis so that chain can be started locally with one node.",
SuggestionsMinimumDistance: 2,
RunE: func(cmd *cobra.Command, args []string) error {
return genesisMutator.AlterConsumerModuleState(cmd, func(state *GenesisData, _ map[string]json.RawMessage) error {
genesisState := testutil.CreateMinimalConsumerTestGenesis()
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
config.SetRoot(clientCtx.HomeDir)
privValidator := pvm.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
pk, err := privValidator.GetPubKey()
if err != nil {
return err
}
sdkPublicKey, err := cryptocodec.FromTmPubKeyInterface(pk)
if err != nil {
return err
}
tmProtoPublicKey, err := cryptocodec.ToTmProtoPublicKey(sdkPublicKey)
if err != nil {
return err
}
initialValset := []types1.ValidatorUpdate{{PubKey: tmProtoPublicKey, Power: 100}}
vals, err := tmtypes.PB2TM.ValidatorUpdates(initialValset)
if err != nil {
return errorsmod.Wrap(err, "could not convert val updates to validator set")
}
genesisState.InitialValSet = initialValset
genesisState.ProviderConsensusState.NextValidatorsHash = tmtypes.NewValidatorSet(vals).Hash()
state.ConsumerModuleState = genesisState
return nil
})
},
}
txCmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
flags.AddQueryFlagsToCmd(txCmd)
return txCmd
}
type GenesisMutator interface {
AlterConsumerModuleState(cmd *cobra.Command, callback func(state *GenesisData, appState map[string]json.RawMessage) error) error
}
type DefaultGenesisIO struct {
DefaultGenesisReader
}
func NewDefaultGenesisIO() *DefaultGenesisIO {
return &DefaultGenesisIO{DefaultGenesisReader: DefaultGenesisReader{}}
}
func (x DefaultGenesisIO) AlterConsumerModuleState(cmd *cobra.Command, callback func(state *GenesisData, appState map[string]json.RawMessage) error) error {
g, err := x.ReadGenesis(cmd)
if err != nil {
return err
}
if err := callback(g, g.AppState); err != nil {
return err
}
if err := g.ConsumerModuleState.Validate(); err != nil {
return err
}
clientCtx := client.GetClientContextFromCmd(cmd)
consumerGenStateBz, err := clientCtx.Codec.MarshalJSON(g.ConsumerModuleState)
if err != nil {
return errorsmod.Wrap(err, "marshal consumer genesis state")
}
g.AppState[ccvconsumertypes.ModuleName] = consumerGenStateBz
appStateJSON, err := json.Marshal(g.AppState)
if err != nil {
return errorsmod.Wrap(err, "marshal application genesis state")
}
g.GenDoc.AppState = appStateJSON
return genutil.ExportGenesisFile(g.GenDoc, g.GenesisFile)
}
type DefaultGenesisReader struct{}
func (d DefaultGenesisReader) ReadGenesis(cmd *cobra.Command) (*GenesisData, 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)
}
return NewGenesisData(
genFile,
genDoc,
appState,
nil,
), nil
}
type GenesisData struct {
GenesisFile string
GenDoc *tmtypes.GenesisDoc
AppState map[string]json.RawMessage
ConsumerModuleState *ccvconsumertypes.GenesisState
}
func NewGenesisData(genesisFile string, genDoc *tmtypes.GenesisDoc, appState map[string]json.RawMessage, consumerModuleState *ccvconsumertypes.GenesisState) *GenesisData {
return &GenesisData{GenesisFile: genesisFile, GenDoc: genDoc, AppState: appState, ConsumerModuleState: consumerModuleState}
}