-
Notifications
You must be signed in to change notification settings - Fork 342
/
contract.rs
699 lines (621 loc) · 25.1 KB
/
contract.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use cosmwasm_std::{
entry_point, from_json, to_json_binary, wasm_execute, BankMsg, Binary, CosmosMsg, Deps,
DepsMut, Empty, Env, Event, Ibc3ChannelOpenResponse, IbcAcknowledgement, IbcBasicResponse,
IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcMsg,
IbcOrder, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse,
MessageInfo, Never, QueryResponse, Reply, Response, StdError, StdResult, SubMsg,
SubMsgResponse, SubMsgResult, WasmMsg,
};
use crate::msg::{
AccountInfo, AccountResponse, AcknowledgementMsg, BalancesResponse, DispatchResponse,
ExecuteMsg, InstantiateMsg, ListAccountsResponse, PacketMsg, QueryMsg, ReflectExecuteMsg,
ReturnMsgsResponse, WhoAmIResponse,
};
use crate::state::{
load_account, load_item, may_load_account, range_accounts, remove_account, save_account,
save_item, Config, KEY_CONFIG, KEY_PENDING_CHANNEL,
};
pub const IBC_APP_VERSION: &str = "ibc-reflect-v1";
pub const RECEIVE_DISPATCH_ID: u64 = 1234;
pub const INIT_CALLBACK_ID: u64 = 7890;
#[entry_point]
pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
// we store the reflect_id for creating accounts later
let cfg = Config {
reflect_code_id: msg.reflect_code_id,
};
save_item(deps.storage, KEY_CONFIG, &cfg)?;
Ok(Response::new().add_attribute("action", "instantiate"))
}
#[entry_point]
pub fn execute(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response> {
match msg {
ExecuteMsg::AsyncAck {
channel_id,
packet_sequence,
ack,
} => execute_async_ack(channel_id, packet_sequence.u64(), ack),
}
}
fn execute_async_ack(
channel_id: String,
packet_sequence: u64,
ack: IbcAcknowledgement,
) -> StdResult<Response> {
Ok(Response::new().add_message(IbcMsg::WriteAcknowledgement {
channel_id,
packet_sequence,
ack,
}))
}
#[entry_point]
pub fn reply(deps: DepsMut, _env: Env, reply: Reply) -> StdResult<Response> {
match (reply.id, reply.result) {
(RECEIVE_DISPATCH_ID, SubMsgResult::Err(err)) => {
Ok(Response::new().set_data(encode_ibc_error(err)))
}
(INIT_CALLBACK_ID, SubMsgResult::Ok(response)) => handle_init_callback(deps, response),
_ => Err(StdError::generic_err("invalid reply id or result")),
}
}
// updated with https://github.com/CosmWasm/wasmd/pull/586 (emitted in keeper.Instantiate)
fn parse_contract_from_event(events: Vec<Event>) -> Option<String> {
events
.into_iter()
.find(|e| e.ty == "instantiate")
.and_then(|ev| {
ev.attributes
.into_iter()
.find(|a| a.key == "_contract_address")
})
.map(|a| a.value)
}
pub fn handle_init_callback(deps: DepsMut, response: SubMsgResponse) -> StdResult<Response> {
// we use storage to pass info from the caller to the reply
let id: String = load_item(deps.storage, KEY_PENDING_CHANNEL)?;
deps.storage.remove(KEY_PENDING_CHANNEL);
// parse contract info from events
let contract_addr = match parse_contract_from_event(response.events) {
Some(addr) => deps.api.addr_validate(&addr),
None => Err(StdError::generic_err(
"No _contract_address found in callback events",
)),
}?;
// store id -> contract_addr if it is empty
// id comes from: `let chan_id = msg.endpoint.channel_id;` in `ibc_channel_connect`
match may_load_account(deps.storage, &id)? {
Some(_) => {
return Err(StdError::generic_err(
"Cannot register over an existing channel",
))
}
None => save_account(deps.storage, &id, &contract_addr)?,
}
Ok(Response::new().add_attribute("action", "execute_init_callback"))
}
#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
match msg {
QueryMsg::Account { channel_id } => to_json_binary(&query_account(deps, channel_id)?),
QueryMsg::ListAccounts {} => to_json_binary(&query_list_accounts(deps)?),
}
}
pub fn query_account(deps: Deps, channel_id: String) -> StdResult<AccountResponse> {
let account = load_account(deps.storage, &channel_id)?;
Ok(AccountResponse {
account: Some(account.into()),
})
}
pub fn query_list_accounts(deps: Deps) -> StdResult<ListAccountsResponse> {
let accounts: StdResult<Vec<_>> = range_accounts(deps.storage)
.map(|item| {
let (key, account) = item?;
Ok(AccountInfo {
account: account.into(),
channel_id: key,
})
})
.collect();
Ok(ListAccountsResponse {
accounts: accounts?,
})
}
#[entry_point]
/// enforces ordering and versioning constraints
pub fn ibc_channel_open(
_deps: DepsMut,
_env: Env,
msg: IbcChannelOpenMsg,
) -> StdResult<IbcChannelOpenResponse> {
let channel = msg.channel();
if channel.order != IbcOrder::Ordered {
return Err(StdError::generic_err("Only supports ordered channels"));
}
// In ibcv3 we don't check the version string passed in the message
// and only check the counterparty version.
if let Some(counter_version) = msg.counterparty_version() {
if counter_version != IBC_APP_VERSION {
return Err(StdError::generic_err(format!(
"Counterparty version must be `{IBC_APP_VERSION}`"
)));
}
}
// We return the version we need (which could be different than the counterparty version)
Ok(Some(Ibc3ChannelOpenResponse {
version: IBC_APP_VERSION.to_string(),
}))
}
#[entry_point]
/// once it's established, we create the reflect contract
pub fn ibc_channel_connect(
deps: DepsMut,
_env: Env,
msg: IbcChannelConnectMsg,
) -> StdResult<IbcBasicResponse> {
let channel = msg.channel();
let cfg: Config = load_item(deps.storage, KEY_CONFIG)?;
let chan_id = &channel.endpoint.channel_id;
let msg = WasmMsg::Instantiate {
admin: None,
code_id: cfg.reflect_code_id,
msg: b"{}".into(),
funds: vec![],
label: format!("ibc-reflect-{chan_id}"),
};
let msg = SubMsg::reply_on_success(msg, INIT_CALLBACK_ID);
// store the channel id for the reply handler
save_item(deps.storage, KEY_PENDING_CHANNEL, chan_id)?;
Ok(IbcBasicResponse::new()
.add_submessage(msg)
.add_attribute("action", "ibc_connect")
.add_attribute("channel_id", chan_id)
.add_event(Event::new("ibc").add_attribute("channel", "connect")))
}
#[entry_point]
/// On closed channel, we take all tokens from reflect contract to this contract.
/// We also delete the channel entry from accounts.
pub fn ibc_channel_close(
deps: DepsMut,
env: Env,
msg: IbcChannelCloseMsg,
) -> StdResult<IbcBasicResponse> {
let channel = msg.channel();
// get contract address and remove lookup
let channel_id = channel.endpoint.channel_id.as_str();
let reflect_addr = load_account(deps.storage, channel_id)?;
remove_account(deps.storage, channel_id);
// transfer current balance if any (steal the money)
#[allow(deprecated)]
let amount = deps.querier.query_all_balances(&reflect_addr)?;
let messages: Vec<SubMsg<Empty>> = if !amount.is_empty() {
let bank_msg = BankMsg::Send {
to_address: env.contract.address.into(),
amount,
};
let reflect_msg = ReflectExecuteMsg::ReflectMsg {
msgs: vec![bank_msg.into()],
};
let wasm_msg = wasm_execute(reflect_addr, &reflect_msg, vec![])?;
vec![SubMsg::new(wasm_msg)]
} else {
vec![]
};
let steal_funds = !messages.is_empty();
Ok(IbcBasicResponse::new()
.add_submessages(messages)
.add_attribute("action", "ibc_close")
.add_attribute("channel_id", channel_id)
.add_attribute("steal_funds", steal_funds.to_string()))
}
/// this is a no-op just to test how this integrates with wasmd
#[entry_point]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: Empty) -> StdResult<Response> {
Ok(Response::default())
}
// this encode an error or error message into a proper acknowledgement to the receiver
fn encode_ibc_error(msg: impl Into<String>) -> Binary {
// this cannot error, unwrap to keep the interface simple
to_json_binary(&AcknowledgementMsg::<()>::Error(msg.into())).unwrap()
}
#[entry_point]
/// we look for a the proper reflect contract to relay to and send the message
/// We cannot return any meaningful response value as we do not know the response value
/// of execution. We just return ok if we dispatched, error if we failed to dispatch
pub fn ibc_packet_receive(
deps: DepsMut,
_env: Env,
msg: IbcPacketReceiveMsg,
) -> Result<IbcReceiveResponse, Never> {
// put this in a closure so we can convert all error responses into acknowledgements
(|| {
let packet = msg.packet;
// which local channel did this packet come on
let caller = packet.dest.channel_id;
let msg: PacketMsg = from_json(packet.data)?;
match msg {
PacketMsg::Dispatch { msgs } => receive_dispatch(deps, caller, msgs),
PacketMsg::WhoAmI {} => receive_who_am_i(deps, caller),
PacketMsg::Balances {} => receive_balances(deps, caller),
PacketMsg::Panic {} => execute_panic(),
PacketMsg::ReturnErr { text } => execute_error(text),
PacketMsg::ReturnMsgs { msgs } => execute_return_msgs(msgs),
PacketMsg::NoAck {} => Ok(IbcReceiveResponse::without_ack()),
}
})()
.or_else(|e| {
// we try to capture all app-level errors and convert them into
// acknowledgement packets that contain an error code.
let acknowledgement = encode_ibc_error(format!("invalid packet: {e}"));
Ok(IbcReceiveResponse::new(acknowledgement)
.add_event(Event::new("ibc").add_attribute("packet", "receive")))
})
}
// processes PacketMsg::WhoAmI variant
fn receive_who_am_i(deps: DepsMut, caller: String) -> StdResult<IbcReceiveResponse> {
let account = load_account(deps.storage, &caller)?;
let response = WhoAmIResponse {
account: account.into(),
};
let acknowledgement = to_json_binary(&AcknowledgementMsg::Ok(response))?;
// and we are golden
Ok(IbcReceiveResponse::new(acknowledgement).add_attribute("action", "receive_who_am_i"))
}
// processes PacketMsg::Balances variant
fn receive_balances(deps: DepsMut, caller: String) -> StdResult<IbcReceiveResponse> {
let account = load_account(deps.storage, &caller)?;
#[allow(deprecated)]
let balances = deps.querier.query_all_balances(&account)?;
let response = BalancesResponse {
account: account.into(),
balances,
};
let acknowledgement = to_json_binary(&AcknowledgementMsg::Ok(response))?;
// and we are golden
Ok(IbcReceiveResponse::new(acknowledgement).add_attribute("action", "receive_balances"))
}
// processes PacketMsg::Dispatch variant
fn receive_dispatch(
deps: DepsMut,
caller: String,
msgs: Vec<CosmosMsg>,
) -> StdResult<IbcReceiveResponse> {
// what is the reflect contract here
let reflect_addr = load_account(deps.storage, &caller)?;
// let them know we're fine
let acknowledgement = to_json_binary(&AcknowledgementMsg::<DispatchResponse>::Ok(()))?;
// create the message to re-dispatch to the reflect contract
let reflect_msg = ReflectExecuteMsg::ReflectMsg { msgs };
let wasm_msg = wasm_execute(reflect_addr, &reflect_msg, vec![])?;
// we wrap it in a submessage to properly report errors
let msg = SubMsg::reply_on_error(wasm_msg, RECEIVE_DISPATCH_ID);
Ok(IbcReceiveResponse::new(acknowledgement)
.add_submessage(msg)
.add_attribute("action", "receive_dispatch"))
}
fn execute_panic() -> StdResult<IbcReceiveResponse> {
panic!("This page intentionally faulted");
}
fn execute_error(text: String) -> StdResult<IbcReceiveResponse> {
Err(StdError::generic_err(text))
}
fn execute_return_msgs(msgs: Vec<CosmosMsg>) -> StdResult<IbcReceiveResponse> {
let acknowledgement = to_json_binary(&AcknowledgementMsg::<ReturnMsgsResponse>::Ok(()))?;
Ok(IbcReceiveResponse::new(acknowledgement)
.add_messages(msgs)
.add_attribute("action", "receive_dispatch"))
}
#[entry_point]
/// never should be called as we do not send packets
pub fn ibc_packet_ack(
_deps: DepsMut,
_env: Env,
_msg: IbcPacketAckMsg,
) -> StdResult<IbcBasicResponse> {
Ok(IbcBasicResponse::new().add_attribute("action", "ibc_packet_ack"))
}
#[entry_point]
/// never should be called as we do not send packets
pub fn ibc_packet_timeout(
_deps: DepsMut,
_env: Env,
_msg: IbcPacketTimeoutMsg,
) -> StdResult<IbcBasicResponse> {
Ok(IbcBasicResponse::new().add_attribute("action", "ibc_packet_timeout"))
}
#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::{
message_info, mock_dependencies, mock_env, mock_ibc_channel_close_init,
mock_ibc_channel_connect_ack, mock_ibc_channel_open_init, mock_ibc_channel_open_try,
mock_ibc_packet_recv, mock_wasmd_attr, MockApi, MockQuerier, MockStorage,
MOCK_CONTRACT_ADDR,
};
use cosmwasm_std::{attr, coin, coins, from_json, BankMsg, OwnedDeps, WasmMsg};
const CREATOR: &str = "creator";
// code id of the reflect contract
const REFLECT_ID: u64 = 101;
// address of first reflect contract instance that we created
const REFLECT_ADDR: &str = "reflect-acct-1";
fn setup() -> OwnedDeps<MockStorage, MockApi, MockQuerier> {
let mut deps = mock_dependencies();
let creator = deps.api.addr_make(CREATOR);
let msg = InstantiateMsg {
reflect_code_id: REFLECT_ID,
};
let info = message_info(&creator, &[]);
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(res.messages.len(), 0);
deps
}
fn fake_events(reflect_addr: &str) -> Vec<Event> {
let event = Event::new("instantiate").add_attributes(vec![
attr("code_id", "17"),
// We have to force this one to avoid the debug assertion against _
mock_wasmd_attr("_contract_address", reflect_addr),
]);
vec![event]
}
// connect will run through the entire handshake to set up a proper connect and
// save the account (tested in detail in `proper_handshake_flow`)
fn connect(mut deps: DepsMut, channel_id: &str, account: impl Into<String>) {
let account: String = account.into();
let handshake_open =
mock_ibc_channel_open_init(channel_id, IbcOrder::Ordered, IBC_APP_VERSION);
// first we try to open with a valid handshake
ibc_channel_open(deps.branch(), mock_env(), handshake_open).unwrap();
// then we connect (with counter-party version set)
let handshake_connect =
mock_ibc_channel_connect_ack(channel_id, IbcOrder::Ordered, IBC_APP_VERSION);
let res = ibc_channel_connect(deps.branch(), mock_env(), handshake_connect).unwrap();
assert_eq!(1, res.messages.len());
assert_eq!(1, res.events.len());
assert_eq!(
Event::new("ibc").add_attribute("channel", "connect"),
res.events[0]
);
let id = res.messages[0].id;
let payload = res.messages[0].payload.clone();
// fake a reply and ensure this works
#[allow(deprecated)]
let response = Reply {
id,
payload,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: fake_events(&account),
msg_responses: vec![],
data: None,
}),
};
reply(deps.branch(), mock_env(), response).unwrap();
}
#[test]
fn instantiate_works() {
let mut deps = mock_dependencies();
let creator = deps.api.addr_make(CREATOR);
let msg = InstantiateMsg {
reflect_code_id: 17,
};
let info = message_info(&creator, &[]);
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(0, res.messages.len())
}
#[test]
fn enforce_version_in_handshake() {
let mut deps = setup();
let wrong_order =
mock_ibc_channel_open_try("channel-12", IbcOrder::Unordered, IBC_APP_VERSION);
ibc_channel_open(deps.as_mut(), mock_env(), wrong_order).unwrap_err();
let wrong_version = mock_ibc_channel_open_try("channel-12", IbcOrder::Ordered, "reflect");
ibc_channel_open(deps.as_mut(), mock_env(), wrong_version).unwrap_err();
let valid_handshake =
mock_ibc_channel_open_try("channel-12", IbcOrder::Ordered, IBC_APP_VERSION);
ibc_channel_open(deps.as_mut(), mock_env(), valid_handshake).unwrap();
}
#[test]
fn proper_handshake_flow() {
let mut deps = setup();
let channel_id = "channel-1234";
let reflect_addr = deps.api.addr_make(REFLECT_ADDR);
// first we try to open with a valid handshake
let handshake_open =
mock_ibc_channel_open_init(channel_id, IbcOrder::Ordered, IBC_APP_VERSION);
ibc_channel_open(deps.as_mut(), mock_env(), handshake_open).unwrap();
// then we connect (with counter-party version set)
let handshake_connect =
mock_ibc_channel_connect_ack(channel_id, IbcOrder::Ordered, IBC_APP_VERSION);
let res = ibc_channel_connect(deps.as_mut(), mock_env(), handshake_connect).unwrap();
// and set up a reflect account
assert_eq!(1, res.messages.len());
let id = res.messages[0].id;
let payload = res.messages[0].payload.clone();
if let CosmosMsg::Wasm(WasmMsg::Instantiate {
admin,
code_id,
msg: _,
funds,
label,
}) = &res.messages[0].msg
{
assert_eq!(*admin, None);
assert_eq!(*code_id, REFLECT_ID);
assert_eq!(funds.len(), 0);
assert!(label.contains(channel_id));
} else {
panic!("invalid return message: {:?}", res.messages[0]);
}
// no accounts set yet
let raw = query(deps.as_ref(), mock_env(), QueryMsg::ListAccounts {}).unwrap();
let res: ListAccountsResponse = from_json(raw).unwrap();
assert_eq!(0, res.accounts.len());
// fake a reply and ensure this works
#[allow(deprecated)]
let response = Reply {
id,
payload,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: fake_events(reflect_addr.as_str()),
msg_responses: vec![],
data: None,
}),
};
reply(deps.as_mut(), mock_env(), response).unwrap();
// ensure this is now registered
let raw = query(deps.as_ref(), mock_env(), QueryMsg::ListAccounts {}).unwrap();
let res: ListAccountsResponse = from_json(raw).unwrap();
assert_eq!(1, res.accounts.len());
assert_eq!(
&res.accounts[0],
&AccountInfo {
account: reflect_addr.to_string(),
channel_id: channel_id.to_string(),
}
);
// and the account query also works
let raw = query(
deps.as_ref(),
mock_env(),
QueryMsg::Account {
channel_id: channel_id.to_string(),
},
)
.unwrap();
let res: AccountResponse = from_json(raw).unwrap();
assert_eq!(res.account.unwrap(), reflect_addr.as_str());
}
#[test]
fn handle_dispatch_packet() {
let mut deps = setup();
let channel_id = "channel-123";
let account = deps.api.addr_make("acct-123");
// receive a packet for an unregistered channel returns app-level error (not Result::Err)
let msgs_to_dispatch = vec![BankMsg::Send {
to_address: "my-friend".into(),
amount: coins(123456789, "uatom"),
}
.into()];
let ibc_msg = PacketMsg::Dispatch {
msgs: msgs_to_dispatch.clone(),
};
let msg = mock_ibc_packet_recv(channel_id, &ibc_msg).unwrap();
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();
// we didn't dispatch anything
assert_eq!(0, res.messages.len());
assert_eq!(1, res.events.len());
assert_eq!(
Event::new("ibc").add_attribute("packet", "receive"),
res.events[0]
);
// acknowledgement is an error
let ack: AcknowledgementMsg<DispatchResponse> =
from_json(res.acknowledgement.unwrap()).unwrap();
assert_eq!(
ack.unwrap_err(),
"invalid packet: account channel-123 not found"
);
// register the channel
connect(deps.as_mut(), channel_id, &account);
// receive a packet for an unregistered channel returns app-level error (not Result::Err)
let msg = mock_ibc_packet_recv(channel_id, &ibc_msg).unwrap();
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();
// assert app-level success
let ack: AcknowledgementMsg<()> = from_json(res.acknowledgement.unwrap()).unwrap();
ack.unwrap();
// and we dispatch the BankMsg via submessage
assert_eq!(1, res.messages.len());
assert_eq!(RECEIVE_DISPATCH_ID, res.messages[0].id);
// parse the output, ensuring it matches
if let CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr,
msg,
funds,
}) = &res.messages[0].msg
{
assert_eq!(account.as_str(), contract_addr);
assert_eq!(0, funds.len());
// parse the message - should callback with proper channel_id
let rmsg: ReflectExecuteMsg = from_json(msg).unwrap();
assert_eq!(
rmsg,
ReflectExecuteMsg::ReflectMsg {
msgs: msgs_to_dispatch
}
);
} else {
panic!("invalid return message: {:?}", res.messages[0]);
}
// invalid packet format on registered channel also returns app-level error
let bad_data = InstantiateMsg {
reflect_code_id: 12345,
};
let msg = mock_ibc_packet_recv(channel_id, &bad_data).unwrap();
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();
// we didn't dispatch anything
assert_eq!(0, res.messages.len());
// acknowledgement is an error
let ack: AcknowledgementMsg<DispatchResponse> =
from_json(res.acknowledgement.unwrap()).unwrap();
assert_eq!(ack.unwrap_err(), "invalid packet: Error parsing into type ibc_reflect::msg::PacketMsg: unknown variant `reflect_code_id`, expected one of `dispatch`, `who_am_i`, `balances`, `panic`, `return_err`, `return_msgs`, `no_ack`");
}
#[test]
fn check_close_channel() {
let mut deps = setup();
let channel_id = "channel-123";
let account = deps.api.addr_make("acct-123");
// register the channel
connect(deps.as_mut(), channel_id, &account);
// assign it some funds
let funds = vec![coin(123456, "uatom"), coin(7654321, "tgrd")];
deps.querier.bank.update_balance(&account, funds.clone());
// channel should be listed and have balance
let raw = query(deps.as_ref(), mock_env(), QueryMsg::ListAccounts {}).unwrap();
let res: ListAccountsResponse = from_json(raw).unwrap();
assert_eq!(1, res.accounts.len());
#[allow(deprecated)]
let balance = deps.as_ref().querier.query_all_balances(&account).unwrap();
assert_eq!(funds, balance);
// close the channel
let channel = mock_ibc_channel_close_init(channel_id, IbcOrder::Ordered, IBC_APP_VERSION);
let res = ibc_channel_close(deps.as_mut(), mock_env(), channel).unwrap();
// it pulls out all money from the reflect contract
assert_eq!(1, res.messages.len());
if let CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr, msg, ..
}) = &res.messages[0].msg
{
assert_eq!(contract_addr, account.as_str());
let reflect: ReflectExecuteMsg = from_json(msg).unwrap();
match reflect {
ReflectExecuteMsg::ReflectMsg { msgs } => {
assert_eq!(1, msgs.len());
assert_eq!(
&msgs[0],
&BankMsg::Send {
to_address: MOCK_CONTRACT_ADDR.into(),
amount: funds
}
.into()
)
}
}
} else {
panic!("Unexpected message: {:?}", &res.messages[0]);
}
// and removes the account lookup
let raw = query(deps.as_ref(), mock_env(), QueryMsg::ListAccounts {}).unwrap();
let res: ListAccountsResponse = from_json(raw).unwrap();
assert_eq!(0, res.accounts.len());
}
}