Cache local_commitment_tx in OnchainTxHandler
authorAntoine Riard <ariard@student.42.fr>
Sat, 21 Mar 2020 00:26:23 +0000 (20:26 -0400)
committerAntoine Riard <ariard@student.42.fr>
Fri, 17 Apr 2020 21:43:50 +0000 (17:43 -0400)
As transaction generation and signature is headed to be moved
inside OnchainTxHandler, cache local_commitment_tx signed by remote.

If access to local commitment transaction is needed, we extend Onchain
TxHandler API to do so.

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

index 6855ac15a77e9ad163921995169ea4e903f2dd09..70c92143af8b50d58eb06f9c10878fc6fe190e2d 100644 (file)
@@ -1244,7 +1244,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                self.prev_local_signed_commitment_tx = self.current_local_signed_commitment_tx.take();
                self.current_local_signed_commitment_tx = Some(LocalSignedTx {
                        txid: commitment_tx.txid(),
-                       tx: commitment_tx,
+                       tx: commitment_tx.clone(),
                        revocation_key: local_keys.revocation_key,
                        a_htlc_key: local_keys.a_htlc_key,
                        b_htlc_key: local_keys.b_htlc_key,
@@ -1253,6 +1253,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        feerate_per_kw,
                        htlc_outputs,
                });
+               self.onchain_tx_handler.provide_latest_local_tx(commitment_tx);
                Ok(())
        }
 
index d5db6950f7a1c7364d6272529c59b917a73c16eb..4c935ea654843ab66f6e5a36f0794652e06d3c01 100644 (file)
@@ -15,7 +15,7 @@ use secp256k1;
 
 use ln::msgs::DecodeError;
 use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
-use ln::chan_utils::HTLCType;
+use ln::chan_utils::{HTLCType, LocalCommitmentTransaction};
 use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
 use chain::keysinterface::ChannelKeys;
 use util::logger::Logger;
@@ -142,6 +142,8 @@ macro_rules! subtract_high_prio_fee {
 pub struct OnchainTxHandler<ChanSigner: ChannelKeys> {
        destination_script: Script,
        funding_redeemscript: Script,
+       local_commitment: Option<LocalCommitmentTransaction>,
+       prev_local_commitment: Option<LocalCommitmentTransaction>,
 
        key_storage: ChanSigner,
 
@@ -182,6 +184,8 @@ impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
        pub(crate) fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
                self.destination_script.write(writer)?;
                self.funding_redeemscript.write(writer)?;
+               self.local_commitment.write(writer)?;
+               self.prev_local_commitment.write(writer)?;
 
                self.key_storage.write(writer)?;
 
@@ -224,6 +228,8 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
        fn read<R: ::std::io::Read>(reader: &mut R, logger: Arc<Logger>) -> Result<Self, DecodeError> {
                let destination_script = Readable::read(reader)?;
                let funding_redeemscript = Readable::read(reader)?;
+               let local_commitment = Readable::read(reader)?;
+               let prev_local_commitment = Readable::read(reader)?;
 
                let key_storage = Readable::read(reader)?;
 
@@ -273,6 +279,8 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
                Ok(OnchainTxHandler {
                        destination_script,
                        funding_redeemscript,
+                       local_commitment,
+                       prev_local_commitment,
                        key_storage,
                        claimable_outpoints,
                        pending_claim_requests,
@@ -291,6 +299,8 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
                OnchainTxHandler {
                        destination_script,
                        funding_redeemscript,
+                       local_commitment: None,
+                       prev_local_commitment: None,
                        key_storage,
                        pending_claim_requests: HashMap::new(),
                        claimable_outpoints: HashMap::new(),
@@ -714,4 +724,9 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
                        self.pending_claim_requests.remove(&req);
                }
        }
+
+       pub(super) fn provide_latest_local_tx(&mut self, tx: LocalCommitmentTransaction) {
+               self.prev_local_commitment = self.local_commitment.take();
+               self.local_commitment = Some(tx);
+       }
 }