X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Frouter.rs;h=6bc319d59b8145de6162f47c7c78d746fd318a12;hb=91b23a075442107a93d00c6db1d7f5ffe54f715b;hp=1f713f69de8287fd298776179c51c3f4389e69be;hpb=0881bf4b7460921eff425f0b4833b994922064a5;p=rust-lightning diff --git a/src/ln/router.rs b/src/ln/router.rs index 1f713f69..6bc319d5 100644 --- a/src/ln/router.rs +++ b/src/ln/router.rs @@ -3,7 +3,10 @@ use secp256k1::{Secp256k1,Message}; use secp256k1; use bitcoin::util::hash::Sha256dHash; +use bitcoin::blockdata::script::Builder; +use bitcoin::blockdata::opcodes; +use chain::chaininterface::{ChainError, ChainWatchInterface}; use ln::channelmanager; use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures}; use ln::msgs; @@ -155,6 +158,7 @@ pub struct RouteHint { pub struct Router { secp_ctx: Secp256k1, network_map: RwLock, + chain_monitor: Arc, logger: Arc, } @@ -168,10 +172,14 @@ macro_rules! secp_verify_sig { } impl RoutingMessageHandler for Router { - fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<(), HandleError> { + fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result { let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap(); secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id); + if msg.contents.features.requires_unknown_bits() { + panic!("Unknown-required-features NodeAnnouncements should never deserialize!"); + } + let mut network = self.network_map.write().unwrap(); match network.nodes.get_mut(&msg.contents.node_id) { None => Err(HandleError{err: "No existing channels for node_announcement", action: Some(ErrorAction::IgnoreError)}), @@ -185,23 +193,47 @@ impl RoutingMessageHandler for Router { node.rgb = msg.contents.rgb; node.alias = msg.contents.alias; node.addresses = msg.contents.addresses.clone(); - Ok(()) + Ok(msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty() && !msg.contents.features.supports_unknown_bits()) } } } fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result { + if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 { + return Err(HandleError{err: "Channel announcement node had a channel with itself", action: Some(ErrorAction::IgnoreError)}); + } + let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap(); secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1); secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2); secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1); secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2); - //TODO: Call blockchain thing to ask if the short_channel_id is valid - //TODO: Only allow bitcoin chain_hash - if msg.contents.features.requires_unknown_bits() { - return Err(HandleError{err: "Channel announcement required unknown feature flags", action: None}); + panic!("Unknown-required-features ChannelAnnouncements should never deserialize!"); + } + + match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) { + Ok((script_pubkey, _value)) => { + let expected_script = Builder::new().push_opcode(opcodes::All::OP_PUSHNUM_2) + .push_slice(&msg.contents.bitcoin_key_1.serialize()) + .push_slice(&msg.contents.bitcoin_key_2.serialize()) + .push_opcode(opcodes::All::OP_PUSHNUM_2).push_opcode(opcodes::All::OP_CHECKMULTISIG).into_script().to_v0_p2wsh(); + if script_pubkey != expected_script { + return Err(HandleError{err: "Channel announcement keys didn't match on-chain script", action: Some(ErrorAction::IgnoreError)}); + } + //TODO: Check if value is worth storing, use it to inform routing, and compare it + //to the new HTLC max field in channel_update + }, + Err(ChainError::NotSupported) => { + // Tentatively accept, potentially exposing us to DoS attacks + }, + Err(ChainError::NotWatched) => { + return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)}); + }, + Err(ChainError::UnknownTx) => { + return Err(HandleError{err: "Channel announced without corresponding UTXO entry", action: Some(ErrorAction::IgnoreError)}); + }, } let mut network = self.network_map.write().unwrap(); @@ -285,7 +317,7 @@ impl RoutingMessageHandler for Router { } } - fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<(), HandleError> { + fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result { let mut network = self.network_map.write().unwrap(); let dest_node_id; let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1); @@ -351,7 +383,7 @@ impl RoutingMessageHandler for Router { mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths; } - Ok(()) + Ok(msg.contents.excess_data.is_empty()) } } @@ -384,7 +416,7 @@ struct DummyDirectionalChannelInfo { } impl Router { - pub fn new(our_pubkey: PublicKey, logger: Arc) -> Router { + pub fn new(our_pubkey: PublicKey, chain_monitor: Arc, logger: Arc) -> Router { let mut nodes = HashMap::new(); nodes.insert(our_pubkey.clone(), NodeInfo { channels: Vec::new(), @@ -403,6 +435,7 @@ impl Router { our_node_id: our_pubkey, nodes: nodes, }), + chain_monitor, logger, } } @@ -628,6 +661,7 @@ impl Router { #[cfg(test)] mod tests { + use chain::chaininterface; use ln::channelmanager; use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint}; use ln::msgs::GlobalFeatures; @@ -635,6 +669,7 @@ mod tests { use util::logger::Logger; use bitcoin::util::hash::Sha256dHash; + use bitcoin::network::constants::Network; use hex; @@ -648,7 +683,8 @@ mod tests { let secp_ctx = Secp256k1::new(); let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap()); let logger: Arc = Arc::new(test_utils::TestLogger::new()); - let router = Router::new(our_id, Arc::clone(&logger)); + let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger))); + let router = Router::new(our_id, chain_monitor, Arc::clone(&logger)); // Build network from our_id to node8: //