-impl ChainListener for ChannelManager {
- fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) {
- let mut new_events = Vec::new();
- let mut failed_channels = Vec::new();
- {
- let mut channel_lock = self.channel_state.lock().unwrap();
- let channel_state = channel_lock.borrow_parts();
- let short_to_id = channel_state.short_to_id;
- channel_state.by_id.retain(|_, channel| {
- let chan_res = channel.block_connected(header, height, txn_matched, indexes_of_txn_matched);
- if let Ok(Some(funding_locked)) = chan_res {
- let announcement_sigs = match self.get_announcement_sigs(channel) {
- Ok(res) => res,
- Err(_e) => {
- //TODO: push e on events and blow up the channel (it has bad keys)
- return true;
- }
- };
- new_events.push(events::Event::SendFundingLocked {
- node_id: channel.get_their_node_id(),
- msg: funding_locked,
- announcement_sigs: announcement_sigs
- });
- short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
- } else if let Err(e) = chan_res {
- new_events.push(events::Event::HandleError {
- node_id: channel.get_their_node_id(),
- action: e.action,
- });
- if channel.is_shutdown() {
- return false;
- }
- }
- if let Some(funding_txo) = channel.get_funding_txo() {
- for tx in txn_matched {
- for inp in tx.input.iter() {
- if inp.prev_hash == funding_txo.txid && inp.prev_index == funding_txo.index as u32 {
- if let Some(short_id) = channel.get_short_channel_id() {
- short_to_id.remove(&short_id);
- }
- // It looks like our counterparty went on-chain. We go ahead and
- // broadcast our latest local state as well here, just in case its
- // some kind of SPV attack, though we expect these to be dropped.
- failed_channels.push(channel.force_shutdown());
- if let Ok(update) = self.get_channel_update(&channel) {
- new_events.push(events::Event::BroadcastChannelUpdate {
- msg: update
- });
- }
- return false;
- }
- }
- }
- }
- if channel.is_funding_initiated() && channel.channel_monitor().would_broadcast_at_height(height) {
- if let Some(short_id) = channel.get_short_channel_id() {
- short_to_id.remove(&short_id);
- }
- failed_channels.push(channel.force_shutdown());
- // If would_broadcast_at_height() is true, the channel_monitor will broadcast
- // the latest local tx for us, so we should skip that here (it doesn't really
- // hurt anything, but does make tests a bit simpler).
- failed_channels.last_mut().unwrap().0 = Vec::new();
- if let Ok(update) = self.get_channel_update(&channel) {
- new_events.push(events::Event::BroadcastChannelUpdate {
- msg: update
- });
- }
- return false;
- }
- true
- });
- }
- for failure in failed_channels.drain(..) {
- self.finish_force_close_channel(failure);
- }
- let mut pending_events = self.pending_events.lock().unwrap();
- for funding_locked in new_events.drain(..) {
- pending_events.push(funding_locked);
- }
- self.latest_block_height.store(height as usize, Ordering::Release);
- }
-
- /// We force-close the channel without letting our counterparty participate in the shutdown
- fn block_disconnected(&self, header: &BlockHeader) {
- let mut new_events = Vec::new();
- let mut failed_channels = Vec::new();
- {
- let mut channel_lock = self.channel_state.lock().unwrap();
- let channel_state = channel_lock.borrow_parts();
- let short_to_id = channel_state.short_to_id;
- channel_state.by_id.retain(|_, v| {
- if v.block_disconnected(header) {
- if let Some(short_id) = v.get_short_channel_id() {
- short_to_id.remove(&short_id);
- }
- failed_channels.push(v.force_shutdown());
- if let Ok(update) = self.get_channel_update(&v) {
- new_events.push(events::Event::BroadcastChannelUpdate {
- msg: update
- });
- }
- false
- } else {
- true
- }
- });
- }
- for failure in failed_channels.drain(..) {
- self.finish_force_close_channel(failure);
- }
- if !new_events.is_empty() {
- let mut pending_events = self.pending_events.lock().unwrap();
- for funding_locked in new_events.drain(..) {
- pending_events.push(funding_locked);
- }
- }
- self.latest_block_height.fetch_sub(1, Ordering::AcqRel);
- }
-}
-
-impl ChannelMessageHandler for ChannelManager {
- //TODO: Handle errors and close channel (or so)
- fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &msgs::OpenChannel) -> Result<msgs::AcceptChannel, HandleError> {
- if msg.chain_hash != self.genesis_hash {
- return Err(HandleError{err: "Unknown genesis block hash", action: None});
- }
- let mut channel_state = self.channel_state.lock().unwrap();
- if channel_state.by_id.contains_key(&msg.temporary_channel_id) {
- return Err(HandleError{err: "temporary_channel_id collision!", action: None});
- }
-
- let chan_keys = if cfg!(feature = "fuzztarget") {
- ChannelKeys {
- funding_key: SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]).unwrap(),
- revocation_base_key: SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0]).unwrap(),
- payment_base_key: SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0]).unwrap(),
- delayed_payment_base_key: SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0]).unwrap(),
- htlc_base_key: SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0]).unwrap(),
- channel_close_key: SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0]).unwrap(),
- channel_monitor_claim_key: SecretKey::from_slice(&self.secp_ctx, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0]).unwrap(),
- commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- }
- } else {
- let mut key_seed = [0u8; 32];
- rng::fill_bytes(&mut key_seed);
- match ChannelKeys::new_from_seed(&key_seed) {
- Ok(key) => key,
- Err(_) => panic!("RNG is busted!")
- }
- };
-
- let channel = Channel::new_from_req(&*self.fee_estimator, chan_keys, their_node_id.clone(), msg, 0, false, self.announce_channels_publicly)?;
- let accept_msg = channel.get_accept_channel()?;
- channel_state.by_id.insert(channel.channel_id(), channel);
- Ok(accept_msg)
- }
-
- fn handle_accept_channel(&self, their_node_id: &PublicKey, msg: &msgs::AcceptChannel) -> Result<(), HandleError> {