Merge pull request #16 from TheBlueMatt/2018-03-fixups
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Tue, 27 Mar 2018 16:54:54 +0000 (12:54 -0400)
committerGitHub <noreply@github.com>
Tue, 27 Mar 2018 16:54:54 +0000 (12:54 -0400)
Assorted fixes/cleanups

fuzz/fuzz_targets/channel_target.rs
src/ln/channel.rs
src/ln/channelmanager.rs
src/ln/msgs.rs
src/ln/peer_handler.rs

index b8da2165885f473ba80f2631232d2dc1b1315efc..ec82fbd65abb3261166fef44a79b669cc824c90b 100644 (file)
@@ -114,6 +114,7 @@ pub fn do_test(data: &[u8]) {
                                        msgs::DecodeError::UnknownRealmByte => return,
                                        msgs::DecodeError::BadPublicKey => return,
                                        msgs::DecodeError::BadSignature => return,
+                                       msgs::DecodeError::ExtraAddressesPerType => return,
                                        msgs::DecodeError::WrongLength => panic!("We picked the length..."),
                                }
                        }
@@ -133,6 +134,7 @@ pub fn do_test(data: &[u8]) {
                                                msgs::DecodeError::UnknownRealmByte => return,
                                                msgs::DecodeError::BadPublicKey => return,
                                                msgs::DecodeError::BadSignature => return,
+                                               msgs::DecodeError::ExtraAddressesPerType => return,
                                                msgs::DecodeError::WrongLength => panic!("We picked the length..."),
                                        }
                                }
index 6c6dacc84c3821c2285b0156a62498dc068b6a49..fe1cfd24a25d74ce890ee6336a1311539af1a365 100644 (file)
@@ -211,7 +211,7 @@ pub struct Channel {
        channel_monitor: ChannelMonitor,
 }
 
-const OUR_MAX_HTLCS: u16 = 1; //TODO
+const OUR_MAX_HTLCS: u16 = 5; //TODO
 const CONF_TARGET: u32 = 12; //TODO: Should be much higher
 /// Confirmation count threshold at which we close a channel. Ideally we'd keep the channel around
 /// on ice until the funding transaction gets more confirmations, but the LN protocol doesn't
@@ -765,6 +765,7 @@ impl Channel {
 
                let mut htlc_id = 0;
                let mut htlc_amount_msat = 0;
+               //TODO: swap_remove since we dont need to maintain ordering here
                self.pending_htlcs.retain(|ref htlc| {
                        if !htlc.outbound && htlc.payment_hash == payment_hash {
                                if htlc_id != 0 {
@@ -796,6 +797,7 @@ impl Channel {
 
                let mut htlc_id = 0;
                let mut htlc_amount_msat = 0;
+               //TODO: swap_remove since we dont need to maintain ordering here
                self.pending_htlcs.retain(|ref htlc| {
                        if !htlc.outbound && htlc.payment_hash == *payment_hash {
                                if htlc_id != 0 {
@@ -1103,23 +1105,7 @@ impl Channel {
                }
        }
 
-       /// Checks if there are any LocalAnnounced HTLCs remaining and sets
-       /// ChannelState::AwaitingRemoteRevoke accordingly, possibly calling free_holding_cell_htlcs.
-       fn check_and_free_holding_cell_htlcs(&mut self) -> Result<Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>, HandleError> {
-               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
-                       for htlc in self.pending_htlcs.iter() {
-                               if htlc.state == HTLCState::LocalAnnounced {
-                                       return Ok(None);
-                               }
-                       }
-                       self.channel_state &= !(ChannelState::AwaitingRemoteRevoke as u32);
-                       self.free_holding_cell_htlcs()
-               } else {
-                       Ok(None)
-               }
-       }
-
-       pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>, HandleError> {
+       pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
                if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
                        return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", msg: None});
                }
@@ -1137,11 +1123,10 @@ impl Channel {
                                self.value_to_self_msat -= htlc.amount_msat;
                        }
                }
-
-               self.check_and_free_holding_cell_htlcs()
+               Ok(())
        }
 
-       pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC) -> Result<([u8; 32], Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>), HandleError> {
+       pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC) -> Result<[u8; 32], HandleError> {
                if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
                        return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", msg: None});
                }
@@ -1154,12 +1139,10 @@ impl Channel {
                                htlc.payment_hash
                        }
                };
-
-               let holding_cell_freedom = self.check_and_free_holding_cell_htlcs()?;
-               Ok((payment_hash, holding_cell_freedom))
+               Ok(payment_hash)
        }
 
-       pub fn update_fail_malformed_htlc(&mut self, msg: &msgs::UpdateFailMalformedHTLC) -> Result<([u8; 32], Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>), HandleError> {
+       pub fn update_fail_malformed_htlc(&mut self, msg: &msgs::UpdateFailMalformedHTLC) -> Result<[u8; 32], HandleError> {
                if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
                        return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", msg: None});
                }
@@ -1172,9 +1155,7 @@ impl Channel {
                                htlc.payment_hash
                        }
                };
-
-               let holding_cell_freedom = self.check_and_free_holding_cell_htlcs()?;
-               Ok((payment_hash, holding_cell_freedom))
+               Ok(payment_hash)
        }
 
        pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned) -> Result<(msgs::RevokeAndACK, Vec<PendingForwardHTLCInfo>), HandleError> {
@@ -1258,7 +1239,7 @@ impl Channel {
         if self.channel_outbound {
                        return Err(HandleError{err: "Non-funding remote tried to update channel fee", msg: None});
         }
-               Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw).unwrap();
+               Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
                self.feerate_per_kw = msg.feerate_per_kw as u64;
                Ok(())
        }
@@ -1640,6 +1621,19 @@ impl Channel {
                if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
                        return Err(HandleError{err: "Cannot create commitment tx until channel is fully established", msg: None});
                }
+               if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
+                       return Err(HandleError{err: "Cannot create commitment tx until remote revokes their previous commitment", msg: None});
+               }
+               let mut have_updates = false; // TODO initialize with "have we sent a fee update?"
+               for htlc in self.pending_htlcs.iter() {
+                       if htlc.state == HTLCState::LocalAnnounced {
+                               have_updates = true;
+                       }
+                       if have_updates { break; }
+               }
+               if !have_updates {
+                       return Err(HandleError{err: "Cannot create commitment tx until we have some updates to send", msg: None});
+               }
 
                let funding_script = self.get_funding_redeemscript();
 
index 0dddfdac6760c473b5e0efabc864eb7dc062b550..ff6f924c61f5c18b3cd3bae946ce3f005c781ddf 100644 (file)
@@ -26,11 +26,10 @@ use crypto::digest::Digest;
 use crypto::symmetriccipher::SynchronousStreamCipher;
 use crypto::chacha20::ChaCha20;
 
-use std::sync::{Mutex,Arc};
+use std::sync::{Mutex,MutexGuard,Arc};
 use std::collections::HashMap;
 use std::collections::hash_map;
-use std::ptr;
-use std::mem;
+use std::{ptr, mem};
 use std::time::{Instant,Duration};
 
 /// Stores the info we will need to send when we want to forward an HTLC onwards
@@ -79,6 +78,7 @@ enum HTLCFailReason<'a> {
        },
        Reason {
                failure_code: u16,
+               data: &'a[u8],
        }
 }
 
@@ -572,6 +572,7 @@ impl ChannelManager {
 
        pub fn process_pending_htlc_forward(&self) {
                let mut new_events = Vec::new();
+               let mut failed_forwards = Vec::new();
                {
                        let mut channel_state_lock = self.channel_state.lock().unwrap();
                        let channel_state = channel_state_lock.borrow_parts();
@@ -585,6 +586,10 @@ impl ChannelManager {
                                        let forward_chan_id = match channel_state.short_to_id.get(&short_chan_id) {
                                                Some(chan_id) => chan_id.clone(),
                                                None => {
+                                                       failed_forwards.reserve(pending_forwards.len());
+                                                       for forward_info in pending_forwards {
+                                                               failed_forwards.push((forward_info.payment_hash, 0x4000 | 10, None));
+                                                       }
                                                        // TODO: Send a failure packet back on each pending_forward
                                                        continue;
                                                }
@@ -595,7 +600,8 @@ impl ChannelManager {
                                        for forward_info in pending_forwards {
                                                match forward_chan.send_htlc(forward_info.amt_to_forward, forward_info.payment_hash, forward_info.outgoing_cltv_value, forward_info.onion_packet.unwrap()) {
                                                        Err(_e) => {
-                                                               // TODO: Send a failure packet back
+                                                               let chan_update = self.get_channel_update(forward_chan).unwrap();
+                                                               failed_forwards.push((forward_info.payment_hash, 0x4000 | 7, Some(chan_update)));
                                                                continue;
                                                        },
                                                        Ok(update_add) => {
@@ -640,6 +646,13 @@ impl ChannelManager {
                        }
                }
 
+               for failed_forward in failed_forwards.drain(..) {
+                       match failed_forward.2 {
+                               None => self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), &failed_forward.0, HTLCFailReason::Reason { failure_code: failed_forward.1, data: &[0;0] }),
+                               Some(chan_update) => self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), &failed_forward.0, HTLCFailReason::Reason { failure_code: failed_forward.1, data: &chan_update.encode()[..] }),
+                       };
+               }
+
                if new_events.is_empty() { return }
 
                let mut events = self.pending_events.lock().unwrap();
@@ -651,11 +664,10 @@ impl ChannelManager {
 
        /// Indicates that the preimage for payment_hash is unknown after a PaymentReceived event.
        pub fn fail_htlc_backwards(&self, payment_hash: &[u8; 32]) -> bool {
-               self.fail_htlc_backwards_internal(payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 15 })
+               self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: &[0;0] })
        }
 
-       fn fail_htlc_backwards_internal(&self, payment_hash: &[u8; 32], onion_error: HTLCFailReason) -> bool {
-               let mut channel_state = self.channel_state.lock().unwrap();
+       fn fail_htlc_backwards_internal(&self, mut channel_state: MutexGuard<ChannelHolder>, payment_hash: &[u8; 32], onion_error: HTLCFailReason) -> bool {
                let mut pending_htlc = {
                        match channel_state.claimable_htlcs.remove(payment_hash) {
                                Some(pending_htlc) => pending_htlc,
@@ -674,6 +686,7 @@ impl ChannelManager {
                        PendingOutboundHTLC::CycledRoute { .. } => { panic!("WAT"); },
                        PendingOutboundHTLC::OutboundRoute { .. } => {
                                //TODO: DECRYPT route from OutboundRoute
+                               mem::drop(channel_state);
                                let mut pending_events = self.pending_events.lock().unwrap();
                                pending_events.push(events::Event::PaymentFailed {
                                        payment_hash: payment_hash.clone()
@@ -682,8 +695,8 @@ impl ChannelManager {
                        },
                        PendingOutboundHTLC::IntermediaryHopData { source_short_channel_id, incoming_packet_shared_secret } => {
                                let err_packet = match onion_error {
-                                       HTLCFailReason::Reason { failure_code } => {
-                                               let packet = ChannelManager::build_failure_packet(&incoming_packet_shared_secret, failure_code, &[0; 0]).encode();
+                                       HTLCFailReason::Reason { failure_code, data } => {
+                                               let packet = ChannelManager::build_failure_packet(&incoming_packet_shared_secret, failure_code, data).encode();
                                                ChannelManager::encrypt_failure_packet(&incoming_packet_shared_secret, &packet)
                                        },
                                        HTLCFailReason::ErrorPacket { err } => {
@@ -707,6 +720,7 @@ impl ChannelManager {
                                        }
                                };
 
+                               mem::drop(channel_state);
                                let mut pending_events = self.pending_events.lock().unwrap();
                                pending_events.push(events::Event::SendFailHTLC {
                                        node_id,
@@ -758,6 +772,7 @@ impl ChannelManager {
                                if from_user {
                                        panic!("Called claim_funds with a preimage for an outgoing payment. There is nothing we can do with this, and something is seriously wrong if you knew this...");
                                }
+                               mem::drop(channel_state);
                                let mut pending_events = self.pending_events.lock().unwrap();
                                pending_events.push(events::Event::PaymentSent {
                                        payment_preimage
@@ -781,6 +796,7 @@ impl ChannelManager {
                                        }
                                };
 
+                               mem::drop(channel_state);
                                let mut pending_events = self.pending_events.lock().unwrap();
                                pending_events.push(events::Event::SendFulfillHTLC {
                                        node_id: node_id,
@@ -985,6 +1001,33 @@ impl ChannelMessageHandler for ChannelManager {
 
                let associated_data = Vec::new(); //TODO: What to put here?
 
+               macro_rules! get_onion_hash {
+                       () => {
+                               {
+                                       let mut sha = Sha256::new();
+                                       sha.input(&msg.onion_routing_packet.hop_data);
+                                       let mut onion_hash = [0; 32];
+                                       sha.result(&mut onion_hash);
+                                       onion_hash
+                               }
+                       }
+               }
+
+               macro_rules! return_err {
+                       ($msg: expr, $err_code: expr, $data: expr) => {
+                               return Err(msgs::HandleError {
+                                       err: $msg,
+                                       msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
+                                               msg: msgs::UpdateFailHTLC {
+                                                       channel_id: msg.channel_id,
+                                                       htlc_id: msg.htlc_id,
+                                                       reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, $err_code, $data),
+                                               }
+                                       }),
+                               });
+                       }
+               }
+
                if msg.onion_routing_packet.version != 0 {
                        //TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
                        //sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
@@ -992,39 +1035,14 @@ impl ChannelMessageHandler for ChannelManager {
                        //receiving node would have to brute force to figure out which version was put in the
                        //packet by the node that send us the message, in the case of hashing the hop_data, the
                        //node knows the HMAC matched, so they already know what is there...
-                       let mut sha = Sha256::new();
-                       sha.input(&msg.onion_routing_packet.hop_data);
-                       let mut onion_hash = [0; 32];
-                       sha.result(&mut onion_hash);
-                       return Err(msgs::HandleError {
-                               err: "Unknown onion packet version",
-                               msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
-                                       msg: msgs::UpdateFailHTLC {
-                                               channel_id: msg.channel_id,
-                                               htlc_id: msg.htlc_id,
-                                               reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x8000 | 0x4000 | 4, &onion_hash),
-                                       }
-                               }),
-                       });
+                       return_err!("Unknown onion packet version", 0x8000 | 0x4000 | 4, &get_onion_hash!());
                }
 
                let mut hmac = Hmac::new(Sha256::new(), &mu);
                hmac.input(&msg.onion_routing_packet.hop_data);
                hmac.input(&associated_data[..]);
                if hmac.result() != MacResult::new(&msg.onion_routing_packet.hmac) {
-                       let mut sha = Sha256::new();
-                       sha.input(&msg.onion_routing_packet.hop_data);
-                       let mut onion_hash = [0; 32];
-                       sha.result(&mut onion_hash);
-                       return Err(HandleError{err: "HMAC Check failed",
-                               msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
-                                       msg: msgs::UpdateFailHTLC {
-                                               channel_id: msg.channel_id,
-                                               htlc_id: msg.htlc_id,
-                                               reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x8000 | 0x4000 | 5, &onion_hash),
-                                       }
-                               }),
-                       });
+                       return_err!("HMAC Check failed", 0x8000 | 0x4000 | 5, &get_onion_hash!());
                }
 
                let mut chacha = ChaCha20::new(&rho, &[0u8; 8]);
@@ -1037,15 +1055,7 @@ impl ChannelMessageHandler for ChannelManager {
                                                msgs::DecodeError::UnknownRealmByte => 0x4000 | 1,
                                                _ => 0x2000 | 2, // Should never happen
                                        };
-                                       return Err(HandleError{err: "Unable to decode our hop data",
-                                               msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
-                                                       msg: msgs::UpdateFailHTLC {
-                                                               channel_id: msg.channel_id,
-                                                               htlc_id: msg.htlc_id,
-                                                               reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, error_code, &[0;0]),
-                                                       }
-                                               }),
-                                       });
+                                       return_err!("Unable to decode our hop data", error_code, &[0;0]);
                                },
                                Ok(msg) => msg
                        }
@@ -1054,26 +1064,10 @@ impl ChannelMessageHandler for ChannelManager {
                let mut pending_forward_info = if next_hop_data.hmac == [0; 32] {
                                // OUR PAYMENT!
                                if next_hop_data.data.amt_to_forward != msg.amount_msat {
-                                       return Err(HandleError{err: "Upstream node sent less than we were supposed to receive in payment",
-                                               msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
-                                                       msg: msgs::UpdateFailHTLC {
-                                                               channel_id: msg.channel_id,
-                                                               htlc_id: msg.htlc_id,
-                                                               reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 19, &byte_utils::be64_to_array(msg.amount_msat)),
-                                                       }
-                                               }),
-                                       });
+                                       return_err!("Upstream node sent less than we were supposed to receive in payment", 19, &byte_utils::be64_to_array(msg.amount_msat));
                                }
                                if next_hop_data.data.outgoing_cltv_value != msg.cltv_expiry {
-                                       return Err(HandleError{err: "Upstream node set CLTV to the wrong value",
-                                               msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
-                                                       msg: msgs::UpdateFailHTLC {
-                                                               channel_id: msg.channel_id,
-                                                               htlc_id: msg.htlc_id,
-                                                               reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 18, &byte_utils::be32_to_array(msg.cltv_expiry)),
-                                                       }
-                                               }),
-                                       });
+                                       return_err!("Upstream node set CLTV to the wrong value", 18, &byte_utils::be32_to_array(msg.cltv_expiry));
                                }
 
                                // Note that we could obviously respond immediately with an update_fulfill_htlc
@@ -1106,15 +1100,7 @@ impl ChannelMessageHandler for ChannelManager {
                                                Err(_) => {
                                                        // Return temporary node failure as its technically our issue, not the
                                                        // channel's issue.
-                                                       return Err(HandleError{err: "Blinding factor is an invalid private key",
-                                                               msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
-                                                                       msg: msgs::UpdateFailHTLC {
-                                                                               channel_id: msg.channel_id,
-                                                                               htlc_id: msg.htlc_id,
-                                                                               reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x2000 | 2, &[0;0]),
-                                                                       }
-                                                               }),
-                                                       });
+                                                       return_err!("Blinding factor is an invalid private key", 0x2000 | 2, &[0;0]);
                                                },
                                                Ok(key) => key
                                        }
@@ -1124,15 +1110,7 @@ impl ChannelMessageHandler for ChannelManager {
                                        Err(_) => {
                                                // Return temporary node failure as its technically our issue, not the
                                                // channel's issue.
-                                               return Err(HandleError{err: "New blinding factor is an invalid private key",
-                                                       msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
-                                                               msg: msgs::UpdateFailHTLC {
-                                                                       channel_id: msg.channel_id,
-                                                                       htlc_id: msg.htlc_id,
-                                                                       reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x2000 | 2, &[0;0]),
-                                                               }
-                                                       }),
-                                               });
+                                               return_err!("New blinding factor is an invalid private key", 0x2000 | 2, &[0;0]);
                                        },
                                        Ok(_) => {}
                                };
@@ -1162,30 +1140,14 @@ impl ChannelMessageHandler for ChannelManager {
                if pending_forward_info.onion_packet.is_some() { // If short_channel_id is 0 here, we'll reject them in the body here
                        let forwarding_id = match channel_state.short_to_id.get(&pending_forward_info.short_channel_id) {
                                None => {
-                                       return Err(HandleError{err: "Don't have available channel for forwarding as requested.",
-                                               msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
-                                                       msg: msgs::UpdateFailHTLC {
-                                                               channel_id: msg.channel_id,
-                                                               htlc_id: msg.htlc_id,
-                                                               reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x4000 | 10, &[0;0]),
-                                                       }
-                                               }),
-                                       });
+                                       return_err!("Don't have available channel for forwarding as requested.", 0x4000 | 10, &[0;0]);
                                },
                                Some(id) => id.clone(),
                        };
                        let chan = channel_state.by_id.get_mut(&forwarding_id).unwrap();
                        if !chan.is_live() {
                                let chan_update = self.get_channel_update(chan).unwrap();
-                               return Err(HandleError{err: "Forwarding channel is not in a ready state.",
-                                       msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
-                                               msg: msgs::UpdateFailHTLC {
-                                                       channel_id: msg.channel_id,
-                                                       htlc_id: msg.htlc_id,
-                                                       reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x4000 | 10, &chan_update.encode()[..]),
-                                               }
-                                       }),
-                               });
+                               return_err!("Forwarding channel is not in a ready state.", 0x4000 | 10, &chan_update.encode()[..]);
                        }
                }
 
@@ -1205,15 +1167,7 @@ impl ChannelMessageHandler for ChannelManager {
                                        _ => {},
                                }
                                if !acceptable_cycle {
-                                       return Err(HandleError{err: "Payment looped through us twice",
-                                               msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
-                                                       msg: msgs::UpdateFailHTLC {
-                                                               channel_id: msg.channel_id,
-                                                               htlc_id: msg.htlc_id,
-                                                               reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x4000 | 0x2000|2, &[0;0]),
-                                                       }
-                                               }),
-                                       });
+                                       return_err!("Payment looped through us twice", 0x4000 | 0x2000 | 2, &[0;0]);
                                }
                        },
                        _ => {},
@@ -1236,7 +1190,7 @@ impl ChannelMessageHandler for ChannelManager {
 
                match claimable_htlcs_entry {
                        hash_map::Entry::Occupied(mut e) => {
-                               let mut outbound_route = e.get_mut();
+                               let outbound_route = e.get_mut();
                                let route = match outbound_route {
                                        &mut PendingOutboundHTLC::OutboundRoute { ref route } => {
                                                route.clone()
@@ -1260,56 +1214,52 @@ impl ChannelMessageHandler for ChannelManager {
                Ok(res)
        }
 
-       fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>, HandleError> {
-               let res = {
+       fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<(), HandleError> {
+               {
                        let mut channel_state = self.channel_state.lock().unwrap();
                        match channel_state.by_id.get_mut(&msg.channel_id) {
                                Some(chan) => {
                                        if chan.get_their_node_id() != *their_node_id {
                                                return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
                                        }
-                                       chan.update_fulfill_htlc(&msg)
+                                       chan.update_fulfill_htlc(&msg)?;
                                },
                                None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
                        }
-               };
+               }
                //TODO: Delay the claimed_funds relaying just like we do outbound relay!
                self.claim_funds_internal(msg.payment_preimage.clone(), false);
-               res
+               Ok(())
        }
 
-       fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>, HandleError> {
-               let res = {
-                       let mut channel_state = self.channel_state.lock().unwrap();
-                       match channel_state.by_id.get_mut(&msg.channel_id) {
-                               Some(chan) => {
-                                       if chan.get_their_node_id() != *their_node_id {
-                                               return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
-                                       }
-                                       chan.update_fail_htlc(&msg)?
-                               },
-                               None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
-                       }
+       fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<(), HandleError> {
+               let mut channel_state = self.channel_state.lock().unwrap();
+               let payment_hash = match channel_state.by_id.get_mut(&msg.channel_id) {
+                       Some(chan) => {
+                               if chan.get_their_node_id() != *their_node_id {
+                                       return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
+                               }
+                               chan.update_fail_htlc(&msg)?
+                       },
+                       None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
                };
-               self.fail_htlc_backwards_internal(&res.0, HTLCFailReason::ErrorPacket { err: &msg.reason });
-               Ok(res.1)
+               self.fail_htlc_backwards_internal(channel_state, &payment_hash, HTLCFailReason::ErrorPacket { err: &msg.reason });
+               Ok(())
        }
 
-       fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>, HandleError> {
-               let res = {
-                       let mut channel_state = self.channel_state.lock().unwrap();
-                       match channel_state.by_id.get_mut(&msg.channel_id) {
-                               Some(chan) => {
-                                       if chan.get_their_node_id() != *their_node_id {
-                                               return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
-                                       }
-                                       chan.update_fail_malformed_htlc(&msg)?
-                               },
-                               None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
-                       }
+       fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), HandleError> {
+               let mut channel_state = self.channel_state.lock().unwrap();
+               let payment_hash = match channel_state.by_id.get_mut(&msg.channel_id) {
+                       Some(chan) => {
+                               if chan.get_their_node_id() != *their_node_id {
+                                       return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
+                               }
+                               chan.update_fail_malformed_htlc(&msg)?
+                       },
+                       None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
                };
-               self.fail_htlc_backwards_internal(&res.0, HTLCFailReason::Reason { failure_code: msg.failure_code });
-               Ok(res.1)
+               self.fail_htlc_backwards_internal(channel_state, &payment_hash, HTLCFailReason::Reason { failure_code: msg.failure_code, data: &[0;0] });
+               Ok(())
        }
 
        fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &msgs::CommitmentSigned) -> Result<msgs::RevokeAndACK, HandleError> {
@@ -1360,22 +1310,21 @@ impl ChannelMessageHandler for ChannelManager {
                Ok(res)
        }
 
-       fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), HandleError> {
-               let monitor = {
+       fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>, HandleError> {
+               let (res, monitor) = {
                        let mut channel_state = self.channel_state.lock().unwrap();
                        match channel_state.by_id.get_mut(&msg.channel_id) {
                                Some(chan) => {
                                        if chan.get_their_node_id() != *their_node_id {
                                                return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
                                        }
-                                       chan.revoke_and_ack(&msg)?;
-                                       chan.channel_monitor()
+                                       (chan.revoke_and_ack(&msg)?, chan.channel_monitor())
                                },
                                None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
                        }
                };
                self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor)?;
-               Ok(())
+               Ok(res)
        }
 
        fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), HandleError> {
@@ -1622,7 +1571,7 @@ mod tests {
        }
 
        fn create_chan_between_nodes(node_a: &ChannelManager, chain_a: &chaininterface::ChainWatchInterfaceUtil, node_b: &ChannelManager, chain_b: &chaininterface::ChainWatchInterfaceUtil) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate) {
-               let open_chan = node_a.create_channel(node_b.get_our_node_id(), (1 << 24) - 1, 42).unwrap();
+               let open_chan = node_a.create_channel(node_b.get_our_node_id(), 100000, 42).unwrap();
                let accept_chan = node_b.handle_open_channel(&node_a.get_our_node_id(), &open_chan).unwrap();
                node_a.handle_accept_channel(&node_b.get_our_node_id(), &accept_chan).unwrap();
 
@@ -1634,7 +1583,7 @@ mod tests {
                assert_eq!(events_1.len(), 1);
                match events_1[0] {
                        Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, output_script: _, user_channel_id } => {
-                               assert_eq!(*channel_value_satoshis, (1 << 24) - 1);
+                               assert_eq!(*channel_value_satoshis, 100000);
                                assert_eq!(user_channel_id, 42);
 
                                node_a.funding_transaction_generated(&temporary_channel_id, funding_output.clone());
@@ -1726,7 +1675,7 @@ mod tests {
        impl SendEvent {
                fn from_event(event: Event) -> SendEvent {
                        match event {
-                               Event::SendHTLCs{ node_id, msgs, commitment_msg } => {
+                               Event::SendHTLCs { node_id, msgs, commitment_msg } => {
                                        SendEvent { node_id: node_id, msgs: msgs, commitment_msg: commitment_msg }
                                },
                                _ => panic!("Unexpected event type!"),
@@ -1761,7 +1710,7 @@ mod tests {
 
                        node.handle_update_add_htlc(&prev_node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
                        let revoke_and_ack = node.handle_commitment_signed(&prev_node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
-                       prev_node.handle_revoke_and_ack(&node.get_our_node_id(), &revoke_and_ack).unwrap();
+                       assert!(prev_node.handle_revoke_and_ack(&node.get_our_node_id(), &revoke_and_ack).unwrap().is_none());
 
                        let events_1 = node.get_and_clear_pending_events();
                        assert_eq!(events_1.len(), 1);
@@ -1796,15 +1745,7 @@ mod tests {
                (our_payment_preimage, our_payment_hash)
        }
 
-       fn send_payment(origin_node: &ChannelManager, origin_router: &Router, expected_route: &[&ChannelManager], recv_value: u64) {
-               let route = origin_router.get_route(&expected_route.last().unwrap().get_our_node_id(), &Vec::new(), recv_value, 142).unwrap();
-               assert_eq!(route.hops.len(), expected_route.len());
-               for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
-                       assert_eq!(hop.pubkey, node.get_our_node_id());
-               }
-
-               let our_payment_preimage = send_along_route(origin_node, route, expected_route, recv_value).0;
-
+       fn claim_payment(origin_node: &ChannelManager, expected_route: &[&ChannelManager], our_payment_preimage: [u8; 32]) {
                assert!(expected_route.last().unwrap().claim_funds(our_payment_preimage));
 
                let mut expected_next_node = expected_route.last().unwrap().get_our_node_id();
@@ -1814,7 +1755,7 @@ mod tests {
                        assert_eq!(expected_next_node, node.get_our_node_id());
                        match next_msg {
                                Some(msg) => {
-                                       assert!(node.handle_update_fulfill_htlc(&prev_node.get_our_node_id(), &msg).unwrap().is_none());
+                                       node.handle_update_fulfill_htlc(&prev_node.get_our_node_id(), &msg).unwrap();
                                }, None => {}
                        }
 
@@ -1832,7 +1773,7 @@ mod tests {
                }
 
                assert_eq!(expected_next_node, origin_node.get_our_node_id());
-               assert!(origin_node.handle_update_fulfill_htlc(&expected_route.first().unwrap().get_our_node_id(), &next_msg.unwrap()).unwrap().is_none());
+               origin_node.handle_update_fulfill_htlc(&expected_route.first().unwrap().get_our_node_id(), &next_msg.unwrap()).unwrap();
 
                let events = origin_node.get_and_clear_pending_events();
                assert_eq!(events.len(), 1);
@@ -1844,6 +1785,42 @@ mod tests {
                }
        }
 
+       fn route_payment(origin_node: &ChannelManager, origin_router: &Router, expected_route: &[&ChannelManager], recv_value: u64) -> ([u8; 32], [u8; 32]) {
+               let route = origin_router.get_route(&expected_route.last().unwrap().get_our_node_id(), &Vec::new(), recv_value, 142).unwrap();
+               assert_eq!(route.hops.len(), expected_route.len());
+               for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
+                       assert_eq!(hop.pubkey, node.get_our_node_id());
+               }
+
+               send_along_route(origin_node, route, expected_route, recv_value)
+       }
+
+       fn route_over_limit(origin_node: &ChannelManager, origin_router: &Router, expected_route: &[&ChannelManager], recv_value: u64) {
+               let route = origin_router.get_route(&expected_route.last().unwrap().get_our_node_id(), &Vec::new(), recv_value, 142).unwrap();
+               assert_eq!(route.hops.len(), expected_route.len());
+               for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
+                       assert_eq!(hop.pubkey, node.get_our_node_id());
+               }
+
+               let our_payment_preimage = unsafe { [PAYMENT_COUNT; 32] };
+               unsafe { PAYMENT_COUNT += 1 };
+               let our_payment_hash = {
+                       let mut sha = Sha256::new();
+                       sha.input(&our_payment_preimage[..]);
+                       let mut ret = [0; 32];
+                       sha.result(&mut ret);
+                       ret
+               };
+
+               let err = origin_node.send_payment(route, our_payment_hash).err().unwrap();
+               assert_eq!(err.err, "Cannot send value that would put us over our max HTLC value in flight");
+       }
+
+       fn send_payment(origin_node: &ChannelManager, origin_router: &Router, expected_route: &[&ChannelManager], recv_value: u64) {
+               let our_payment_preimage = route_payment(origin_node, origin_router, expected_route, recv_value).0;
+               claim_payment(origin_node, expected_route, our_payment_preimage);
+       }
+
        fn send_failed_payment(origin_node: &ChannelManager, origin_router: &Router, expected_route: &[&ChannelManager]) {
                let route = origin_router.get_route(&expected_route.last().unwrap().get_our_node_id(), &Vec::new(), 1000000, 142).unwrap();
                assert_eq!(route.hops.len(), expected_route.len());
@@ -1861,7 +1838,7 @@ mod tests {
                        assert_eq!(expected_next_node, node.get_our_node_id());
                        match next_msg {
                                Some(msg) => {
-                                       assert!(node.handle_update_fail_htlc(&prev_node.get_our_node_id(), &msg).unwrap().is_none());
+                                       node.handle_update_fail_htlc(&prev_node.get_our_node_id(), &msg).unwrap();
                                }, None => {}
                        }
 
@@ -1879,7 +1856,7 @@ mod tests {
                }
 
                assert_eq!(expected_next_node, origin_node.get_our_node_id());
-               assert!(origin_node.handle_update_fail_htlc(&expected_route.first().unwrap().get_our_node_id(), &next_msg.unwrap()).unwrap().is_none());
+               origin_node.handle_update_fail_htlc(&expected_route.first().unwrap().get_our_node_id(), &next_msg.unwrap()).unwrap();
 
                let events = origin_node.get_and_clear_pending_events();
                assert_eq!(events.len(), 1);
@@ -1962,11 +1939,17 @@ mod tests {
                        router.handle_channel_update(&chan_announcement_3.2).unwrap();
                }
 
-               // Send some payments
-               send_payment(&node_1, &router_1, &vec!(&*node_2, &*node_3, &*node_4)[..], 1000000);
+               // Rebalance the network a bit by relaying one payment through all the channels...
+               send_payment(&node_1, &router_1, &vec!(&*node_2, &*node_3, &*node_4)[..], 8000000);
+               send_payment(&node_1, &router_1, &vec!(&*node_2, &*node_3, &*node_4)[..], 8000000);
+               send_payment(&node_1, &router_1, &vec!(&*node_2, &*node_3, &*node_4)[..], 8000000);
+               send_payment(&node_1, &router_1, &vec!(&*node_2, &*node_3, &*node_4)[..], 8000000);
+               send_payment(&node_1, &router_1, &vec!(&*node_2, &*node_3, &*node_4)[..], 8000000);
+
+               // Send some more payments
                send_payment(&node_2, &router_2, &vec!(&*node_3, &*node_4)[..], 1000000);
                send_payment(&node_4, &router_4, &vec!(&*node_3, &*node_2, &*node_1)[..], 1000000);
-               send_payment(&node_4, &router_4, &vec!(&*node_3, &*node_2)[..], 250000);
+               send_payment(&node_4, &router_4, &vec!(&*node_3, &*node_2)[..], 1000000);
 
                // Test failure packets
                send_failed_payment(&node_1, &router_1, &vec!(&*node_2, &*node_3, &*node_4)[..]);
@@ -1980,8 +1963,14 @@ mod tests {
                }
 
                send_payment(&node_1, &router_1, &vec!(&*node_2, &*node_4)[..], 1000000);
-
-               // Rebalance a bit
+               send_payment(&node_3, &router_3, &vec!(&*node_4)[..], 1000000);
+               send_payment(&node_2, &router_2, &vec!(&*node_4)[..], 8000000);
+               send_payment(&node_2, &router_2, &vec!(&*node_4)[..], 8000000);
+               send_payment(&node_2, &router_2, &vec!(&*node_4)[..], 8000000);
+               send_payment(&node_2, &router_2, &vec!(&*node_4)[..], 8000000);
+               send_payment(&node_2, &router_2, &vec!(&*node_4)[..], 8000000);
+
+               // Do some rebalance loop payments, simultaneously
                let mut hops = Vec::with_capacity(3);
                hops.push(RouteHop {
                        pubkey: node_3.get_our_node_id(),
@@ -1998,12 +1987,60 @@ mod tests {
                hops.push(RouteHop {
                        pubkey: node_2.get_our_node_id(),
                        short_channel_id: chan_announcement_4.1.contents.short_channel_id,
-                       fee_msat: 250000,
+                       fee_msat: 1000000,
                        cltv_expiry_delta: 142,
                });
                hops[1].fee_msat = chan_announcement_4.2.contents.fee_base_msat as u64 + chan_announcement_4.2.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
                hops[0].fee_msat = chan_announcement_3.1.contents.fee_base_msat as u64 + chan_announcement_3.1.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
-               send_along_route(&node_2, Route { hops }, &vec!(&*node_3, &*node_4, &*node_2)[..], 250000);
+               let payment_preimage_1 = send_along_route(&node_2, Route { hops }, &vec!(&*node_3, &*node_4, &*node_2)[..], 1000000).0;
+
+               let mut hops = Vec::with_capacity(3);
+               hops.push(RouteHop {
+                       pubkey: node_4.get_our_node_id(),
+                       short_channel_id: chan_announcement_4.1.contents.short_channel_id,
+                       fee_msat: 0,
+                       cltv_expiry_delta: chan_announcement_3.2.contents.cltv_expiry_delta as u32
+               });
+               hops.push(RouteHop {
+                       pubkey: node_3.get_our_node_id(),
+                       short_channel_id: chan_announcement_3.1.contents.short_channel_id,
+                       fee_msat: 0,
+                       cltv_expiry_delta: chan_announcement_2.2.contents.cltv_expiry_delta as u32
+               });
+               hops.push(RouteHop {
+                       pubkey: node_2.get_our_node_id(),
+                       short_channel_id: chan_announcement_2.1.contents.short_channel_id,
+                       fee_msat: 1000000,
+                       cltv_expiry_delta: 142,
+               });
+               hops[1].fee_msat = chan_announcement_2.2.contents.fee_base_msat as u64 + chan_announcement_2.2.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
+               hops[0].fee_msat = chan_announcement_3.2.contents.fee_base_msat as u64 + chan_announcement_3.2.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
+               let payment_preimage_2 = send_along_route(&node_2, Route { hops }, &vec!(&*node_4, &*node_3, &*node_2)[..], 1000000).0;
+
+               // Claim the rebalances...
+               claim_payment(&node_2, &vec!(&*node_4, &*node_3, &*node_2)[..], payment_preimage_2);
+               claim_payment(&node_2, &vec!(&*node_3, &*node_4, &*node_2)[..], payment_preimage_1);
+
+               // Add a duplicate new channel from 2 to 4
+               let chan_announcement_5 = create_chan_between_nodes(&node_2, &chain_monitor_2, &node_4, &chain_monitor_4);
+               for router in vec!(&router_1, &router_2, &router_3, &router_4) {
+                       assert!(router.handle_channel_announcement(&chan_announcement_5.0).unwrap());
+                       router.handle_channel_update(&chan_announcement_5.1).unwrap();
+                       router.handle_channel_update(&chan_announcement_5.2).unwrap();
+               }
+
+               // Send some payments across both channels
+               let payment_preimage_3 = route_payment(&node_1, &router_1, &vec!(&*node_2, &*node_4)[..], 3000000).0;
+               let payment_preimage_4 = route_payment(&node_1, &router_1, &vec!(&*node_2, &*node_4)[..], 3000000).0;
+               let payment_preimage_5 = route_payment(&node_1, &router_1, &vec!(&*node_2, &*node_4)[..], 3000000).0;
+
+               route_over_limit(&node_1, &router_1, &vec!(&*node_2, &*node_4)[..], 3000000);
+
+               //TODO: Test that routes work again here as we've been notified that the channel is full
+
+               claim_payment(&node_1, &vec!(&*node_2, &*node_4)[..], payment_preimage_3);
+               claim_payment(&node_1, &vec!(&*node_2, &*node_4)[..], payment_preimage_4);
+               claim_payment(&node_1, &vec!(&*node_2, &*node_4)[..], payment_preimage_5);
 
                // Check that we processed all pending events
                for node in vec!(&node_1, &node_2, &node_3, &node_4) {
index 97ed1e320f9350b4ac69fc04ca7a8ce49abfbb5b..6bb900967566149f6ae5264dc336cfc62ee585d3 100644 (file)
@@ -13,6 +13,8 @@ use util::{byte_utils, internal_traits, events};
 
 pub trait MsgEncodable {
        fn encode(&self) -> Vec<u8>;
+       #[inline]
+       fn encoded_len(&self) -> usize { self.encode().len() }
 }
 #[derive(Debug)]
 pub enum DecodeError {
@@ -24,6 +26,8 @@ pub enum DecodeError {
        BadSignature,
        /// Buffer not of right length (either too short or too long)
        WrongLength,
+       /// node_announcement included more than one address of a given type!
+       ExtraAddressesPerType,
 }
 pub trait MsgDecodable: Sized {
        fn decode(v: &[u8]) -> Result<Self, DecodeError>;
@@ -256,7 +260,6 @@ pub struct AnnouncementSignatures {
 
 #[derive(Clone)]
 pub enum NetAddress {
-       Dummy,
        IPv4 {
                addr: [u8; 4],
                port: u16,
@@ -273,9 +276,19 @@ pub enum NetAddress {
                ed25519_pubkey: [u8; 32],
                checksum: u16,
                version: u8,
-               //TODO: Do we need a port number here???
+               port: u16,
        },
 }
+impl NetAddress {
+       fn get_id(&self) -> u8 {
+               match self {
+                       &NetAddress::IPv4 {..} => { 1 },
+                       &NetAddress::IPv6 {..} => { 2 },
+                       &NetAddress::OnionV2 {..} => { 3 },
+                       &NetAddress::OnionV3 {..} => { 4 },
+               }
+       }
+}
 
 pub struct UnsignedNodeAnnouncement {
        pub features: GlobalFeatures,
@@ -283,6 +296,8 @@ pub struct UnsignedNodeAnnouncement {
        pub node_id: PublicKey,
        pub rgb: [u8; 3],
        pub alias: [u8; 32],
+       /// List of addresses on which this node is reachable. Note that you may only have up to one
+       /// address of each type, if you have more, they may be silently discarded or we may panic!
        pub addresses: Vec<NetAddress>,
 }
 pub struct NodeAnnouncement {
@@ -356,11 +371,11 @@ pub trait ChannelMessageHandler : events::EventsProvider {
 
        // HTLC handling:
        fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC) -> Result<(), HandleError>;
-       fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<Option<(Vec<UpdateAddHTLC>, CommitmentSigned)>, HandleError>;
-       fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<Option<(Vec<UpdateAddHTLC>, CommitmentSigned)>, HandleError>;
-       fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<Option<(Vec<UpdateAddHTLC>, CommitmentSigned)>, HandleError>;
+       fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), HandleError>;
+       fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<(), HandleError>;
+       fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), HandleError>;
        fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<RevokeAndACK, HandleError>;
-       fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<(), HandleError>;
+       fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<Option<(Vec<UpdateAddHTLC>, CommitmentSigned)>, HandleError>;
 
        fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), HandleError>;
 
@@ -418,6 +433,7 @@ impl Error for DecodeError {
                        DecodeError::BadPublicKey => "Invalid public key in packet",
                        DecodeError::BadSignature => "Invalid signature in packet",
                        DecodeError::WrongLength => "Data was wrong length for packet",
+                       DecodeError::ExtraAddressesPerType => "More than one address of a single type",
                }
        }
 }
@@ -470,6 +486,7 @@ impl MsgEncodable for LocalFeatures {
                res.extend_from_slice(&self.flags[..]);
                res
        }
+       fn encoded_len(&self) -> usize { self.flags.len() + 2 }
 }
 
 impl MsgDecodable for GlobalFeatures {
@@ -491,6 +508,7 @@ impl MsgEncodable for GlobalFeatures {
                res.extend_from_slice(&self.flags[..]);
                res
        }
+       fn encoded_len(&self) -> usize { self.flags.len() + 2 }
 }
 
 impl MsgDecodable for Init {
@@ -662,8 +680,18 @@ impl MsgEncodable for FundingLocked {
 }
 
 impl MsgDecodable for Shutdown {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32 + 2 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let scriptlen = byte_utils::slice_to_be16(&v[32..34]) as usize;
+               if v.len() < 32 + 2 + scriptlen {
+                       return Err(DecodeError::WrongLength);
+               }
+               Ok(Self {
+                       channel_id: deserialize(&v[0..32]).unwrap(),
+                       scriptpubkey: Script::from(v[34..34 + scriptlen].to_vec()),
+               })
        }
 }
 impl MsgEncodable for Shutdown {
@@ -673,8 +701,16 @@ impl MsgEncodable for Shutdown {
 }
 
 impl MsgDecodable for ClosingSigned {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32 + 8 + 64 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let secp_ctx = Secp256k1::without_caps();
+               Ok(Self {
+                       channel_id: deserialize(&v[0..32]).unwrap(),
+                       fee_satoshis: byte_utils::slice_to_be64(&v[32..40]),
+                       signature: secp_signature!(&secp_ctx, &v[40..104]),
+               })
        }
 }
 impl MsgEncodable for ClosingSigned {
@@ -842,8 +878,17 @@ impl MsgEncodable for ChannelReestablish {
 }
 
 impl MsgDecodable for AnnouncementSignatures {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32+8+64*2 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let secp_ctx = Secp256k1::without_caps();
+               Ok(Self {
+                       channel_id: deserialize(&v[0..32]).unwrap(),
+                       short_channel_id: byte_utils::slice_to_be64(&v[32..40]),
+                       node_signature: secp_signature!(&secp_ctx, &v[40..104]),
+                       bitcoin_signature: secp_signature!(&secp_ctx, &v[104..168]),
+               })
        }
 }
 impl MsgEncodable for AnnouncementSignatures {
@@ -853,8 +898,105 @@ impl MsgEncodable for AnnouncementSignatures {
 }
 
 impl MsgDecodable for UnsignedNodeAnnouncement {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               let features = GlobalFeatures::decode(&v[..])?;
+               if v.len() < features.encoded_len() + 4 + 33 + 3 + 32 + 2 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let start = features.encoded_len();
+
+               let mut rgb = [0; 3];
+               rgb.copy_from_slice(&v[start + 37..start + 40]);
+
+               let mut alias = [0; 32];
+               alias.copy_from_slice(&v[start + 40..start + 72]);
+
+               let addrlen = byte_utils::slice_to_be16(&v[start + 72..start + 74]) as usize;
+               if v.len() < start + 74 + addrlen {
+                       return Err(DecodeError::WrongLength);
+               }
+
+               let mut addresses = Vec::with_capacity(4);
+               let mut read_pos = start + 74;
+               loop {
+                       if v.len() <= read_pos { break; }
+                       match v[read_pos] {
+                               0 => { read_pos += 1; },
+                               1 => {
+                                       if v.len() < read_pos + 1 + 6 {
+                                               return Err(DecodeError::WrongLength);
+                                       }
+                                       if addresses.len() > 0 {
+                                               return Err(DecodeError::ExtraAddressesPerType);
+                                       }
+                                       let mut addr = [0; 4];
+                                       addr.copy_from_slice(&v[read_pos + 1..read_pos + 5]);
+                                       addresses.push(NetAddress::IPv4 {
+                                               addr,
+                                               port: byte_utils::slice_to_be16(&v[read_pos + 5..read_pos + 7]),
+                                       });
+                                       read_pos += 1 + 6;
+                               },
+                               2 => {
+                                       if v.len() < read_pos + 1 + 18 {
+                                               return Err(DecodeError::WrongLength);
+                                       }
+                                       if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
+                                               return Err(DecodeError::ExtraAddressesPerType);
+                                       }
+                                       let mut addr = [0; 16];
+                                       addr.copy_from_slice(&v[read_pos + 1..read_pos + 17]);
+                                       addresses.push(NetAddress::IPv6 {
+                                               addr,
+                                               port: byte_utils::slice_to_be16(&v[read_pos + 17..read_pos + 19]),
+                                       });
+                                       read_pos += 1 + 18;
+                               },
+                               3 => {
+                                       if v.len() < read_pos + 1 + 12 {
+                                               return Err(DecodeError::WrongLength);
+                                       }
+                                       if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
+                                               return Err(DecodeError::ExtraAddressesPerType);
+                                       }
+                                       let mut addr = [0; 10];
+                                       addr.copy_from_slice(&v[read_pos + 1..read_pos + 11]);
+                                       addresses.push(NetAddress::OnionV2 {
+                                               addr,
+                                               port: byte_utils::slice_to_be16(&v[read_pos + 11..read_pos + 13]),
+                                       });
+                                       read_pos += 1 + 12;
+                               },
+                               4 => {
+                                       if v.len() < read_pos + 1 + 37 {
+                                               return Err(DecodeError::WrongLength);
+                                       }
+                                       if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
+                                               return Err(DecodeError::ExtraAddressesPerType);
+                                       }
+                                       let mut ed25519_pubkey = [0; 32];
+                                       ed25519_pubkey.copy_from_slice(&v[read_pos + 1..read_pos + 33]);
+                                       addresses.push(NetAddress::OnionV3 {
+                                               ed25519_pubkey,
+                                               checksum: byte_utils::slice_to_be16(&v[read_pos + 33..read_pos + 35]),
+                                               version: v[read_pos + 35],
+                                               port: byte_utils::slice_to_be16(&v[read_pos + 36..read_pos + 38]),
+                                       });
+                                       read_pos += 1 + 37;
+                               },
+                               _ => { break; } // We've read all we can, we dont understand anything higher (and they're sorted)
+                       }
+               }
+
+               let secp_ctx = Secp256k1::without_caps();
+               Ok(Self {
+                       features,
+                       timestamp: byte_utils::slice_to_be32(&v[start..start + 4]),
+                       node_id: secp_pubkey!(&secp_ctx, &v[start + 4..start + 37]),
+                       rgb,
+                       alias,
+                       addresses,
+               })
        }
 }
 impl MsgEncodable for UnsignedNodeAnnouncement {
@@ -867,25 +1009,32 @@ impl MsgEncodable for UnsignedNodeAnnouncement {
                res.extend_from_slice(&self.rgb);
                res.extend_from_slice(&self.alias);
                let mut addr_slice = Vec::with_capacity(self.addresses.len() * 18);
-               for addr in self.addresses.iter() {
+               let mut addrs_to_encode = self.addresses.clone();
+               addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
+               addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
+               for addr in addrs_to_encode.iter() {
                        match addr {
-                               &NetAddress::Dummy => {},
                                &NetAddress::IPv4{addr, port} => {
+                                       addr_slice.push(1);
                                        addr_slice.extend_from_slice(&addr);
                                        addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
                                },
                                &NetAddress::IPv6{addr, port} => {
+                                       addr_slice.push(2);
                                        addr_slice.extend_from_slice(&addr);
                                        addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
                                },
                                &NetAddress::OnionV2{addr, port} => {
+                                       addr_slice.push(3);
                                        addr_slice.extend_from_slice(&addr);
                                        addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
                                },
-                               &NetAddress::OnionV3{ed25519_pubkey, checksum, version} => {
+                               &NetAddress::OnionV3{ed25519_pubkey, checksum, version, port} => {
+                                       addr_slice.push(4);
                                        addr_slice.extend_from_slice(&ed25519_pubkey);
                                        addr_slice.extend_from_slice(&byte_utils::be16_to_array(checksum));
                                        addr_slice.push(version);
+                                       addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
                                },
                        }
                }
@@ -896,8 +1045,15 @@ impl MsgEncodable for UnsignedNodeAnnouncement {
 }
 
 impl MsgDecodable for NodeAnnouncement {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 64 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let secp_ctx = Secp256k1::without_caps();
+               Ok(Self {
+                       signature: secp_signature!(&secp_ctx, &v[0..64]),
+                       contents: UnsignedNodeAnnouncement::decode(&v[64..])?,
+               })
        }
 }
 impl MsgEncodable for NodeAnnouncement {
@@ -907,8 +1063,22 @@ impl MsgEncodable for NodeAnnouncement {
 }
 
 impl MsgDecodable for UnsignedChannelAnnouncement {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               let features = GlobalFeatures::decode(&v[..])?;
+               if v.len() < features.encoded_len() + 32 + 8 + 33*4 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let start = features.encoded_len();
+               let secp_ctx = Secp256k1::without_caps();
+               Ok(Self {
+                       features,
+                       chain_hash: deserialize(&v[start..start + 32]).unwrap(),
+                       short_channel_id: byte_utils::slice_to_be64(&v[start + 32..start + 40]),
+                       node_id_1: secp_pubkey!(&secp_ctx, &v[start + 40..start + 73]),
+                       node_id_2: secp_pubkey!(&secp_ctx, &v[start + 73..start + 106]),
+                       bitcoin_key_1: secp_pubkey!(&secp_ctx, &v[start + 106..start + 139]),
+                       bitcoin_key_2: secp_pubkey!(&secp_ctx, &v[start + 139..start + 172]),
+               })
        }
 }
 impl MsgEncodable for UnsignedChannelAnnouncement {
@@ -927,8 +1097,18 @@ impl MsgEncodable for UnsignedChannelAnnouncement {
 }
 
 impl MsgDecodable for ChannelAnnouncement {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 64*4 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let secp_ctx = Secp256k1::without_caps();
+               Ok(Self {
+                       node_signature_1: secp_signature!(&secp_ctx, &v[0..64]),
+                       node_signature_2: secp_signature!(&secp_ctx, &v[64..128]),
+                       bitcoin_signature_1: secp_signature!(&secp_ctx, &v[128..192]),
+                       bitcoin_signature_2: secp_signature!(&secp_ctx, &v[192..256]),
+                       contents: UnsignedChannelAnnouncement::decode(&v[256..])?,
+               })
        }
 }
 impl MsgEncodable for ChannelAnnouncement {
@@ -938,8 +1118,20 @@ impl MsgEncodable for ChannelAnnouncement {
 }
 
 impl MsgDecodable for UnsignedChannelUpdate {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32+8+4+2+2+8+4+4 {
+                       return Err(DecodeError::WrongLength);
+               }
+               Ok(Self {
+                       chain_hash: deserialize(&v[0..32]).unwrap(),
+                       short_channel_id: byte_utils::slice_to_be64(&v[32..40]),
+                       timestamp: byte_utils::slice_to_be32(&v[40..44]),
+                       flags: byte_utils::slice_to_be16(&v[44..46]),
+                       cltv_expiry_delta: byte_utils::slice_to_be16(&v[46..48]),
+                       htlc_minimum_msat: byte_utils::slice_to_be64(&v[48..56]),
+                       fee_base_msat: byte_utils::slice_to_be32(&v[56..60]),
+                       fee_proportional_millionths: byte_utils::slice_to_be32(&v[60..64]),
+               })
        }
 }
 impl MsgEncodable for UnsignedChannelUpdate {
@@ -958,15 +1150,21 @@ impl MsgEncodable for UnsignedChannelUpdate {
 }
 
 impl MsgDecodable for ChannelUpdate {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 128 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let secp_ctx = Secp256k1::without_caps();
+               Ok(Self {
+                       signature: secp_signature!(&secp_ctx, &v[0..64]),
+                       contents: UnsignedChannelUpdate::decode(&v[64..])?,
+               })
        }
 }
 impl MsgEncodable for ChannelUpdate {
        fn encode(&self) -> Vec<u8> {
                let mut res = Vec::with_capacity(128);
-               //TODO: Should avoid creating a new secp ctx just for a serialize call :(
-               res.extend_from_slice(&self.signature.serialize_der(&Secp256k1::new())[..]); //TODO: Need in non-der form! (probably elsewhere too)
+               res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps())[..]);
                res.extend_from_slice(&self.contents.encode()[..]);
                res
        }
index 2b60bbf122bce3af47a16997afe48740ede9805a..9715ee965c3374c63494e584721138341452e589 100644 (file)
@@ -370,42 +370,15 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                        },
                                                                                        130 => {
                                                                                                let msg = try_potential_decodeerror!(msgs::UpdateFulfillHTLC::decode(&msg_data[2..]));
-                                                                                               let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg));
-                                                                                               match resp_option {
-                                                                                                       Some(resps) => {
-                                                                                                               for resp in resps.0 {
-                                                                                                                       encode_and_send_msg!(resp, 128);
-                                                                                                               }
-                                                                                                               encode_and_send_msg!(resps.1, 132);
-                                                                                                       },
-                                                                                                       None => {},
-                                                                                               }
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg));
                                                                                        },
                                                                                        131 => {
                                                                                                let msg = try_potential_decodeerror!(msgs::UpdateFailHTLC::decode(&msg_data[2..]));
-                                                                                               let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg));
-                                                                                               match resp_option {
-                                                                                                       Some(resps) => {
-                                                                                                               for resp in resps.0 {
-                                                                                                                       encode_and_send_msg!(resp, 128);
-                                                                                                               }
-                                                                                                               encode_and_send_msg!(resps.1, 132);
-                                                                                                       },
-                                                                                                       None => {},
-                                                                                               }
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg));
                                                                                        },
                                                                                        135 => {
                                                                                                let msg = try_potential_decodeerror!(msgs::UpdateFailMalformedHTLC::decode(&msg_data[2..]));
-                                                                                               let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg));
-                                                                                               match resp_option {
-                                                                                                       Some(resps) => {
-                                                                                                               for resp in resps.0 {
-                                                                                                                       encode_and_send_msg!(resp, 128);
-                                                                                                               }
-                                                                                                               encode_and_send_msg!(resps.1, 132);
-                                                                                                       },
-                                                                                                       None => {},
-                                                                                               }
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg));
                                                                                        },
 
                                                                                        132 => {
@@ -415,9 +388,17 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                        },
                                                                                        133 => {
                                                                                                let msg = try_potential_decodeerror!(msgs::RevokeAndACK::decode(&msg_data[2..]));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg));
+                                                                                               let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg));
+                                                                                               match resp_option {
+                                                                                                       Some(resps) => {
+                                                                                                               for resp in resps.0 {
+                                                                                                                       encode_and_send_msg!(resp, 128);
+                                                                                                               }
+                                                                                                               encode_and_send_msg!(resps.1, 132);
+                                                                                                       },
+                                                                                                       None => {},
+                                                                                               }
                                                                                        },
-
                                                                                        134 => {
                                                                                                let msg = try_potential_decodeerror!(msgs::UpdateFee::decode(&msg_data[2..]));
                                                                                                try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg));