+
+ #[cfg(anchors)]
+ #[test]
+ fn test_supports_anchors_zero_htlc_tx_fee() {
+ // Tests that if both sides support and negotiate `anchors_zero_fee_htlc_tx`, it is the
+ // resulting `channel_type`.
+ let secp_ctx = Secp256k1::new();
+ let fee_estimator = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
+ let network = Network::Testnet;
+ let keys_provider = test_utils::TestKeysInterface::new(&[42; 32], network);
+ let logger = test_utils::TestLogger::new();
+
+ let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
+ let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
+
+ let mut config = UserConfig::default();
+ config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
+
+ // It is not enough for just the initiator to signal `option_anchors_zero_fee_htlc_tx`, both
+ // need to signal it.
+ let channel_a = Channel::<EnforcingSigner>::new_outbound(
+ &fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
+ &channelmanager::provided_init_features(&UserConfig::default()), 10000000, 100000, 42,
+ &config, 0, 42
+ ).unwrap();
+ assert!(!channel_a.channel_type.supports_anchors_zero_fee_htlc_tx());
+
+ let mut expected_channel_type = ChannelTypeFeatures::empty();
+ expected_channel_type.set_static_remote_key_required();
+ expected_channel_type.set_anchors_zero_fee_htlc_tx_required();
+
+ let channel_a = Channel::<EnforcingSigner>::new_outbound(
+ &fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
+ &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
+ ).unwrap();
+
+ let open_channel_msg = channel_a.get_open_channel(genesis_block(network).header.block_hash());
+ let channel_b = Channel::<EnforcingSigner>::new_from_req(
+ &fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
+ &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
+ &open_channel_msg, 7, &config, 0, &&logger, 42
+ ).unwrap();
+
+ assert_eq!(channel_a.channel_type, expected_channel_type);
+ assert_eq!(channel_b.channel_type, expected_channel_type);
+ }
+
+ #[cfg(anchors)]
+ #[test]
+ fn test_rejects_implicit_simple_anchors() {
+ // Tests that if `option_anchors` is being negotiated implicitly through the intersection of
+ // each side's `InitFeatures`, it is rejected.
+ let secp_ctx = Secp256k1::new();
+ let fee_estimator = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
+ let network = Network::Testnet;
+ let keys_provider = test_utils::TestKeysInterface::new(&[42; 32], network);
+ let logger = test_utils::TestLogger::new();
+
+ let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
+ let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
+
+ let config = UserConfig::default();
+
+ // See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
+ let static_remote_key_required: u64 = 1 << 12;
+ let simple_anchors_required: u64 = 1 << 20;
+ let raw_init_features = static_remote_key_required | simple_anchors_required;
+ let init_features_with_simple_anchors = InitFeatures::from_le_bytes(raw_init_features.to_le_bytes().to_vec());
+
+ let channel_a = Channel::<EnforcingSigner>::new_outbound(
+ &fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
+ &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
+ ).unwrap();
+
+ // Set `channel_type` to `None` to force the implicit feature negotiation.
+ let mut open_channel_msg = channel_a.get_open_channel(genesis_block(network).header.block_hash());
+ open_channel_msg.channel_type = None;
+
+ // Since A supports both `static_remote_key` and `option_anchors`, but B only accepts
+ // `static_remote_key`, it will fail the channel.
+ let channel_b = Channel::<EnforcingSigner>::new_from_req(
+ &fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
+ &channelmanager::provided_channel_type_features(&config), &init_features_with_simple_anchors,
+ &open_channel_msg, 7, &config, 0, &&logger, 42
+ );
+ assert!(channel_b.is_err());
+ }
+
+ #[cfg(anchors)]
+ #[test]
+ fn test_rejects_simple_anchors_channel_type() {
+ // Tests that if `option_anchors` is being negotiated through the `channel_type` feature,
+ // it is rejected.
+ let secp_ctx = Secp256k1::new();
+ let fee_estimator = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
+ let network = Network::Testnet;
+ let keys_provider = test_utils::TestKeysInterface::new(&[42; 32], network);
+ let logger = test_utils::TestLogger::new();
+
+ let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
+ let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
+
+ let config = UserConfig::default();
+
+ // See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
+ let static_remote_key_required: u64 = 1 << 12;
+ let simple_anchors_required: u64 = 1 << 20;
+ let simple_anchors_raw_features = static_remote_key_required | simple_anchors_required;
+ let simple_anchors_init = InitFeatures::from_le_bytes(simple_anchors_raw_features.to_le_bytes().to_vec());
+ let simple_anchors_channel_type = ChannelTypeFeatures::from_le_bytes(simple_anchors_raw_features.to_le_bytes().to_vec());
+ assert!(simple_anchors_init.requires_unknown_bits());
+ assert!(simple_anchors_channel_type.requires_unknown_bits());
+
+ // First, we'll try to open a channel between A and B where A requests a channel type for
+ // the original `option_anchors` feature (non zero fee htlc tx). This should be rejected by
+ // B as it's not supported by LDK.
+ let channel_a = Channel::<EnforcingSigner>::new_outbound(
+ &fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
+ &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42
+ ).unwrap();
+
+ let mut open_channel_msg = channel_a.get_open_channel(genesis_block(network).header.block_hash());
+ open_channel_msg.channel_type = Some(simple_anchors_channel_type.clone());
+
+ let res = Channel::<EnforcingSigner>::new_from_req(
+ &fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
+ &channelmanager::provided_channel_type_features(&config), &simple_anchors_init,
+ &open_channel_msg, 7, &config, 0, &&logger, 42
+ );
+ assert!(res.is_err());
+
+ // Then, we'll try to open another channel where A requests a channel type for
+ // `anchors_zero_fee_htlc_tx`. B is malicious and tries to downgrade the channel type to the
+ // original `option_anchors` feature, which should be rejected by A as it's not supported by
+ // LDK.
+ let mut channel_a = Channel::<EnforcingSigner>::new_outbound(
+ &fee_estimator, &&keys_provider, &&keys_provider, node_id_b, &simple_anchors_init,
+ 10000000, 100000, 42, &config, 0, 42
+ ).unwrap();
+
+ let open_channel_msg = channel_a.get_open_channel(genesis_block(network).header.block_hash());
+
+ let channel_b = Channel::<EnforcingSigner>::new_from_req(
+ &fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
+ &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
+ &open_channel_msg, 7, &config, 0, &&logger, 42
+ ).unwrap();
+
+ let mut accept_channel_msg = channel_b.get_accept_channel_message();
+ accept_channel_msg.channel_type = Some(simple_anchors_channel_type.clone());
+
+ let res = channel_a.accept_channel(
+ &accept_channel_msg, &config.channel_handshake_limits, &simple_anchors_init
+ );
+ assert!(res.is_err());
+ }