Skip to content

Commit

Permalink
chore: Fix clippy, rpc docs
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Feb 17, 2025
1 parent 0b51562 commit 1ed77fd
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 94 deletions.
4 changes: 2 additions & 2 deletions src/cch/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ impl CchActor {
created_at: duration_since_epoch.as_secs(),
ckb_final_tlc_expiry_delta: self.config.ckb_final_tlc_expiry_delta,
btc_pay_req: send_btc.btc_pay_req,
fiber_payee_pubkey: self.pubkey.clone(),
fiber_payee_pubkey: self.pubkey,
fiber_pay_req: Default::default(),
payment_hash: format!("0x{}", invoice.payment_hash().encode_hex::<String>()),
payment_preimage: None,
Expand All @@ -389,7 +389,7 @@ impl CchActor {
order.generate_ckb_invoice()?;

let fiber_invoice = CkbInvoice::from_str(&order.fiber_pay_req).expect("parse invoice");
let hash = Hash256::from(*fiber_invoice.payment_hash());
let hash = *fiber_invoice.payment_hash();

let message = move |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::AddInvoice(
Expand Down
8 changes: 4 additions & 4 deletions src/cch/tests/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn get_simple_udt_script() -> Script {
}

fn get_always_success_script() -> Script {
get_script_by_contract(Contract::AlwaysSuccess, &vec![])
get_script_by_contract(Contract::AlwaysSuccess, &[])
}

async fn do_test_cross_chain_payment_hub_send_btc(udt_script: Script, multiple_hops: bool) {
Expand All @@ -56,7 +56,7 @@ async fn do_test_cross_chain_payment_hub_send_btc(udt_script: Script, multiple_h
})
.await;

let (hub_channel, mut fiber_node, mut hub) = if multiple_hops {
let (hub_channel, fiber_node, mut hub) = if multiple_hops {
let [mut fiber_node, mut middle_hop, mut hub] = nodes.try_into().expect("3 nodes");
let (_channel, funding_tx_1) = establish_udt_channel_between_nodes(
&mut fiber_node,
Expand Down Expand Up @@ -169,7 +169,7 @@ async fn do_test_cross_chain_payment_hub_send_btc(udt_script: Script, multiple_h

let hub_amount = fiber_invoice.amount.expect("has amount");
assert!(
hub_amount >= lnd_amount_sats.try_into().expect("valid amount"),
hub_amount >= lnd_amount_sats.into(),
"hub should receive more money than lnd, but we have hub_amount: {}, lnd_amount: {}",
hub_amount,
lnd_amount_sats
Expand Down Expand Up @@ -331,7 +331,7 @@ async fn do_test_cross_chain_payment_hub_receive_btc(udt_script: Script, multipl
let preimage = gen_rand_sha256_hash();
let fiber_invoice = InvoiceBuilder::new(Currency::Fibd)
.amount(Some(fiber_amount_msats))
.payment_preimage(preimage.clone())
.payment_preimage(preimage)
.hash_algorithm(HashAlgorithm::Sha256)
.payee_pub_key(fiber_node.pubkey.into())
.expiry_time(Duration::from_secs(100))
Expand Down
10 changes: 5 additions & 5 deletions src/ckb/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub enum CellStatus {
Consumed,
}

pub static MOCK_CONTEXT: Lazy<MockContext> = Lazy::new(|| MockContext::new());
pub static MOCK_CONTEXT: Lazy<MockContext> = Lazy::new(MockContext::new);

pub struct MockContext {
pub context: RwLock<Context>,
Expand Down Expand Up @@ -144,7 +144,7 @@ impl MockContext {
let cell_dep = script_cell_deps
.get(&contract)
.map(|x| x.iter().map(Into::into).collect())
.unwrap_or_else(|| vec![]);
.unwrap_or_else(std::vec::Vec::new);
UdtArgInfo {
name: format!("{:?}", contract),
script: UdtScript::allow_all_for_script(script),
Expand Down Expand Up @@ -451,7 +451,7 @@ impl Actor for MockChainActor {
request
.udt_type_script
.as_ref()
.and_then(|script| get_udt_cell_deps(script))
.and_then(get_udt_cell_deps)
.unwrap_or_default(),
// AlwaysSuccess is needed to unlock the input cells
get_cell_deps_by_contracts(vec![Contract::AlwaysSuccess]),
Expand Down Expand Up @@ -479,7 +479,7 @@ impl Actor for MockChainActor {
context.create_cell_with_out_point(
outpoint.clone(),
CellOutput::new_builder()
.lock(get_script_by_contract(Contract::AlwaysSuccess, &vec![]))
.lock(get_script_by_contract(Contract::AlwaysSuccess, &[]))
.type_(request.udt_type_script.clone().pack())
.build(),
u128::MAX.to_le_bytes().to_vec().into(),
Expand Down Expand Up @@ -533,7 +533,7 @@ impl Actor for MockChainActor {
match tx.input_pts_iter().find(|input| {
state
.cell_status
.get(&input)
.get(input)
.map_or(false, |status| *status == CellStatus::Consumed)
}) {
Some(input) => (
Expand Down
37 changes: 15 additions & 22 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,29 +1016,22 @@ where
.get_invoice_preimage(&add_tlc.payment_hash)
.ok_or(ProcessingChannelError::FinalIncorrectPaymentHash)?,
Some(preimage) if preimage == Hash256::default() => {
match self.store.get_invoice_status(&payment_hash) {
Some(status) => {
let is_active = status == CkbInvoiceStatus::Open
|| status == CkbInvoiceStatus::Received;
let is_settled =
self.store.get_invoice_preimage(&payment_hash).is_some();
if is_active && !is_settled {
// This TLC is added to applied_add_tlcs in above, but
// TLCs in the list applied_add_tlcs wouldn't be processed again.
// For the unsettled active hold invoice TLCs, we should process them indefinitely
// until they expire or are settled.
state.tlc_state.applied_add_tlcs.remove(&add_tlc.tlc_id);
}
if status == CkbInvoiceStatus::Open {
self.store
.update_invoice_status(
&payment_hash,
CkbInvoiceStatus::Received,
)
.expect("update invoice status failed");
}
if let Some(status) = self.store.get_invoice_status(&payment_hash) {
let is_active = status == CkbInvoiceStatus::Open
|| status == CkbInvoiceStatus::Received;
let is_settled = self.store.get_invoice_preimage(&payment_hash).is_some();
if is_active && !is_settled {
// This TLC is added to applied_add_tlcs in above, but
// TLCs in the list applied_add_tlcs wouldn't be processed again.
// For the unsettled active hold invoice TLCs, we should process them indefinitely
// until they expire or are settled.
state.tlc_state.applied_add_tlcs.remove(&add_tlc.tlc_id);
}
if status == CkbInvoiceStatus::Open {
self.store
.update_invoice_status(&payment_hash, CkbInvoiceStatus::Received)
.expect("update invoice status failed");
}
None => {}
}
if let Err(e) = self.store.add_invoice_channel_info(
&payment_hash,
Expand Down
42 changes: 21 additions & 21 deletions src/fiber/tests/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6227,13 +6227,13 @@ async fn test_send_payment_succeed_with_hold_invoice_settled_direct_payment() {
let last_channel = *channels.last().unwrap();
let [mut node_0, mut node_1] = nodes.try_into().expect("2 nodes");
let source_node = &mut node_0;
let target_pubkey = node_1.pubkey.clone();
let target_pubkey = node_1.pubkey;
let old_amount = node_1.get_local_balance_from_channel(last_channel);

let preimage = gen_rand_sha256_hash();
let ckb_invoice = InvoiceBuilder::new(Currency::Fibd)
.amount(Some(100))
.payment_preimage(preimage.clone())
.payment_preimage(preimage)
.payee_pub_key(target_pubkey.into())
.expiry_time(Duration::from_secs(100))
.build()
Expand All @@ -6243,7 +6243,7 @@ async fn test_send_payment_succeed_with_hold_invoice_settled_direct_payment() {

let res = source_node
.send_payment(SendPaymentCommand {
target_pubkey: Some(target_pubkey.clone()),
target_pubkey: Some(target_pubkey),
amount: Some(100),
payment_hash: None,
final_tlc_expiry_delta: None,
Expand Down Expand Up @@ -6312,13 +6312,13 @@ async fn test_send_payment_succeed_with_hold_invoice_settled_indirect_payment()
.await;
let [mut node_0, _node_1, mut node_2] = nodes.try_into().expect("3 nodes");
let source_node = &mut node_0;
let target_pubkey = node_2.pubkey.clone();
let target_pubkey = node_2.pubkey;
let old_amount = node_2.get_local_balance_from_channel(channels[1]);

let preimage = gen_rand_sha256_hash();
let ckb_invoice = InvoiceBuilder::new(Currency::Fibd)
.amount(Some(100))
.payment_preimage(preimage.clone())
.payment_preimage(preimage)
.payee_pub_key(target_pubkey.into())
.expiry_time(Duration::from_secs(100))
.build()
Expand All @@ -6328,7 +6328,7 @@ async fn test_send_payment_succeed_with_hold_invoice_settled_indirect_payment()

let res = source_node
.send_payment(SendPaymentCommand {
target_pubkey: Some(target_pubkey.clone()),
target_pubkey: Some(target_pubkey),
amount: Some(100),
payment_hash: None,
final_tlc_expiry_delta: None,
Expand Down Expand Up @@ -6398,13 +6398,13 @@ async fn test_send_payment_succeed_settle_hold_invoice_multiple_times() {
.await;
let [mut node_0, _node_1, mut node_2] = nodes.try_into().expect("3 nodes");
let source_node = &mut node_0;
let target_pubkey = node_2.pubkey.clone();
let target_pubkey = node_2.pubkey;
let old_amount = node_2.get_local_balance_from_channel(channels[1]);

let preimage = gen_rand_sha256_hash();
let ckb_invoice = InvoiceBuilder::new(Currency::Fibd)
.amount(Some(100))
.payment_preimage(preimage.clone())
.payment_preimage(preimage)
.payee_pub_key(target_pubkey.into())
.expiry_time(Duration::from_secs(100))
.build()
Expand All @@ -6414,7 +6414,7 @@ async fn test_send_payment_succeed_settle_hold_invoice_multiple_times() {

let res = source_node
.send_payment(SendPaymentCommand {
target_pubkey: Some(target_pubkey.clone()),
target_pubkey: Some(target_pubkey),
amount: Some(100),
payment_hash: None,
final_tlc_expiry_delta: None,
Expand Down Expand Up @@ -6492,13 +6492,13 @@ async fn test_send_payment_succeed_settle_hold_invoice_when_sender_offline() {
.await;
let [node_0, _node_1, mut node_2] = nodes.try_into().expect("3 nodes");
let mut source_node = node_0;
let target_pubkey = node_2.pubkey.clone();
let target_pubkey = node_2.pubkey;
let old_amount = node_2.get_local_balance_from_channel(channels[1]);

let preimage = gen_rand_sha256_hash();
let ckb_invoice = InvoiceBuilder::new(Currency::Fibd)
.amount(Some(100))
.payment_preimage(preimage.clone())
.payment_preimage(preimage)
.payee_pub_key(target_pubkey.into())
.expiry_time(Duration::from_secs(100))
.build()
Expand All @@ -6508,7 +6508,7 @@ async fn test_send_payment_succeed_settle_hold_invoice_when_sender_offline() {

let res = source_node
.send_payment(SendPaymentCommand {
target_pubkey: Some(target_pubkey.clone()),
target_pubkey: Some(target_pubkey),
amount: Some(100),
payment_hash: None,
final_tlc_expiry_delta: None,
Expand Down Expand Up @@ -6581,13 +6581,13 @@ async fn test_send_payment_succeed_settle_hold_invoice_when_forwarder_offline()
.await;
let [mut node_0, mut node_1, mut node_2] = nodes.try_into().expect("3 nodes");
let source_node = &mut node_0;
let target_pubkey = node_2.pubkey.clone();
let target_pubkey = node_2.pubkey;
let old_amount = node_2.get_local_balance_from_channel(channels[1]);

let preimage = gen_rand_sha256_hash();
let ckb_invoice = InvoiceBuilder::new(Currency::Fibd)
.amount(Some(100))
.payment_preimage(preimage.clone())
.payment_preimage(preimage)
.payee_pub_key(target_pubkey.into())
.expiry_time(Duration::from_secs(100))
.build()
Expand All @@ -6597,7 +6597,7 @@ async fn test_send_payment_succeed_settle_hold_invoice_when_forwarder_offline()

let res = source_node
.send_payment(SendPaymentCommand {
target_pubkey: Some(target_pubkey.clone()),
target_pubkey: Some(target_pubkey),
amount: Some(100),
payment_hash: None,
final_tlc_expiry_delta: None,
Expand Down Expand Up @@ -6670,13 +6670,13 @@ async fn test_send_payment_succeed_settle_invoice_before_send_payment() {
.await;
let [mut node_0, _node_1, mut node_2] = nodes.try_into().expect("3 nodes");
let source_node = &mut node_0;
let target_pubkey = node_2.pubkey.clone();
let target_pubkey = node_2.pubkey;
let old_amount = node_2.get_local_balance_from_channel(channels[1]);

let preimage = gen_rand_sha256_hash();
let ckb_invoice = InvoiceBuilder::new(Currency::Fibd)
.amount(Some(100))
.payment_preimage(preimage.clone())
.payment_preimage(preimage)
.payee_pub_key(target_pubkey.into())
.expiry_time(Duration::from_secs(100))
.build()
Expand All @@ -6689,7 +6689,7 @@ async fn test_send_payment_succeed_settle_invoice_before_send_payment() {

let res = source_node
.send_payment(SendPaymentCommand {
target_pubkey: Some(target_pubkey.clone()),
target_pubkey: Some(target_pubkey),
amount: Some(100),
payment_hash: None,
final_tlc_expiry_delta: None,
Expand Down Expand Up @@ -6742,14 +6742,14 @@ async fn test_send_payment_succeed_settle_invoice_with_wrong_then_right_hash() {
.await;
let [mut node_0, _node_1, mut node_2] = nodes.try_into().expect("3 nodes");
let source_node = &mut node_0;
let target_pubkey = node_2.pubkey.clone();
let target_pubkey = node_2.pubkey;
let old_amount = node_2.get_local_balance_from_channel(channels[1]);

let preimage = gen_rand_sha256_hash();
let bogus_preimage = gen_rand_sha256_hash();
let ckb_invoice = InvoiceBuilder::new(Currency::Fibd)
.amount(Some(100))
.payment_preimage(preimage.clone())
.payment_preimage(preimage)
.payee_pub_key(target_pubkey.into())
.expiry_time(Duration::from_secs(100))
.build()
Expand All @@ -6761,7 +6761,7 @@ async fn test_send_payment_succeed_settle_invoice_with_wrong_then_right_hash() {

let res = source_node
.send_payment(SendPaymentCommand {
target_pubkey: Some(target_pubkey.clone()),
target_pubkey: Some(target_pubkey),
amount: Some(100),
payment_hash: None,
final_tlc_expiry_delta: None,
Expand Down
12 changes: 6 additions & 6 deletions src/fiber/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ impl FromStr for Hash256 {
}
}

impl Into<[u8; 32]> for Hash256 {
fn into(self) -> [u8; 32] {
self.0
impl From<Hash256> for [u8; 32] {
fn from(val: Hash256) -> Self {
val.0
}
}

impl Into<Vec<u8>> for Hash256 {
fn into(self) -> Vec<u8> {
self.0.to_vec()
impl From<Hash256> for Vec<u8> {
fn from(val: Hash256) -> Self {
val.0.to_vec()
}
}

Expand Down
Loading

0 comments on commit 1ed77fd

Please sign in to comment.