forked from hyperledger-labs/yui-ibc-solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockClient.sol
212 lines (193 loc) · 7.77 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
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;
import "./IClient.sol";
import "./IBCHost.sol";
import "./IBCMsgs.sol";
import "./IBCHeight.sol";
import "./types/Client.sol";
import {
IbcLightclientsMockV1ClientState as ClientState,
IbcLightclientsMockV1ConsensusState as ConsensusState,
IbcLightclientsMockV1Header as Header
} from "./types/MockClient.sol";
import {GoogleProtobufAny as Any} from "./types/GoogleProtobufAny.sol";
import "../lib/Bytes.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 IClient {
using Bytes for bytes;
using IBCHeight for Height.Data;
struct protoTypes {
bytes32 clientState;
bytes32 consensusState;
bytes32 header;
}
protoTypes pts;
constructor() {
// TODO The typeUrl should be defined in types/MockClient.sol
// The schema of typeUrl follows cosmos/cosmos-sdk/codec/types/any.go
pts = protoTypes({
clientState: keccak256(abi.encodePacked("/ibc.lightclients.mock.v1.ClientState")),
consensusState: keccak256(abi.encodePacked("/ibc.lightclients.mock.v1.ConsensusState")),
header: keccak256(abi.encodePacked("/ibc.lightclients.mock.v1.Header"))
});
}
/**
* @dev getTimestampAtHeight returns the timestamp of the consensus state at the given height.
*/
function getTimestampAtHeight(
IBCHost host,
string memory clientId,
Height.Data memory height
) public override view returns (uint64, bool) {
(ConsensusState.Data memory consensusState, bool found) = getConsensusState(host, clientId, height);
if (!found) {
return (0, false);
}
return (consensusState.timestamp, true);
}
function getLatestHeight(
IBCHost host,
string memory clientId
) public override view returns (Height.Data memory, bool) {
(ClientState.Data memory clientState, bool found) = getClientState(host, clientId);
if (!found) {
return (Height.Data(0, 0), false);
}
return (clientState.latest_height, true);
}
/**
* @dev checkHeaderAndUpdateState checks if the provided header is valid
*/
function checkHeaderAndUpdateState(
IBCHost,
string memory,
bytes memory clientStateBytes,
bytes memory headerBytes
) public override view returns (bytes memory newClientStateBytes, bytes memory newConsensusStateBytes, Height.Data memory height) {
uint64 timestamp;
Any.Data memory anyClientState;
Any.Data memory anyConsensusState;
anyClientState = Any.decode(clientStateBytes);
require(keccak256(abi.encodePacked(anyClientState.type_url)) == pts.clientState, "invalid client type");
ClientState.Data memory clientState = ClientState.decode(anyClientState.value);
(height, timestamp) = parseHeader(headerBytes);
if (height.gt(clientState.latest_height)) {
clientState.latest_height = height;
}
anyClientState.value = ClientState.encode(clientState);
anyConsensusState.type_url = "/ibc.lightclients.mock.v1.ConsensusState";
anyConsensusState.value = ConsensusState.encode(ConsensusState.Data({timestamp: timestamp}));
return (Any.encode(anyClientState), Any.encode(anyConsensusState), height);
}
function verifyClientState(
IBCHost host,
string memory clientId,
Height.Data memory height,
bytes memory,
string memory,
bytes memory proof,
bytes memory clientStateBytes // serialized with pb
) public override view returns (bool) {
(, bool found) = host.getConsensusState(clientId, height);
require(found, "consensus state not found");
return sha256(clientStateBytes) == proof.toBytes32();
}
function verifyClientConsensusState(
IBCHost host,
string memory clientId,
Height.Data memory height,
string memory,
Height.Data memory,
bytes memory,
bytes memory proof,
bytes memory consensusStateBytes // serialized with pb
) public override view returns (bool) {
(, bool found) = host.getConsensusState(clientId, height);
require(found, "consensus state not found");
return sha256(consensusStateBytes) == proof.toBytes32();
}
function verifyConnectionState(
IBCHost host,
string memory clientId,
Height.Data memory height,
bytes memory,
bytes memory proof,
string memory,
bytes memory connectionBytes // serialized with pb
) public override view returns (bool) {
(, bool found) = host.getConsensusState(clientId, height);
require(found, "consensus state not found");
return sha256(connectionBytes) == proof.toBytes32();
}
function verifyChannelState(
IBCHost host,
string memory clientId,
Height.Data memory height,
bytes memory,
bytes memory proof,
string memory,
string memory,
bytes memory channelBytes // serialized with pb
) public override view returns (bool) {
(, bool found) = host.getConsensusState(clientId, height);
require(found, "consensus state not found");
return sha256(channelBytes) == proof.toBytes32();
}
function verifyPacketCommitment(
IBCHost host,
string memory clientId,
Height.Data memory height,
uint64,
uint64,
bytes memory,
bytes memory proof,
string memory,
string memory,
uint64,
bytes32 commitmentBytes
) public override view returns (bool) {
(, bool found) = host.getConsensusState(clientId, height);
require(found, "consensus state not found");
return commitmentBytes == proof.toBytes32();
}
function verifyPacketAcknowledgement(
IBCHost host,
string memory clientId,
Height.Data memory height,
uint64,
uint64,
bytes memory,
bytes memory proof,
string memory,
string memory,
uint64,
bytes memory acknowledgement
) public override view returns (bool) {
(, bool found) = host.getConsensusState(clientId, height);
require(found, "consensus state not found");
return host.makePacketAcknowledgementCommitment(acknowledgement) == proof.toBytes32();
}
function getClientState(IBCHost host, string memory clientId) public view returns (ClientState.Data memory clientState, bool found) {
bytes memory clientStateBytes;
(clientStateBytes, found) = host.getClientState(clientId);
if (!found) {
return (clientState, false);
}
return (ClientState.decode(Any.decode(clientStateBytes).value), true);
}
function getConsensusState(IBCHost host, string memory clientId, Height.Data memory height) public view returns (ConsensusState.Data memory consensusState, bool found) {
bytes memory consensusStateBytes;
(consensusStateBytes, found) = host.getConsensusState(clientId, height);
if (!found) {
return (consensusState, false);
}
return (ConsensusState.decode(Any.decode(consensusStateBytes).value), true);
}
function parseHeader(bytes memory headerBytes) internal view returns (Height.Data memory, uint64) {
Any.Data memory any = Any.decode(headerBytes);
require(keccak256(abi.encodePacked(any.type_url)) == pts.header, "invalid header type");
Header.Data memory header = Header.decode(any.value);
return (header.height, header.timestamp);
}
}