]> 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>
Wed, 6 May 2020 10:24:52 +0000 (06:24 -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 c8fe6f1e306fd0840226580510ef469377ba319d..75bf6d7c60951c9ef0e23f78c01bd63a504a14a4 100644 (file)
@@ -1045,7 +1045,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, logger.clone());
+               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, logger.clone());
 
                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;
@@ -1080,8 +1080,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 19c901fc9d009eb7dc564d8e9b4b857c7859c398..c57b11aaf16d64ba3ded1883d537b5fe1628fbd7 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};
@@ -48,6 +49,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 {
@@ -195,6 +203,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,
 
@@ -241,6 +251,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))?;
@@ -289,6 +303,16 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
 
                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)?;
@@ -341,6 +365,8 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
                        prev_local_commitment,
                        prev_local_htlc_sigs,
                        local_csv,
+                       remote_tx_cache,
+                       remote_csv,
                        key_storage,
                        claimable_outpoints,
                        pending_claim_requests,
@@ -352,10 +378,15 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
 }
 
 impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
-       pub(super) fn new(destination_script: Script, keys: ChanSigner, local_csv: u16, logger: Arc<Logger>) -> 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, logger: Arc<Logger>) -> 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,
@@ -363,6 +394,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(),