-
Notifications
You must be signed in to change notification settings - Fork 4
/
module.go
executable file
·366 lines (311 loc) · 12.4 KB
/
module.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package oracle
import (
"context"
"encoding/json"
"errors"
"math"
abci "github.com/cometbft/cometbft/abci/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
oraclecli "github.com/ODIN-PROTOCOL/odin-core/x/oracle/client/cli"
oraclekeeper "github.com/ODIN-PROTOCOL/odin-core/x/oracle/keeper"
oracletypes "github.com/ODIN-PROTOCOL/odin-core/x/oracle/types"
)
var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
// AppModuleBasic is Band Oracle's module basic object.
type AppModuleBasic struct {
cdc codec.Codec
}
// Name returns this module's name - "oracle" (SDK AppModuleBasic interface).
func (AppModuleBasic) Name() string {
return oracletypes.ModuleName
}
// RegisterLegacyAminoCodec registers the staking module's oracletypes on the given LegacyAmino codec.
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
oracletypes.RegisterLegacyAminoCodec(cdc)
}
// RegisterInterfaces registers the module's interface oracletypes
func (b AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
oracletypes.RegisterInterfaces(registry)
}
// DefaultGenesis returns the default genesis state as raw bytes.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(oracletypes.DefaultGenesisState())
}
// ValidateGenesis validates genesis
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
var gs oracletypes.GenesisState
err := cdc.UnmarshalJSON(bz, &gs)
if err != nil {
return err
}
return gs.Validate()
}
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the oracle module.
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
err := oracletypes.RegisterQueryHandlerClient(context.WithValue(context.Background(), client.ClientContextKey, clientCtx), mux, oracletypes.NewQueryClient(clientCtx))
if err != nil {
panic(err)
}
}
// GetTxCmd returns cobra CLI command to send txs for this module (SDK AppModuleBasic interface).
func (AppModuleBasic) GetTxCmd() *cobra.Command {
return oraclecli.GetTxCmd()
}
// GetQueryCmd returns cobra CLI command to query chain state (SDK AppModuleBasic interface).
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return oraclecli.GetQueryCmd()
}
// AppModule represents the AppModule for this module.
type AppModule struct {
AppModuleBasic
keeper oraclekeeper.Keeper
}
func (am AppModule) NegotiateAppVersion(ctx sdk.Context, order channeltypes.Order, connectionID string, portID string, counterparty channeltypes.Counterparty, proposedVersion string) (version string, err error) {
if proposedVersion != oracletypes.Version {
return "", sdkerrors.Wrapf(oracletypes.ErrInvalidVersion, "failed to negotiate app version: expected %s, got %s", oracletypes.Version, proposedVersion)
}
return oracletypes.Version, nil
}
// NewAppModule creates a new AppModule object.
func NewAppModule(k oraclekeeper.Keeper) AppModule {
return AppModule{
keeper: k,
}
}
// RegisterInvariants is a noop function to satisfy SDK AppModule interface.
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {}
// QuerierRoute returns the oracle module's querier route name.
func (AppModule) QuerierRoute() string {
return oracletypes.QuerierRoute
}
// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
oracletypes.RegisterMsgServer(cfg.MsgServer(), oraclekeeper.NewMsgServerImpl(am.keeper))
oracletypes.RegisterQueryServer(cfg.QueryServer(), oraclekeeper.Querier{Keeper: am.keeper})
}
// BeginBlock processes ABCI begin block message for this oracle module (SDK AppModule interface).
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
handleBeginBlock(ctx, req, am.keeper)
}
// EndBlock processes ABCI end block message for this oracle module (SDK AppModule interface).
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
handleEndBlock(ctx, am.keeper)
return []abci.ValidatorUpdate{}
}
// InitGenesis performs genesis initialization for the oracle module.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState oracletypes.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
oraclekeeper.InitGenesis(ctx, am.keeper, &genesisState)
return []abci.ValidatorUpdate{}
}
// ExportGenesis returns the current state as genesis raw bytes.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
gs := oraclekeeper.ExportGenesis(ctx, am.keeper)
return cdc.MustMarshalJSON(gs)
}
//____________________________________________________________________________
// AppModuleSimulation functions
// GenerateGenesisState creates a randomized GenState of the transfer module.
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
// simulation.RandomizedGenState(simState)
}
// ProposalContents doesn't return any content functions for governance proposals.
func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent {
return nil
}
// RegisterStoreDecoder registers a decoder for transfer module's oracletypes
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
// sdr[oracletypes.StoreKey] = simulation.NewDecodeStore(am.keeperoraclekeeper)
}
// WeightedOperations returns the all the transfer module operations with their respective weights.
func (am AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation {
return nil
}
//____________________________________________________________________________
// ValidateOracleChannelParams does validation of a newly created oracle channel. A oracle
// channel must be UNORDERED, use the correct port (by default 'oracle'), and use the current
// supported version. Only 2^32 channels are allowed to be created.
func ValidateOracleChannelParams(
ctx sdk.Context,
keeper oraclekeeper.Keeper,
order channeltypes.Order,
portID string,
channelID string,
) error {
// NOTE: for escrow address security only 2^32 channels are allowed to be created
// Issue: https://github.com/cosmos/cosmos-sdk/issues/7737
channelSequence, err := channeltypes.ParseChannelSequence(channelID)
if err != nil {
return err
}
if channelSequence > uint64(math.MaxUint32) {
return sdkerrors.Wrapf(oracletypes.ErrMaxOracleChannels, "channel sequence %d is greater than max allowed oracle channels %d", channelSequence, uint64(math.MaxUint32))
}
if order != channeltypes.UNORDERED {
return sdkerrors.Wrapf(channeltypes.ErrInvalidChannelOrdering, "expected %s channel, got %s ", channeltypes.UNORDERED, order)
}
// Require portID is the portID oracle module is bound to
boundPort := keeper.GetPort(ctx)
if boundPort != portID {
return sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort)
}
return nil
}
// IBCModule implements the ICS26 interface for transfer given the transfer keeper.
type IBCModule struct {
keeper oraclekeeper.Keeper
}
// NewIBCModule creates a new IBCModule given the keeper
func NewIBCModule(k oraclekeeper.Keeper) IBCModule {
return IBCModule{
keeper: k,
}
}
// OnChanOpenInit implements the IBCModule interface
func (am IBCModule) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID string,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
version string,
) (string, error) {
if err := ValidateOracleChannelParams(ctx, am.keeper, order, portID, channelID); err != nil {
return "", err
}
if version != oracletypes.Version {
return "", sdkerrors.Wrapf(oracletypes.ErrInvalidVersion, "got %s, expected %s", version, oracletypes.Version)
}
// Claim channel capability passed back by IBC module
if err := am.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
return "", err
}
return "", nil
}
// OnChanOpenTry implements the IBCModule interface
func (am IBCModule) OnChanOpenTry(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
counterpartyVersion string,
) (string, error) {
if err := ValidateOracleChannelParams(ctx, am.keeper, order, portID, channelID); err != nil {
return "", err
}
if counterpartyVersion != oracletypes.Version {
return "", sdkerrors.Wrapf(oracletypes.ErrInvalidVersion, "invalid counterparty version: got: %s, expected %s", counterpartyVersion, oracletypes.Version)
}
// Module may have already claimed capability in OnChanOpenInit in the case of crossing hellos
// (ie chainA and chainB both call ChanOpenInit before one of them calls ChanOpenTry)
// If module can already authenticate the capability then module already owns it so we don't need to claim
// Otherwise, module does not have channel capability and we must claim it from IBC
if !am.keeper.AuthenticateCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)) {
// Only claim channel capability passed back by IBC module if we do not already own it
if err := am.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
return "", err
}
}
return oracletypes.Version, nil
}
// OnChanOpenAck implements the IBCModule interface
func (am IBCModule) OnChanOpenAck(
ctx sdk.Context,
portID,
channelID string,
_ string,
counterpartyVersion string,
) error {
if counterpartyVersion != oracletypes.Version {
return sdkerrors.Wrapf(oracletypes.ErrInvalidVersion, "invalid counterparty version: %s, expected %s", counterpartyVersion, oracletypes.Version)
}
return nil
}
// OnChanOpenConfirm implements the IBCModule interface
func (am IBCModule) OnChanOpenConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
return nil
}
// OnChanCloseInit implements the IBCModule interface
func (am IBCModule) OnChanCloseInit(
ctx sdk.Context,
portID,
channelID string,
) error {
// Disallow user-initiated channel closing for oracle channels
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel")
}
// OnChanCloseConfirm implements the IBCModule interface
func (am IBCModule) OnChanCloseConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
return nil
}
// OnRecvPacket implements the IBCModule interface
func (am IBCModule) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) ibcexported.Acknowledgement {
var data oracletypes.OracleRequestPacketData
if err := oracletypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
// TODO: Ack with non-deterministic error break consensus?
return channeltypes.NewErrorAcknowledgement(errors.New("cannot unmarshal oracle request packet data"))
}
id, err := am.keeper.OnRecvPacket(ctx, packet, data, relayer)
if err != nil {
return channeltypes.NewErrorAcknowledgement(errors.New(err.Error()))
}
return channeltypes.NewResultAcknowledgement(oracletypes.ModuleCdc.MustMarshalJSON(oracletypes.NewOracleRequestPacketAcknowledgement(id)))
}
// OnAcknowledgementPacket implements the IBCModule interface
func (am IBCModule) OnAcknowledgementPacket(
ctx sdk.Context,
packet channeltypes.Packet,
acknowledgement []byte,
relayer sdk.AccAddress,
) error {
// Do nothing for out-going packet
return nil
}
// OnTimeoutPacket implements the IBCModule interface
func (am IBCModule) OnTimeoutPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) error {
// Do nothing for out-going packet
return nil
}
// ConsensusVersion returns the current module store definitions version
func (am AppModule) ConsensusVersion() uint64 {
return oracletypes.ModuleVersion
}