X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fchannelmonitor.rs;h=0a70eb0e676bb34d84e521d92524664530a6243e;hb=c20e930b31e973e0fb290322c9ac425002e3b672;hp=41c7eb89a069df737ce30006371df6c7c9838439;hpb=4833d1acf9fd7755db5aaaaa50f3e54e8446d6b3;p=rust-lightning diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index 41c7eb89..0a70eb0e 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -35,7 +35,7 @@ use ln::chan_utils::{HTLCOutputInCommitment, LocalCommitmentTransaction, HTLCTyp use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash}; use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT}; use chain::transaction::OutPoint; -use chain::keysinterface::SpendableOutputDescriptor; +use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys}; use util::logger::Logger; use util::ser::{ReadableArgs, Readable, Writer, Writeable, U48}; use util::{byte_utils, events}; @@ -114,13 +114,13 @@ pub struct HTLCUpdate { /// than calling these methods directly, the user should register implementors as listeners to the /// BlockNotifier and call the BlockNotifier's `block_(dis)connected` methods, which will notify /// all registered listeners in one go. -pub trait ManyChannelMonitor: Send + Sync { +pub trait ManyChannelMonitor: Send + Sync { /// Adds or updates a monitor for the given `funding_txo`. /// /// Implementor must also ensure that the funding_txo outpoint is registered with any relevant /// ChainWatchInterfaces such that the provided monitor receives block_connected callbacks with /// any spends of it. - fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>; + fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>; /// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated /// with success or failure backward @@ -138,11 +138,11 @@ pub trait ManyChannelMonitor: Send + Sync { /// /// If you're using this for local monitoring of your own channels, you probably want to use /// `OutPoint` as the key, which will give you a ManyChannelMonitor implementation. -pub struct SimpleManyChannelMonitor { +pub struct SimpleManyChannelMonitor { #[cfg(test)] // Used in ChannelManager tests to manipulate channels directly - pub monitors: Mutex>, + pub monitors: Mutex>>, #[cfg(not(test))] - monitors: Mutex>, + monitors: Mutex>>, chain_monitor: Arc, broadcaster: Arc, pending_events: Mutex>, @@ -151,7 +151,7 @@ pub struct SimpleManyChannelMonitor { fee_estimator: Arc } -impl<'a, Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonitor { +impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys> ChainListener for SimpleManyChannelMonitor { fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) { let block_hash = header.bitcoin_hash(); let mut new_events: Vec = Vec::with_capacity(0); @@ -215,10 +215,10 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelM } } -impl SimpleManyChannelMonitor { +impl SimpleManyChannelMonitor { /// Creates a new object which can be used to monitor several channels given the chain /// interface with which to register to receive notifications. - pub fn new(chain_monitor: Arc, broadcaster: Arc, logger: Arc, feeest: Arc) -> SimpleManyChannelMonitor { + pub fn new(chain_monitor: Arc, broadcaster: Arc, logger: Arc, feeest: Arc) -> SimpleManyChannelMonitor { let res = SimpleManyChannelMonitor { monitors: Mutex::new(HashMap::new()), chain_monitor, @@ -233,7 +233,7 @@ impl SimpleManyChannelMonitor } /// Adds or updates the monitor which monitors the channel referred to by the given key. - pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), MonitorUpdateError> { + pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), MonitorUpdateError> { let mut monitors = self.monitors.lock().unwrap(); match monitors.get_mut(&key) { Some(orig_monitor) => { @@ -264,8 +264,8 @@ impl SimpleManyChannelMonitor } } -impl ManyChannelMonitor for SimpleManyChannelMonitor { - fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> { +impl ManyChannelMonitor for SimpleManyChannelMonitor { + fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> { match self.add_update_monitor_by_key(funding_txo, monitor) { Ok(_) => Ok(()), Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure), @@ -288,7 +288,7 @@ impl ManyChannelMonitor for SimpleManyChannelMonitor { } } -impl events::EventsProvider for SimpleManyChannelMonitor { +impl events::EventsProvider for SimpleManyChannelMonitor { fn get_and_clear_pending_events(&self) -> Vec { let mut pending_events = self.pending_events.lock().unwrap(); let mut ret = Vec::new(); @@ -326,9 +326,10 @@ pub(crate) const LATENCY_GRACE_PERIOD_BLOCKS: u32 = 3; /// keeping bumping another claim tx to solve the outpoint. pub(crate) const ANTI_REORG_DELAY: u32 = 6; -#[derive(Clone, PartialEq)] -enum Storage { +#[derive(Clone)] +enum Storage { Local { + keys: ChanSigner, funding_key: SecretKey, revocation_base_key: SecretKey, htlc_base_key: SecretKey, @@ -345,6 +346,29 @@ enum Storage { } } +#[cfg(any(test, feature = "fuzztarget"))] +impl PartialEq for Storage { + fn eq(&self, other: &Self) -> bool { + match *self { + Storage::Local { ref keys, .. } => { + let k = keys; + match *other { + Storage::Local { ref keys, .. } => keys.pubkeys() == k.pubkeys(), + Storage::Watchtower { .. } => false, + } + }, + Storage::Watchtower {ref revocation_base_key, ref htlc_base_key} => { + let (rbk, hbk) = (revocation_base_key, htlc_base_key); + match *other { + Storage::Local { .. } => false, + Storage::Watchtower {ref revocation_base_key, ref htlc_base_key} => + revocation_base_key == rbk && htlc_base_key == hbk, + } + }, + } + } +} + #[derive(Clone, PartialEq)] struct LocalSignedTx { /// txid of the transaction in tx, just used to make comparison faster @@ -563,10 +587,10 @@ const MIN_SERIALIZATION_VERSION: u8 = 1; /// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date /// information and are actively monitoring the chain. #[derive(Clone)] -pub struct ChannelMonitor { +pub struct ChannelMonitor { commitment_transaction_number_obscure_factor: u64, - key_storage: Storage, + key_storage: Storage, their_htlc_base_key: Option, their_delayed_payment_base_key: Option, funding_redeemscript: Option