+ // Check that, during channel creation, we use the same feerate in the open channel message
+ // as we do in the Channel object creation itself.
+ #[test]
+ fn test_open_channel_msg_fee() {
+ let original_fee = 253;
+ let mut fee_est = TestFeeEstimator{fee_est: original_fee };
+ let secp_ctx = Secp256k1::new();
+ let seed = [42; 32];
+ let network = Network::Testnet;
+ let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
+
+ let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
+ let config = UserConfig::default();
+ let node_a_chan = Channel::<EnforcingSigner>::new_outbound(&&fee_est, &&keys_provider, node_a_node_id, 10000000, 100000, 42, &config).unwrap();
+
+ // Now change the fee so we can check that the fee in the open_channel message is the
+ // same as the old fee.
+ fee_est.fee_est = 500;
+ let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
+ assert_eq!(open_channel_msg.feerate_per_kw, original_fee);
+ }
+
+ #[test]
+ fn test_holder_vs_counterparty_dust_limit() {
+ // Test that when calculating the local and remote commitment transaction fees, the correct
+ // dust limits are used.
+ let feeest = TestFeeEstimator{fee_est: 15000};
+ let secp_ctx = Secp256k1::new();
+ let seed = [42; 32];
+ let network = Network::Testnet;
+ let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
+
+ // Go through the flow of opening a channel between two nodes, making sure
+ // they have different dust limits.
+
+ // Create Node A's channel pointing to Node B's pubkey
+ let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
+ let config = UserConfig::default();
+ let mut node_a_chan = Channel::<EnforcingSigner>::new_outbound(&&feeest, &&keys_provider, node_b_node_id, 10000000, 100000, 42, &config).unwrap();
+
+ // Create Node B's channel by receiving Node A's open_channel message
+ // Make sure A's dust limit is as we expect.
+ let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
+ let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
+ let node_b_chan = Channel::<EnforcingSigner>::new_from_req(&&feeest, &&keys_provider, node_b_node_id, InitFeatures::known(), &open_channel_msg, 7, &config).unwrap();
+
+ // Node B --> Node A: accept channel, explicitly setting B's dust limit.
+ let mut accept_channel_msg = node_b_chan.get_accept_channel();
+ accept_channel_msg.dust_limit_satoshis = 546;
+ node_a_chan.accept_channel(&accept_channel_msg, &config, InitFeatures::known()).unwrap();
+ node_a_chan.holder_dust_limit_satoshis = 1560;
+
+ // Put some inbound and outbound HTLCs in A's channel.
+ let htlc_amount_msat = 11_092_000; // put an amount below A's effective dust limit but above B's.
+ node_a_chan.pending_inbound_htlcs.push(InboundHTLCOutput {
+ htlc_id: 0,
+ amount_msat: htlc_amount_msat,
+ payment_hash: PaymentHash(Sha256::hash(&[42; 32]).into_inner()),
+ cltv_expiry: 300000000,
+ state: InboundHTLCState::Committed,
+ });
+
+ node_a_chan.pending_outbound_htlcs.push(OutboundHTLCOutput {
+ htlc_id: 1,
+ amount_msat: htlc_amount_msat, // put an amount below A's dust amount but above B's.
+ payment_hash: PaymentHash(Sha256::hash(&[43; 32]).into_inner()),
+ cltv_expiry: 200000000,
+ state: OutboundHTLCState::Committed,
+ source: HTLCSource::OutboundRoute {
+ path: Vec::new(),
+ session_priv: SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
+ first_hop_htlc_msat: 548,
+ }
+ });
+
+ // Make sure when Node A calculates their local commitment transaction, none of the HTLCs pass
+ // the dust limit check.
+ let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
+ let local_commit_tx_fee = node_a_chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
+ let local_commit_fee_0_htlcs = node_a_chan.commit_tx_fee_msat(0);
+ assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
+
+ // Finally, make sure that when Node A calculates the remote's commitment transaction fees, all
+ // of the HTLCs are seen to be above the dust limit.
+ node_a_chan.channel_transaction_parameters.is_outbound_from_holder = false;
+ let remote_commit_fee_3_htlcs = node_a_chan.commit_tx_fee_msat(3);
+ let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
+ let remote_commit_tx_fee = node_a_chan.next_remote_commit_tx_fee_msat(htlc_candidate, None);
+ assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
+ }
+
+ #[test]
+ fn test_timeout_vs_success_htlc_dust_limit() {
+ // Make sure that when `next_remote_commit_tx_fee_msat` and `next_local_commit_tx_fee_msat`
+ // calculate the real dust limits for HTLCs (i.e. the dust limit given by the counterparty
+ // *plus* the fees paid for the HTLC) they don't swap `HTLC_SUCCESS_TX_WEIGHT` for
+ // `HTLC_TIMEOUT_TX_WEIGHT`, and vice versa.
+ let fee_est = TestFeeEstimator{fee_est: 253 };
+ let secp_ctx = Secp256k1::new();
+ let seed = [42; 32];
+ let network = Network::Testnet;
+ let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
+
+ let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
+ let config = UserConfig::default();
+ let mut chan = Channel::<EnforcingSigner>::new_outbound(&&fee_est, &&keys_provider, node_id, 10000000, 100000, 42, &config).unwrap();
+
+ let commitment_tx_fee_0_htlcs = chan.commit_tx_fee_msat(0);
+ let commitment_tx_fee_1_htlc = chan.commit_tx_fee_msat(1);
+
+ // If HTLC_SUCCESS_TX_WEIGHT and HTLC_TIMEOUT_TX_WEIGHT were swapped: then this HTLC would be
+ // counted as dust when it shouldn't be.
+ let htlc_amt_above_timeout = ((253 * HTLC_TIMEOUT_TX_WEIGHT / 1000) + chan.holder_dust_limit_satoshis + 1) * 1000;
+ let htlc_candidate = HTLCCandidate::new(htlc_amt_above_timeout, HTLCInitiator::LocalOffered);
+ let commitment_tx_fee = chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
+ assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
+
+ // If swapped: this HTLC would be counted as non-dust when it shouldn't be.
+ let dust_htlc_amt_below_success = ((253 * HTLC_SUCCESS_TX_WEIGHT / 1000) + chan.holder_dust_limit_satoshis - 1) * 1000;
+ let htlc_candidate = HTLCCandidate::new(dust_htlc_amt_below_success, HTLCInitiator::RemoteOffered);
+ let commitment_tx_fee = chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
+ assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
+
+ chan.channel_transaction_parameters.is_outbound_from_holder = false;
+
+ // If swapped: this HTLC would be counted as non-dust when it shouldn't be.
+ let dust_htlc_amt_above_timeout = ((253 * HTLC_TIMEOUT_TX_WEIGHT / 1000) + chan.counterparty_dust_limit_satoshis + 1) * 1000;
+ let htlc_candidate = HTLCCandidate::new(dust_htlc_amt_above_timeout, HTLCInitiator::LocalOffered);
+ let commitment_tx_fee = chan.next_remote_commit_tx_fee_msat(htlc_candidate, None);
+ assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
+
+ // If swapped: this HTLC would be counted as dust when it shouldn't be.
+ let htlc_amt_below_success = ((253 * HTLC_SUCCESS_TX_WEIGHT / 1000) + chan.counterparty_dust_limit_satoshis - 1) * 1000;
+ let htlc_candidate = HTLCCandidate::new(htlc_amt_below_success, HTLCInitiator::RemoteOffered);
+ let commitment_tx_fee = chan.next_remote_commit_tx_fee_msat(htlc_candidate, None);
+ assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
+ }
+