+ // ...now when the messages get delivered everyone should be happy
+ nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+ let (as_revoke_msg, as_commitment_signed) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap(); // (2)
+ assert!(as_commitment_signed.is_none()); // nodes[0] is awaiting nodes[1] revoke_and_ack
+ check_added_monitors!(nodes[0], 1);
+
+ // deliver(1), generate (3):
+ let (bs_revoke_msg, bs_commitment_signed) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
+ assert!(bs_commitment_signed.is_none()); // nodes[1] is awaiting nodes[0] revoke_and_ack
+ check_added_monitors!(nodes[1], 1);
+
+ let bs_update = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg).unwrap(); // deliver (2)
+ assert!(bs_update.as_ref().unwrap().update_add_htlcs.is_empty()); // (4)
+ assert!(bs_update.as_ref().unwrap().update_fulfill_htlcs.is_empty()); // (4)
+ assert!(bs_update.as_ref().unwrap().update_fail_htlcs.is_empty()); // (4)
+ assert!(bs_update.as_ref().unwrap().update_fail_malformed_htlcs.is_empty()); // (4)
+ assert!(bs_update.as_ref().unwrap().update_fee.is_none()); // (4)
+ check_added_monitors!(nodes[1], 1);
+
+ let as_update = nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_msg).unwrap(); // deliver (3)
+ assert!(as_update.as_ref().unwrap().update_add_htlcs.is_empty()); // (5)
+ assert!(as_update.as_ref().unwrap().update_fulfill_htlcs.is_empty()); // (5)
+ assert!(as_update.as_ref().unwrap().update_fail_htlcs.is_empty()); // (5)
+ assert!(as_update.as_ref().unwrap().update_fail_malformed_htlcs.is_empty()); // (5)
+ assert!(as_update.as_ref().unwrap().update_fee.is_none()); // (5)
+ check_added_monitors!(nodes[0], 1);
+
+ let (as_second_revoke, as_second_commitment_signed) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.unwrap().commitment_signed).unwrap(); // deliver (4)
+ assert!(as_second_commitment_signed.is_none()); // only (6)
+ check_added_monitors!(nodes[0], 1);
+
+ let (bs_second_revoke, bs_second_commitment_signed) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update.unwrap().commitment_signed).unwrap(); // deliver (5)
+ assert!(bs_second_commitment_signed.is_none());
+ check_added_monitors!(nodes[1], 1);
+
+ assert!(nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke).unwrap().is_none());
+ check_added_monitors!(nodes[0], 1);
+
+ let events_2 = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events_2.len(), 1);
+ match events_2[0] {
+ Event::PendingHTLCsForwardable {..} => {}, // If we actually processed we'd receive the payment
+ _ => panic!("Unexpected event"),
+ }
+
+ assert!(nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke).unwrap().is_none()); // deliver (6)
+ check_added_monitors!(nodes[1], 1);
+ }
+
+ #[test]
+ fn test_update_fee_unordered_raa() {
+ // Just the intro to the previous test followed by an out-of-order RAA (which caused a
+ // crash in an earlier version of the update_fee patch)
+ let mut nodes = create_network(2);
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
+ let channel_id = chan.2;
+
+ macro_rules! get_feerate {
+ ($node: expr) => {{
+ let chan_lock = $node.node.channel_state.lock().unwrap();
+ let chan = chan_lock.by_id.get(&channel_id).unwrap();
+ chan.get_feerate()
+ }}
+ }
+
+ // balancing
+ send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+
+ // First nodes[0] generates an update_fee
+ nodes[0].node.update_fee(channel_id, get_feerate!(nodes[0]) + 20).unwrap();
+ check_added_monitors!(nodes[0], 1);
+
+ let events_0 = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events_0.len(), 1);
+ let update_msg = match events_0[0] { // (1)
+ Event::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
+ update_fee.as_ref()
+ },
+ _ => panic!("Unexpected event"),
+ };
+
+ nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+
+ // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
+ let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+ nodes[1].node.send_payment(nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap(), our_payment_hash).unwrap();
+ check_added_monitors!(nodes[1], 1);
+
+ let payment_event = {
+ let mut events_1 = nodes[1].node.get_and_clear_pending_events();
+ assert_eq!(events_1.len(), 1);
+ SendEvent::from_event(events_1.remove(0))
+ };
+ assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
+ assert_eq!(payment_event.msgs.len(), 1);
+
+ // ...now when the messages get delivered everyone should be happy
+ nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
+ let (as_revoke_msg, as_commitment_signed) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap(); // (2)
+ assert!(as_commitment_signed.is_none()); // nodes[0] is awaiting nodes[1] revoke_and_ack
+ check_added_monitors!(nodes[0], 1);
+
+ assert!(nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg).unwrap().is_none()); // deliver (2)
+ check_added_monitors!(nodes[1], 1);
+
+ // We can't continue, sadly, because our (1) now has a bogus signature
+ }
+
+ #[test]
+ fn test_multi_flight_update_fee() {
+ let nodes = create_network(2);
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
+ let channel_id = chan.2;
+
+ macro_rules! get_feerate {
+ ($node: expr) => {{
+ let chan_lock = $node.node.channel_state.lock().unwrap();
+ let chan = chan_lock.by_id.get(&channel_id).unwrap();
+ chan.get_feerate()
+ }}
+ }
+
+ // A B
+ // update_fee/commitment_signed ->
+ // .- send (1) RAA and (2) commitment_signed
+ // update_fee (never committed) ->
+ // (3) update_fee ->
+ // We have to manually generate the above update_fee, it is allowed by the protocol but we
+ // don't track which updates correspond to which revoke_and_ack responses so we're in
+ // AwaitingRAA mode and will not generate the update_fee yet.
+ // <- (1) RAA delivered
+ // (3) is generated and send (4) CS -.
+ // Note that A cannot generate (4) prior to (1) being delivered as it otherwise doesn't
+ // know the per_commitment_point to use for it.
+ // <- (2) commitment_signed delivered
+ // revoke_and_ack ->
+ // B should send no response here
+ // (4) commitment_signed delivered ->
+ // <- RAA/commitment_signed delivered
+ // revoke_and_ack ->
+
+ // First nodes[0] generates an update_fee
+ let initial_feerate = get_feerate!(nodes[0]);
+ nodes[0].node.update_fee(channel_id, initial_feerate + 20).unwrap();
+ check_added_monitors!(nodes[0], 1);
+
+ let events_0 = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events_0.len(), 1);
+ let (update_msg_1, commitment_signed_1) = match events_0[0] { // (1)
+ Event::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
+ (update_fee.as_ref().unwrap(), commitment_signed)
+ },
+ _ => panic!("Unexpected event"),
+ };
+
+ // Deliver first update_fee/commitment_signed pair, generating (1) and (2):
+ nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg_1).unwrap();
+ let (bs_revoke_msg, bs_commitment_signed) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed_1).unwrap();
+ check_added_monitors!(nodes[1], 1);
+
+ // nodes[0] is awaiting a revoke from nodes[1] before it will create a new commitment
+ // transaction:
+ nodes[0].node.update_fee(channel_id, initial_feerate + 40).unwrap();
+ assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+
+ // Create the (3) update_fee message that nodes[0] will generate before it does...
+ let mut update_msg_2 = msgs::UpdateFee {
+ channel_id: update_msg_1.channel_id.clone(),
+ feerate_per_kw: (initial_feerate + 30) as u32,
+ };
+
+ nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2).unwrap();
+
+ update_msg_2.feerate_per_kw = (initial_feerate + 40) as u32;
+ // Deliver (3)
+ nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2).unwrap();
+
+ // Deliver (1), generating (3) and (4)
+ let as_second_update = nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_msg).unwrap();
+ check_added_monitors!(nodes[0], 1);
+ assert!(as_second_update.as_ref().unwrap().update_add_htlcs.is_empty());
+ assert!(as_second_update.as_ref().unwrap().update_fulfill_htlcs.is_empty());
+ assert!(as_second_update.as_ref().unwrap().update_fail_htlcs.is_empty());
+ assert!(as_second_update.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
+ // Check that the update_fee newly generated matches what we delivered:
+ assert_eq!(as_second_update.as_ref().unwrap().update_fee.as_ref().unwrap().channel_id, update_msg_2.channel_id);
+ assert_eq!(as_second_update.as_ref().unwrap().update_fee.as_ref().unwrap().feerate_per_kw, update_msg_2.feerate_per_kw);
+
+ // Deliver (2) commitment_signed
+ let (as_revoke_msg, as_commitment_signed) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), bs_commitment_signed.as_ref().unwrap()).unwrap();
+ check_added_monitors!(nodes[0], 1);
+ assert!(as_commitment_signed.is_none());
+
+ assert!(nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg).unwrap().is_none());
+ check_added_monitors!(nodes[1], 1);
+
+ // Delever (4)
+ let (bs_second_revoke, bs_second_commitment) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_update.unwrap().commitment_signed).unwrap();
+ check_added_monitors!(nodes[1], 1);
+
+ assert!(nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke).unwrap().is_none());
+ check_added_monitors!(nodes[0], 1);
+
+ let (as_second_revoke, as_second_commitment) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment.unwrap()).unwrap();
+ assert!(as_second_commitment.is_none());
+ check_added_monitors!(nodes[0], 1);
+
+ assert!(nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke).unwrap().is_none());
+ check_added_monitors!(nodes[1], 1);
+ }
+
+ #[test]
+ fn test_update_fee_vanilla() {
+ let nodes = create_network(2);
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
+ let channel_id = chan.2;
+
+ macro_rules! get_feerate {
+ ($node: expr) => {{
+ let chan_lock = $node.node.channel_state.lock().unwrap();
+ let chan = chan_lock.by_id.get(&channel_id).unwrap();
+ chan.get_feerate()
+ }}
+ }
+
+ let feerate = get_feerate!(nodes[0]);
+ nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
+
+ let events_0 = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events_0.len(), 1);
+ let (update_msg, commitment_signed) = match events_0[0] {
+ Event::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
+ (update_fee.as_ref(), commitment_signed)
+ },
+ _ => panic!("Unexpected event"),
+ };
+ nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+
+ let (revoke_msg, commitment_signed) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
+ let commitment_signed = commitment_signed.unwrap();
+ check_added_monitors!(nodes[0], 1);
+ check_added_monitors!(nodes[1], 1);
+
+ let resp_option = nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
+ assert!(resp_option.is_none());
+ check_added_monitors!(nodes[0], 1);
+
+ let (revoke_msg, commitment_signed) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
+ assert!(commitment_signed.is_none());
+ check_added_monitors!(nodes[0], 1);
+
+ let resp_option = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
+ assert!(resp_option.is_none());
+ check_added_monitors!(nodes[1], 1);
+ }
+
+ #[test]
+ fn test_update_fee_with_fundee_update_add_htlc() {
+ let mut nodes = create_network(2);
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
+ let channel_id = chan.2;
+
+ macro_rules! get_feerate {
+ ($node: expr) => {{
+ let chan_lock = $node.node.channel_state.lock().unwrap();
+ let chan = chan_lock.by_id.get(&channel_id).unwrap();
+ chan.get_feerate()
+ }}
+ }
+
+ // balancing
+ send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+
+ let feerate = get_feerate!(nodes[0]);
+ nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
+
+ let events_0 = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events_0.len(), 1);
+ let (update_msg, commitment_signed) = match events_0[0] {
+ Event::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
+ (update_fee.as_ref(), commitment_signed)
+ },
+ _ => panic!("Unexpected event"),
+ };
+ nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+ check_added_monitors!(nodes[0], 1);
+ let (revoke_msg, commitment_signed) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
+ let commitment_signed = commitment_signed.unwrap();
+ check_added_monitors!(nodes[1], 1);
+
+ let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 800000, TEST_FINAL_CLTV).unwrap();
+
+ let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[1]);
+
+ // nothing happens since node[1] is in AwaitingRemoteRevoke
+ nodes[1].node.send_payment(route, our_payment_hash).unwrap();
+ {
+ let mut added_monitors = nodes[0].chan_monitor.added_monitors.lock().unwrap();
+ assert_eq!(added_monitors.len(), 0);
+ added_monitors.clear();
+ }
+ let events = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events.len(), 0);
+ // node[1] has nothing to do
+
+ let resp_option = nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
+ assert!(resp_option.is_none());
+ check_added_monitors!(nodes[0], 1);
+
+ let (revoke_msg, commitment_signed) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
+ assert!(commitment_signed.is_none());
+ check_added_monitors!(nodes[0], 1);
+ let resp_option = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
+ // AwaitingRemoteRevoke ends here
+
+ let commitment_update = resp_option.unwrap();
+ assert_eq!(commitment_update.update_add_htlcs.len(), 1);
+ assert_eq!(commitment_update.update_fulfill_htlcs.len(), 0);
+ assert_eq!(commitment_update.update_fail_htlcs.len(), 0);
+ assert_eq!(commitment_update.update_fail_malformed_htlcs.len(), 0);
+ assert_eq!(commitment_update.update_fee.is_none(), true);
+
+ nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &commitment_update.update_add_htlcs[0]).unwrap();
+ let (revoke, commitment_signed) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
+ check_added_monitors!(nodes[0], 1);
+ check_added_monitors!(nodes[1], 1);
+ let commitment_signed = commitment_signed.unwrap();
+ let resp_option = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke).unwrap();
+ check_added_monitors!(nodes[1], 1);
+ assert!(resp_option.is_none());
+
+ let (revoke, commitment_signed) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed).unwrap();
+ check_added_monitors!(nodes[1], 1);
+ assert!(commitment_signed.is_none());
+ let resp_option = nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke).unwrap();
+ check_added_monitors!(nodes[0], 1);
+ assert!(resp_option.is_none());
+
+ let events = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events.len(), 1);
+ match events[0] {
+ Event::PendingHTLCsForwardable { .. } => { },
+ _ => panic!("Unexpected event"),
+ };
+ nodes[0].node.channel_state.lock().unwrap().next_forward = Instant::now();
+ nodes[0].node.process_pending_htlc_forwards();
+
+ let events = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events.len(), 1);
+ match events[0] {
+ Event::PaymentReceived { .. } => { },
+ _ => panic!("Unexpected event"),
+ };
+
+ claim_payment(&nodes[1], &vec!(&nodes[0])[..], our_payment_preimage);
+
+ send_payment(&nodes[1], &vec!(&nodes[0])[..], 800000);
+ send_payment(&nodes[0], &vec!(&nodes[1])[..], 800000);
+ close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
+ }
+
+ #[test]
+ fn test_update_fee() {
+ let nodes = create_network(2);
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
+ let channel_id = chan.2;
+
+ macro_rules! get_feerate {
+ ($node: expr) => {{
+ let chan_lock = $node.node.channel_state.lock().unwrap();
+ let chan = chan_lock.by_id.get(&channel_id).unwrap();
+ chan.get_feerate()
+ }}
+ }
+
+ // A B
+ // (1) update_fee/commitment_signed ->
+ // <- (2) revoke_and_ack
+ // .- send (3) commitment_signed
+ // (4) update_fee/commitment_signed ->
+ // .- send (5) revoke_and_ack (no CS as we're awaiting a revoke)
+ // <- (3) commitment_signed delivered
+ // send (6) revoke_and_ack -.
+ // <- (5) deliver revoke_and_ack
+ // (6) deliver revoke_and_ack ->
+ // .- send (7) commitment_signed in response to (4)
+ // <- (7) deliver commitment_signed
+ // revoke_and_ack ->
+
+ // Create and deliver (1)...
+ let feerate = get_feerate!(nodes[0]);
+ nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
+
+ let events_0 = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events_0.len(), 1);
+ let (update_msg, commitment_signed) = match events_0[0] {
+ Event::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
+ (update_fee.as_ref(), commitment_signed)
+ },
+ _ => panic!("Unexpected event"),
+ };
+ nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+
+ // Generate (2) and (3):
+ let (revoke_msg, commitment_signed) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
+ let commitment_signed_0 = commitment_signed.unwrap();
+ check_added_monitors!(nodes[0], 1);
+ check_added_monitors!(nodes[1], 1);
+
+ // Deliver (2):
+ let resp_option = nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
+ assert!(resp_option.is_none());
+ check_added_monitors!(nodes[0], 1);
+
+ // Create and deliver (4)...
+ nodes[0].node.update_fee(channel_id, feerate+30).unwrap();
+ let events_0 = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events_0.len(), 1);
+ let (update_msg, commitment_signed) = match events_0[0] {
+ Event::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
+ (update_fee.as_ref(), commitment_signed)
+ },
+ _ => panic!("Unexpected event"),
+ };
+ nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
+
+ let (revoke_msg, commitment_signed) = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
+ // ... creating (5)
+ assert!(commitment_signed.is_none());
+ check_added_monitors!(nodes[0], 1);
+ check_added_monitors!(nodes[1], 1);
+
+ // Handle (3), creating (6):
+ let (revoke_msg_0, commitment_signed) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed_0).unwrap();
+ assert!(commitment_signed.is_none());
+ check_added_monitors!(nodes[0], 1);
+
+ // Deliver (5):
+ let resp_option = nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
+ assert!(resp_option.is_none());
+ check_added_monitors!(nodes[0], 1);
+
+ // Deliver (6), creating (7):
+ let resp_option = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg_0).unwrap();
+ let commitment_signed = resp_option.unwrap().commitment_signed;
+ check_added_monitors!(nodes[1], 1);
+
+ // Deliver (7)
+ let (revoke_msg, commitment_signed) = nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
+ assert!(commitment_signed.is_none());
+ check_added_monitors!(nodes[0], 1);
+ let resp_option = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
+ assert!(resp_option.is_none());
+ check_added_monitors!(nodes[1], 1);
+
+ assert_eq!(get_feerate!(nodes[0]), feerate + 30);
+ assert_eq!(get_feerate!(nodes[1]), feerate + 30);
+ close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
+ }
+
+ #[test]
+ fn fake_network_test() {
+ // Simple test which builds a network of ChannelManagers, connects them to each other, and
+ // tests that payments get routed and transactions broadcast in semi-reasonable ways.
+ let nodes = create_network(4);
+
+ // Create some initial channels
+ let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
+ let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
+ let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
+
+ // Rebalance the network a bit by relaying one payment through all the channels...
+ send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
+ send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
+ send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
+ send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
+
+ // Send some more payments
+ send_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 1000000);
+ send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1], &nodes[0])[..], 1000000);
+ send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1])[..], 1000000);
+
+ // Test failure packets
+ let payment_hash_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 1000000).1;
+ fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], payment_hash_1);
+
+ // Add a new channel that skips 3
+ let chan_4 = create_announced_chan_between_nodes(&nodes, 1, 3);
+
+ send_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 1000000);
+ send_payment(&nodes[2], &vec!(&nodes[3])[..], 1000000);