chain_monitor: Arc<ChainWatchInterface>,
broadcaster: Arc<BroadcasterInterface>,
pending_events: Mutex<Vec<events::Event>>,
+ logger: Arc<Logger>,
}
impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonitor<Key> {
impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key> {
/// 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<ChainWatchInterface>, broadcaster: Arc<BroadcasterInterface>) -> Arc<SimpleManyChannelMonitor<Key>> {
+ pub fn new(chain_monitor: Arc<ChainWatchInterface>, broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>) -> Arc<SimpleManyChannelMonitor<Key>> {
let res = Arc::new(SimpleManyChannelMonitor {
monitors: Mutex::new(HashMap::new()),
chain_monitor,
broadcaster,
pending_events: Mutex::new(Vec::new()),
+ logger,
});
let weak_res = Arc::downgrade(&res);
res.chain_monitor.register_listener(weak_res);
pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), HandleError> {
let mut monitors = self.monitors.lock().unwrap();
match monitors.get_mut(&key) {
- Some(orig_monitor) => return orig_monitor.insert_combine(monitor),
+ Some(orig_monitor) => {
+ log_trace!(self, "Updating Channel Monitor for channel {}", log_funding_option!(monitor.funding_txo));
+ return orig_monitor.insert_combine(monitor);
+ },
None => {}
};
match &monitor.funding_txo {
- &None => self.chain_monitor.watch_all_txn(),
+ &None => {
+ log_trace!(self, "Got new Channel Monitor for no-funding-set channel (monitoring all txn!)");
+ self.chain_monitor.watch_all_txn()
+ },
&Some((ref outpoint, ref script)) => {
+ log_trace!(self, "Got new Channel Monitor for channel {}", log_bytes!(outpoint.to_channel_id()[..]));
self.chain_monitor.install_watch_tx(&outpoint.txid, script);
self.chain_monitor.install_watch_outpoint((outpoint.txid, outpoint.index as u32), script);
},
prev_local_signed_commitment_tx: Option<LocalSignedTx>,
current_local_signed_commitment_tx: Option<LocalSignedTx>,
+ // Used just for ChannelManager to make sure it has the latest channel data during
+ // deserialization
+ current_remote_commitment_number: u64,
+
payment_preimages: HashMap<[u8; 32], [u8; 32]>,
destination_script: Script,
self.remote_commitment_txn_on_chain != other.remote_commitment_txn_on_chain ||
self.remote_hash_commitment_number != other.remote_hash_commitment_number ||
self.prev_local_signed_commitment_tx != other.prev_local_signed_commitment_tx ||
+ self.current_remote_commitment_number != other.current_remote_commitment_number ||
self.current_local_signed_commitment_tx != other.current_local_signed_commitment_tx ||
self.payment_preimages != other.payment_preimages ||
self.destination_script != other.destination_script
prev_local_signed_commitment_tx: None,
current_local_signed_commitment_tx: None,
+ current_remote_commitment_number: 1 << 48,
payment_preimages: HashMap::new(),
destination_script: destination_script,
self.remote_hash_commitment_number.insert(htlc.payment_hash, commitment_number);
}
self.remote_claimable_outpoints.insert(unsigned_commitment_tx.txid(), htlc_outputs);
+ self.current_remote_commitment_number = commitment_number;
}
/// Informs this monitor of the latest local (ie broadcastable) commitment transaction. The
if our_min_secret > other_min_secret {
self.provide_secret(other_min_secret, other.get_secret(other_min_secret).unwrap(), None)?;
}
+ // TODO: We should use current_remote_commitment_number and the commitment number out of
+ // local transactions to decide how to merge
if our_min_secret >= other_min_secret {
self.their_cur_revocation_points = other.their_cur_revocation_points;
for (txid, htlcs) in other.remote_claimable_outpoints.drain() {
}
self.payment_preimages = other.payment_preimages;
}
+ self.current_remote_commitment_number = cmp::min(self.current_remote_commitment_number, other.current_remote_commitment_number);
Ok(())
}
}
writer.write_all(&byte_utils::be64_to_array(self.remote_claimable_outpoints.len() as u64))?;
- for (txid, htlc_outputs) in self.remote_claimable_outpoints.iter() {
+ for (ref txid, ref htlc_outputs) in self.remote_claimable_outpoints.iter() {
writer.write_all(&txid[..])?;
writer.write_all(&byte_utils::be64_to_array(htlc_outputs.len() as u64))?;
for htlc_output in htlc_outputs.iter() {
}
writer.write_all(&byte_utils::be64_to_array(self.remote_commitment_txn_on_chain.len() as u64))?;
- for (txid, (commitment_number, txouts)) in self.remote_commitment_txn_on_chain.iter() {
+ for (ref txid, &(commitment_number, ref txouts)) in self.remote_commitment_txn_on_chain.iter() {
writer.write_all(&txid[..])?;
- writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
+ writer.write_all(&byte_utils::be48_to_array(commitment_number))?;
(txouts.len() as u64).write(writer)?;
for script in txouts.iter() {
script.write(writer)?;
if for_local_storage {
writer.write_all(&byte_utils::be64_to_array(self.remote_hash_commitment_number.len() as u64))?;
- for (payment_hash, commitment_number) in self.remote_hash_commitment_number.iter() {
- writer.write_all(payment_hash)?;
+ for (ref payment_hash, commitment_number) in self.remote_hash_commitment_number.iter() {
+ writer.write_all(*payment_hash)?;
writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
}
} else {
writer.write_all(&[0; 1])?;
}
+ if for_local_storage {
+ writer.write_all(&byte_utils::be48_to_array(self.current_remote_commitment_number))?;
+ } else {
+ writer.write_all(&byte_utils::be48_to_array(0))?;
+ }
+
writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
for payment_preimage in self.payment_preimages.values() {
writer.write_all(payment_preimage)?;
min
}
+ pub(super) fn get_cur_remote_commitment_number(&self) -> u64 {
+ self.current_remote_commitment_number
+ }
+
+ pub(super) fn get_cur_local_commitment_number(&self) -> u64 {
+ if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
+ 0xffff_ffff_ffff - ((((local_tx.tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (local_tx.tx.lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor)
+ } else { 0xffff_ffff_ffff }
+ }
+
/// Attempts to claim a remote commitment transaction's outputs using the revocation key and
/// data in remote_claimable_outpoints. Will directly claim any HTLC outputs which expire at a
/// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
(Vec::new(), Vec::new())
}
+ /// Used by ChannelManager deserialization to broadcast the latest local state if it's copy of
+ /// the Channel was out-of-date.
+ pub(super) fn get_latest_local_commitment_txn(&self) -> Vec<Transaction> {
+ if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
+ let mut res = vec![local_tx.tx.clone()];
+ match self.key_storage {
+ KeyStorage::PrivMode { ref delayed_payment_base_key, ref prev_latest_per_commitment_point, .. } => {
+ res.append(&mut self.broadcast_by_local_state(local_tx, prev_latest_per_commitment_point, &Some(*delayed_payment_base_key)).0);
+ },
+ _ => panic!("Can only broadcast by local channelmonitor"),
+ };
+ res
+ } else {
+ Vec::new()
+ }
+ }
+
fn block_connected(&mut self, txn_matched: &[&Transaction], height: u32, block_hash: &Sha256dHash, broadcaster: &BroadcasterInterface)-> (Vec<(Sha256dHash, Vec<TxOut>)>, Vec<SpendableOutputDescriptor>) {
let mut watch_outputs = Vec::new();
let mut spendable_outputs = Vec::new();
_ => return Err(DecodeError::InvalidValue),
};
+ let current_remote_commitment_number = <U48 as Readable<R>>::read(reader)?.0;
+
let payment_preimages_len: u64 = Readable::read(reader)?;
let mut payment_preimages = HashMap::with_capacity(cmp::min(payment_preimages_len as usize, MAX_ALLOC_SIZE / 32));
let mut sha = Sha256::new();
prev_local_signed_commitment_tx,
current_local_signed_commitment_tx,
+ current_remote_commitment_number,
payment_preimages,