-
Notifications
You must be signed in to change notification settings - Fork 201
/
consumer.go
159 lines (134 loc) · 5.16 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cmd
import (
"encoding/json"
"fmt"
"strconv"
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/v4/x/ccv/consumer/types"
ccvtypes "github.com/cosmos/interchain-security/v4/x/ccv/types"
"github.com/spf13/cobra"
"github.com/Stride-Labs/stride/v22/testutil"
)
func AddConsumerSectionCmd(defaultNodeHome string) *cobra.Command {
genesisMutator := NewDefaultGenesisIO()
txCmd := &cobra.Command{
Use: "add-consumer-section [num_nodes]",
Args: cobra.ExactArgs(1),
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 {
numNodes, err := strconv.Atoi(args[0])
if err != nil {
return errorsmod.Wrap(err, "invalid number of nodes")
} else if numNodes == 0 {
return errorsmod.Wrap(nil, "num_nodes can not be zero")
}
return genesisMutator.AlterConsumerModuleState(cmd, func(state *GenesisData, _ map[string]json.RawMessage) error {
initialValset := []types1.ValidatorUpdate{}
genesisState := testutil.CreateMinimalConsumerTestGenesis()
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
homeDir := clientCtx.HomeDir
for i := 1; i <= numNodes; i++ {
homeDir = fmt.Sprintf("%s%d", homeDir[:len(homeDir)-1], i)
config.SetRoot(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 = append(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.Provider.InitialValSet = initialValset
genesisState.Provider.ConsensusState.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 *ccvtypes.ConsumerGenesisState
}
func NewGenesisData(genesisFile string, genDoc *tmtypes.GenesisDoc, appState map[string]json.RawMessage, consumerModuleState *ccvtypes.ConsumerGenesisState) *GenesisData {
return &GenesisData{GenesisFile: genesisFile, GenDoc: genDoc, AppState: appState, ConsumerModuleState: consumerModuleState}
}