From: Matt Corallo Date: Tue, 24 Mar 2020 00:24:32 +0000 (-0400) Subject: WIP X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=HEAD;p=rust-lightning WIP --- diff --git a/lightning/src/ln/chan_utils.rs b/lightning/src/ln/chan_utils.rs index c229819c3..2d0af708f 100644 --- a/lightning/src/ln/chan_utils.rs +++ b/lightning/src/ln/chan_utils.rs @@ -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 { - 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(), }); diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index e22186ec2..9a586254b 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -961,6 +961,8 @@ impl Channel { } 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 Channel { }, 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 = Arc::new(test_utils::TestLogger::new()); diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index a8c4d2797..890ce010a 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -1119,8 +1119,8 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec OnchainTxHandler { 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 OnchainTxHandler { { 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(), }); }