Skip to content

Commit 4b7a93b

Browse files
committed
wip: fix tests for multi-hop cch
- Using `Script::default()` for udt script is not allowed in tests. - Fix cell consumed error when sending tx to the mock chain actor.
1 parent f1726b5 commit 4b7a93b

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed

crates/fiber-lib/src/ckb/tests/test_utils.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,25 @@ impl Actor for MockChainActor {
593593
}
594594
SendTx(tx, reply_port) => {
595595
const MAX_CYCLES: u64 = 100_000_000;
596-
let (tx_status, result) = {
596+
let (tx_status, result) = if let Some(resp) = state.txs.get(&tx.hash().into()) {
597+
// Like a real CKB node, this mock should allow sending
598+
// duplicate transactions. This is required because the
599+
// `Fund(..)` branch may add inputs for UDT channels, and
600+
// processing the same UDT funding transaction multiple
601+
// times would otherwise lead to consumed cell errors.
602+
(
603+
resp.tx_status.clone(),
604+
match &resp.tx_status {
605+
TxStatus::Committed(..) => Ok(()),
606+
TxStatus::Rejected(reason) => {
607+
Err(ckb_sdk::RpcError::Other(anyhow!("{}", reason)))
608+
}
609+
_ => {
610+
unreachable!();
611+
}
612+
},
613+
)
614+
} else {
597615
match tx.input_pts_iter().find(|input| {
598616
state
599617
.cell_status
@@ -735,8 +753,9 @@ pub async fn submit_tx(mock_actor: ActorRef<CkbChainMessage>, tx: TransactionVie
735753
if let Err(error) = call_t!(mock_actor, CkbChainMessage::SendTx, TIMEOUT, tx.clone())
736754
.expect("chain actor alive")
737755
{
738-
error!("submit tx failed: {:?}", error);
739-
return TxStatus::Rejected("submit tx failed".to_string());
756+
let reject_reason = format!("submit tx failed: {:?}", error);
757+
error!("{}", reject_reason);
758+
return TxStatus::Rejected(reject_reason);
740759
}
741760
trace_tx(mock_actor, tx.hash().into()).await
742761
}

crates/fiber-lib/src/fiber/channel.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,12 +1228,7 @@ where
12281228

12291229
let (tlc_info, remove_reason) = state.remove_tlc_with_reason(tlc_id)?;
12301230

1231-
// We should only update invoice status to paid if the peer is paying us (i.e., the TLC is received).
1232-
// For the TLC sent by ourselves (we're paying the peer), we should leave any invoice as it is.
1233-
// The reason for this is that it is possible that we're paying another peer by sending a TLC and
1234-
// we have a local invoice with the same payment hash. See the test
1235-
// test_store_update_subscription_mock_cross_chain_payment for a concrete example.
1236-
if tlc_id.is_received() && matches!(remove_reason, RemoveTlcReason::RemoveTlcFulfill(_)) {
1231+
if matches!(remove_reason, RemoveTlcReason::RemoveTlcFulfill(_)) {
12371232
if self.store.get_invoice(&tlc_info.payment_hash).is_some() {
12381233
debug!(channel = ?channel_id, hash = ?tlc_info.payment_hash, "update invoice status to paid");
12391234
self.store

crates/fiber-lib/src/fiber/tests/channel.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::ckb::contracts::get_script_by_contract;
12
use crate::ckb::tests::test_utils::complete_commitment_tx;
23
use crate::fiber::channel::{ChannelState, CloseFlags, UpdateCommand, XUDT_COMPATIBLE_WITNESS};
34
use crate::fiber::config::{DEFAULT_TLC_EXPIRY_DELTA, MAX_PAYMENT_TLC_EXPIRY_LIMIT};
@@ -296,18 +297,28 @@ async fn test_create_channel_with_too_large_amounts() {
296297
"The total funding amount (18446744069509551614) should be less than 18446744065309551615"
297298
));
298299

300+
let udt_args =
301+
hex::decode("32e555f3ff8e135cece1351a6a2971518392c1e30375c1e006ad0ce8eac07947").unwrap();
302+
let udt_script = get_script_by_contract(Contract::SimpleUDT, &udt_args);
303+
299304
let params = ChannelParameters {
300305
node_a_funding_amount: u128::MAX - 100,
301306
node_b_funding_amount: 101,
302-
funding_udt_type_script: Some(Script::default()),
307+
funding_udt_type_script: Some(udt_script),
303308
..Default::default()
304309
};
305310
let res = create_channel_with_nodes(&mut node_a, &mut node_b, params).await;
306-
assert!(res.is_err(), "Create channel failed: {:?}", res);
307-
assert!(res
308-
.unwrap_err()
309-
.to_string()
310-
.contains("The total UDT funding amount should be less"));
311+
assert!(
312+
res.is_err(),
313+
"Create channel should fail but succeeded: {:?}",
314+
res
315+
);
316+
let err = res.unwrap_err().to_string();
317+
assert!(
318+
err.contains("The total UDT funding amount should be less"),
319+
"Unmatched error: {}",
320+
err
321+
);
311322
}
312323

313324
#[tokio::test]

crates/fiber-lib/src/fiber/tests/payment.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![allow(clippy::needless_range_loop)]
2+
use crate::ckb::contracts::get_script_by_contract;
3+
use crate::ckb::contracts::Contract;
24
use crate::fiber::channel::*;
35
use crate::fiber::config::DEFAULT_TLC_EXPIRY_DELTA;
46
use crate::fiber::config::DEFAULT_TLC_FEE_PROPORTIONAL_MILLIONTHS;
@@ -29,6 +31,12 @@ use std::time::SystemTime;
2931
use tracing::debug;
3032
use tracing::error;
3133

34+
fn get_simple_udt_script() -> Script {
35+
let args =
36+
hex::decode("32e555f3ff8e135cece1351a6a2971518392c1e30375c1e006ad0ce8eac07947").unwrap();
37+
get_script_by_contract(Contract::SimpleUDT, &args)
38+
}
39+
3240
#[tokio::test]
3341
async fn test_send_payment_custom_records() {
3442
let (nodes, _channels) = create_n_nodes_network(
@@ -785,7 +793,7 @@ async fn test_send_payment_hophint_for_mixed_channels_with_udt() {
785793
node_a_funding_amount: HUGE_CKB_AMOUNT,
786794
node_b_funding_amount: HUGE_CKB_AMOUNT,
787795
public: true, // not a private channel
788-
funding_udt_type_script: Some(Script::default()), // a UDT channel
796+
funding_udt_type_script: Some(get_simple_udt_script()), // a UDT channel
789797
..Default::default()
790798
},
791799
),
@@ -3135,6 +3143,7 @@ async fn test_send_payment_self_with_two_nodes() {
31353143
async fn test_send_payment_self_with_mixed_channel() {
31363144
// #678, payself with mixed channel got wrong
31373145
init_tracing();
3146+
let udt_script = get_simple_udt_script();
31383147

31393148
let funding_amount = HUGE_CKB_AMOUNT;
31403149
let (nodes, _channels) = create_n_nodes_network_with_params(
@@ -3154,7 +3163,7 @@ async fn test_send_payment_self_with_mixed_channel() {
31543163
public: true,
31553164
node_a_funding_amount: funding_amount,
31563165
node_b_funding_amount: funding_amount,
3157-
funding_udt_type_script: Some(Script::default()),
3166+
funding_udt_type_script: Some(udt_script.clone()),
31583167
..Default::default()
31593168
},
31603169
),
@@ -3184,7 +3193,7 @@ async fn test_send_payment_self_with_mixed_channel() {
31843193
public: true,
31853194
node_a_funding_amount: funding_amount,
31863195
node_b_funding_amount: funding_amount,
3187-
funding_udt_type_script: Some(Script::default()),
3196+
funding_udt_type_script: Some(udt_script.clone()),
31883197
..Default::default()
31893198
},
31903199
),
@@ -3216,7 +3225,7 @@ async fn test_send_payment_self_with_mixed_channel() {
32163225
public: true,
32173226
node_a_funding_amount: funding_amount,
32183227
node_b_funding_amount: funding_amount,
3219-
funding_udt_type_script: Some(Script::default()),
3228+
funding_udt_type_script: Some(udt_script.clone()),
32203229
..Default::default()
32213230
},
32223231
),
@@ -3226,7 +3235,7 @@ async fn test_send_payment_self_with_mixed_channel() {
32263235
public: true,
32273236
node_a_funding_amount: funding_amount,
32283237
node_b_funding_amount: funding_amount,
3229-
funding_udt_type_script: Some(Script::default()),
3238+
funding_udt_type_script: Some(udt_script.clone()),
32303239
..Default::default()
32313240
},
32323241
),
@@ -3236,7 +3245,7 @@ async fn test_send_payment_self_with_mixed_channel() {
32363245
public: true,
32373246
node_a_funding_amount: funding_amount,
32383247
node_b_funding_amount: funding_amount,
3239-
funding_udt_type_script: Some(Script::default()),
3248+
funding_udt_type_script: Some(udt_script.clone()),
32403249
..Default::default()
32413250
},
32423251
),
@@ -3252,7 +3261,7 @@ async fn test_send_payment_self_with_mixed_channel() {
32523261
amount: Some(1000),
32533262
keysend: Some(true),
32543263
allow_self_payment: true,
3255-
udt_type_script: Some(Script::default()),
3264+
udt_type_script: Some(udt_script.clone()),
32563265
..Default::default()
32573266
})
32583267
.await;
@@ -4519,6 +4528,7 @@ async fn test_send_payment_no_preimage_invoice_will_make_payment_failed() {
45194528
async fn test_send_payment_with_mixed_channel_hops() {
45204529
init_tracing();
45214530
let _span = tracing::info_span!("node", node = "test").entered();
4531+
let udt_script = get_simple_udt_script();
45224532
let (nodes, channels) = create_n_nodes_network(
45234533
&[
45244534
((0, 1), (HUGE_CKB_AMOUNT, HUGE_CKB_AMOUNT)),
@@ -4537,7 +4547,7 @@ async fn test_send_payment_with_mixed_channel_hops() {
45374547
public: false,
45384548
node_a_funding_amount: HUGE_CKB_AMOUNT,
45394549
node_b_funding_amount: HUGE_CKB_AMOUNT,
4540-
funding_udt_type_script: Some(Script::default()), // UDT type
4550+
funding_udt_type_script: Some(udt_script.clone()), // UDT type
45414551
..Default::default()
45424552
},
45434553
)

crates/fiber-lib/src/tests/test_utils.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,11 @@ pub(crate) async fn create_n_nodes_network_with_params(
628628
for node in nodes.iter_mut() {
629629
let res = node.submit_tx(funding_tx.clone()).await;
630630
node.add_channel_tx(channel_id, funding_tx.hash().into());
631-
assert!(matches!(res, TxStatus::Committed(..)));
631+
assert!(
632+
matches!(res, TxStatus::Committed(..)),
633+
"expect committed tx, got {:?}",
634+
res
635+
);
632636
}
633637
}
634638
// sleep for a while to make sure network graph is updated

0 commit comments

Comments
 (0)