+ macro_rules! dumb_counterparty_offered_output {
+ ($secp_ctx: expr, $amt: expr, $opt_anchors: expr) => {
+ {
+ let dumb_scalar = SecretKey::from_slice(&<Vec<u8>>::from_hex("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
+ let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
+ let hash = PaymentHash([1; 32]);
+ let preimage = PaymentPreimage([2;32]);
+ let htlc = HTLCOutputInCommitment { offered: false, amount_msat: $amt, cltv_expiry: 1000, payment_hash: hash, transaction_output_index: None };
+ PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(dumb_point, DelayedPaymentBasepoint::from(dumb_point), HtlcBasepoint::from(dumb_point), preimage, htlc, $opt_anchors))
+ }
+ }
+ }
+
+ macro_rules! dumb_htlc_output {
+ () => {
+ {
+ let preimage = PaymentPreimage([2;32]);
+ PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build_accepted(preimage, 0, ChannelTypeFeatures::only_static_remote_key()))
+ }
+ }
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_package_differing_heights() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+ let revk_outp = dumb_revk_output!(secp_ctx, false);
+
+ let mut package_one_hundred = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, 100);
+ let package_two_hundred = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, 200);
+ package_one_hundred.merge_package(package_two_hundred);
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_package_untractable_merge_to() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+ let revk_outp = dumb_revk_output!(secp_ctx, false);
+ let htlc_outp = dumb_htlc_output!();
+
+ let mut untractable_package = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, 100);
+ let malleable_package = PackageTemplate::build_package(txid, 1, htlc_outp.clone(), 1000, 100);
+ untractable_package.merge_package(malleable_package);
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_package_untractable_merge_from() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+ let htlc_outp = dumb_htlc_output!();
+ let revk_outp = dumb_revk_output!(secp_ctx, false);
+
+ let mut malleable_package = PackageTemplate::build_package(txid, 0, htlc_outp.clone(), 1000, 100);
+ let untractable_package = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, 100);
+ malleable_package.merge_package(untractable_package);
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_package_noaggregation_to() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+ let revk_outp = dumb_revk_output!(secp_ctx, false);
+ let revk_outp_counterparty_balance = dumb_revk_output!(secp_ctx, true);
+
+ let mut noaggregation_package = PackageTemplate::build_package(txid, 0, revk_outp_counterparty_balance.clone(), 1000, 100);
+ let aggregation_package = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, 100);
+ noaggregation_package.merge_package(aggregation_package);
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_package_noaggregation_from() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+ let revk_outp = dumb_revk_output!(secp_ctx, false);
+ let revk_outp_counterparty_balance = dumb_revk_output!(secp_ctx, true);
+
+ let mut aggregation_package = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, 100);
+ let noaggregation_package = PackageTemplate::build_package(txid, 1, revk_outp_counterparty_balance.clone(), 1000, 100);
+ aggregation_package.merge_package(noaggregation_package);
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_package_empty() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+ let revk_outp = dumb_revk_output!(secp_ctx, false);
+
+ let mut empty_package = PackageTemplate::build_package(txid, 0, revk_outp.clone(), 1000, 100);
+ empty_package.inputs = vec![];
+ let package = PackageTemplate::build_package(txid, 1, revk_outp.clone(), 1000, 100);
+ empty_package.merge_package(package);
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_package_differing_categories() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+ let revk_outp = dumb_revk_output!(secp_ctx, false);
+ let counterparty_outp = dumb_counterparty_output!(secp_ctx, 0, ChannelTypeFeatures::only_static_remote_key());
+
+ let mut revoked_package = PackageTemplate::build_package(txid, 0, revk_outp, 1000, 100);
+ let counterparty_package = PackageTemplate::build_package(txid, 1, counterparty_outp, 1000, 100);
+ revoked_package.merge_package(counterparty_package);
+ }
+
+ #[test]
+ fn test_package_split_malleable() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+ let revk_outp_one = dumb_revk_output!(secp_ctx, false);
+ let revk_outp_two = dumb_revk_output!(secp_ctx, false);
+ let revk_outp_three = dumb_revk_output!(secp_ctx, false);
+
+ let mut package_one = PackageTemplate::build_package(txid, 0, revk_outp_one, 1000, 100);
+ let package_two = PackageTemplate::build_package(txid, 1, revk_outp_two, 1000, 100);
+ let package_three = PackageTemplate::build_package(txid, 2, revk_outp_three, 1000, 100);
+
+ package_one.merge_package(package_two);
+ package_one.merge_package(package_three);
+ assert_eq!(package_one.outpoints().len(), 3);
+
+ if let Some(split_package) = package_one.split_package(&BitcoinOutPoint { txid, vout: 1 }) {
+ // Packages attributes should be identical
+ assert!(split_package.is_malleable());
+ assert_eq!(split_package.soonest_conf_deadline, package_one.soonest_conf_deadline);
+ assert_eq!(split_package.aggregable, package_one.aggregable);
+ assert_eq!(split_package.feerate_previous, package_one.feerate_previous);
+ assert_eq!(split_package.height_timer, package_one.height_timer);
+ assert_eq!(split_package.height_original, package_one.height_original);
+ } else { panic!(); }
+ assert_eq!(package_one.outpoints().len(), 2);
+ }
+
+ #[test]
+ fn test_package_split_untractable() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let htlc_outp_one = dumb_htlc_output!();
+
+ let mut package_one = PackageTemplate::build_package(txid, 0, htlc_outp_one, 1000, 100);
+ let ret_split = package_one.split_package(&BitcoinOutPoint { txid, vout: 0});
+ assert!(ret_split.is_none());
+ }
+
+ #[test]
+ fn test_package_timer() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+ let revk_outp = dumb_revk_output!(secp_ctx, false);
+
+ let mut package = PackageTemplate::build_package(txid, 0, revk_outp, 1000, 100);
+ assert_eq!(package.timer(), 100);
+ package.set_timer(101);
+ assert_eq!(package.timer(), 101);
+ }
+
+ #[test]
+ fn test_package_amounts() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+ let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000, ChannelTypeFeatures::only_static_remote_key());
+
+ let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, 100);
+ assert_eq!(package.package_amount(), 1000);
+ }
+
+ #[test]
+ fn test_package_weight() {
+ let txid = Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
+ let secp_ctx = Secp256k1::new();
+
+ // (nVersion (4) + nLocktime (4) + count_tx_in (1) + prevout (36) + sequence (4) + script_length (1) + count_tx_out (1) + value (8) + var_int (1)) * WITNESS_SCALE_FACTOR + witness marker (2)
+ let weight_sans_output = (4 + 4 + 1 + 36 + 4 + 1 + 1 + 8 + 1) * WITNESS_SCALE_FACTOR as u64 + 2;
+
+ {
+ let revk_outp = dumb_revk_output!(secp_ctx, false);
+ let package = PackageTemplate::build_package(txid, 0, revk_outp, 0, 100);
+ assert_eq!(package.package_weight(&ScriptBuf::new()), weight_sans_output + WEIGHT_REVOKED_OUTPUT);
+ }
+
+ {
+ for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
+ let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000, channel_type_features.clone());
+ let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, 100);
+ assert_eq!(package.package_weight(&ScriptBuf::new()), weight_sans_output + weight_received_htlc(channel_type_features));
+ }
+ }
+
+ {
+ for channel_type_features in [ChannelTypeFeatures::only_static_remote_key(), ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies()].iter() {
+ let counterparty_outp = dumb_counterparty_offered_output!(secp_ctx, 1_000_000, channel_type_features.clone());
+ let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, 100);
+ assert_eq!(package.package_weight(&ScriptBuf::new()), weight_sans_output + weight_offered_htlc(channel_type_features));
+ }
+ }
+ }
+}