-
Notifications
You must be signed in to change notification settings - Fork 36
/
module_ibc.go
167 lines (147 loc) · 5.02 KB
/
module_ibc.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
package oracle
import (
cerrs "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
icqtypes "github.com/cosmos/ibc-apps/modules/async-icq/v6/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v6/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/v6/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v6/modules/core/exported"
"github.com/provenance-io/provenance/x/oracle/types"
)
// OnChanOpenInit implements the IBCModule interface
func (am AppModule) OnChanOpenInit(
ctx sdk.Context,
_ channeltypes.Order,
_ []string,
portID string,
channelID string,
chanCap *capabilitytypes.Capability,
_ channeltypes.Counterparty,
version string,
) (string, error) {
// Require portID is the portID module is bound to
boundPort := am.keeper.GetPort(ctx)
if boundPort != portID {
return "", cerrs.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort)
}
if version != types.Version {
return "", cerrs.Wrapf(types.ErrInvalidVersion, "got %s, expected %s", version, types.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 version, nil
}
// OnChanOpenTry implements the IBCModule interface
func (am AppModule) OnChanOpenTry(
ctx sdk.Context,
_ channeltypes.Order,
_ []string,
portID,
channelID string,
chanCap *capabilitytypes.Capability,
_ channeltypes.Counterparty,
counterpartyVersion string,
) (string, error) {
// Require portID is the portID module is bound to
boundPort := am.keeper.GetPort(ctx)
if boundPort != portID {
return "", cerrs.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort)
}
if counterpartyVersion != types.Version {
return "", cerrs.Wrapf(types.ErrInvalidVersion, "invalid counterparty version: got: %s, expected %s", counterpartyVersion, types.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 types.Version, nil
}
// OnChanOpenAck implements the IBCModule interface
func (am AppModule) OnChanOpenAck(
_ sdk.Context,
_,
_ string,
_ string,
counterpartyVersion string,
) error {
if counterpartyVersion != types.Version {
return cerrs.Wrapf(types.ErrInvalidVersion, "invalid counterparty version: %s, expected %s", counterpartyVersion, types.Version)
}
return nil
}
// OnChanOpenConfirm implements the IBCModule interface
func (am AppModule) OnChanOpenConfirm(
_ sdk.Context,
_,
_ string,
) error {
return nil
}
// OnChanCloseInit implements the IBCModule interface
func (am AppModule) OnChanCloseInit(
_ sdk.Context,
_,
_ string,
) error {
// Disallow user-initiated channel closing for channels
return cerrs.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel")
}
// OnChanCloseConfirm implements the IBCModule interface
func (am AppModule) OnChanCloseConfirm(
_ sdk.Context,
_,
_ string,
) error {
return nil
}
// OnRecvPacket implements the IBCModule interface
func (am AppModule) OnRecvPacket(
_ sdk.Context,
_ channeltypes.Packet,
_ sdk.AccAddress,
) ibcexported.Acknowledgement {
return channeltypes.NewErrorAcknowledgement(cerrs.Wrapf(icqtypes.ErrInvalidChannelFlow, "oracle module can not receive packets"))
}
// OnAcknowledgementPacket implements the IBCModule interface
func (am AppModule) OnAcknowledgementPacket(
ctx sdk.Context,
modulePacket channeltypes.Packet,
acknowledgement []byte,
_ sdk.AccAddress,
) error {
var ack channeltypes.Acknowledgement
if err := types.ModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil {
return cerrs.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet acknowledgement: %v", err)
}
return am.keeper.OnAcknowledgementPacket(ctx, modulePacket, ack)
}
// OnTimeoutPacket implements the IBCModule interface
func (am AppModule) OnTimeoutPacket(
ctx sdk.Context,
modulePacket channeltypes.Packet,
_ sdk.AccAddress,
) error {
return am.keeper.OnTimeoutPacket(ctx, modulePacket)
}
// NegotiateAppVersion implements the IBCModule interface
func (am AppModule) NegotiateAppVersion(
_ sdk.Context,
_ channeltypes.Order,
_ string,
_ string,
_ channeltypes.Counterparty,
proposedVersion string,
) (version string, err error) {
return proposedVersion, nil
}