Skip to content

Commit 2be7eda

Browse files
authored
Merge pull request #179 from TheBlueMatt/2018-09-pre-178-cleanups
Pre-reconnect ChannelManager test cleanups
2 parents ed71995 + 5737b32 commit 2be7eda

File tree

2 files changed

+108
-68
lines changed

2 files changed

+108
-68
lines changed

Diff for: src/ln/channel.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -2431,8 +2431,6 @@ impl Channel {
24312431
}
24322432
/// Only fails in case of bad keys
24332433
fn send_commitment_no_status_check(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor), HandleError> {
2434-
let funding_script = self.get_funding_redeemscript();
2435-
24362434
// We can upgrade the status of some HTLCs that are waiting on a commitment, even if we
24372435
// fail to generate this, we still are at least at a position where upgrading their status
24382436
// is acceptable.
@@ -2447,6 +2445,22 @@ impl Channel {
24472445
}
24482446
}
24492447

2448+
match self.send_commitment_no_state_update() {
2449+
Ok((res, remote_commitment_tx)) => {
2450+
// Update state now that we've passed all the can-fail calls...
2451+
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx.0, remote_commitment_tx.1, self.cur_remote_commitment_transaction_number);
2452+
self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
2453+
Ok((res, self.channel_monitor.clone()))
2454+
},
2455+
Err(e) => Err(e),
2456+
}
2457+
}
2458+
2459+
/// Only fails in case of bad keys. Used for channel_reestablish commitment_signed generation
2460+
/// when we shouldn't change HTLC/channel state.
2461+
fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<HTLCOutputInCommitment>)), HandleError> {
2462+
let funding_script = self.get_funding_redeemscript();
2463+
24502464
let remote_keys = self.build_remote_transaction_keys()?;
24512465
let remote_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, true);
24522466
let remote_commitment_txid = remote_commitment_tx.0.txid();
@@ -2463,15 +2477,11 @@ impl Channel {
24632477
htlc_sigs.push(self.secp_ctx.sign(&htlc_sighash, &our_htlc_key));
24642478
}
24652479

2466-
// Update state now that we've passed all the can-fail calls...
2467-
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx.0, remote_commitment_tx.1, self.cur_remote_commitment_transaction_number);
2468-
self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
2469-
24702480
Ok((msgs::CommitmentSigned {
24712481
channel_id: self.channel_id,
24722482
signature: our_sig,
24732483
htlc_signatures: htlc_sigs,
2474-
}, self.channel_monitor.clone()))
2484+
}, remote_commitment_tx))
24752485
}
24762486

24772487
/// Adds a pending outbound HTLC to this channel, and creates a signed commitment transaction

Diff for: src/ln/channelmanager.rs

+91-61
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ impl ChannelManager {
12181218
};
12191219
match channel_state.claimable_htlcs.entry(forward_info.payment_hash) {
12201220
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push(prev_hop_data),
1221-
hash_map::Entry::Vacant(mut entry) => { entry.insert(vec![prev_hop_data]); },
1221+
hash_map::Entry::Vacant(entry) => { entry.insert(vec![prev_hop_data]); },
12221222
};
12231223
new_events.push((None, events::Event::PaymentReceived {
12241224
payment_hash: forward_info.payment_hash,
@@ -2198,7 +2198,6 @@ mod tests {
21982198
use bitcoin::util::hash::Sha256dHash;
21992199
use bitcoin::blockdata::block::{Block, BlockHeader};
22002200
use bitcoin::blockdata::transaction::{Transaction, TxOut};
2201-
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
22022201
use bitcoin::blockdata::constants::genesis_block;
22032202
use bitcoin::network::constants::Network;
22042203
use bitcoin::network::serialize::serialize;
@@ -2391,6 +2390,13 @@ mod tests {
23912390
network_payment_count: Rc<RefCell<u8>>,
23922391
network_chan_count: Rc<RefCell<u32>>,
23932392
}
2393+
impl Drop for Node {
2394+
fn drop(&mut self) {
2395+
// Check that we processed all pending events
2396+
assert_eq!(self.node.get_and_clear_pending_events().len(), 0);
2397+
assert_eq!(self.chan_monitor.added_monitors.lock().unwrap().len(), 0);
2398+
}
2399+
}
23942400

23952401
fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
23962402
node_a.node.create_channel(node_b.node.get_our_node_id(), 100000, 10001, 42).unwrap();
@@ -2735,7 +2741,7 @@ mod tests {
27352741
(our_payment_preimage, our_payment_hash)
27362742
}
27372743

2738-
fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: [u8; 32]) {
2744+
fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: [u8; 32]) {
27392745
assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
27402746
{
27412747
let mut added_monitors = expected_route.last().unwrap().chan_monitor.added_monitors.lock().unwrap();
@@ -2764,40 +2770,51 @@ mod tests {
27642770

27652771
let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
27662772
let mut prev_node = expected_route.last().unwrap();
2767-
for node in expected_route.iter().rev() {
2773+
for (idx, node) in expected_route.iter().rev().enumerate() {
27682774
assert_eq!(expected_next_node, node.node.get_our_node_id());
27692775
if next_msgs.is_some() {
27702776
update_fulfill_dance!(node, prev_node, false);
27712777
}
27722778

27732779
let events = node.node.get_and_clear_pending_events();
2780+
if !skip_last || idx != expected_route.len() - 1 {
2781+
assert_eq!(events.len(), 1);
2782+
match events[0] {
2783+
Event::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed } } => {
2784+
assert!(update_add_htlcs.is_empty());
2785+
assert_eq!(update_fulfill_htlcs.len(), 1);
2786+
assert!(update_fail_htlcs.is_empty());
2787+
assert!(update_fail_malformed_htlcs.is_empty());
2788+
expected_next_node = node_id.clone();
2789+
next_msgs = Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()));
2790+
},
2791+
_ => panic!("Unexpected event"),
2792+
}
2793+
} else {
2794+
assert!(events.is_empty());
2795+
}
2796+
if !skip_last && idx == expected_route.len() - 1 {
2797+
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2798+
}
2799+
2800+
prev_node = node;
2801+
}
2802+
2803+
if !skip_last {
2804+
update_fulfill_dance!(origin_node, expected_route.first().unwrap(), true);
2805+
let events = origin_node.node.get_and_clear_pending_events();
27742806
assert_eq!(events.len(), 1);
27752807
match events[0] {
2776-
Event::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed } } => {
2777-
assert!(update_add_htlcs.is_empty());
2778-
assert_eq!(update_fulfill_htlcs.len(), 1);
2779-
assert!(update_fail_htlcs.is_empty());
2780-
assert!(update_fail_malformed_htlcs.is_empty());
2781-
expected_next_node = node_id.clone();
2782-
next_msgs = Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()));
2808+
Event::PaymentSent { payment_preimage } => {
2809+
assert_eq!(payment_preimage, our_payment_preimage);
27832810
},
27842811
_ => panic!("Unexpected event"),
2785-
};
2786-
2787-
prev_node = node;
2812+
}
27882813
}
2814+
}
27892815

2790-
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2791-
update_fulfill_dance!(origin_node, expected_route.first().unwrap(), true);
2792-
2793-
let events = origin_node.node.get_and_clear_pending_events();
2794-
assert_eq!(events.len(), 1);
2795-
match events[0] {
2796-
Event::PaymentSent { payment_preimage } => {
2797-
assert_eq!(payment_preimage, our_payment_preimage);
2798-
},
2799-
_ => panic!("Unexpected event"),
2800-
}
2816+
fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: [u8; 32]) {
2817+
claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
28012818
}
28022819

28032820
const TEST_FINAL_CLTV: u32 = 32;
@@ -2841,7 +2858,7 @@ mod tests {
28412858
claim_payment(&origin, expected_route, our_payment_preimage);
28422859
}
28432860

2844-
fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: [u8; 32]) {
2861+
fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: [u8; 32]) {
28452862
assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
28462863
{
28472864
let mut added_monitors = expected_route.last().unwrap().chan_monitor.added_monitors.lock().unwrap();
@@ -2861,42 +2878,57 @@ mod tests {
28612878

28622879
let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
28632880
let mut prev_node = expected_route.last().unwrap();
2864-
for node in expected_route.iter().rev() {
2881+
for (idx, node) in expected_route.iter().rev().enumerate() {
28652882
assert_eq!(expected_next_node, node.node.get_our_node_id());
28662883
if next_msgs.is_some() {
2867-
update_fail_dance!(node, prev_node, false);
2884+
// We may be the "last node" for the purpose of the commitment dance if we're
2885+
// skipping the last node (implying it is disconnected) and we're the
2886+
// second-to-last node!
2887+
update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
28682888
}
28692889

28702890
let events = node.node.get_and_clear_pending_events();
2871-
assert_eq!(events.len(), 1);
2872-
match events[0] {
2873-
Event::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed } } => {
2874-
assert!(update_add_htlcs.is_empty());
2875-
assert!(update_fulfill_htlcs.is_empty());
2876-
assert_eq!(update_fail_htlcs.len(), 1);
2877-
assert!(update_fail_malformed_htlcs.is_empty());
2878-
expected_next_node = node_id.clone();
2879-
next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
2880-
},
2881-
_ => panic!("Unexpected event"),
2882-
};
2891+
if !skip_last || idx != expected_route.len() - 1 {
2892+
assert_eq!(events.len(), 1);
2893+
match events[0] {
2894+
Event::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed } } => {
2895+
assert!(update_add_htlcs.is_empty());
2896+
assert!(update_fulfill_htlcs.is_empty());
2897+
assert_eq!(update_fail_htlcs.len(), 1);
2898+
assert!(update_fail_malformed_htlcs.is_empty());
2899+
expected_next_node = node_id.clone();
2900+
next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
2901+
},
2902+
_ => panic!("Unexpected event"),
2903+
}
2904+
} else {
2905+
assert!(events.is_empty());
2906+
}
2907+
if !skip_last && idx == expected_route.len() - 1 {
2908+
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2909+
}
28832910

28842911
prev_node = node;
28852912
}
28862913

2887-
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2888-
update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
2914+
if !skip_last {
2915+
update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
28892916

2890-
let events = origin_node.node.get_and_clear_pending_events();
2891-
assert_eq!(events.len(), 1);
2892-
match events[0] {
2893-
Event::PaymentFailed { payment_hash } => {
2894-
assert_eq!(payment_hash, our_payment_hash);
2895-
},
2896-
_ => panic!("Unexpected event"),
2917+
let events = origin_node.node.get_and_clear_pending_events();
2918+
assert_eq!(events.len(), 1);
2919+
match events[0] {
2920+
Event::PaymentFailed { payment_hash } => {
2921+
assert_eq!(payment_hash, our_payment_hash);
2922+
},
2923+
_ => panic!("Unexpected event"),
2924+
}
28972925
}
28982926
}
28992927

2928+
fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: [u8; 32]) {
2929+
fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
2930+
}
2931+
29002932
fn create_network(node_count: usize) -> Vec<Node> {
29012933
let mut nodes = Vec::new();
29022934
let mut rng = thread_rng();
@@ -3037,12 +3069,6 @@ mod tests {
30373069
close_channel(&nodes[2], &nodes[3], &chan_3.2, chan_3.3, true);
30383070
close_channel(&nodes[1], &nodes[3], &chan_4.2, chan_4.3, false);
30393071
close_channel(&nodes[1], &nodes[3], &chan_5.2, chan_5.3, false);
3040-
3041-
// Check that we processed all pending events
3042-
for node in nodes {
3043-
assert_eq!(node.node.get_and_clear_pending_events().len(), 0);
3044-
assert_eq!(node.chan_monitor.added_monitors.lock().unwrap().len(), 0);
3045-
}
30463072
}
30473073

30483074
#[test]
@@ -3352,12 +3378,6 @@ mod tests {
33523378
get_announce_close_broadcast_events(&nodes, 0, 1);
33533379
assert_eq!(nodes[0].node.list_channels().len(), 0);
33543380
assert_eq!(nodes[1].node.list_channels().len(), 0);
3355-
3356-
// Check that we processed all pending events
3357-
for node in nodes {
3358-
assert_eq!(node.node.get_and_clear_pending_events().len(), 0);
3359-
assert_eq!(node.chan_monitor.added_monitors.lock().unwrap().len(), 0);
3360-
}
33613381
}
33623382

33633383
#[test]
@@ -3542,6 +3562,16 @@ mod tests {
35423562
while !headers.is_empty() {
35433563
nodes[0].node.block_disconnected(&headers.pop().unwrap());
35443564
}
3565+
{
3566+
let events = nodes[0].node.get_and_clear_pending_events();
3567+
assert_eq!(events.len(), 1);
3568+
match events[0] {
3569+
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { contents: msgs::UnsignedChannelUpdate { flags, .. }, .. } } => {
3570+
assert_eq!(flags & 0b10, 0b10);
3571+
},
3572+
_ => panic!("Unexpected event"),
3573+
}
3574+
}
35453575
let channel_state = nodes[0].node.channel_state.lock().unwrap();
35463576
assert_eq!(channel_state.by_id.len(), 0);
35473577
assert_eq!(channel_state.short_to_id.len(), 0);

0 commit comments

Comments
 (0)