X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fmsgs.rs;h=354376649c7b69ed79304ff51e872fb6d68d88df;hb=b14baa03ab20911489bee50688785f2631f0d0f1;hp=5dd3165bfc0977728f8c80b03ba9cfeff3ecdb9f;hpb=66fbc66da0c064e4ee271ad5a8e0a37410c80078;p=rust-lightning diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index 5dd3165b..35437664 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -1,9 +1,11 @@ //! Wire messages, traits representing wire message handlers, and a few error types live here. +//! //! For a normal node you probably don't need to use anything here, however, if you wish to split a //! node into an internet-facing route/message socket handling daemon and a separate daemon (or //! server entirely) which handles only channel-related messages you may wish to implement //! ChannelMessageHandler yourself and use it to re-serialize messages and pass them across //! daemons/servers. +//! //! Note that if you go with such an architecture (instead of passing raw socket events to a //! non-internet-facing system) you trust the frontend internet-facing system to not lie about the //! source node_id of the mssage, however this does allow you to significantly reduce bandwidth @@ -30,16 +32,14 @@ use util::ser::{Readable, Writeable, Writer}; /// An error in decoding a message or struct. #[derive(Debug)] pub enum DecodeError { - /// Unknown realm byte in an OnionHopData packet - UnknownRealmByte, + /// A version byte specified something we don't know how to handle. + /// Includes unknown realm byte in an OnionHopData packet + UnknownVersion, /// Unknown feature mandating we fail to parse message UnknownRequiredFeature, - /// Failed to decode a public key (ie it's invalid) - BadPublicKey, - /// Failed to decode a signature (ie it's invalid) - BadSignature, - /// Value expected to be text wasn't decodable as text - BadText, + /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0 + /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc + InvalidValue, /// Buffer too short ShortRead, /// node_announcement included more than one address of a given type! @@ -49,8 +49,6 @@ pub enum DecodeError { BadLengthDescriptor, /// Error from std::io Io(::std::io::Error), - /// 1 or 0 is not found for boolean value - InvalidValue, } /// Tracks localfeatures which are only in init messages @@ -153,6 +151,7 @@ pub struct Init { } /// An error message to be sent or received from a peer +#[derive(Clone)] pub struct ErrorMessage { pub(crate) channel_id: [u8; 32], pub(crate) data: String, @@ -170,6 +169,7 @@ pub struct Pong { } /// An open_channel message to be sent or received from a peer +#[derive(Clone)] pub struct OpenChannel { pub(crate) chain_hash: Sha256dHash, pub(crate) temporary_channel_id: [u8; 32], @@ -193,6 +193,7 @@ pub struct OpenChannel { } /// An accept_channel message to be sent or received from a peer +#[derive(Clone)] pub struct AcceptChannel { pub(crate) temporary_channel_id: [u8; 32], pub(crate) dust_limit_satoshis: u64, @@ -212,6 +213,7 @@ pub struct AcceptChannel { } /// A funding_created message to be sent or received from a peer +#[derive(Clone)] pub struct FundingCreated { pub(crate) temporary_channel_id: [u8; 32], pub(crate) funding_txid: Sha256dHash, @@ -220,24 +222,28 @@ pub struct FundingCreated { } /// A funding_signed message to be sent or received from a peer +#[derive(Clone)] pub struct FundingSigned { pub(crate) channel_id: [u8; 32], pub(crate) signature: Signature, } /// A funding_locked message to be sent or received from a peer +#[derive(Clone, PartialEq)] pub struct FundingLocked { pub(crate) channel_id: [u8; 32], pub(crate) next_per_commitment_point: PublicKey, } /// A shutdown message to be sent or received from a peer +#[derive(Clone, PartialEq)] pub struct Shutdown { pub(crate) channel_id: [u8; 32], pub(crate) scriptpubkey: Script, } /// A closing_signed message to be sent or received from a peer +#[derive(Clone, PartialEq)] pub struct ClosingSigned { pub(crate) channel_id: [u8; 32], pub(crate) fee_satoshis: u64, @@ -245,7 +251,7 @@ pub struct ClosingSigned { } /// An update_add_htlc message to be sent or received from a peer -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct UpdateAddHTLC { pub(crate) channel_id: [u8; 32], pub(crate) htlc_id: u64, @@ -256,7 +262,7 @@ pub struct UpdateAddHTLC { } /// An update_fulfill_htlc message to be sent or received from a peer -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct UpdateFulfillHTLC { pub(crate) channel_id: [u8; 32], pub(crate) htlc_id: u64, @@ -264,7 +270,7 @@ pub struct UpdateFulfillHTLC { } /// An update_fail_htlc message to be sent or received from a peer -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct UpdateFailHTLC { pub(crate) channel_id: [u8; 32], pub(crate) htlc_id: u64, @@ -272,7 +278,7 @@ pub struct UpdateFailHTLC { } /// An update_fail_malformed_htlc message to be sent or received from a peer -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct UpdateFailMalformedHTLC { pub(crate) channel_id: [u8; 32], pub(crate) htlc_id: u64, @@ -281,7 +287,7 @@ pub struct UpdateFailMalformedHTLC { } /// A commitment_signed message to be sent or received from a peer -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct CommitmentSigned { pub(crate) channel_id: [u8; 32], pub(crate) signature: Signature, @@ -289,6 +295,7 @@ pub struct CommitmentSigned { } /// A revoke_and_ack message to be sent or received from a peer +#[derive(Clone, PartialEq)] pub struct RevokeAndACK { pub(crate) channel_id: [u8; 32], pub(crate) per_commitment_secret: [u8; 32], @@ -296,17 +303,20 @@ pub struct RevokeAndACK { } /// An update_fee message to be sent or received from a peer +#[derive(PartialEq, Clone)] pub struct UpdateFee { pub(crate) channel_id: [u8; 32], pub(crate) feerate_per_kw: u32, } +#[derive(PartialEq, Clone)] pub(crate) struct DataLossProtect { pub(crate) your_last_per_commitment_secret: [u8; 32], pub(crate) my_current_per_commitment_point: PublicKey, } /// A channel_reestablish message to be sent or received from a peer +#[derive(PartialEq, Clone)] pub struct ChannelReestablish { pub(crate) channel_id: [u8; 32], pub(crate) next_local_commitment_number: u64, @@ -372,6 +382,7 @@ impl NetAddress { } } +#[derive(Clone)] // Only exposed as broadcast of node_announcement should be filtered by node_id /// The unsigned part of a node_announcement pub struct UnsignedNodeAnnouncement { @@ -388,6 +399,7 @@ pub struct UnsignedNodeAnnouncement { pub(crate) excess_address_data: Vec, pub(crate) excess_data: Vec, } +#[derive(Clone)] /// A node_announcement message to be sent or received from a peer pub struct NodeAnnouncement { pub(crate) signature: Signature, @@ -439,6 +451,7 @@ pub struct ChannelUpdate { } /// Used to put an error message in a HandleError +#[derive(Clone)] pub enum ErrorAction { /// The peer took some action which made us think they were useless. Disconnect them. DisconnectPeer { @@ -464,17 +477,20 @@ pub struct HandleError { //TODO: rename me /// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment /// transaction updates if they were pending. +#[derive(PartialEq, Clone)] pub struct CommitmentUpdate { pub(crate) update_add_htlcs: Vec, pub(crate) update_fulfill_htlcs: Vec, pub(crate) update_fail_htlcs: Vec, pub(crate) update_fail_malformed_htlcs: Vec, + pub(crate) update_fee: Option, pub(crate) commitment_signed: CommitmentSigned, } /// The information we received from a peer along the route of a payment we originated. This is /// returned by ChannelMessageHandler::handle_update_fail_htlc to be passed into /// RoutingMessageHandler::handle_htlc_fail_channel_update to update our network map. +#[derive(Clone)] pub enum HTLCFailChannelUpdate { /// We received an error which included a full ChannelUpdate message. ChannelUpdateMessage { @@ -485,30 +501,43 @@ pub enum HTLCFailChannelUpdate { ChannelClosed { /// The short_channel_id which has now closed. short_channel_id: u64, + /// when this true, this channel should be permanently removed from the + /// consideration. Otherwise, this channel can be restored as new channel_update is received + is_permanent: bool, }, + /// We received an error which indicated only that a node has failed + NodeFailure { + /// The node_id that has failed. + node_id: PublicKey, + /// when this true, node should be permanently removed from the + /// consideration. Otherwise, the channels connected to this node can be + /// restored as new channel_update is received + is_permanent: bool, + } } -/// A trait to describe an object which can receive channel messages. Messages MAY be called in -/// parallel when they originate from different their_node_ids, however they MUST NOT be called in -/// parallel when the two calls have the same their_node_id. -pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync { +/// A trait to describe an object which can receive channel messages. +/// +/// Messages MAY be called in parallel when they originate from different their_node_ids, however +/// they MUST NOT be called in parallel when the two calls have the same their_node_id. +pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Sync { //Channel init: /// Handle an incoming open_channel message from the given peer. - fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &OpenChannel) -> Result; + fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &OpenChannel) -> Result<(), HandleError>; /// Handle an incoming accept_channel message from the given peer. fn handle_accept_channel(&self, their_node_id: &PublicKey, msg: &AcceptChannel) -> Result<(), HandleError>; /// Handle an incoming funding_created message from the given peer. - fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result; + fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<(), HandleError>; /// Handle an incoming funding_signed message from the given peer. fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned) -> Result<(), HandleError>; /// Handle an incoming funding_locked message from the given peer. - fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked) -> Result, HandleError>; + fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked) -> Result<(), HandleError>; // Channl close: /// Handle an incoming shutdown message from the given peer. - fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown) -> Result<(Option, Option), HandleError>; + fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown) -> Result<(), HandleError>; /// Handle an incoming closing_signed message from the given peer. - fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned) -> Result, HandleError>; + fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned) -> Result<(), HandleError>; // HTLC handling: /// Handle an incoming update_add_htlc message from the given peer. @@ -516,13 +545,13 @@ pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync { /// Handle an incoming update_fulfill_htlc message from the given peer. fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), HandleError>; /// Handle an incoming update_fail_htlc message from the given peer. - fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result, HandleError>; + fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<(), HandleError>; /// Handle an incoming update_fail_malformed_htlc message from the given peer. fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), HandleError>; /// Handle an incoming commitment_signed message from the given peer. - fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(RevokeAndACK, Option), HandleError>; + fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(), HandleError>; /// Handle an incoming revoke_and_ack message from the given peer. - 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<(), HandleError>; /// Handle an incoming update_fee message from the given peer. fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), HandleError>; @@ -539,9 +568,9 @@ pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync { fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool); /// Handle a peer reconnecting, possibly generating channel_reestablish message(s). - fn peer_connected(&self, their_node_id: &PublicKey) -> Vec; + fn peer_connected(&self, their_node_id: &PublicKey); /// Handle an incoming channel_reestablish message from the given peer. - fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(Option, Option, Option), HandleError>; + fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(), HandleError>; // Error: /// Handle an incoming error message from the given peer. @@ -561,6 +590,14 @@ pub trait RoutingMessageHandler : Send + Sync { fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result; /// Handle some updates to the route graph that we learned due to an outbound failed payment. fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate); + /// Gets a subset of the channel announcements and updates required to dump our routing table + /// to a remote node, starting at the short_channel_id indicated by starting_point and + /// including batch_amount entries. + fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, ChannelUpdate, ChannelUpdate)>; + /// Gets a subset of the node announcements required to dump our routing table to a remote node, + /// starting at the node *after* the provided publickey and including batch_amount entries. + /// If None is provided for starting_point, we start at the first node. + fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec; } pub(crate) struct OnionRealm0HopData { @@ -604,7 +641,18 @@ pub(crate) struct OnionPacket { pub(crate) hmac: [u8; 32], } -#[derive(Clone)] +impl PartialEq for OnionPacket { + fn eq(&self, other: &OnionPacket) -> bool { + for (i, j) in self.hop_data.iter().zip(other.hop_data.iter()) { + if i != j { return false; } + } + self.version == other.version && + self.public_key == other.public_key && + self.hmac == other.hmac + } +} + +#[derive(Clone, PartialEq)] pub(crate) struct OnionErrorPacket { // This really should be a constant size slice, but the spec lets these things be up to 128KB? // (TODO) We limit it in decode to much lower... @@ -614,16 +662,13 @@ pub(crate) struct OnionErrorPacket { impl Error for DecodeError { fn description(&self) -> &str { match *self { - DecodeError::UnknownRealmByte => "Unknown realm byte in Onion packet", + DecodeError::UnknownVersion => "Unknown realm byte in Onion packet", DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode", - DecodeError::BadPublicKey => "Invalid public key in packet", - DecodeError::BadSignature => "Invalid signature in packet", - DecodeError::BadText => "Invalid text in packet", + DecodeError::InvalidValue => "Nonsense bytes didn't map to the type they were interpreted as", DecodeError::ShortRead => "Packet extended beyond the provided bytes", DecodeError::ExtraAddressesPerType => "More than one address of a single type", DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly", DecodeError::Io(ref e) => e.description(), - DecodeError::InvalidValue => "0 or 1 is not found for boolean", } } } @@ -919,7 +964,7 @@ impl Readable for OnionHopData { realm: { let r: u8 = Readable::read(r)?; if r != 0 { - return Err(DecodeError::UnknownRealmByte); + return Err(DecodeError::UnknownVersion); } r }, @@ -1087,7 +1132,7 @@ impl Readable for ErrorMessage { sz = cmp::min(data_len, sz); match String::from_utf8(data[..sz as usize].to_vec()) { Ok(s) => s, - Err(_) => return Err(DecodeError::BadText), + Err(_) => return Err(DecodeError::InvalidValue), } } })