forked from 0fatih/yui-ibc-solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMockClient.sol
245 lines (220 loc) · 9.9 KB
/
MockClient.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
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
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;
import "../core/02-client/ILightClient.sol";
import "../core/02-client/IBCHeight.sol";
import "../proto/Client.sol";
import {
IbcLightclientsMockV1ClientState as ClientState,
IbcLightclientsMockV1ConsensusState as ConsensusState,
IbcLightclientsMockV1Header as Header
} from "../proto/MockClient.sol";
import {GoogleProtobufAny as Any} from "../proto/GoogleProtobufAny.sol";
import "solidity-bytes-utils/contracts/BytesLib.sol";
// MockClient implements https://github.com/datachainlab/ibc-mock-client
// WARNING: This client is intended to be used for testing purpose. Therefore, it is not generally available in a production, except in a fully trusted environment.
contract MockClient is ILightClient {
using BytesLib for bytes;
using IBCHeight for Height.Data;
string private constant HEADER_TYPE_URL = "/ibc.lightclients.mock.v1.Header";
string private constant CLIENT_STATE_TYPE_URL = "/ibc.lightclients.mock.v1.ClientState";
string private constant CONSENSUS_STATE_TYPE_URL = "/ibc.lightclients.mock.v1.ConsensusState";
bytes32 private constant HEADER_TYPE_URL_HASH = keccak256(abi.encodePacked(HEADER_TYPE_URL));
bytes32 private constant CLIENT_STATE_TYPE_URL_HASH = keccak256(abi.encodePacked(CLIENT_STATE_TYPE_URL));
bytes32 private constant CONSENSUS_STATE_TYPE_URL_HASH = keccak256(abi.encodePacked(CONSENSUS_STATE_TYPE_URL));
address internal ibcHandler;
mapping(string => ClientState.Data) internal clientStates;
mapping(string => mapping(uint128 => ConsensusState.Data)) internal consensusStates;
constructor(address ibcHandler_) {
ibcHandler = ibcHandler_;
}
/**
* @dev createClient creates a new client with the given state
*/
function createClient(string calldata clientId, bytes calldata clientStateBytes, bytes calldata consensusStateBytes)
external
override
onlyIBC
returns (bytes32 clientStateCommitment, ConsensusStateUpdate memory update, bool ok)
{
ClientState.Data memory clientState;
ConsensusState.Data memory consensusState;
(clientState, ok) = unmarshalClientState(clientStateBytes);
if (!ok) {
return (clientStateCommitment, update, false);
}
(consensusState, ok) = unmarshalConsensusState(consensusStateBytes);
if (!ok) {
return (clientStateCommitment, update, false);
}
if (
clientState.latest_height.revision_number != 0 || clientState.latest_height.revision_height == 0
|| consensusState.timestamp == 0
) {
return (clientStateCommitment, update, false);
}
clientStates[clientId] = clientState;
consensusStates[clientId][clientState.latest_height.toUint128()] = consensusState;
return (
keccak256(clientStateBytes),
ConsensusStateUpdate({
consensusStateCommitment: keccak256(consensusStateBytes),
height: clientState.latest_height
}),
true
);
}
/**
* @dev getTimestampAtHeight returns the timestamp of the consensus state at the given height.
* The timestamp is nanoseconds since unix epoch.
*/
function getTimestampAtHeight(string calldata clientId, Height.Data calldata height)
external
view
override
returns (uint64, bool)
{
ConsensusState.Data storage consensusState = consensusStates[clientId][height.toUint128()];
return (consensusState.timestamp, consensusState.timestamp != 0);
}
/**
* @dev getLatestHeight returns the latest height of the client state corresponding to `clientId`.
*/
function getLatestHeight(string calldata clientId) external view override returns (Height.Data memory, bool) {
ClientState.Data storage clientState = clientStates[clientId];
return (clientState.latest_height, clientState.latest_height.revision_height != 0);
}
/**
* @dev updateClient is intended to perform the followings:
* 1. verify a given client message(e.g. header)
* 2. check misbehaviour such like duplicate block height
* 3. if misbehaviour is found, update state accordingly and return
* 4. update state(s) with the client message
* 5. persist the state(s) on the host
*/
function updateClient(string calldata clientId, bytes calldata clientMessageBytes)
external
override
onlyIBC
returns (bytes32 clientStateCommitment, ConsensusStateUpdate[] memory updates, bool ok)
{
Height.Data memory height;
uint64 timestamp;
Any.Data memory anyConsensusState;
(height, timestamp) = parseHeader(clientMessageBytes);
if (height.gt(clientStates[clientId].latest_height)) {
Any.Data memory anyClientState;
clientStates[clientId].latest_height = height;
anyClientState.type_url = CLIENT_STATE_TYPE_URL;
anyClientState.value = ClientState.encode(clientStates[clientId]);
clientStateCommitment = keccak256(Any.encode(anyClientState));
}
ConsensusState.Data storage consensusState = consensusStates[clientId][height.toUint128()];
consensusState.timestamp = timestamp;
anyConsensusState.type_url = CONSENSUS_STATE_TYPE_URL;
anyConsensusState.value = ConsensusState.encode(consensusState);
updates = new ConsensusStateUpdate[](1);
updates[0] =
ConsensusStateUpdate({consensusStateCommitment: keccak256(Any.encode(anyConsensusState)), height: height});
return (clientStateCommitment, updates, true);
}
/**
* @dev verifyMembership is a generic proof verification method which verifies a proof of the existence of a value at a given CommitmentPath at the specified height.
* The caller is expected to construct the full CommitmentPath from a CommitmentPrefix and a standardized path (as defined in ICS 24).
*/
function verifyMembership(
string calldata clientId,
Height.Data calldata height,
uint64,
uint64,
bytes calldata proof,
bytes memory,
bytes memory,
bytes calldata value
) external view override returns (bool) {
require(consensusStates[clientId][height.toUint128()].timestamp != 0, "consensus state not found");
return sha256(value) == proof.toBytes32(0);
}
/**
* @dev verifyNonMembership is a generic proof verification method which verifies the absence of a given CommitmentPath at a specified height.
* The caller is expected to construct the full CommitmentPath from a CommitmentPrefix and a standardized path (as defined in ICS 24).
*/
function verifyNonMembership(
string calldata clientId,
Height.Data calldata height,
uint64,
uint64,
bytes calldata proof,
bytes memory,
bytes memory
) external view override returns (bool) {
require(consensusStates[clientId][height.toUint128()].timestamp != 0, "consensus state not found");
return proof.length == 0;
}
/* State accessors */
/**
* @dev getClientState returns the clientState corresponding to `clientId`.
* If it's not found, the function returns false.
*/
function getClientState(string calldata clientId) external view returns (bytes memory clientStateBytes, bool) {
ClientState.Data storage clientState = clientStates[clientId];
if (clientState.latest_height.revision_height == 0) {
return (clientStateBytes, false);
}
return (Any.encode(Any.Data({type_url: CLIENT_STATE_TYPE_URL, value: ClientState.encode(clientState)})), true);
}
/**
* @dev getConsensusState returns the consensusState corresponding to `clientId` and `height`.
* If it's not found, the function returns false.
*/
function getConsensusState(string calldata clientId, Height.Data calldata height)
external
view
returns (bytes memory consensusStateBytes, bool)
{
ConsensusState.Data storage consensusState = consensusStates[clientId][height.toUint128()];
if (consensusState.timestamp == 0) {
return (consensusStateBytes, false);
}
return (
Any.encode(Any.Data({type_url: CONSENSUS_STATE_TYPE_URL, value: ConsensusState.encode(consensusState)})),
true
);
}
/* Internal functions */
function parseHeader(bytes memory bz) internal pure returns (Height.Data memory, uint64) {
Any.Data memory any = Any.decode(bz);
require(keccak256(abi.encodePacked(any.type_url)) == HEADER_TYPE_URL_HASH, "invalid header type");
Header.Data memory header = Header.decode(any.value);
require(
header.height.revision_number == 0 && header.height.revision_height != 0 && header.timestamp != 0,
"invalid header"
);
return (header.height, header.timestamp);
}
function unmarshalClientState(bytes calldata bz)
internal
pure
returns (ClientState.Data memory clientState, bool ok)
{
Any.Data memory anyClientState = Any.decode(bz);
if (keccak256(abi.encodePacked(anyClientState.type_url)) != CLIENT_STATE_TYPE_URL_HASH) {
return (clientState, false);
}
return (ClientState.decode(anyClientState.value), true);
}
function unmarshalConsensusState(bytes calldata bz)
internal
pure
returns (ConsensusState.Data memory consensusState, bool ok)
{
Any.Data memory anyConsensusState = Any.decode(bz);
if (keccak256(abi.encodePacked(anyConsensusState.type_url)) != CONSENSUS_STATE_TYPE_URL_HASH) {
return (consensusState, false);
}
return (ConsensusState.decode(anyConsensusState.value), true);
}
modifier onlyIBC() {
require(msg.sender == ibcHandler);
_;
}
}