-
Notifications
You must be signed in to change notification settings - Fork 352
/
create_client.rs
270 lines (243 loc) · 9.95 KB
/
create_client.rs
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
//! Protocol logic specific to processing ICS2 messages of type `MsgCreateAnyClient`.
use crate::events::IBCEvent;
use crate::handler::{HandlerOutput, HandlerResult};
use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::context::ClientReader;
use crate::ics02_client::error::{Error, Kind};
use crate::ics02_client::events::Attributes;
use crate::ics02_client::handler::ClientResult;
use crate::ics02_client::msgs::create_client::MsgCreateAnyClient;
use crate::ics24_host::identifier::ClientId;
/// The result following the successful processing of a `MsgCreateAnyClient` message. Preferably
/// this data type should be used with a qualified name `create_client::Result` to avoid ambiguity.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Result {
pub client_id: ClientId,
pub client_type: ClientType,
pub client_state: AnyClientState,
pub consensus_state: AnyConsensusState,
}
pub fn process(
ctx: &dyn ClientReader,
msg: MsgCreateAnyClient,
) -> HandlerResult<ClientResult, Error> {
let mut output = HandlerOutput::builder();
// Construct this client's identifier
let id_counter = ctx.client_counter();
let client_id = ClientId::new(msg.client_state().client_type(), id_counter).map_err(|e| {
Kind::ClientIdentifierConstructor(msg.client_state().client_type(), id_counter).context(e)
})?;
output.log("success: generated new client identifier");
let result = ClientResult::Create(Result {
client_id: client_id.clone(),
client_type: msg.client_state().client_type(),
client_state: msg.client_state(),
consensus_state: msg.consensus_state(),
});
let event_attributes = Attributes {
client_id,
..Default::default()
};
output.emit(IBCEvent::CreateClient(event_attributes.into()));
Ok(output.with_result(result))
}
#[cfg(test)]
mod tests {
use std::convert::TryInto;
use std::time::Duration;
use tendermint_light_client::types::TrustThreshold;
use crate::events::IBCEvent;
use crate::handler::HandlerOutput;
use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::handler::{dispatch, ClientResult};
use crate::ics02_client::msgs::create_client::MsgCreateAnyClient;
use crate::ics02_client::msgs::ClientMsg;
use crate::ics07_tendermint::client_state::ClientState;
use crate::ics07_tendermint::header::test_util::get_dummy_tendermint_header;
use crate::ics24_host::identifier::ClientId;
use crate::mock::client_state::{MockClientState, MockConsensusState};
use crate::mock::context::MockContext;
use crate::mock::header::MockHeader;
use crate::test_utils::get_dummy_account_id;
use crate::Height;
#[test]
fn test_create_client_ok() {
let ctx = MockContext::default();
let signer = get_dummy_account_id();
let height = Height::new(0, 42);
let msg = MsgCreateAnyClient::new(
MockClientState(MockHeader(height)).into(),
MockConsensusState(MockHeader(height)).into(),
signer,
)
.unwrap();
let output = dispatch(&ctx, ClientMsg::CreateClient(msg.clone()));
match output {
Ok(HandlerOutput {
result, mut events, ..
}) => {
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
let expected_client_id = ClientId::new(ClientType::Mock, 0).unwrap();
assert!(
matches!(event, IBCEvent::CreateClient(e) if e.client_id() == &expected_client_id)
);
match result {
ClientResult::Create(create_result) => {
assert_eq!(create_result.client_type, ClientType::Mock);
assert_eq!(create_result.client_id, expected_client_id);
assert_eq!(create_result.client_state, msg.client_state());
assert_eq!(create_result.consensus_state, msg.consensus_state());
}
_ => {
panic!("unexpected result type: expected ClientResult::CreateResult!");
}
}
}
Err(err) => {
panic!("unexpected error: {}", err);
}
}
}
#[test]
fn test_create_client_ok_multiple() {
let existing_client_id = ClientId::default();
let signer = get_dummy_account_id();
let height = Height::new(0, 80);
let ctx = MockContext::default().with_client(&existing_client_id, height);
let create_client_msgs: Vec<MsgCreateAnyClient> = vec![
MsgCreateAnyClient::new(
MockClientState(MockHeader(Height {
revision_height: 42,
..height
}))
.into(),
MockConsensusState(MockHeader(Height {
revision_height: 42,
..height
}))
.into(),
signer,
)
.unwrap(),
MsgCreateAnyClient::new(
MockClientState(MockHeader(Height {
revision_height: 42,
..height
}))
.into(),
MockConsensusState(MockHeader(Height {
revision_height: 42,
..height
}))
.into(),
signer,
)
.unwrap(),
MsgCreateAnyClient::new(
MockClientState(MockHeader(Height {
revision_height: 50,
..height
}))
.into(),
MockConsensusState(MockHeader(Height {
revision_height: 50,
..height
}))
.into(),
signer,
)
.unwrap(),
]
.into_iter()
.collect();
// The expected client id that will be generated will be identical to "9999-mock-0" for all
// tests. This is because we're not persisting any client results (which is done via the
// tests for `ics26_routing::dispatch`.
let expected_client_id = ClientId::new(ClientType::Mock, 0).unwrap();
for msg in create_client_msgs {
let output = dispatch(&ctx, ClientMsg::CreateClient(msg.clone()));
match output {
Ok(HandlerOutput {
result, mut events, ..
}) => {
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
assert!(
matches!(event, IBCEvent::CreateClient(e) if e.client_id() == &expected_client_id)
);
match result {
ClientResult::Create(create_res) => {
assert_eq!(create_res.client_type, msg.client_state().client_type());
assert_eq!(create_res.client_id, expected_client_id);
assert_eq!(create_res.client_state, msg.client_state());
assert_eq!(create_res.consensus_state, msg.consensus_state());
}
_ => {
panic!("expected result of type ClientResult::CreateResult");
}
}
}
Err(err) => {
panic!("unexpected error: {}", err);
}
}
}
}
#[test]
fn test_tm_create_client_ok() {
let signer = get_dummy_account_id();
let ctx = MockContext::default();
let tm_header = get_dummy_tendermint_header();
let tm_client_state = AnyClientState::Tendermint(ClientState {
chain_id: tm_header.chain_id.to_string(),
trust_level: TrustThreshold {
numerator: 1,
denominator: 3,
},
trusting_period: Duration::from_secs(64000),
unbonding_period: Duration::from_secs(128000),
max_clock_drift: Duration::from_millis(3000),
latest_height: Height::new(0, u64::from(tm_header.height)),
frozen_height: Height::zero(),
allow_update_after_expiry: false,
allow_update_after_misbehaviour: false,
upgrade_path: vec!["".to_string()],
});
let msg = MsgCreateAnyClient::new(
tm_client_state,
AnyConsensusState::Tendermint(tm_header.try_into().unwrap()),
signer,
)
.unwrap();
let output = dispatch(&ctx, ClientMsg::CreateClient(msg.clone()));
match output {
Ok(HandlerOutput {
result, mut events, ..
}) => {
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
let expected_client_id = ClientId::new(ClientType::Tendermint, 0).unwrap();
assert!(
matches!(event, IBCEvent::CreateClient(e) if e.client_id() == &expected_client_id)
);
match result {
ClientResult::Create(create_res) => {
assert_eq!(create_res.client_type, ClientType::Tendermint);
assert_eq!(create_res.client_id, expected_client_id);
assert_eq!(create_res.client_state, msg.client_state());
assert_eq!(create_res.consensus_state, msg.consensus_state());
}
_ => {
panic!("expected result of type ClientResult::CreateResult");
}
}
}
Err(err) => {
panic!("unexpected error: {}", err);
}
}
}
}