X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fmsgs.rs;h=d01595b72164bfe09917144f2bf47c54a0c1809b;hb=e382a7b4b3e5f3e59a9300b9d8a4d8bff06366fe;hp=7b0254bddfd5fbb470cb9e4e6ee3ba8b1162cc52;hpb=920d1458c40f9d57679a17090c9fc090b1001d7b;p=rust-lightning diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index 7b0254bd..d01595b7 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -168,6 +168,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], @@ -191,6 +192,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, @@ -210,6 +212,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, @@ -218,13 +221,14 @@ 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)] +#[derive(Clone, PartialEq)] pub struct FundingLocked { pub(crate) channel_id: [u8; 32], pub(crate) next_per_commitment_point: PublicKey, @@ -244,7 +248,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, @@ -255,7 +259,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, @@ -263,7 +267,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, @@ -271,7 +275,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, @@ -280,7 +284,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, @@ -288,6 +292,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], @@ -295,17 +300,20 @@ pub struct RevokeAndACK { } /// An update_fee message to be sent or received from a peer +#[derive(PartialEq)] pub struct UpdateFee { pub(crate) channel_id: [u8; 32], pub(crate) feerate_per_kw: u32, } +#[derive(PartialEq)] 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)] pub struct ChannelReestablish { pub(crate) channel_id: [u8; 32], pub(crate) next_local_commitment_number: u64, @@ -463,6 +471,7 @@ 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)] pub struct CommitmentUpdate { pub(crate) update_add_htlcs: Vec, pub(crate) update_fulfill_htlcs: Vec, @@ -500,22 +509,34 @@ pub enum HTLCFailChannelUpdate { } } +/// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should +/// be sent in the order they appear in the return value, however sometimes the order needs to be +/// variable at runtime (eg handle_channel_reestablish needs to re-send messages in the order they +/// were originally sent). In those cases, this enum is also returned. +#[derive(Clone, PartialEq)] +pub enum RAACommitmentOrder { + /// Send the CommitmentUpdate messages first + CommitmentFirst, + /// Send the RevokeAndACK message first + RevokeAndACKFirst, +} + /// 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 { +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. @@ -554,7 +575,7 @@ pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync { /// Handle a peer reconnecting, possibly generating channel_reestablish message(s). fn peer_connected(&self, their_node_id: &PublicKey) -> Vec; /// 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<(Option, Option, Option, RAACommitmentOrder), HandleError>; // Error: /// Handle an incoming error message from the given peer. @@ -617,7 +638,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...