Merge pull request #355 from ariard/2019-07-fix-csv-delay-check-remote-htlc
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Wed, 24 Jul 2019 22:33:20 +0000 (22:33 +0000)
committerGitHub <noreply@github.com>
Wed, 24 Jul 2019 22:33:20 +0000 (22:33 +0000)
Fix bug in check_spend_remote_htlc and let csv delays being user configurable

1  2 
src/ln/functional_tests.rs

index 918ba3ca9352e908cfc9af9d73d1453b01a3a2c4,8c0283828641e6ead41cd887a78415c313ade9ca..3e5a21b0ee79f0b86042e48970d12e3f618a7b69
@@@ -4,11 -4,11 +4,11 @@@
  
  use chain::transaction::OutPoint;
  use chain::chaininterface::{ChainListener, ChainWatchInterface};
- use chain::keysinterface::{KeysInterface, SpendableOutputDescriptor};
- use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC, BREAKDOWN_TIMEOUT};
- use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash};
+ use chain::keysinterface::{KeysInterface, SpendableOutputDescriptor, KeysManager};
+ use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
+ use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT};
  use ln::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ManyChannelMonitor, ANTI_REORG_DELAY};
- use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT};
+ use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT, Channel, ChannelError};
  use ln::onion_utils;
  use ln::router::{Route, RouteHop};
  use ln::msgs;
@@@ -1727,7 -1727,15 +1727,15 @@@ fn channel_monitor_network_test() 
  fn test_justice_tx() {
        // Test justice txn built on revoked HTLC-Success tx, against both sides
  
-       let nodes = create_network(2, &[None, None]);
+       let mut alice_config = UserConfig::new();
+       alice_config.channel_options.announced_channel = true;
+       alice_config.peer_channel_config_limits.force_announced_channel_preference = false;
+       alice_config.own_channel_config.our_to_self_delay = 6 * 24 * 5;
+       let mut bob_config = UserConfig::new();
+       bob_config.channel_options.announced_channel = true;
+       bob_config.peer_channel_config_limits.force_announced_channel_preference = false;
+       bob_config.own_channel_config.our_to_self_delay = 6 * 24 * 3;
+       let nodes = create_network(2, &[Some(alice_config), Some(bob_config)]);
        // Create some new channels:
        let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
  
@@@ -5664,8 -5672,8 +5672,8 @@@ fn do_test_sweep_outbound_htlc_failure_
  
        let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
  
 -      let (payment_preimage_1, dust_hash) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
 -      let (payment_preimage_2, non_dust_hash) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
 +      let (_payment_preimage_1, dust_hash) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
 +      let (_payment_preimage_2, non_dust_hash) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
  
        let as_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
        let bs_commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
@@@ -5879,3 -5887,61 +5887,61 @@@ fn test_upfront_shutdown_script() 
                _ => panic!("Unexpected event"),
        }
  }
+ #[test]
+ fn test_user_configurable_csv_delay() {
+       // We test our channel constructors yield errors when we pass them absurd csv delay
+       let mut low_our_to_self_config = UserConfig::new();
+       low_our_to_self_config.own_channel_config.our_to_self_delay = 6;
+       let mut high_their_to_self_config = UserConfig::new();
+       high_their_to_self_config.peer_channel_config_limits.their_to_self_delay = 100;
+       let nodes = create_network(2, &[Some(high_their_to_self_config.clone()), None]);
+       // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_outbound()
+       let keys_manager: Arc<KeysInterface> = Arc::new(KeysManager::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new()), 10, 20));
+       if let Err(error) = Channel::new_outbound(&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), 1000000, 1000000, 0, Arc::new(test_utils::TestLogger::new()), &low_our_to_self_config) {
+               match error {
+                       APIError::APIMisuseError { err } => { assert_eq!(err, "Configured with an unreasonable our_to_self_delay putting user funds at risks"); },
+                       _ => panic!("Unexpected event"),
+               }
+       } else { assert!(false) }
+       // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_from_req()
+       nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42).unwrap();
+       let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
+       open_channel.to_self_delay = 200;
+       if let Err(error) = Channel::new_from_req(&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), LocalFeatures::new(), &open_channel, 0, Arc::new(test_utils::TestLogger::new()), &low_our_to_self_config) {
+               match error {
+                       ChannelError::Close(err) => { assert_eq!(err, "Configured with an unreasonable our_to_self_delay putting user funds at risks"); },
+                       _ => panic!("Unexpected event"),
+               }
+       } else { assert!(false); }
+       // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Chanel::accept_channel()
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1000000, 1000000, 42).unwrap();
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id())).unwrap();
+       let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
+       accept_channel.to_self_delay = 200;
+       if let Err(error) = nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), LocalFeatures::new(), &accept_channel) {
+               if let Some(error) = error.action {
+                       match error {
+                               ErrorAction::SendErrorMessage { msg } => {
+                                       assert_eq!(msg.data,"They wanted our payments to be delayed by a needlessly long period");
+                               },
+                               _ => { assert!(false); }
+                       }
+               } else { assert!(false); }
+       } else { assert!(false); }
+       // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Channel::new_from_req()
+       nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42).unwrap();
+       let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
+       open_channel.to_self_delay = 200;
+       if let Err(error) = Channel::new_from_req(&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), LocalFeatures::new(), &open_channel, 0, Arc::new(test_utils::TestLogger::new()), &high_their_to_self_config) {
+               match error {
+                       ChannelError::Close(err) => { assert_eq!(err, "They wanted our payments to be delayed by a needlessly long period"); },
+                       _ => panic!("Unexpected event"),
+               }
+       } else { assert!(false); }
+ }