use crate::util::logger::Logger;
use crate::util::scid_utils;
use crate::util::test_channel_signer::TestChannelSigner;
+#[cfg(test)]
+use crate::util::test_channel_signer::SignerOp;
use crate::util::test_utils;
use crate::util::test_utils::{panicking, TestChainMonitor, TestScorer, TestKeysInterface};
use crate::util::ser::{ReadableArgs, Writeable};
+use bitcoin::amount::Amount;
use bitcoin::blockdata::block::{Block, Header, Version};
use bitcoin::blockdata::locktime::absolute::LockTime;
use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut};
use bitcoin::hash_types::{BlockHash, TxMerkleNode};
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::Hash as _;
-use bitcoin::network::constants::Network;
+use bitcoin::network::Network;
use bitcoin::pow::CompactTarget;
use bitcoin::secp256k1::{PublicKey, SecretKey};
+use bitcoin::transaction;
use alloc::rc::Rc;
use core::cell::RefCell;
txdata: Vec::new(),
};
for _ in 0..*node.network_chan_count.borrow() { // Make sure we don't end up with channels at the same short id by offsetting by chan_count
- block.txdata.push(Transaction { version: 0, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() });
+ block.txdata.push(Transaction { version: transaction::Version(0), lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() });
}
block.txdata.push((*tx).clone());
do_connect_block_without_consistency_checks(node, block, false);
}
let mut txdata = Vec::new();
for _ in 0..*node.network_chan_count.borrow() { // Make sure we don't end up with channels at the same short id by offsetting by chan_count
- txdata.push(Transaction { version: 0, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() });
+ txdata.push(Transaction { version: transaction::Version(0), lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() });
}
for tx in txn {
txdata.push((*tx).clone());
&'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
&'node_cfg test_utils::TestMessageRouter<'chan_mon_cfg>,
&'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
+ &'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
IgnoringMessageHandler,
>;
pub fn get_block_header(&self, height: u32) -> Header {
self.blocks.lock().unwrap()[height as usize].0.header
}
- /// Changes the channel signer's availability for the specified peer and channel.
+
+ /// Toggles this node's signer to be available for the given signer operation.
+ /// This is useful for testing behavior for restoring an async signer that previously
+ /// could not return a signature immediately.
+ #[cfg(test)]
+ pub fn enable_channel_signer_op(&self, peer_id: &PublicKey, chan_id: &ChannelId, signer_op: SignerOp) {
+ self.set_channel_signer_ops(peer_id, chan_id, signer_op, true);
+ }
+
+ /// Toggles this node's signer to be unavailable, returning `Err` for the given signer operation.
+ /// This is useful for testing behavior for an async signer that cannot return a signature
+ /// immediately.
+ #[cfg(test)]
+ pub fn disable_channel_signer_op(&self, peer_id: &PublicKey, chan_id: &ChannelId, signer_op: SignerOp) {
+ self.set_channel_signer_ops(peer_id, chan_id, signer_op, false);
+ }
+
+ /// Changes the channel signer's availability for the specified peer, channel, and signer
+ /// operation.
///
- /// When `available` is set to `true`, the channel signer will behave normally. When set to
- /// `false`, the channel signer will act like an off-line remote signer and will return `Err` for
- /// several of the signing methods. Currently, only `get_per_commitment_point` and
- /// `release_commitment_secret` are affected by this setting.
+ /// For the specified signer operation, when `available` is set to `true`, the channel signer
+ /// will behave normally, returning `Ok`. When set to `false`, and the channel signer will
+ /// act like an off-line remote signer, returning `Err`. This applies to the signer in all
+ /// relevant places, i.e. the channel manager, chain monitor, and the keys manager.
#[cfg(test)]
- pub fn set_channel_signer_available(&self, peer_id: &PublicKey, chan_id: &ChannelId, available: bool) {
+ fn set_channel_signer_ops(&self, peer_id: &PublicKey, chan_id: &ChannelId, signer_op: SignerOp, available: bool) {
use crate::sign::ChannelSigner;
log_debug!(self.logger, "Setting channel signer for {} as available={}", chan_id, available);
let per_peer_state = self.node.per_peer_state.read().unwrap();
- let chan_lock = per_peer_state.get(peer_id).unwrap().lock().unwrap();
+ let mut chan_lock = per_peer_state.get(peer_id).unwrap().lock().unwrap();
let mut channel_keys_id = None;
- if let Some(chan) = chan_lock.channel_by_id.get(chan_id).map(|phase| phase.context()) {
- chan.get_signer().as_ecdsa().unwrap().set_available(available);
+ if let Some(chan) = chan_lock.channel_by_id.get_mut(chan_id).map(|phase| phase.context_mut()) {
+ let signer = chan.get_mut_signer().as_mut_ecdsa().unwrap();
+ if available {
+ signer.enable_op(signer_op);
+ } else {
+ signer.disable_op(signer_op);
+ }
channel_keys_id = Some(chan.channel_keys_id);
}
- let mut monitor = None;
- for (funding_txo, channel_id) in self.chain_monitor.chain_monitor.list_monitors() {
- if *chan_id == channel_id {
- monitor = self.chain_monitor.chain_monitor.get_monitor(funding_txo).ok();
- }
- }
+ let monitor = self.chain_monitor.chain_monitor.list_monitors().into_iter()
+ .find(|(_, channel_id)| *channel_id == *chan_id)
+ .and_then(|(funding_txo, _)| self.chain_monitor.chain_monitor.get_monitor(funding_txo).ok());
if let Some(monitor) = monitor {
- monitor.do_signer_call(|signer| {
+ monitor.do_mut_signer_call(|signer| {
channel_keys_id = channel_keys_id.or(Some(signer.inner.channel_keys_id()));
- signer.set_available(available)
+ if available {
+ signer.enable_op(signer_op);
+ } else {
+ signer.disable_op(signer_op);
+ }
});
}
+ let channel_keys_id = channel_keys_id.unwrap();
+ let mut unavailable_signers_ops = self.keys_manager.unavailable_signers_ops.lock().unwrap();
+ let entry = unavailable_signers_ops.entry(channel_keys_id).or_insert(new_hash_set());
if available {
- self.keys_manager.unavailable_signers.lock().unwrap()
- .remove(channel_keys_id.as_ref().unwrap());
+ entry.remove(&signer_op);
+ if entry.is_empty() {
+ unavailable_signers_ops.remove(&channel_keys_id);
+ }
} else {
- self.keys_manager.unavailable_signers.lock().unwrap()
- .insert(channel_keys_id.unwrap());
- }
+ entry.insert(signer_op);
+ };
}
}
MessageSendEvent::SendStfu { node_id, .. } => {
node_id == msg_node_id
},
- MessageSendEvent::SendSplice { node_id, .. } => {
+ MessageSendEvent::SendSpliceInit { node_id, .. } => {
node_id == msg_node_id
},
MessageSendEvent::SendSpliceAck { node_id, .. } => {
Vec::new()
};
- let tx = Transaction { version: chan_id as i32, lock_time: LockTime::ZERO, input, output: vec![TxOut {
- value: *channel_value_satoshis, script_pubkey: output_script.clone(),
+ let tx = Transaction { version: transaction::Version(chan_id as i32), lock_time: LockTime::ZERO, input, output: vec![TxOut {
+ value: Amount::from_sat(*channel_value_satoshis), script_pubkey: output_script.clone(),
}]};
let funding_outpoint = OutPoint { txid: tx.txid(), index: 0 };
(*temporary_channel_id, tx, funding_outpoint)
pub fn do_check_spends<F: Fn(&bitcoin::blockdata::transaction::OutPoint) -> Option<TxOut>>(tx: &Transaction, get_output: F) {
for outp in tx.output.iter() {
- assert!(outp.value >= outp.script_pubkey.dust_value().to_sat(), "Spending tx output didn't meet dust limit");
+ assert!(outp.value >= outp.script_pubkey.dust_value(), "Spending tx output didn't meet dust limit");
}
let mut total_value_in = 0;
for input in tx.input.iter() {
- total_value_in += get_output(&input.previous_output).unwrap().value;
+ total_value_in += get_output(&input.previous_output).unwrap().value.to_sat();
}
let mut total_value_out = 0;
for output in tx.output.iter() {
- total_value_out += output.value;
+ total_value_out += output.value.to_sat();
}
let min_fee = (tx.weight().to_wu() as u64 + 3) / 4; // One sat per vbyte (ie per weight/4, rounded up)
// Input amount - output amount = fee, so check that out + min_fee is smaller than input
{
$(
for outp in $spends_txn.output.iter() {
- assert!(outp.value >= outp.script_pubkey.dust_value().to_sat(), "Input tx output didn't meet dust limit");
+ assert!(outp.value >= outp.script_pubkey.dust_value(), "Input tx output didn't meet dust limit");
}
)*
let get_output = |out_point: &bitcoin::blockdata::transaction::OutPoint| {
assert!(events.len() == 1 || events.len() == 2);
(match events[events.len() - 1] {
MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
- assert_eq!(msg.contents.flags & 2, 2);
+ assert_eq!(msg.contents.channel_flags & 2, 2);
msg.clone()
},
_ => panic!("Unexpected event"),
msg_events.into_iter().filter_map(|msg_event| {
match msg_event {
MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
- assert_eq!(msg.contents.flags & 2, 2);
+ assert_eq!(msg.contents.channel_flags & 2, 2);
None
},
MessageSendEvent::HandleError { action: msgs::ErrorAction::SendErrorMessage { msg }, node_id: _ } => {
/// Gets a route from the given sender to the node described in `payment_params`.
pub fn get_route(send_node: &Node, route_params: &RouteParameters) -> Result<Route, msgs::LightningError> {
let scorer = TestScorer::new();
- let keys_manager = TestKeysInterface::new(&[0u8; 32], bitcoin::network::constants::Network::Testnet);
+ let keys_manager = TestKeysInterface::new(&[0u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
router::get_route(
&send_node.node.get_our_node_id(), route_params, &send_node.network_graph.read_only(),
/// Like `get_route` above, but adds a random CLTV offset to the final hop.
pub fn find_route(send_node: &Node, route_params: &RouteParameters) -> Result<Route, msgs::LightningError> {
let scorer = TestScorer::new();
- let keys_manager = TestKeysInterface::new(&[0u8; 32], bitcoin::network::constants::Network::Testnet);
+ let keys_manager = TestKeysInterface::new(&[0u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
router::find_route(
&send_node.node.get_our_node_id(), route_params, &send_node.network_graph,
(our_payment_preimage, our_payment_hash, our_payment_secret, payment_id)
}
-pub fn do_claim_payment_along_route<'a, 'b, 'c>(
- origin_node: &Node<'a, 'b, 'c>, expected_paths: &[&[&Node<'a, 'b, 'c>]], skip_last: bool,
- our_payment_preimage: PaymentPreimage
-) -> u64 {
- for path in expected_paths.iter() {
- assert_eq!(path.last().unwrap().node.get_our_node_id(), expected_paths[0].last().unwrap().node.get_our_node_id());
+pub fn do_claim_payment_along_route(args: ClaimAlongRouteArgs) -> u64 {
+ for path in args.expected_paths.iter() {
+ assert_eq!(path.last().unwrap().node.get_our_node_id(), args.expected_paths[0].last().unwrap().node.get_our_node_id());
}
- expected_paths[0].last().unwrap().node.claim_funds(our_payment_preimage);
- pass_claimed_payment_along_route(
- ClaimAlongRouteArgs::new(origin_node, expected_paths, our_payment_preimage)
- .skip_last(skip_last)
- )
+ args.expected_paths[0].last().unwrap().node.claim_funds(args.payment_preimage);
+ pass_claimed_payment_along_route(args)
}
pub struct ClaimAlongRouteArgs<'a, 'b, 'c, 'd> {
pub expected_min_htlc_overpay: Vec<u32>,
pub skip_last: bool,
pub payment_preimage: PaymentPreimage,
+ pub custom_tlvs: Vec<(u64, Vec<u8>)>,
// Allow forwarding nodes to have taken 1 msat more fee than expected based on the downstream
// fulfill amount.
//
Self {
origin_node, expected_paths, expected_extra_fees: vec![0; expected_paths.len()],
expected_min_htlc_overpay: vec![0; expected_paths.len()], skip_last: false, payment_preimage,
- allow_1_msat_fee_overpay: false,
+ allow_1_msat_fee_overpay: false, custom_tlvs: vec![],
}
}
pub fn skip_last(mut self, skip_last: bool) -> Self {
self.allow_1_msat_fee_overpay = true;
self
}
+ pub fn with_custom_tlvs(mut self, custom_tlvs: Vec<(u64, Vec<u8>)>) -> Self {
+ self.custom_tlvs = custom_tlvs;
+ self
+ }
}
-pub fn pass_claimed_payment_along_route<'a, 'b, 'c, 'd>(args: ClaimAlongRouteArgs) -> u64 {
+pub fn pass_claimed_payment_along_route(args: ClaimAlongRouteArgs) -> u64 {
let ClaimAlongRouteArgs {
origin_node, expected_paths, expected_extra_fees, expected_min_htlc_overpay, skip_last,
- payment_preimage: our_payment_preimage, allow_1_msat_fee_overpay,
+ payment_preimage: our_payment_preimage, allow_1_msat_fee_overpay, custom_tlvs,
} = args;
let claim_event = expected_paths[0].last().unwrap().node.get_and_clear_pending_events();
assert_eq!(claim_event.len(), 1);
| PaymentPurpose::Bolt12RefundPayment { payment_preimage: Some(preimage), .. },
amount_msat,
ref htlcs,
+ ref onion_fields,
..
} => {
assert_eq!(preimage, our_payment_preimage);
assert_eq!(htlcs.len(), expected_paths.len()); // One per path.
assert_eq!(htlcs.iter().map(|h| h.value_msat).sum::<u64>(), amount_msat);
+ assert_eq!(onion_fields.as_ref().unwrap().custom_tlvs, custom_tlvs);
expected_paths.iter().zip(htlcs).for_each(|(path, htlc)| check_claimed_htlc_channel(origin_node, path, htlc));
fwd_amt_msat = amount_msat;
},
payment_hash,
amount_msat,
ref htlcs,
+ ref onion_fields,
..
} => {
assert_eq!(&payment_hash.0, &Sha256::hash(&our_payment_preimage.0)[..]);
assert_eq!(htlcs.len(), expected_paths.len()); // One per path.
assert_eq!(htlcs.iter().map(|h| h.value_msat).sum::<u64>(), amount_msat);
+ assert_eq!(onion_fields.as_ref().unwrap().custom_tlvs, custom_tlvs);
expected_paths.iter().zip(htlcs).for_each(|(path, htlc)| check_claimed_htlc_channel(origin_node, path, htlc));
fwd_amt_msat = amount_msat;
}
expected_total_fee_msat
}
-pub fn claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_paths: &[&[&Node<'a, 'b, 'c>]], skip_last: bool, our_payment_preimage: PaymentPreimage) {
- let expected_total_fee_msat = do_claim_payment_along_route(origin_node, expected_paths, skip_last, our_payment_preimage);
+pub fn claim_payment_along_route(args: ClaimAlongRouteArgs) {
+ let origin_node = args.origin_node;
+ let payment_preimage = args.payment_preimage;
+ let skip_last = args.skip_last;
+ let expected_total_fee_msat = do_claim_payment_along_route(args);
if !skip_last {
- expect_payment_sent!(origin_node, our_payment_preimage, Some(expected_total_fee_msat));
+ expect_payment_sent!(origin_node, payment_preimage, Some(expected_total_fee_msat));
}
}
pub fn claim_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], our_payment_preimage: PaymentPreimage) {
- claim_payment_along_route(origin_node, &[expected_route], false, our_payment_preimage);
+ claim_payment_along_route(
+ ClaimAlongRouteArgs::new(origin_node, &[expected_route], our_payment_preimage)
+ );
}
pub const TEST_FINAL_CLTV: u32 = 70;
let dedicated_entropy = DedicatedEntropy(RandomBytes::new([i as u8; 32]));
let onion_messenger = OnionMessenger::new(
dedicated_entropy, cfgs[i].keys_manager, cfgs[i].logger, &chan_mgrs[i],
- &cfgs[i].message_router, &chan_mgrs[i], IgnoringMessageHandler {},
+ &cfgs[i].message_router, &chan_mgrs[i], &chan_mgrs[i], IgnoringMessageHandler {},
);
let gossip_sync = P2PGossipSync::new(cfgs[i].network_graph.as_ref(), None, cfgs[i].logger);
let wallet_source = Arc::new(test_utils::TestWalletSource::new(SecretKey::from_slice(&[i as u8 + 1; 32]).unwrap()));
for i in 0..node_count {
for j in (i+1)..node_count {
- let node_id_i = nodes[i].node.get_our_node_id();
- let node_id_j = nodes[j].node.get_our_node_id();
-
- let init_i = msgs::Init {
- features: nodes[i].init_features(&node_id_j),
- networks: None,
- remote_network_address: None,
- };
- let init_j = msgs::Init {
- features: nodes[j].init_features(&node_id_i),
- networks: None,
- remote_network_address: None,
- };
-
- nodes[i].node.peer_connected(&node_id_j, &init_j, true).unwrap();
- nodes[j].node.peer_connected(&node_id_i, &init_i, false).unwrap();
- nodes[i].onion_messenger.peer_connected(&node_id_j, &init_j, true).unwrap();
- nodes[j].onion_messenger.peer_connected(&node_id_i, &init_i, false).unwrap();
+ connect_nodes(&nodes[i], &nodes[j]);
}
}
nodes
}
+fn connect_nodes<'a, 'b: 'a, 'c: 'b>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
+ let node_id_a = node_a.node.get_our_node_id();
+ let node_id_b = node_b.node.get_our_node_id();
+
+ let init_a = msgs::Init {
+ features: node_a.init_features(&node_id_b),
+ networks: None,
+ remote_network_address: None,
+ };
+ let init_b = msgs::Init {
+ features: node_b.init_features(&node_id_a),
+ networks: None,
+ remote_network_address: None,
+ };
+
+ node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
+ node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
+ node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
+ node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
+}
+
pub fn connect_dummy_node<'a, 'b: 'a, 'c: 'b>(node: &Node<'a, 'b, 'c>) {
let node_id_dummy = PublicKey::from_slice(&[2; 33]).unwrap();
pending_cell_htlc_claims, pending_cell_htlc_fails, pending_raa,
pending_responding_commitment_signed, pending_responding_commitment_signed_dup_monitor,
} = args;
- node_a.node.peer_connected(&node_b.node.get_our_node_id(), &msgs::Init {
- features: node_b.node.init_features(), networks: None, remote_network_address: None
- }, true).unwrap();
+ connect_nodes(node_a, node_b);
let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
- node_b.node.peer_connected(&node_a.node.get_our_node_id(), &msgs::Init {
- features: node_a.node.init_features(), networks: None, remote_network_address: None
- }, false).unwrap();
let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
if send_channel_ready.0 {
assert_eq!(channel_value_satoshis, event_channel_value_satoshis);
assert_eq!(user_channel_id, event_user_channel_id);
tx_outs.push(TxOut {
- value: *channel_value_satoshis, script_pubkey: output_script.clone(),
+ value: Amount::from_sat(*channel_value_satoshis), script_pubkey: output_script.clone(),
});
},
_ => panic!("Unexpected event"),
// Compose the batch funding transaction and give it to the ChannelManager.
let tx = Transaction {
- version: 2,
+ version: transaction::Version::TWO,
lock_time: LockTime::ZERO,
input: Vec::new(),
output: tx_outs,