-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.go
More file actions
333 lines (288 loc) · 9.74 KB
/
module.go
File metadata and controls
333 lines (288 loc) · 9.74 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, NASD Inc. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.
package nova
import (
"context"
"encoding/json"
"fmt"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/event"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ismv1 "github.com/noble-assets/nova/api/ism/v1"
modulev1 "github.com/noble-assets/nova/api/module/v1"
novav1 "github.com/noble-assets/nova/api/v1"
"github.com/noble-assets/nova/client/cli"
"github.com/noble-assets/nova/keeper"
ismkeeper "github.com/noble-assets/nova/keeper/ism"
"github.com/noble-assets/nova/types"
ismtypes "github.com/noble-assets/nova/types/ism"
)
// ConsensusVersion defines the current Nova module consensus version.
const ConsensusVersion = 1
var (
_ module.AppModuleBasic = AppModule{}
_ appmodule.AppModule = AppModule{}
_ module.HasConsensusVersion = AppModule{}
_ module.HasGenesis = AppModule{}
_ module.HasGenesisBasics = AppModuleBasic{}
_ module.HasServices = AppModule{}
)
//
type AppModuleBasic struct{}
func NewAppModuleBasic() AppModuleBasic {
return AppModuleBasic{}
}
func (AppModuleBasic) Name() string { return types.ModuleName }
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterLegacyAminoCodec(cdc)
}
func (AppModuleBasic) RegisterInterfaces(reg codectypes.InterfaceRegistry) {
types.RegisterInterfaces(reg)
}
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
if err := ismtypes.RegisterQueryHandlerClient(context.Background(), mux, ismtypes.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
}
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesisState())
}
func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error {
var genesis types.GenesisState
if err := cdc.UnmarshalJSON(bz, &genesis); err != nil {
return fmt.Errorf("failed to unmarshal Nova genesis state: %w", err)
}
return genesis.Validate()
}
//
type AppModule struct {
AppModuleBasic
keeper *keeper.Keeper
ismKeeper *ismkeeper.Keeper
}
func NewAppModule(keeper *keeper.Keeper, ismKeeper *ismkeeper.Keeper) AppModule {
return AppModule{
AppModuleBasic: NewAppModuleBasic(),
keeper: keeper,
ismKeeper: ismKeeper,
}
}
func (AppModule) IsOnePerModuleType() {}
func (AppModule) IsAppModule() {}
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
func (m AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, bz json.RawMessage) {
var genesis types.GenesisState
cdc.MustUnmarshalJSON(bz, &genesis)
m.keeper.InitGenesis(ctx, genesis)
m.ismKeeper.InitGenesis(ctx, genesis.Ism)
}
func (m AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
genesis := m.keeper.ExportGenesis(ctx)
genesis.Ism = m.ismKeeper.ExportGenesis(ctx)
return cdc.MustMarshalJSON(genesis)
}
func (m AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(m.keeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(m.keeper))
ismtypes.RegisterMsgServer(cfg.MsgServer(), ismkeeper.NewMsgServer(m.ismKeeper))
ismtypes.RegisterQueryServer(cfg.QueryServer(), ismkeeper.NewQueryServer(m.ismKeeper))
}
//
func (AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
return &autocliv1.ModuleOptions{
Tx: &autocliv1.ServiceCommandDescriptor{
Service: novav1.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "SetEpochLength",
Use: "set-epoch-length [epoch-length]",
Short: "Set a new epoch length (authority gated)",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "epoch_length"}},
},
{
RpcMethod: "SetHookAddress",
Use: "set-hook-address [hook-address]",
Short: "Set a new hook address (authority gated)",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "hook_address"}},
},
{
RpcMethod: "SetEnrolledValidators",
Use: "set-enrolled-validators [enrolled-validators ...]",
Short: "Set new enrolled validators (authority gated)",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "enrolled_validators", Varargs: true},
},
},
},
SubCommands: map[string]*autocliv1.ServiceCommandDescriptor{
"ism": {
Service: ismv1.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Pause",
Use: "pause",
Short: "Pause the ISM (authority gated)",
},
{
RpcMethod: "Unpause",
Use: "unpause",
Short: "Unpause the ISM (authority gated)",
},
},
Short: "Transaction commands for the Nova ISM submodule",
},
},
},
Query: &autocliv1.ServiceCommandDescriptor{
Service: novav1.Query_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Config",
Use: "config",
Short: "Query the module configuration",
},
{
RpcMethod: "PendingEpoch",
Use: "pending-epoch",
Short: "Query the currently pending epoch",
},
{
RpcMethod: "FinalizedEpochs",
Use: "finalized-epochs",
Short: "Query all finalized epochs",
},
// NOTE: LatestFinalizedEpoch and FinalizedEpoch are combined into a single custom command.
{
RpcMethod: "LatestFinalizedEpoch",
Skip: true,
},
{
RpcMethod: "FinalizedEpoch",
Skip: true,
},
{
RpcMethod: "StateRoots",
Use: "state-roots",
Short: "Query all finalized state roots",
},
// NOTE: LastestStateRoot and StateRoot are combined into a single custom command.
{
RpcMethod: "LatestStateRoot",
Skip: true,
},
{
RpcMethod: "StateRoot",
Skip: true,
},
{
RpcMethod: "MailboxRoots",
Use: "mailbox-roots",
Short: "Query all finalized mailbox roots",
},
// NOTE: LatestMailboxRoot and MailboxRoot are combined into a single custom command.
{
RpcMethod: "LatestMailboxRoot",
Skip: true,
},
{
RpcMethod: "MailboxRoot",
Skip: true,
},
},
SubCommands: map[string]*autocliv1.ServiceCommandDescriptor{
"ism": {
Service: ismv1.Query_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Paused",
Use: "paused",
Short: "Query paused state",
},
},
Short: "Querying commands for the Nova ISM submodule",
},
},
EnhanceCustomCommand: true,
},
}
}
func (AppModule) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd()
}
//
func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(ProvideModule),
)
}
type ModuleInputs struct {
depinject.In
Config *modulev1.Module
Codec codec.Codec
StoreService store.KVStoreService
EventService event.Service
Logger log.Logger
StakingKeeper types.StakingKeeper
HyperlaneKeeper ismtypes.HyperlaneKeeper
AppOpts servertypes.AppOptions `optional:"true"`
Viper *viper.Viper `optional:"true"`
}
type ModuleOutputs struct {
depinject.Out
Keeper *keeper.Keeper
IsmKeeper *ismkeeper.Keeper
Module appmodule.AppModule
}
func ProvideModule(in ModuleInputs) ModuleOutputs {
if in.Config.Authority == "" {
panic("authority for nova module must be set")
}
var rpcAddress string
if in.Viper != nil { // viper takes precedence over app options
rpcAddress = in.Viper.GetString(FlagRPCAddress)
} else if in.AppOpts != nil {
rpcAddress = cast.ToString(in.AppOpts.Get(FlagRPCAddress))
}
if rpcAddress == "" {
rpcAddress = DefaultRPCAddress
}
authority := authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
k := keeper.NewKeeper(authority.String(), in.Codec, in.StoreService, in.EventService, in.Logger, rpcAddress, in.StakingKeeper)
ismKeeper := ismkeeper.NewKeeper(authority.String(), in.StoreService, in.EventService, in.Logger, k, in.HyperlaneKeeper)
m := NewAppModule(k, ismKeeper)
return ModuleOutputs{Keeper: k, IsmKeeper: ismKeeper, Module: m}
}