-
Notifications
You must be signed in to change notification settings - Fork 39
/
IBCConnection.sol
217 lines (206 loc) · 7.62 KB
/
IBCConnection.sol
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
pragma solidity ^0.8.27;
import "../24-host/IBCStore.sol";
import "../25-handler/IBCMsgs.sol";
import "../24-host/IBCCommitment.sol";
import "../03-connection/IIBCConnection.sol";
library IBCConnectionLib {
event ConnectionOpenInit(
uint32 connectionId, uint32 clientId, uint32 counterpartyClientId
);
event ConnectionOpenTry(
uint32 connectionId,
uint32 clientId,
uint32 counterpartyClientId,
uint32 counterpartyConnectionId
);
event ConnectionOpenAck(
uint32 connectionId,
uint32 clientId,
uint32 counterpartyClientId,
uint32 counterpartyConnectionId
);
event ConnectionOpenConfirm(
uint32 connectionId,
uint32 clientId,
uint32 counterpartyClientId,
uint32 counterpartyConnectionId
);
}
/**
* @dev IBCConnection is a contract that implements [ICS-3](https://github.com/cosmos/ibc/tree/main/spec/core/ics-003-connection-semantics).
*/
abstract contract IBCConnectionImpl is IBCStore, IIBCConnection {
/**
* @dev connectionOpenInit initialises a connection attempt on chain A. The generated connection identifier
* is returned.
*/
function connectionOpenInit(
IBCMsgs.MsgConnectionOpenInit calldata msg_
) external override returns (uint32) {
uint32 connectionId = generateConnectionIdentifier();
IBCConnection storage connection = connections[connectionId];
connection.clientId = msg_.clientId;
connection.state = IBCConnectionState.Init;
connection.counterpartyClientId = msg_.counterpartyClientId;
commitConnection(connectionId, connection);
emit IBCConnectionLib.ConnectionOpenInit(
connectionId, msg_.clientId, msg_.counterpartyClientId
);
return connectionId;
}
/**
* @dev connectionOpenTry relays notice of a connection attempt on chain A to chain B (this
* code is executed on chain B).
*/
function connectionOpenTry(
IBCMsgs.MsgConnectionOpenTry calldata msg_
) external override returns (uint32) {
uint32 connectionId = generateConnectionIdentifier();
IBCConnection storage connection = connections[connectionId];
connection.clientId = msg_.clientId;
connection.state = IBCConnectionState.TryOpen;
connection.counterpartyClientId = msg_.counterpartyClientId;
connection.counterpartyConnectionId = msg_.counterpartyConnectionId;
IBCConnection memory expectedConnection = IBCConnection({
state: IBCConnectionState.Init,
clientId: msg_.counterpartyClientId,
counterpartyClientId: msg_.clientId,
counterpartyConnectionId: 0
});
if (
!verifyConnectionState(
connection,
msg_.proofHeight,
msg_.proofInit,
msg_.counterpartyConnectionId,
expectedConnection
)
) {
revert IBCErrors.ErrInvalidProof();
}
commitConnection(connectionId, connection);
emit IBCConnectionLib.ConnectionOpenTry(
connectionId,
msg_.clientId,
msg_.counterpartyClientId,
msg_.counterpartyConnectionId
);
return connectionId;
}
/**
* @dev connectionOpenAck relays acceptance of a connection open attempt from chain B back
* to chain A (this code is executed on chain A).
*/
function connectionOpenAck(
IBCMsgs.MsgConnectionOpenAck calldata msg_
) external override {
IBCConnection storage connection = connections[msg_.connectionId];
if (connection.state != IBCConnectionState.Init) {
revert IBCErrors.ErrInvalidConnectionState();
}
IBCConnection memory expectedConnection = IBCConnection({
state: IBCConnectionState.TryOpen,
clientId: connection.counterpartyClientId,
counterpartyClientId: connection.clientId,
counterpartyConnectionId: msg_.connectionId
});
if (
!verifyConnectionState(
connection,
msg_.proofHeight,
msg_.proofTry,
msg_.counterpartyConnectionId,
expectedConnection
)
) {
revert IBCErrors.ErrInvalidProof();
}
connection.state = IBCConnectionState.Open;
connection.counterpartyConnectionId = msg_.counterpartyConnectionId;
commitConnection(msg_.connectionId, connection);
emit IBCConnectionLib.ConnectionOpenAck(
msg_.connectionId,
connection.clientId,
connection.counterpartyClientId,
connection.counterpartyConnectionId
);
}
/**
* @dev connectionOpenConfirm confirms opening of a connection on chain A to chain B, after
* which the connection is open on both chains (this code is executed on chain B).
*/
function connectionOpenConfirm(
IBCMsgs.MsgConnectionOpenConfirm calldata msg_
) external override {
IBCConnection storage connection = connections[msg_.connectionId];
if (connection.state != IBCConnectionState.TryOpen) {
revert IBCErrors.ErrInvalidConnectionState();
}
IBCConnection memory expectedConnection = IBCConnection({
state: IBCConnectionState.Open,
clientId: connection.counterpartyClientId,
counterpartyClientId: connection.clientId,
counterpartyConnectionId: msg_.connectionId
});
if (
!verifyConnectionState(
connection,
msg_.proofHeight,
msg_.proofAck,
connection.counterpartyConnectionId,
expectedConnection
)
) {
revert IBCErrors.ErrInvalidProof();
}
connection.state = IBCConnectionState.Open;
commitConnection(msg_.connectionId, connection);
emit IBCConnectionLib.ConnectionOpenConfirm(
msg_.connectionId,
connection.clientId,
connection.counterpartyClientId,
connection.counterpartyConnectionId
);
}
function encodeConnection(
IBCConnection memory connection
) internal pure returns (bytes32) {
return keccak256(abi.encode(connection));
}
function encodeConnectionStorage(
IBCConnection storage connection
) internal pure returns (bytes32) {
return keccak256(abi.encode(connection));
}
function commitConnection(
uint32 connectionId,
IBCConnection storage connection
) internal {
commitments[IBCCommitment.connectionCommitmentKey(connectionId)] =
encodeConnectionStorage(connection);
}
function verifyConnectionState(
IBCConnection storage connection,
uint64 height,
bytes calldata proof,
uint32 connectionId,
IBCConnection memory counterpartyConnection
) internal returns (bool) {
return getClientInternal(connection.clientId).verifyMembership(
connection.clientId,
height,
proof,
abi.encodePacked(
IBCCommitment.connectionCommitmentKey(connectionId)
),
abi.encodePacked(encodeConnection(counterpartyConnection))
);
}
function generateConnectionIdentifier() internal returns (uint32) {
uint32 nextConnectionSequence =
uint32(uint256(commitments[nextConnectionSequencePath]));
commitments[nextConnectionSequencePath] =
bytes32(uint256(nextConnectionSequence + 1));
return nextConnectionSequence;
}
}