-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection.go
306 lines (251 loc) · 12.7 KB
/
connection.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
package keeper
import (
"bytes"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/dbadapter"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types"
commitmenttypes "github.com/cosmos/ibc-go/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
"github.com/cosmos/ibc-go/modules/core/exported"
dbm "github.com/tendermint/tm-db"
multivtypes "github.com/datachainlab/ibc-proxy/modules/light-clients/xx-multiv/types"
proxytypes "github.com/datachainlab/ibc-proxy/modules/light-clients/xx-proxy/types"
)
// upstream: chainA, downstream: chainB
func (k Keeper) ConnOpenTry(
ctx sdk.Context,
connectionID string, // the connection ID corresponding to chainB on chainA
upstreamPrefix exported.Prefix, // store prefix on chainA
connection connectiontypes.ConnectionEnd, // the connection corresponding to chainB on chainA (its state must be INIT)
downstreamClientState exported.ClientState, // clientState for chainB
downstreamConsensusState exported.ConsensusState, // consensusState for chainB
proxyClientState exported.ClientState, // clientState for proxy
proofInit []byte, // proof that chainA stored connectionEnd in state (on ConnOpenInit)
proofClient []byte, // proof that chainA stored chainB's clientState
proofConsensus []byte, // proof that chainA stored chainB's consensus state at consensus height
proofHeight exported.Height, // height at which relayer constructs proof of chainA storing connectionEnd in state
consensusHeight exported.Height, // latest height of chain B which chain A has stored in its chain B client
proofProxyClient []byte, // proof that chainB stored proxy's client state at proofProxyHeight
proofProxyConsensus []byte, // proof that chainB stored proxy's consensus state at proxyConsensusHeight
proofProxyHeight exported.Height, // height at which relayer constructs proof that chainB tracks proxy's client state
proxyConsensusHeight exported.Height, // height at which relayer constructs proof that chainB tracks proxy's consensus state
) error {
proxyClientState, proxyConsensusState, upstreamClientID, err := k.produceVerificationArgs(ctx, proxyClientState, proxyConsensusHeight)
if err != nil {
return err
}
_, found := k.GetProxyConnection(ctx, upstreamPrefix, upstreamClientID, connectionID)
if found {
return fmt.Errorf("connection '%v:%v' already exists", upstreamClientID, connectionID)
}
if connection.State != connectiontypes.INIT {
return fmt.Errorf("connection state must be %s", connectiontypes.INIT)
}
// Ensure that chainA stored the clientState provided in the msg
if err := k.VerifyAndProxyClientState(ctx, upstreamClientID, upstreamPrefix, connection.GetClientID(), proofHeight, proofClient, downstreamClientState); err != nil {
return err
}
// Ensure that chainA stored the correct ConsensusState of chainB or proxy at the given consensusHeight
if err := k.VerifyAndProxyClientConsensusState(
ctx, upstreamClientID, upstreamPrefix, connection.GetClientID(), proofHeight, consensusHeight, proofConsensus, downstreamConsensusState,
); err != nil {
return err
}
if dcs, ok := downstreamClientState.(*multivtypes.ClientState); ok {
downstreamClientState = dcs.GetUnderlyingClientState()
}
store := makeMemStore(k.cdc, downstreamConsensusState, proofProxyHeight)
if err := downstreamClientState.VerifyClientState(
store, k.cdc, proofProxyHeight, connection.Counterparty.GetPrefix(), connection.Counterparty.ClientId, proofProxyClient, proxyClientState,
); err != nil {
return err
}
if err := downstreamClientState.VerifyClientConsensusState(
store, k.cdc, proofProxyHeight, connection.Counterparty.ClientId, proxyConsensusHeight, connection.Counterparty.GetPrefix(), proofProxyConsensus, proxyConsensusState,
); err != nil {
return err
}
// Ensure that chainA stored expected connectionEnd in its state during ConnOpenInit
if err := k.VerifyAndProxyConnectionState(
ctx, upstreamClientID, upstreamPrefix, connection, proofHeight, proofInit, connectionID,
); err != nil {
return err
}
return nil
}
// upstream: chainA, downstream: chainB
func (k Keeper) ConnOpenAck(
ctx sdk.Context,
connectionID string, // the connection ID corresponding to chainB on chainA
upstreamPrefix exported.Prefix, // store prefix on chainA
connectionEnd connectiontypes.ConnectionEnd, // the connection corresponding to chainB on chainA (its state must be TRYOPEN)
downstreamClientState exported.ClientState, // clientState for chainB
downstreamConsensusState exported.ConsensusState, // consensusState for chainB
proxyClientState exported.ClientState, // clientState for proxy
proofTry []byte, // proof that chainA stored connectionEnd in state (on ConnOpenTry)
proofClient []byte, // proof that chainA stored chainB's clientState
proofConsensus []byte, // proof that chainA stored chainB's consensus state at consensus height
proofHeight exported.Height, // height at which relayer constructs proof of chainA storing connectionEnd in state
consensusHeight exported.Height, // latest height of chain B which chain A has stored in its chain B client
proofProxyClient []byte, // proof that chainB stored proxy's client state at proofProxyHeight
proofProxyConsensus []byte, // proof that chainB stored proxy's consensus state at proxyConsensusHeight
proofProxyHeight exported.Height, // height at which relayer constructs proof that chainB tracks proxy's client state
proxyConsensusHeight exported.Height, // height at which relayer constructs proof that chainB tracks proxy's consensus state
) error {
proxyClientState, proxyConsensusState, upstreamClientID, err := k.produceVerificationArgs(ctx, proxyClientState, proxyConsensusHeight)
if err != nil {
return err
}
_, found := k.GetProxyConnection(ctx, upstreamPrefix, upstreamClientID, connectionID)
if found {
return fmt.Errorf("connection '%v:%v' already exists", upstreamClientID, connectionID)
}
if connectionEnd.State != connectiontypes.TRYOPEN {
return sdkerrors.Wrapf(
connectiontypes.ErrInvalidConnectionState,
"connection state is not TRYOPEN (got %s)", connectiontypes.State(connectionEnd.GetState()).String(),
)
}
// Ensure that chainA stored the clientState provided in the msg
if err := k.VerifyAndProxyClientState(ctx, upstreamClientID, upstreamPrefix, connectionEnd.GetClientID(), proofHeight, proofClient, downstreamClientState); err != nil {
return err
}
// Ensure that chainA has stored the correct ConsensusState for chainA at the consensusHeight
if err := k.VerifyAndProxyClientConsensusState(
ctx, upstreamClientID, upstreamPrefix, connectionEnd.GetClientID(), proofHeight, consensusHeight, proofConsensus, downstreamConsensusState,
); err != nil {
return err
}
if dcs, ok := downstreamClientState.(*multivtypes.ClientState); ok {
downstreamClientState = dcs.GetUnderlyingClientState()
}
store := makeMemStore(k.cdc, downstreamConsensusState, proofProxyHeight)
if err := downstreamClientState.VerifyClientState(
store, k.cdc, proofProxyHeight, connectionEnd.Counterparty.GetPrefix(), connectionEnd.Counterparty.ClientId, proofProxyClient, proxyClientState,
); err != nil {
return err
}
if err := downstreamClientState.VerifyClientConsensusState(
store, k.cdc, proofProxyHeight, connectionEnd.Counterparty.ClientId, proxyConsensusHeight, connectionEnd.Counterparty.GetPrefix(), proofProxyConsensus, proxyConsensusState,
); err != nil {
return err
}
// Ensure that chainB stored expected connectionEnd in its state during ConnOpenTry
if err := k.VerifyAndProxyConnectionState(
ctx, upstreamClientID, upstreamPrefix, connectionEnd, proofHeight, proofTry, connectionID,
); err != nil {
return err
}
return nil
}
// upstream: chainA, downstream: chainB
func (k Keeper) ConnOpenConfirm(
ctx sdk.Context,
connectionID string, // the connection ID corresponding to chainB on chainA
upstreamClientID string, // the client ID corresponding to light client for chainA on chainB
upstreamPrefix exported.Prefix, // store prefix on chainA
counterpartyConnectionID string, // the connection ID corresponding to chainA on chainB
proofAck []byte, // proof that connection opened on chainA during ConnOpenAck
proofHeight exported.Height, // height that relayer constructed proofAck
) error {
connectionEnd, found := k.GetProxyConnection(ctx, upstreamPrefix, upstreamClientID, connectionID)
if !found {
return sdkerrors.Wrapf(
connectiontypes.ErrConnectionNotFound,
"connection '%#v:%v:%v' not found", upstreamPrefix, upstreamClientID, connectionID,
)
}
if connectionEnd.State != connectiontypes.INIT {
return sdkerrors.Wrapf(
connectiontypes.ErrInvalidConnectionState,
"connection state is not INIT (got %s)", connectiontypes.State(connectionEnd.GetState()).String(),
)
}
connectionEnd.State = connectiontypes.OPEN
connectionEnd.Counterparty.ConnectionId = counterpartyConnectionID
// Ensure that chainA stored expected connectionEnd in its state during ConnOpenAck
if err := k.VerifyAndProxyConnectionState(
ctx, upstreamClientID, upstreamPrefix, connectionEnd, proofHeight, proofAck, connectionID,
); err != nil {
return err
}
return nil
}
// upstream: chainA, downstream: chainB
func (k Keeper) ConnOpenFinalize(
ctx sdk.Context,
connectionID string, // the connection ID corresponding to chainB on chainA
upstreamClientID string, // the client ID corresponding to light client for chainA on chainB
upstreamPrefix exported.Prefix, // store prefix on chainA
proofConfirm []byte, // proof that connection opened on chainA during ConnOpenConfirm
proofHeight exported.Height, // height that relayer constructed proofConfirm
) error {
connectionEnd, found := k.GetProxyConnection(ctx, upstreamPrefix, upstreamClientID, connectionID)
if !found {
return sdkerrors.Wrapf(
connectiontypes.ErrConnectionNotFound,
"connection '%#v:%v:%v' not found", upstreamPrefix, upstreamClientID, connectionID,
)
}
if connectionEnd.State != connectiontypes.TRYOPEN {
return sdkerrors.Wrapf(
connectiontypes.ErrInvalidConnectionState,
"connection state is not TRYOPEN (got %s)", connectiontypes.State(connectionEnd.GetState()).String(),
)
}
connectionEnd.State = connectiontypes.OPEN
// Ensure that chainA stored expected connectionEnd in its state during ConnOpenConfirm
if err := k.VerifyAndProxyConnectionState(
ctx, upstreamClientID, upstreamPrefix, connectionEnd, proofHeight, proofConfirm, connectionID,
); err != nil {
return err
}
return nil
}
func (k Keeper) produceVerificationArgs(ctx sdk.Context, proxyClientState exported.ClientState, proxyConsensusHeight exported.Height) (*proxytypes.ClientState, *proxytypes.ConsensusState, string, error) {
clientState, ok := proxyClientState.(*proxytypes.ClientState)
if !ok {
return nil, nil, "", fmt.Errorf("invalid client type '%v'", proxyClientState.ClientType())
}
if err := k.validateSelfClient(ctx, clientState); err != nil {
return nil, nil, "", err
}
consensusState, err := k.getSelfConsensusState(ctx, proxyConsensusHeight)
if err != nil {
return nil, nil, "", err
}
return clientState, consensusState, clientState.UpstreamClientId, nil
}
func (k Keeper) validateSelfClient(ctx sdk.Context, clientState *proxytypes.ClientState) error {
if !bytes.Equal(k.GetIBCCommitmentPrefix().(*commitmenttypes.MerklePrefix).Bytes(), clientState.IbcPrefix.Bytes()) {
return fmt.Errorf("IBC commitment prefix mismatch: %X != %X", k.GetIBCCommitmentPrefix().(*commitmenttypes.MerklePrefix).Bytes(), clientState.IbcPrefix.Bytes())
}
if !bytes.Equal(k.GetProxyCommitmentPrefix().(*commitmenttypes.MerklePrefix).Bytes(), clientState.ProxyPrefix.Bytes()) {
return fmt.Errorf("Proxy commitment prefix mismatch: %X != %X", k.GetProxyCommitmentPrefix().(*commitmenttypes.MerklePrefix).Bytes(), clientState.ProxyPrefix.Bytes())
}
return k.clientKeeper.ValidateSelfClient(ctx, clientState.GetProxyClientState())
}
func (k Keeper) getSelfConsensusState(ctx sdk.Context, consensusHeight exported.Height) (*proxytypes.ConsensusState, error) {
selfConsensusState, ok := k.clientKeeper.GetSelfConsensusState(ctx, consensusHeight)
if !ok {
return nil, fmt.Errorf("self consensus state not found: height=%v", consensusHeight)
}
anyConsensusState, err := clienttypes.PackConsensusState(selfConsensusState)
if err != nil {
return nil, err
}
return proxytypes.NewConsensusState(anyConsensusState), nil
}
func makeMemStore(cdc codec.BinaryCodec, consensusState exported.ConsensusState, proofHeight exported.Height) dbadapter.Store {
consensusStateBytes, err := clienttypes.MarshalConsensusState(cdc, consensusState)
if err != nil {
panic(err)
}
store := dbadapter.Store{DB: dbm.NewMemDB()}
store.Set(host.ConsensusStateKey(proofHeight), consensusStateBytes)
return store
}