Skip to content

Commit f681015

Browse files
committed
ln/refactor: move channel type tests into own test file
1 parent 7c04596 commit f681015

File tree

3 files changed

+245
-228
lines changed

3 files changed

+245
-228
lines changed

lightning/src/ln/channel.rs

Lines changed: 4 additions & 228 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
20562056
holding_cell_update_fee: Option<u32>,
20572057
next_holder_htlc_id: u64,
20582058
pub(super) next_counterparty_htlc_id: u64,
2059-
feerate_per_kw: u32,
2059+
pub(super) feerate_per_kw: u32,
20602060

20612061
/// The timestamp set on our latest `channel_update` message for this channel. It is updated
20622062
/// when the channel is updated in ways which may impact the `channel_update` message or when a
@@ -11844,10 +11844,11 @@ mod tests {
1184411844
use crate::types::payment::{PaymentHash, PaymentPreimage};
1184511845
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
1184611846
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
11847-
use crate::ln::channel::InitFeatures;
1184811847
use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
1184911848
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
11850-
use crate::types::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
11849+
use crate::types::features::{ChannelFeatures, NodeFeatures};
11850+
#[cfg(ldk_test_vectors)]
11851+
use crate::types::features::ChannelTypeFeatures;
1185111852
use crate::ln::msgs;
1185211853
use crate::ln::msgs::{ChannelUpdate, UnsignedChannelUpdate, MAX_VALUE_MSAT};
1185311854
use crate::ln::script::ShutdownScript;
@@ -13299,231 +13300,6 @@ mod tests {
1329913300
SecretKey::from_slice(&<Vec<u8>>::from_hex("d09ffff62ddb2297ab000cc85bcb4283fdeb6aa052affbc9dddcf33b61078110").unwrap()[..]).unwrap());
1330013301
}
1330113302

13302-
#[test]
13303-
fn test_zero_conf_channel_type_support() {
13304-
let test_est = TestFeeEstimator::new(15000);
13305-
let feeest = LowerBoundedFeeEstimator::new(&test_est);
13306-
let secp_ctx = Secp256k1::new();
13307-
let seed = [42; 32];
13308-
let network = Network::Testnet;
13309-
let keys_provider = TestKeysInterface::new(&seed, network);
13310-
let logger = TestLogger::new();
13311-
13312-
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
13313-
let config = UserConfig::default();
13314-
let mut node_a_chan = OutboundV1Channel::<&TestKeysInterface>::new(&feeest, &&keys_provider, &&keys_provider,
13315-
node_b_node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42, None, &logger).unwrap();
13316-
13317-
let mut channel_type_features = ChannelTypeFeatures::only_static_remote_key();
13318-
channel_type_features.set_zero_conf_required();
13319-
13320-
let mut open_channel_msg = node_a_chan.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
13321-
open_channel_msg.common_fields.channel_type = Some(channel_type_features);
13322-
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
13323-
let res = InboundV1Channel::<&TestKeysInterface>::new(&feeest, &&keys_provider, &&keys_provider,
13324-
node_b_node_id, &channelmanager::provided_channel_type_features(&config),
13325-
&channelmanager::provided_init_features(&config), &open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false);
13326-
assert!(res.is_ok());
13327-
}
13328-
13329-
#[test]
13330-
fn test_supports_anchors_zero_htlc_tx_fee() {
13331-
// Tests that if both sides support and negotiate `anchors_zero_fee_htlc_tx`, it is the
13332-
// resulting `channel_type`.
13333-
let mut config = UserConfig::default();
13334-
config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
13335-
13336-
let mut expected_channel_type = ChannelTypeFeatures::empty();
13337-
expected_channel_type.set_static_remote_key_required();
13338-
expected_channel_type.set_anchors_zero_fee_htlc_tx_required();
13339-
13340-
do_test_supports_channel_type(config, expected_channel_type)
13341-
}
13342-
13343-
#[test]
13344-
fn test_supports_zero_fee_commitments() {
13345-
// Tests that if both sides support and negotiate `anchors_zero_fee_commitments`, it is
13346-
// the resulting `channel_type`.
13347-
let mut config = UserConfig::default();
13348-
config.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
13349-
13350-
let mut expected_channel_type = ChannelTypeFeatures::empty();
13351-
expected_channel_type.set_anchor_zero_fee_commitments_required();
13352-
13353-
do_test_supports_channel_type(config, expected_channel_type)
13354-
}
13355-
13356-
#[test]
13357-
fn test_supports_zero_fee_commitments_and_htlc_tx_fee() {
13358-
// Tests that if both sides support and negotiate `anchors_zero_fee_commitments` and
13359-
// `anchors_zero_fee_htlc_tx`, the resulting `channel_type` is
13360-
// `anchors_zero_fee_commitments`.
13361-
let mut config = UserConfig::default();
13362-
config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
13363-
config.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
13364-
13365-
let mut expected_channel_type = ChannelTypeFeatures::empty();
13366-
expected_channel_type.set_anchor_zero_fee_commitments_required();
13367-
13368-
do_test_supports_channel_type(config, expected_channel_type)
13369-
}
13370-
13371-
fn do_test_supports_channel_type(config: UserConfig, expected_channel_type: ChannelTypeFeatures) {
13372-
let secp_ctx = Secp256k1::new();
13373-
let test_est = TestFeeEstimator::new(15000);
13374-
let fee_estimator = LowerBoundedFeeEstimator::new(&test_est);
13375-
let network = Network::Testnet;
13376-
let keys_provider = TestKeysInterface::new(&[42; 32], network);
13377-
let logger = TestLogger::new();
13378-
13379-
let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
13380-
let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
13381-
13382-
// Assert that we get `static_remotekey` when no custom config is negotiated.
13383-
let channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
13384-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
13385-
&channelmanager::provided_init_features(&UserConfig::default()), 10000000, 100000, 42,
13386-
&config, 0, 42, None, &logger
13387-
).unwrap();
13388-
assert_eq!(channel_a.funding.get_channel_type(), &ChannelTypeFeatures::only_static_remote_key());
13389-
13390-
let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
13391-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
13392-
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42,
13393-
None, &logger
13394-
).unwrap();
13395-
13396-
let open_channel_msg = channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
13397-
let channel_b = InboundV1Channel::<&TestKeysInterface>::new(
13398-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
13399-
&channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
13400-
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
13401-
).unwrap();
13402-
13403-
assert_eq!(channel_a.funding.get_channel_type(), &expected_channel_type);
13404-
assert_eq!(channel_b.funding.get_channel_type(), &expected_channel_type);
13405-
13406-
if expected_channel_type.supports_anchor_zero_fee_commitments() {
13407-
assert_eq!(channel_a.context.feerate_per_kw, 0);
13408-
assert_eq!(channel_b.context.feerate_per_kw, 0);
13409-
} else {
13410-
assert_ne!(channel_a.context.feerate_per_kw, 0);
13411-
assert_ne!(channel_b.context.feerate_per_kw, 0);
13412-
}
13413-
}
13414-
13415-
#[test]
13416-
fn test_rejects_implicit_simple_anchors() {
13417-
// Tests that if `option_anchors` is being negotiated implicitly through the intersection of
13418-
// each side's `InitFeatures`, it is rejected.
13419-
let secp_ctx = Secp256k1::new();
13420-
let test_est = TestFeeEstimator::new(15000);
13421-
let fee_estimator = LowerBoundedFeeEstimator::new(&test_est);
13422-
let network = Network::Testnet;
13423-
let keys_provider = TestKeysInterface::new(&[42; 32], network);
13424-
let logger = TestLogger::new();
13425-
13426-
let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
13427-
let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
13428-
13429-
let config = UserConfig::default();
13430-
13431-
// See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
13432-
let static_remote_key_required: u64 = 1 << 12;
13433-
let simple_anchors_required: u64 = 1 << 20;
13434-
let raw_init_features = static_remote_key_required | simple_anchors_required;
13435-
let init_features_with_simple_anchors = InitFeatures::from_le_bytes(raw_init_features.to_le_bytes().to_vec());
13436-
13437-
let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
13438-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
13439-
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42,
13440-
None, &logger
13441-
).unwrap();
13442-
13443-
// Set `channel_type` to `None` to force the implicit feature negotiation.
13444-
let mut open_channel_msg = channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
13445-
open_channel_msg.common_fields.channel_type = None;
13446-
13447-
// Since A supports both `static_remote_key` and `option_anchors`, but B only accepts
13448-
// `static_remote_key`, it will fail the channel.
13449-
let channel_b = InboundV1Channel::<&TestKeysInterface>::new(
13450-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
13451-
&channelmanager::provided_channel_type_features(&config), &init_features_with_simple_anchors,
13452-
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
13453-
);
13454-
assert!(channel_b.is_err());
13455-
}
13456-
13457-
#[test]
13458-
fn test_rejects_simple_anchors_channel_type() {
13459-
// Tests that if `option_anchors` is being negotiated through the `channel_type` feature,
13460-
// it is rejected.
13461-
let secp_ctx = Secp256k1::new();
13462-
let test_est = TestFeeEstimator::new(15000);
13463-
let fee_estimator = LowerBoundedFeeEstimator::new(&test_est);
13464-
let network = Network::Testnet;
13465-
let keys_provider = TestKeysInterface::new(&[42; 32], network);
13466-
let logger = TestLogger::new();
13467-
13468-
let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
13469-
let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
13470-
13471-
let config = UserConfig::default();
13472-
13473-
// See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
13474-
let static_remote_key_required: u64 = 1 << 12;
13475-
let simple_anchors_required: u64 = 1 << 20;
13476-
let simple_anchors_raw_features = static_remote_key_required | simple_anchors_required;
13477-
let simple_anchors_init = InitFeatures::from_le_bytes(simple_anchors_raw_features.to_le_bytes().to_vec());
13478-
let simple_anchors_channel_type = ChannelTypeFeatures::from_le_bytes(simple_anchors_raw_features.to_le_bytes().to_vec());
13479-
assert!(!simple_anchors_init.requires_unknown_bits());
13480-
assert!(!simple_anchors_channel_type.requires_unknown_bits());
13481-
13482-
// First, we'll try to open a channel between A and B where A requests a channel type for
13483-
// the original `option_anchors` feature (non zero fee htlc tx). This should be rejected by
13484-
// B as it's not supported by LDK.
13485-
let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
13486-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
13487-
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42,
13488-
None, &logger
13489-
).unwrap();
13490-
13491-
let mut open_channel_msg = channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
13492-
open_channel_msg.common_fields.channel_type = Some(simple_anchors_channel_type.clone());
13493-
13494-
let res = InboundV1Channel::<&TestKeysInterface>::new(
13495-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
13496-
&channelmanager::provided_channel_type_features(&config), &simple_anchors_init,
13497-
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
13498-
);
13499-
assert!(res.is_err());
13500-
13501-
// Then, we'll try to open another channel where A requests a channel type for
13502-
// `anchors_zero_fee_htlc_tx`. B is malicious and tries to downgrade the channel type to the
13503-
// original `option_anchors` feature, which should be rejected by A as it's not supported by
13504-
// LDK.
13505-
let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
13506-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b, &simple_anchors_init,
13507-
10000000, 100000, 42, &config, 0, 42, None, &logger
13508-
).unwrap();
13509-
13510-
let open_channel_msg = channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
13511-
13512-
let mut channel_b = InboundV1Channel::<&TestKeysInterface>::new(
13513-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
13514-
&channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
13515-
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
13516-
).unwrap();
13517-
13518-
let mut accept_channel_msg = channel_b.get_accept_channel_message(&&logger).unwrap();
13519-
accept_channel_msg.common_fields.channel_type = Some(simple_anchors_channel_type.clone());
13520-
13521-
let res = channel_a.accept_channel(
13522-
&accept_channel_msg, &config.channel_handshake_limits, &simple_anchors_init
13523-
);
13524-
assert!(res.is_err());
13525-
}
13526-
1352713303
#[test]
1352813304
fn test_waiting_for_batch() {
1352913305
let test_est = TestFeeEstimator::new(15000);

0 commit comments

Comments
 (0)