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 969f732
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 113 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
19 changes: 11 additions & 8 deletions src/cch/tests/lnd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ impl LndNode {
.listen_url
.clone()
.expect("have listening address"),
..Default::default()
};
let request = lnd::tonic_lnd::lnrpc::ConnectPeerRequest {
addr: Some(address),
Expand Down Expand Up @@ -241,11 +240,13 @@ impl LndNode {
other.wait_synced_to_chain().await;

let other_info = other.get_info().await;
let mut request = lnd::tonic_lnd::lnrpc::OpenChannelRequest::default();
request.node_pubkey = hex::decode(other_info.identity_pubkey).expect("valid pubkey hex");
request.local_funding_amount = 1_000_000;
request.sat_per_vbyte = 1;
request.min_confs = 0;
let request = lnd::tonic_lnd::lnrpc::OpenChannelRequest {
node_pubkey: hex::decode(other_info.identity_pubkey).expect("valid pubkey hex"),
local_funding_amount: 1_000_000,
sat_per_vbyte: 1,
min_confs: 0,
..Default::default()
};

let channel = self
.lnd
Expand All @@ -269,8 +270,10 @@ impl LndNode {
&mut self,
value_msat: u64,
) -> lnd::tonic_lnd::lnrpc::AddInvoiceResponse {
let mut request = lnd::tonic_lnd::lnrpc::Invoice::default();
request.value_msat = value_msat as i64;
let request = lnd::tonic_lnd::lnrpc::Invoice {
value_msat: value_msat as i64,
..Default::default()
};
self.lnd
.client
.lightning()
Expand Down
20 changes: 12 additions & 8 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 @@ -48,15 +48,17 @@ async fn do_test_cross_chain_payment_hub_send_btc(udt_script: Script, multiple_h
let nodes = NetworkNode::new_n_interconnected_nodes_with_config(num_nodes, |n| {
let mut builder = NetworkNodeConfigBuilder::new();
if n == num_nodes - 1 {
let mut cch_config = CchConfig::default();
cch_config.wrapped_btc_type_script = serialize_entity_to_hex_string(&udt_script);
let cch_config = CchConfig {
wrapped_btc_type_script: serialize_entity_to_hex_string(&udt_script),
..Default::default()
};
builder = builder.should_start_lnd(true).cch_config(cch_config);
}
builder.build()
})
.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 +171,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 @@ -235,8 +237,10 @@ async fn do_test_cross_chain_payment_hub_receive_btc(udt_script: Script, multipl
let nodes = NetworkNode::new_n_interconnected_nodes_with_config(num_nodes, |n| {
let mut builder = NetworkNodeConfigBuilder::new();
if n == num_nodes - 1 {
let mut cch_config = CchConfig::default();
cch_config.wrapped_btc_type_script = serialize_entity_to_hex_string(&udt_script);
let cch_config = CchConfig {
wrapped_btc_type_script: serialize_entity_to_hex_string(&udt_script),
..Default::default()
};
builder = builder.should_start_lnd(true).cch_config(cch_config);
}
builder.build()
Expand Down Expand Up @@ -331,7 +335,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
16 changes: 8 additions & 8 deletions src/ckb/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tokio::sync::RwLock;

use crate::{
ckb::{
config::{UdtArgInfo, UdtCfgInfos, UdtScript},
config::{UdtArgInfo, UdtCellDep, UdtCfgInfos, UdtScript},
contracts::{
get_cell_deps_by_contracts, get_script_by_contract, get_udt_cell_deps, Contract,
ContractsContext, ContractsInfo,
Expand Down 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 @@ -141,15 +141,15 @@ impl MockContext {
.into_iter()
.map(|contract| {
let script = contract_default_scripts.get(&contract).unwrap();
let cell_dep = script_cell_deps
let cell_deps: Vec<UdtCellDep> = script_cell_deps
.get(&contract)
.map(|x| x.iter().map(Into::into).collect())
.unwrap_or_else(|| vec![]);
.unwrap_or_default();
UdtArgInfo {
name: format!("{:?}", contract),
script: UdtScript::allow_all_for_script(script),
auto_accept_amount: None,
cell_deps: cell_dep.clone(),
cell_deps,
}
})
.collect();
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
Loading

0 comments on commit 969f732

Please sign in to comment.