]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Cache remote basepoint and remote_csv in new OnchainTxHandler::RemoteTxCache
authorAntoine Riard <ariard@student.42.fr>
Tue, 24 Mar 2020 01:22:14 +0000 (21:22 -0400)
committerAntoine Riard <ariard@student.42.fr>
Mon, 18 May 2020 08:39:26 +0000 (04:39 -0400)
Used in next commits to avoid passing script between ChannelMonitor
and OnchainTxHandler. ChannelMonitor duplicata will be removed
in future commits.

lightning/src/ln/channelmonitor.rs
lightning/src/ln/onchaintx.rs

index ff0b6e9eb8903a0ab3d2c03a7cba8f892026f623..6f00392c214c67002e08110930e945976ca342d9 100644 (file)
@@ -1053,7 +1053,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                let payment_key_hash = WPubkeyHash::hash(&keys.pubkeys().payment_point.serialize());
                let remote_payment_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_key_hash[..]).into_script();
 
-               let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), their_to_self_delay);
+               let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), their_to_self_delay, their_delayed_payment_base_key.clone(), their_htlc_base_key.clone(), our_to_self_delay);
 
                let local_tx_sequence = initial_local_commitment_tx.unsigned_tx.input[0].sequence as u64;
                let local_tx_locktime = initial_local_commitment_tx.unsigned_tx.lock_time as u64;
@@ -1088,8 +1088,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        current_remote_commitment_txid: None,
                        prev_remote_commitment_txid: None,
 
-                       their_htlc_base_key: their_htlc_base_key.clone(),
-                       their_delayed_payment_base_key: their_delayed_payment_base_key.clone(),
+                       their_htlc_base_key: *their_htlc_base_key,
+                       their_delayed_payment_base_key: *their_delayed_payment_base_key,
                        funding_redeemscript,
                        channel_value_satoshis: channel_value_satoshis,
                        their_cur_revocation_points: None,
index f263ddf434b5d83f4c78be7b6bf72fca7fd56d8e..eb30f155cfe3c683fa424fb0c0cce266bfc94aae 100644 (file)
@@ -12,6 +12,7 @@ use bitcoin::hash_types::Txid;
 
 use bitcoin::secp256k1::{Secp256k1, Signature};
 use bitcoin::secp256k1;
+use bitcoin::secp256k1::key::PublicKey;
 
 use ln::msgs::DecodeError;
 use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
@@ -47,6 +48,13 @@ enum OnchainEvent {
        }
 }
 
+/// Cache remote basepoint to compute any transaction on
+/// remote outputs, either justice or preimage/timeout transactions.
+struct RemoteTxCache {
+       remote_delayed_payment_base_key: PublicKey,
+       remote_htlc_base_key: PublicKey
+}
+
 /// Higher-level cache structure needed to re-generate bumped claim txn if needed
 #[derive(Clone, PartialEq)]
 pub struct ClaimTxBumpMaterial {
@@ -194,6 +202,8 @@ pub struct OnchainTxHandler<ChanSigner: ChannelKeys> {
        prev_local_commitment: Option<LocalCommitmentTransaction>,
        prev_local_htlc_sigs: Option<Vec<Option<(usize, Signature)>>>,
        local_csv: u16,
+       remote_tx_cache: RemoteTxCache,
+       remote_csv: u16,
 
        key_storage: ChanSigner,
 
@@ -239,6 +249,10 @@ impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
 
                self.local_csv.write(writer)?;
 
+               self.remote_tx_cache.remote_delayed_payment_base_key.write(writer)?;
+               self.remote_tx_cache.remote_htlc_base_key.write(writer)?;
+               self.remote_csv.write(writer)?;
+
                self.key_storage.write(writer)?;
 
                writer.write_all(&byte_utils::be64_to_array(self.pending_claim_requests.len() as u64))?;
@@ -287,6 +301,16 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for OnchainTxHandler<ChanSigne
 
                let local_csv = Readable::read(reader)?;
 
+               let remote_tx_cache = {
+                       let remote_delayed_payment_base_key = Readable::read(reader)?;
+                       let remote_htlc_base_key = Readable::read(reader)?;
+                       RemoteTxCache {
+                               remote_delayed_payment_base_key,
+                               remote_htlc_base_key,
+                       }
+               };
+               let remote_csv = Readable::read(reader)?;
+
                let key_storage = Readable::read(reader)?;
 
                let pending_claim_requests_len: u64 = Readable::read(reader)?;
@@ -339,6 +363,8 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for OnchainTxHandler<ChanSigne
                        prev_local_commitment,
                        prev_local_htlc_sigs,
                        local_csv,
+                       remote_tx_cache,
+                       remote_csv,
                        key_storage,
                        claimable_outpoints,
                        pending_claim_requests,
@@ -349,10 +375,15 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for OnchainTxHandler<ChanSigne
 }
 
 impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
-       pub(super) fn new(destination_script: Script, keys: ChanSigner, local_csv: u16) -> Self {
+       pub(super) fn new(destination_script: Script, keys: ChanSigner, local_csv: u16, remote_delayed_payment_base_key: PublicKey, remote_htlc_base_key: PublicKey, remote_csv: u16) -> Self {
 
                let key_storage = keys;
 
+               let remote_tx_cache = RemoteTxCache {
+                       remote_delayed_payment_base_key,
+                       remote_htlc_base_key,
+               };
+
                OnchainTxHandler {
                        destination_script,
                        local_commitment: None,
@@ -360,6 +391,8 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
                        prev_local_commitment: None,
                        prev_local_htlc_sigs: None,
                        local_csv,
+                       remote_tx_cache,
+                       remote_csv,
                        key_storage,
                        pending_claim_requests: HashMap::new(),
                        claimable_outpoints: HashMap::new(),