WIP master rlwt
authorMatt Corallo <git@bluematt.me>
Tue, 24 Mar 2020 00:24:32 +0000 (20:24 -0400)
committerMatt Corallo <git@bluematt.me>
Mon, 25 May 2020 19:36:59 +0000 (15:36 -0400)
lightning/src/ln/chan_utils.rs
lightning/src/ln/channel.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/onchaintx.rs

index c229819c3b64e6d3e875002c814190a1d40e153e..2d0af708f2c5e801759d391bec6f3d15e68afa56 100644 (file)
@@ -26,8 +26,8 @@ use std::{cmp, mem};
 
 const MAX_ALLOC_SIZE: usize = 64*1024;
 
-pub(super) const HTLC_SUCCESS_TX_WEIGHT: u64 = 703;
-pub(super) const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663;
+pub(super) const HTLC_SUCCESS_TX_WEIGHT: u64 = 703; // XXX
+pub(super) const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663; // XXX
 
 #[derive(PartialEq)]
 pub(crate) enum HTLCType {
@@ -38,9 +38,9 @@ pub(crate) enum HTLCType {
 impl HTLCType {
        /// Check if a given tx witnessScript len matchs one of a pre-signed HTLC
        pub(crate) fn scriptlen_to_htlctype(witness_script_len: usize) ->  Option<HTLCType> {
-               if witness_script_len == 133 {
+               if witness_script_len == 136 {
                        Some(HTLCType::OfferedHTLC)
-               } else if witness_script_len >= 136 && witness_script_len <= 139 {
+               } else if witness_script_len >= 139 && witness_script_len <= 142 {
                        Some(HTLCType::AcceptedHTLC)
                } else {
                        None
@@ -385,6 +385,9 @@ pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommit
                              .push_opcode(opcodes::all::OP_CHECKSIG)
                              .push_opcode(opcodes::all::OP_ENDIF)
                              .push_opcode(opcodes::all::OP_ENDIF)
+                             .push_int(1)
+                             .push_opcode(opcodes::all::OP_CSV)
+                             .push_opcode(opcodes::all::OP_DROP)
                              .into_script()
        } else {
                Builder::new().push_opcode(opcodes::all::OP_DUP)
@@ -416,6 +419,9 @@ pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommit
                              .push_opcode(opcodes::all::OP_CHECKSIG)
                              .push_opcode(opcodes::all::OP_ENDIF)
                              .push_opcode(opcodes::all::OP_ENDIF)
+                             .push_int(1)
+                             .push_opcode(opcodes::all::OP_CSV)
+                             .push_opcode(opcodes::all::OP_DROP)
                              .into_script()
        }
 }
@@ -452,7 +458,7 @@ pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u64, to_self_del
                        vout: htlc.transaction_output_index.expect("Can't build an HTLC transaction for a dust output"),
                },
                script_sig: Script::new(),
-               sequence: 0,
+               sequence: 1,
                witness: Vec::new(),
        });
 
index e22186ec2d8aae254b6f869fece0ab4ffa0548b6..9a586254beaad85cce559fa898846db5135b2ffe 100644 (file)
@@ -961,6 +961,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                } else {
                        (value_to_self_msat / 1000, value_to_remote_msat / 1000 - total_fee as i64 - ANCHOR_VALUE_SATOSHIS as i64)
                };
+               debug_assert!(value_to_self >= 0);
+               debug_assert!(value_to_remote >= 0);
 
                let value_to_a = if local { value_to_self } else { value_to_remote };
                let value_to_b = if local { value_to_remote } else { value_to_self };
@@ -998,6 +1000,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        }, None));
                }
 
+
                transaction_utils::sort_outputs(&mut txouts, |a, b| {
                        if let &Some(ref a_htlc) = a {
                                if let &Some(ref b_htlc) = b {
@@ -4443,8 +4446,6 @@ mod tests {
 
        #[test]
        fn outbound_commitment_test() {
-               return; // Disabled as we don't have test vectors for the currently-partially-implemented option_anchor_outputs
-
                // Test vectors from BOLT 3 Appendix C:
                let feeest = TestFeeEstimator{fee_est: 15000};
                let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
index a8c4d279763950e8dda2b5d326c7587c4e089214..890ce010a292a12d11e99eb3a2f58a77881696c9 100644 (file)
@@ -1119,8 +1119,8 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
        nodes
 }
 
-pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 138; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test
-pub const OFFERED_HTLC_SCRIPT_WEIGHT: usize = 133;
+pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 141; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test
+pub const OFFERED_HTLC_SCRIPT_WEIGHT: usize = 136;
 
 #[derive(PartialEq)]
 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
index f263ddf434b5d83f4c78be7b6bf72fca7fd56d8e..f4926471d1de4441338e3f64aab761959f5d452c 100644 (file)
@@ -376,19 +376,19 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
                        tx_weight +=  match inp {
                                // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
                                &InputDescriptors::RevokedOfferedHTLC => {
-                                       1 + 1 + 73 + 1 + 33 + 1 + 133
+                                       1 + 1 + 73 + 1 + 33 + 1 + 136
                                },
                                // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
                                &InputDescriptors::RevokedReceivedHTLC => {
-                                       1 + 1 + 73 + 1 + 33 + 1 + 139
+                                       1 + 1 + 73 + 1 + 33 + 1 + 142
                                },
                                // number_of_witness_elements + sig_length + remotehtlc_sig  + preimage_length + preimage + witness_script_length + witness_script
                                &InputDescriptors::OfferedHTLC => {
-                                       1 + 1 + 73 + 1 + 32 + 1 + 133
+                                       1 + 1 + 73 + 1 + 32 + 1 + 136
                                },
                                // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
                                &InputDescriptors::ReceivedHTLC => {
-                                       1 + 1 + 73 + 1 + 1 + 1 + 139
+                                       1 + 1 + 73 + 1 + 1 + 1 + 142
                                },
                                // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
                                &InputDescriptors::RevokedOutput => {
@@ -421,12 +421,16 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
        {
                if cached_claim_datas.per_input_material.len() == 0 { return None } // But don't prune pending claiming request yet, we may have to resurrect HTLCs
                let mut inputs = Vec::new();
-               for outp in cached_claim_datas.per_input_material.keys() {
+               for (outp, material) in cached_claim_datas.per_input_material.iter() {
                        log_trace!(logger, "Outpoint {}:{}", outp.txid, outp.vout);
                        inputs.push(TxIn {
                                previous_output: *outp,
                                script_sig: Script::new(),
-                               sequence: 0xfffffffd,
+                               sequence: match material {
+                                       &InputMaterial::Revoked { .. } => 1, // XXX: Depends on spec discussion
+                                       &InputMaterial::RemoteHTLC { .. } => 1,
+                                       &InputMaterial::LocalHTLC { .. } => 1,
+                               },
                                witness: Vec::new(),
                        });
                }