From: Antoine Riard Date: Tue, 24 Mar 2020 01:22:14 +0000 (-0400) Subject: Cache remote basepoint and remote_csv in new OnchainTxHandler::RemoteTxCache X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=e26c4e9e864582b66dbd0174414c4f0b3ec90ed5;p=rust-lightning Cache remote basepoint and remote_csv in new OnchainTxHandler::RemoteTxCache Used in next commits to avoid passing script between ChannelMonitor and OnchainTxHandler. ChannelMonitor duplicata will be removed in future commits. --- diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index c8fe6f1e3..75bf6d7c6 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -1045,7 +1045,7 @@ impl ChannelMonitor { 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 ChannelMonitor { 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, diff --git a/lightning/src/ln/onchaintx.rs b/lightning/src/ln/onchaintx.rs index 19c901fc9..c57b11aaf 100644 --- a/lightning/src/ln/onchaintx.rs +++ b/lightning/src/ln/onchaintx.rs @@ -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 { prev_local_commitment: Option, prev_local_htlc_sigs: Option>>, local_csv: u16, + remote_tx_cache: RemoteTxCache, + remote_csv: u16, key_storage: ChanSigner, @@ -241,6 +251,10 @@ impl OnchainTxHandler { 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 ReadableArgs> 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 ReadableArgs> 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 ReadableArgs> for OnchainTx } impl OnchainTxHandler { - pub(super) fn new(destination_script: Script, keys: ChanSigner, local_csv: u16, logger: Arc) -> 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) -> 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 OnchainTxHandler { 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(),