Move features into a separate module out of msgs.
[rust-lightning] / lightning / src / ln / msgs.rs
1 //! Wire messages, traits representing wire message handlers, and a few error types live here.
2 //!
3 //! For a normal node you probably don't need to use anything here, however, if you wish to split a
4 //! node into an internet-facing route/message socket handling daemon and a separate daemon (or
5 //! server entirely) which handles only channel-related messages you may wish to implement
6 //! ChannelMessageHandler yourself and use it to re-serialize messages and pass them across
7 //! daemons/servers.
8 //!
9 //! Note that if you go with such an architecture (instead of passing raw socket events to a
10 //! non-internet-facing system) you trust the frontend internet-facing system to not lie about the
11 //! source node_id of the message, however this does allow you to significantly reduce bandwidth
12 //! between the systems as routing messages can represent a significant chunk of bandwidth usage
13 //! (especially for non-channel-publicly-announcing nodes). As an alternate design which avoids
14 //! this issue, if you have sufficient bidirectional bandwidth between your systems, you may send
15 //! raw socket events into your non-internet-facing system and then send routing events back to
16 //! track the network on the less-secure system.
17
18 use secp256k1::key::PublicKey;
19 use secp256k1::Signature;
20 use secp256k1;
21 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
22 use bitcoin::blockdata::script::Script;
23
24 use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
25
26 use std::error::Error;
27 use std::{cmp, fmt};
28 use std::io::Read;
29 use std::result::Result;
30
31 use util::events;
32 use util::ser::{Readable, Writeable, Writer};
33
34 use ln::channelmanager::{PaymentPreimage, PaymentHash};
35
36 /// An error in decoding a message or struct.
37 #[derive(Debug)]
38 pub enum DecodeError {
39         /// A version byte specified something we don't know how to handle.
40         /// Includes unknown realm byte in an OnionHopData packet
41         UnknownVersion,
42         /// Unknown feature mandating we fail to parse message
43         UnknownRequiredFeature,
44         /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0
45         /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc
46         InvalidValue,
47         /// Buffer too short
48         ShortRead,
49         /// node_announcement included more than one address of a given type!
50         ExtraAddressesPerType,
51         /// A length descriptor in the packet didn't describe the later data correctly
52         BadLengthDescriptor,
53         /// Error from std::io
54         Io(::std::io::Error),
55 }
56
57 /// An init message to be sent or received from a peer
58 pub struct Init {
59         pub(crate) features: InitFeatures,
60 }
61
62 /// An error message to be sent or received from a peer
63 #[derive(Clone)]
64 pub struct ErrorMessage {
65         pub(crate) channel_id: [u8; 32],
66         pub(crate) data: String,
67 }
68
69 /// A ping message to be sent or received from a peer
70 pub struct Ping {
71         pub(crate) ponglen: u16,
72         pub(crate) byteslen: u16,
73 }
74
75 /// A pong message to be sent or received from a peer
76 pub struct Pong {
77         pub(crate) byteslen: u16,
78 }
79
80 /// An open_channel message to be sent or received from a peer
81 #[derive(Clone)]
82 pub struct OpenChannel {
83         pub(crate) chain_hash: Sha256dHash,
84         pub(crate) temporary_channel_id: [u8; 32],
85         pub(crate) funding_satoshis: u64,
86         pub(crate) push_msat: u64,
87         pub(crate) dust_limit_satoshis: u64,
88         pub(crate) max_htlc_value_in_flight_msat: u64,
89         pub(crate) channel_reserve_satoshis: u64,
90         pub(crate) htlc_minimum_msat: u64,
91         pub(crate) feerate_per_kw: u32,
92         pub(crate) to_self_delay: u16,
93         pub(crate) max_accepted_htlcs: u16,
94         pub(crate) funding_pubkey: PublicKey,
95         pub(crate) revocation_basepoint: PublicKey,
96         pub(crate) payment_basepoint: PublicKey,
97         pub(crate) delayed_payment_basepoint: PublicKey,
98         pub(crate) htlc_basepoint: PublicKey,
99         pub(crate) first_per_commitment_point: PublicKey,
100         pub(crate) channel_flags: u8,
101         pub(crate) shutdown_scriptpubkey: OptionalField<Script>,
102 }
103
104 /// An accept_channel message to be sent or received from a peer
105 #[derive(Clone)]
106 pub struct AcceptChannel {
107         pub(crate) temporary_channel_id: [u8; 32],
108         pub(crate) dust_limit_satoshis: u64,
109         pub(crate) max_htlc_value_in_flight_msat: u64,
110         pub(crate) channel_reserve_satoshis: u64,
111         pub(crate) htlc_minimum_msat: u64,
112         pub(crate) minimum_depth: u32,
113         pub(crate) to_self_delay: u16,
114         pub(crate) max_accepted_htlcs: u16,
115         pub(crate) funding_pubkey: PublicKey,
116         pub(crate) revocation_basepoint: PublicKey,
117         pub(crate) payment_basepoint: PublicKey,
118         pub(crate) delayed_payment_basepoint: PublicKey,
119         pub(crate) htlc_basepoint: PublicKey,
120         pub(crate) first_per_commitment_point: PublicKey,
121         pub(crate) shutdown_scriptpubkey: OptionalField<Script>
122 }
123
124 /// A funding_created message to be sent or received from a peer
125 #[derive(Clone)]
126 pub struct FundingCreated {
127         pub(crate) temporary_channel_id: [u8; 32],
128         pub(crate) funding_txid: Sha256dHash,
129         pub(crate) funding_output_index: u16,
130         pub(crate) signature: Signature,
131 }
132
133 /// A funding_signed message to be sent or received from a peer
134 #[derive(Clone)]
135 pub struct FundingSigned {
136         pub(crate) channel_id: [u8; 32],
137         pub(crate) signature: Signature,
138 }
139
140 /// A funding_locked message to be sent or received from a peer
141 #[derive(Clone, PartialEq)]
142 pub struct FundingLocked {
143         pub(crate) channel_id: [u8; 32],
144         pub(crate) next_per_commitment_point: PublicKey,
145 }
146
147 /// A shutdown message to be sent or received from a peer
148 #[derive(Clone, PartialEq)]
149 pub struct Shutdown {
150         pub(crate) channel_id: [u8; 32],
151         pub(crate) scriptpubkey: Script,
152 }
153
154 /// A closing_signed message to be sent or received from a peer
155 #[derive(Clone, PartialEq)]
156 pub struct ClosingSigned {
157         pub(crate) channel_id: [u8; 32],
158         pub(crate) fee_satoshis: u64,
159         pub(crate) signature: Signature,
160 }
161
162 /// An update_add_htlc message to be sent or received from a peer
163 #[derive(Clone, PartialEq)]
164 pub struct UpdateAddHTLC {
165         pub(crate) channel_id: [u8; 32],
166         pub(crate) htlc_id: u64,
167         pub(crate) amount_msat: u64,
168         pub(crate) payment_hash: PaymentHash,
169         pub(crate) cltv_expiry: u32,
170         pub(crate) onion_routing_packet: OnionPacket,
171 }
172
173 /// An update_fulfill_htlc message to be sent or received from a peer
174 #[derive(Clone, PartialEq)]
175 pub struct UpdateFulfillHTLC {
176         pub(crate) channel_id: [u8; 32],
177         pub(crate) htlc_id: u64,
178         pub(crate) payment_preimage: PaymentPreimage,
179 }
180
181 /// An update_fail_htlc message to be sent or received from a peer
182 #[derive(Clone, PartialEq)]
183 pub struct UpdateFailHTLC {
184         pub(crate) channel_id: [u8; 32],
185         pub(crate) htlc_id: u64,
186         pub(crate) reason: OnionErrorPacket,
187 }
188
189 /// An update_fail_malformed_htlc message to be sent or received from a peer
190 #[derive(Clone, PartialEq)]
191 pub struct UpdateFailMalformedHTLC {
192         pub(crate) channel_id: [u8; 32],
193         pub(crate) htlc_id: u64,
194         pub(crate) sha256_of_onion: [u8; 32],
195         pub(crate) failure_code: u16,
196 }
197
198 /// A commitment_signed message to be sent or received from a peer
199 #[derive(Clone, PartialEq)]
200 pub struct CommitmentSigned {
201         pub(crate) channel_id: [u8; 32],
202         pub(crate) signature: Signature,
203         pub(crate) htlc_signatures: Vec<Signature>,
204 }
205
206 /// A revoke_and_ack message to be sent or received from a peer
207 #[derive(Clone, PartialEq)]
208 pub struct RevokeAndACK {
209         pub(crate) channel_id: [u8; 32],
210         pub(crate) per_commitment_secret: [u8; 32],
211         pub(crate) next_per_commitment_point: PublicKey,
212 }
213
214 /// An update_fee message to be sent or received from a peer
215 #[derive(PartialEq, Clone)]
216 pub struct UpdateFee {
217         pub(crate) channel_id: [u8; 32],
218         pub(crate) feerate_per_kw: u32,
219 }
220
221 #[derive(PartialEq, Clone)]
222 pub(crate) struct DataLossProtect {
223         pub(crate) your_last_per_commitment_secret: [u8; 32],
224         pub(crate) my_current_per_commitment_point: PublicKey,
225 }
226
227 /// A channel_reestablish message to be sent or received from a peer
228 #[derive(PartialEq, Clone)]
229 pub struct ChannelReestablish {
230         pub(crate) channel_id: [u8; 32],
231         pub(crate) next_local_commitment_number: u64,
232         pub(crate) next_remote_commitment_number: u64,
233         pub(crate) data_loss_protect: OptionalField<DataLossProtect>,
234 }
235
236 /// An announcement_signatures message to be sent or received from a peer
237 #[derive(PartialEq, Clone, Debug)]
238 pub struct AnnouncementSignatures {
239         pub(crate) channel_id: [u8; 32],
240         pub(crate) short_channel_id: u64,
241         pub(crate) node_signature: Signature,
242         pub(crate) bitcoin_signature: Signature,
243 }
244
245 /// An address which can be used to connect to a remote peer
246 #[derive(Clone, PartialEq, Debug)]
247 pub enum NetAddress {
248         /// An IPv4 address/port on which the peer is listening.
249         IPv4 {
250                 /// The 4-byte IPv4 address
251                 addr: [u8; 4],
252                 /// The port on which the node is listening
253                 port: u16,
254         },
255         /// An IPv6 address/port on which the peer is listening.
256         IPv6 {
257                 /// The 16-byte IPv6 address
258                 addr: [u8; 16],
259                 /// The port on which the node is listening
260                 port: u16,
261         },
262         /// An old-style Tor onion address/port on which the peer is listening.
263         OnionV2 {
264                 /// The bytes (usually encoded in base32 with ".onion" appended)
265                 addr: [u8; 10],
266                 /// The port on which the node is listening
267                 port: u16,
268         },
269         /// A new-style Tor onion address/port on which the peer is listening.
270         /// To create the human-readable "hostname", concatenate ed25519_pubkey, checksum, and version,
271         /// wrap as base32 and append ".onion".
272         OnionV3 {
273                 /// The ed25519 long-term public key of the peer
274                 ed25519_pubkey: [u8; 32],
275                 /// The checksum of the pubkey and version, as included in the onion address
276                 checksum: u16,
277                 /// The version byte, as defined by the Tor Onion v3 spec.
278                 version: u8,
279                 /// The port on which the node is listening
280                 port: u16,
281         },
282 }
283 impl NetAddress {
284         fn get_id(&self) -> u8 {
285                 match self {
286                         &NetAddress::IPv4 {..} => { 1 },
287                         &NetAddress::IPv6 {..} => { 2 },
288                         &NetAddress::OnionV2 {..} => { 3 },
289                         &NetAddress::OnionV3 {..} => { 4 },
290                 }
291         }
292
293         /// Strict byte-length of address descriptor, 1-byte type not recorded
294         fn len(&self) -> u16 {
295                 match self {
296                         &NetAddress::IPv4 { .. } => { 6 },
297                         &NetAddress::IPv6 { .. } => { 18 },
298                         &NetAddress::OnionV2 { .. } => { 12 },
299                         &NetAddress::OnionV3 { .. } => { 37 },
300                 }
301         }
302 }
303
304 impl Writeable for NetAddress {
305         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
306                 match self {
307                         &NetAddress::IPv4 { ref addr, ref port } => {
308                                 1u8.write(writer)?;
309                                 addr.write(writer)?;
310                                 port.write(writer)?;
311                         },
312                         &NetAddress::IPv6 { ref addr, ref port } => {
313                                 2u8.write(writer)?;
314                                 addr.write(writer)?;
315                                 port.write(writer)?;
316                         },
317                         &NetAddress::OnionV2 { ref addr, ref port } => {
318                                 3u8.write(writer)?;
319                                 addr.write(writer)?;
320                                 port.write(writer)?;
321                         },
322                         &NetAddress::OnionV3 { ref ed25519_pubkey, ref checksum, ref version, ref port } => {
323                                 4u8.write(writer)?;
324                                 ed25519_pubkey.write(writer)?;
325                                 checksum.write(writer)?;
326                                 version.write(writer)?;
327                                 port.write(writer)?;
328                         }
329                 }
330                 Ok(())
331         }
332 }
333
334 impl<R: ::std::io::Read>  Readable<R> for Result<NetAddress, u8> {
335         fn read(reader: &mut R) -> Result<Result<NetAddress, u8>, DecodeError> {
336                 let byte = <u8 as Readable<R>>::read(reader)?;
337                 match byte {
338                         1 => {
339                                 Ok(Ok(NetAddress::IPv4 {
340                                         addr: Readable::read(reader)?,
341                                         port: Readable::read(reader)?,
342                                 }))
343                         },
344                         2 => {
345                                 Ok(Ok(NetAddress::IPv6 {
346                                         addr: Readable::read(reader)?,
347                                         port: Readable::read(reader)?,
348                                 }))
349                         },
350                         3 => {
351                                 Ok(Ok(NetAddress::OnionV2 {
352                                         addr: Readable::read(reader)?,
353                                         port: Readable::read(reader)?,
354                                 }))
355                         },
356                         4 => {
357                                 Ok(Ok(NetAddress::OnionV3 {
358                                         ed25519_pubkey: Readable::read(reader)?,
359                                         checksum: Readable::read(reader)?,
360                                         version: Readable::read(reader)?,
361                                         port: Readable::read(reader)?,
362                                 }))
363                         },
364                         _ => return Ok(Err(byte)),
365                 }
366         }
367 }
368
369 // Only exposed as broadcast of node_announcement should be filtered by node_id
370 /// The unsigned part of a node_announcement
371 #[derive(PartialEq, Clone, Debug)]
372 pub struct UnsignedNodeAnnouncement {
373         pub(crate) features: NodeFeatures,
374         pub(crate) timestamp: u32,
375         /// The node_id this announcement originated from (don't rebroadcast the node_announcement back
376         /// to this node).
377         pub        node_id: PublicKey,
378         pub(crate) rgb: [u8; 3],
379         pub(crate) alias: [u8; 32],
380         /// List of addresses on which this node is reachable. Note that you may only have up to one
381         /// address of each type, if you have more, they may be silently discarded or we may panic!
382         pub(crate) addresses: Vec<NetAddress>,
383         pub(crate) excess_address_data: Vec<u8>,
384         pub(crate) excess_data: Vec<u8>,
385 }
386 #[derive(PartialEq, Clone)]
387 /// A node_announcement message to be sent or received from a peer
388 pub struct NodeAnnouncement {
389         pub(crate) signature: Signature,
390         pub(crate) contents: UnsignedNodeAnnouncement,
391 }
392
393 // Only exposed as broadcast of channel_announcement should be filtered by node_id
394 /// The unsigned part of a channel_announcement
395 #[derive(PartialEq, Clone, Debug)]
396 pub struct UnsignedChannelAnnouncement {
397         pub(crate) features: ChannelFeatures,
398         pub(crate) chain_hash: Sha256dHash,
399         pub(crate) short_channel_id: u64,
400         /// One of the two node_ids which are endpoints of this channel
401         pub        node_id_1: PublicKey,
402         /// The other of the two node_ids which are endpoints of this channel
403         pub        node_id_2: PublicKey,
404         pub(crate) bitcoin_key_1: PublicKey,
405         pub(crate) bitcoin_key_2: PublicKey,
406         pub(crate) excess_data: Vec<u8>,
407 }
408 /// A channel_announcement message to be sent or received from a peer
409 #[derive(PartialEq, Clone, Debug)]
410 pub struct ChannelAnnouncement {
411         pub(crate) node_signature_1: Signature,
412         pub(crate) node_signature_2: Signature,
413         pub(crate) bitcoin_signature_1: Signature,
414         pub(crate) bitcoin_signature_2: Signature,
415         pub(crate) contents: UnsignedChannelAnnouncement,
416 }
417
418 #[derive(PartialEq, Clone, Debug)]
419 pub(crate) struct UnsignedChannelUpdate {
420         pub(crate) chain_hash: Sha256dHash,
421         pub(crate) short_channel_id: u64,
422         pub(crate) timestamp: u32,
423         pub(crate) flags: u16,
424         pub(crate) cltv_expiry_delta: u16,
425         pub(crate) htlc_minimum_msat: u64,
426         pub(crate) fee_base_msat: u32,
427         pub(crate) fee_proportional_millionths: u32,
428         pub(crate) excess_data: Vec<u8>,
429 }
430 /// A channel_update message to be sent or received from a peer
431 #[derive(PartialEq, Clone, Debug)]
432 pub struct ChannelUpdate {
433         pub(crate) signature: Signature,
434         pub(crate) contents: UnsignedChannelUpdate,
435 }
436
437 /// Used to put an error message in a LightningError
438 #[derive(Clone)]
439 pub enum ErrorAction {
440         /// The peer took some action which made us think they were useless. Disconnect them.
441         DisconnectPeer {
442                 /// An error message which we should make an effort to send before we disconnect.
443                 msg: Option<ErrorMessage>
444         },
445         /// The peer did something harmless that we weren't able to process, just log and ignore
446         IgnoreError,
447         /// The peer did something incorrect. Tell them.
448         SendErrorMessage {
449                 /// The message to send.
450                 msg: ErrorMessage
451         },
452 }
453
454 /// An Err type for failure to process messages.
455 pub struct LightningError {
456         /// A human-readable message describing the error
457         pub err: &'static str,
458         /// The action which should be taken against the offending peer.
459         pub action: ErrorAction,
460 }
461
462 /// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
463 /// transaction updates if they were pending.
464 #[derive(PartialEq, Clone)]
465 pub struct CommitmentUpdate {
466         /// update_add_htlc messages which should be sent
467         pub update_add_htlcs: Vec<UpdateAddHTLC>,
468         /// update_fulfill_htlc messages which should be sent
469         pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
470         /// update_fail_htlc messages which should be sent
471         pub update_fail_htlcs: Vec<UpdateFailHTLC>,
472         /// update_fail_malformed_htlc messages which should be sent
473         pub update_fail_malformed_htlcs: Vec<UpdateFailMalformedHTLC>,
474         /// An update_fee message which should be sent
475         pub update_fee: Option<UpdateFee>,
476         /// Finally, the commitment_signed message which should be sent
477         pub commitment_signed: CommitmentSigned,
478 }
479
480 /// The information we received from a peer along the route of a payment we originated. This is
481 /// returned by ChannelMessageHandler::handle_update_fail_htlc to be passed into
482 /// RoutingMessageHandler::handle_htlc_fail_channel_update to update our network map.
483 #[derive(Clone)]
484 pub enum HTLCFailChannelUpdate {
485         /// We received an error which included a full ChannelUpdate message.
486         ChannelUpdateMessage {
487                 /// The unwrapped message we received
488                 msg: ChannelUpdate,
489         },
490         /// We received an error which indicated only that a channel has been closed
491         ChannelClosed {
492                 /// The short_channel_id which has now closed.
493                 short_channel_id: u64,
494                 /// when this true, this channel should be permanently removed from the
495                 /// consideration. Otherwise, this channel can be restored as new channel_update is received
496                 is_permanent: bool,
497         },
498         /// We received an error which indicated only that a node has failed
499         NodeFailure {
500                 /// The node_id that has failed.
501                 node_id: PublicKey,
502                 /// when this true, node should be permanently removed from the
503                 /// consideration. Otherwise, the channels connected to this node can be
504                 /// restored as new channel_update is received
505                 is_permanent: bool,
506         }
507 }
508
509 /// Messages could have optional fields to use with extended features
510 /// As we wish to serialize these differently from Option<T>s (Options get a tag byte, but
511 /// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a
512 /// separate enum type for them.
513 #[derive(Clone, PartialEq)]
514 pub enum OptionalField<T> {
515         /// Optional field is included in message
516         Present(T),
517         /// Optional field is absent in message
518         Absent
519 }
520
521 /// A trait to describe an object which can receive channel messages.
522 ///
523 /// Messages MAY be called in parallel when they originate from different their_node_ids, however
524 /// they MUST NOT be called in parallel when the two calls have the same their_node_id.
525 pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Sync {
526         //Channel init:
527         /// Handle an incoming open_channel message from the given peer.
528         fn handle_open_channel(&self, their_node_id: &PublicKey, their_features: InitFeatures, msg: &OpenChannel);
529         /// Handle an incoming accept_channel message from the given peer.
530         fn handle_accept_channel(&self, their_node_id: &PublicKey, their_features: InitFeatures, msg: &AcceptChannel);
531         /// Handle an incoming funding_created message from the given peer.
532         fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated);
533         /// Handle an incoming funding_signed message from the given peer.
534         fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned);
535         /// Handle an incoming funding_locked message from the given peer.
536         fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked);
537
538         // Channl close:
539         /// Handle an incoming shutdown message from the given peer.
540         fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown);
541         /// Handle an incoming closing_signed message from the given peer.
542         fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned);
543
544         // HTLC handling:
545         /// Handle an incoming update_add_htlc message from the given peer.
546         fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC);
547         /// Handle an incoming update_fulfill_htlc message from the given peer.
548         fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC);
549         /// Handle an incoming update_fail_htlc message from the given peer.
550         fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC);
551         /// Handle an incoming update_fail_malformed_htlc message from the given peer.
552         fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC);
553         /// Handle an incoming commitment_signed message from the given peer.
554         fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned);
555         /// Handle an incoming revoke_and_ack message from the given peer.
556         fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK);
557
558         /// Handle an incoming update_fee message from the given peer.
559         fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee);
560
561         // Channel-to-announce:
562         /// Handle an incoming announcement_signatures message from the given peer.
563         fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures);
564
565         // Connection loss/reestablish:
566         /// Indicates a connection to the peer failed/an existing connection was lost. If no connection
567         /// is believed to be possible in the future (eg they're sending us messages we don't
568         /// understand or indicate they require unknown feature bits), no_connection_possible is set
569         /// and any outstanding channels should be failed.
570         fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
571
572         /// Handle a peer reconnecting, possibly generating channel_reestablish message(s).
573         fn peer_connected(&self, their_node_id: &PublicKey);
574         /// Handle an incoming channel_reestablish message from the given peer.
575         fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish);
576
577         // Error:
578         /// Handle an incoming error message from the given peer.
579         fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
580 }
581
582 /// A trait to describe an object which can receive routing messages.
583 pub trait RoutingMessageHandler : Send + Sync {
584         /// Handle an incoming node_announcement message, returning true if it should be forwarded on,
585         /// false or returning an Err otherwise.
586         fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<bool, LightningError>;
587         /// Handle a channel_announcement message, returning true if it should be forwarded on, false
588         /// or returning an Err otherwise.
589         fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, LightningError>;
590         /// Handle an incoming channel_update message, returning true if it should be forwarded on,
591         /// false or returning an Err otherwise.
592         fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, LightningError>;
593         /// Handle some updates to the route graph that we learned due to an outbound failed payment.
594         fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
595         /// Gets a subset of the channel announcements and updates required to dump our routing table
596         /// to a remote node, starting at the short_channel_id indicated by starting_point and
597         /// including batch_amount entries.
598         fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, ChannelUpdate, ChannelUpdate)>;
599         /// Gets a subset of the node announcements required to dump our routing table to a remote node,
600         /// starting at the node *after* the provided publickey and including batch_amount entries.
601         /// If None is provided for starting_point, we start at the first node.
602         fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement>;
603 }
604
605 pub(crate) struct OnionRealm0HopData {
606         pub(crate) short_channel_id: u64,
607         pub(crate) amt_to_forward: u64,
608         pub(crate) outgoing_cltv_value: u32,
609         // 12 bytes of 0-padding
610 }
611
612 mod fuzzy_internal_msgs {
613         // These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
614         // them from untrusted input):
615
616         use super::OnionRealm0HopData;
617         pub struct OnionHopData {
618                 pub(crate) realm: u8,
619                 pub(crate) data: OnionRealm0HopData,
620                 pub(crate) hmac: [u8; 32],
621         }
622
623         pub struct DecodedOnionErrorPacket {
624                 pub(crate) hmac: [u8; 32],
625                 pub(crate) failuremsg: Vec<u8>,
626                 pub(crate) pad: Vec<u8>,
627         }
628 }
629 #[cfg(feature = "fuzztarget")]
630 pub use self::fuzzy_internal_msgs::*;
631 #[cfg(not(feature = "fuzztarget"))]
632 pub(crate) use self::fuzzy_internal_msgs::*;
633
634 #[derive(Clone)]
635 pub(crate) struct OnionPacket {
636         pub(crate) version: u8,
637         /// In order to ensure we always return an error on Onion decode in compliance with BOLT 4, we
638         /// have to deserialize OnionPackets contained in UpdateAddHTLCs even if the ephemeral public
639         /// key (here) is bogus, so we hold a Result instead of a PublicKey as we'd like.
640         pub(crate) public_key: Result<PublicKey, secp256k1::Error>,
641         pub(crate) hop_data: [u8; 20*65],
642         pub(crate) hmac: [u8; 32],
643 }
644
645 impl PartialEq for OnionPacket {
646         fn eq(&self, other: &OnionPacket) -> bool {
647                 for (i, j) in self.hop_data.iter().zip(other.hop_data.iter()) {
648                         if i != j { return false; }
649                 }
650                 self.version == other.version &&
651                         self.public_key == other.public_key &&
652                         self.hmac == other.hmac
653         }
654 }
655
656 #[derive(Clone, PartialEq)]
657 pub(crate) struct OnionErrorPacket {
658         // This really should be a constant size slice, but the spec lets these things be up to 128KB?
659         // (TODO) We limit it in decode to much lower...
660         pub(crate) data: Vec<u8>,
661 }
662
663 impl Error for DecodeError {
664         fn description(&self) -> &str {
665                 match *self {
666                         DecodeError::UnknownVersion => "Unknown realm byte in Onion packet",
667                         DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode",
668                         DecodeError::InvalidValue => "Nonsense bytes didn't map to the type they were interpreted as",
669                         DecodeError::ShortRead => "Packet extended beyond the provided bytes",
670                         DecodeError::ExtraAddressesPerType => "More than one address of a single type",
671                         DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
672                         DecodeError::Io(ref e) => e.description(),
673                 }
674         }
675 }
676 impl fmt::Display for DecodeError {
677         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
678                 f.write_str(self.description())
679         }
680 }
681
682 impl fmt::Debug for LightningError {
683         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
684                 f.write_str(self.err)
685         }
686 }
687
688 impl From<::std::io::Error> for DecodeError {
689         fn from(e: ::std::io::Error) -> Self {
690                 if e.kind() == ::std::io::ErrorKind::UnexpectedEof {
691                         DecodeError::ShortRead
692                 } else {
693                         DecodeError::Io(e)
694                 }
695         }
696 }
697
698 impl Writeable for OptionalField<Script> {
699         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
700                 match *self {
701                         OptionalField::Present(ref script) => {
702                                 // Note that Writeable for script includes the 16-bit length tag for us
703                                 script.write(w)?;
704                         },
705                         OptionalField::Absent => {}
706                 }
707                 Ok(())
708         }
709 }
710
711 impl<R: Read> Readable<R> for OptionalField<Script> {
712         fn read(r: &mut R) -> Result<Self, DecodeError> {
713                 match <u16 as Readable<R>>::read(r) {
714                         Ok(len) => {
715                                 let mut buf = vec![0; len as usize];
716                                 r.read_exact(&mut buf)?;
717                                 Ok(OptionalField::Present(Script::from(buf)))
718                         },
719                         Err(DecodeError::ShortRead) => Ok(OptionalField::Absent),
720                         Err(e) => Err(e)
721                 }
722         }
723 }
724
725 impl_writeable_len_match!(AcceptChannel, {
726                 {AcceptChannel{ shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 270 + 2 + script.len()},
727                 {_, 270}
728         }, {
729         temporary_channel_id,
730         dust_limit_satoshis,
731         max_htlc_value_in_flight_msat,
732         channel_reserve_satoshis,
733         htlc_minimum_msat,
734         minimum_depth,
735         to_self_delay,
736         max_accepted_htlcs,
737         funding_pubkey,
738         revocation_basepoint,
739         payment_basepoint,
740         delayed_payment_basepoint,
741         htlc_basepoint,
742         first_per_commitment_point,
743         shutdown_scriptpubkey
744 });
745
746 impl_writeable!(AnnouncementSignatures, 32+8+64*2, {
747         channel_id,
748         short_channel_id,
749         node_signature,
750         bitcoin_signature
751 });
752
753 impl Writeable for ChannelReestablish {
754         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
755                 w.size_hint(if let OptionalField::Present(..) = self.data_loss_protect { 32+2*8+33+32 } else { 32+2*8 });
756                 self.channel_id.write(w)?;
757                 self.next_local_commitment_number.write(w)?;
758                 self.next_remote_commitment_number.write(w)?;
759                 match self.data_loss_protect {
760                         OptionalField::Present(ref data_loss_protect) => {
761                                 (*data_loss_protect).your_last_per_commitment_secret.write(w)?;
762                                 (*data_loss_protect).my_current_per_commitment_point.write(w)?;
763                         },
764                         OptionalField::Absent => {}
765                 }
766                 Ok(())
767         }
768 }
769
770 impl<R: Read> Readable<R> for ChannelReestablish{
771         fn read(r: &mut R) -> Result<Self, DecodeError> {
772                 Ok(Self {
773                         channel_id: Readable::read(r)?,
774                         next_local_commitment_number: Readable::read(r)?,
775                         next_remote_commitment_number: Readable::read(r)?,
776                         data_loss_protect: {
777                                 match <[u8; 32] as Readable<R>>::read(r) {
778                                         Ok(your_last_per_commitment_secret) =>
779                                                 OptionalField::Present(DataLossProtect {
780                                                         your_last_per_commitment_secret,
781                                                         my_current_per_commitment_point: Readable::read(r)?,
782                                                 }),
783                                         Err(DecodeError::ShortRead) => OptionalField::Absent,
784                                         Err(e) => return Err(e)
785                                 }
786                         }
787                 })
788         }
789 }
790
791 impl_writeable!(ClosingSigned, 32+8+64, {
792         channel_id,
793         fee_satoshis,
794         signature
795 });
796
797 impl_writeable_len_match!(CommitmentSigned, {
798                 { CommitmentSigned { ref htlc_signatures, .. }, 32+64+2+htlc_signatures.len()*64 }
799         }, {
800         channel_id,
801         signature,
802         htlc_signatures
803 });
804
805 impl_writeable_len_match!(DecodedOnionErrorPacket, {
806                 { DecodedOnionErrorPacket { ref failuremsg, ref pad, .. }, 32 + 4 + failuremsg.len() + pad.len() }
807         }, {
808         hmac,
809         failuremsg,
810         pad
811 });
812
813 impl_writeable!(FundingCreated, 32+32+2+64, {
814         temporary_channel_id,
815         funding_txid,
816         funding_output_index,
817         signature
818 });
819
820 impl_writeable!(FundingSigned, 32+64, {
821         channel_id,
822         signature
823 });
824
825 impl_writeable!(FundingLocked, 32+33, {
826         channel_id,
827         next_per_commitment_point
828 });
829
830 impl Writeable for Init {
831         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
832                 // global_features gets the bottom 13 bits of our features, and local_features gets all of
833                 // our relevant feature bits. This keeps us compatible with old nodes.
834                 self.features.write_up_to_13(w)?;
835                 self.features.write(w)
836         }
837 }
838
839 impl<R: Read> Readable<R> for Init {
840         fn read(r: &mut R) -> Result<Self, DecodeError> {
841                 let global_features: InitFeatures = Readable::read(r)?;
842                 let features: InitFeatures = Readable::read(r)?;
843                 Ok(Init {
844                         features: features.or(global_features),
845                 })
846         }
847 }
848
849 impl_writeable_len_match!(OpenChannel, {
850                 { OpenChannel { shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 319 + 2 + script.len() },
851                 { _, 319 }
852         }, {
853         chain_hash,
854         temporary_channel_id,
855         funding_satoshis,
856         push_msat,
857         dust_limit_satoshis,
858         max_htlc_value_in_flight_msat,
859         channel_reserve_satoshis,
860         htlc_minimum_msat,
861         feerate_per_kw,
862         to_self_delay,
863         max_accepted_htlcs,
864         funding_pubkey,
865         revocation_basepoint,
866         payment_basepoint,
867         delayed_payment_basepoint,
868         htlc_basepoint,
869         first_per_commitment_point,
870         channel_flags,
871         shutdown_scriptpubkey
872 });
873
874 impl_writeable!(RevokeAndACK, 32+32+33, {
875         channel_id,
876         per_commitment_secret,
877         next_per_commitment_point
878 });
879
880 impl_writeable_len_match!(Shutdown, {
881                 { Shutdown { ref scriptpubkey, .. }, 32 + 2 + scriptpubkey.len() }
882         }, {
883         channel_id,
884         scriptpubkey
885 });
886
887 impl_writeable_len_match!(UpdateFailHTLC, {
888                 { UpdateFailHTLC { ref reason, .. }, 32 + 10 + reason.data.len() }
889         }, {
890         channel_id,
891         htlc_id,
892         reason
893 });
894
895 impl_writeable!(UpdateFailMalformedHTLC, 32+8+32+2, {
896         channel_id,
897         htlc_id,
898         sha256_of_onion,
899         failure_code
900 });
901
902 impl_writeable!(UpdateFee, 32+4, {
903         channel_id,
904         feerate_per_kw
905 });
906
907 impl_writeable!(UpdateFulfillHTLC, 32+8+32, {
908         channel_id,
909         htlc_id,
910         payment_preimage
911 });
912
913 impl_writeable_len_match!(OnionErrorPacket, {
914                 { OnionErrorPacket { ref data, .. }, 2 + data.len() }
915         }, {
916         data
917 });
918
919 impl Writeable for OnionPacket {
920         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
921                 w.size_hint(1 + 33 + 20*65 + 32);
922                 self.version.write(w)?;
923                 match self.public_key {
924                         Ok(pubkey) => pubkey.write(w)?,
925                         Err(_) => [0u8;33].write(w)?,
926                 }
927                 w.write_all(&self.hop_data)?;
928                 self.hmac.write(w)?;
929                 Ok(())
930         }
931 }
932
933 impl<R: Read> Readable<R> for OnionPacket {
934         fn read(r: &mut R) -> Result<Self, DecodeError> {
935                 Ok(OnionPacket {
936                         version: Readable::read(r)?,
937                         public_key: {
938                                 let mut buf = [0u8;33];
939                                 r.read_exact(&mut buf)?;
940                                 PublicKey::from_slice(&buf)
941                         },
942                         hop_data: Readable::read(r)?,
943                         hmac: Readable::read(r)?,
944                 })
945         }
946 }
947
948 impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
949         channel_id,
950         htlc_id,
951         amount_msat,
952         payment_hash,
953         cltv_expiry,
954         onion_routing_packet
955 });
956
957 impl Writeable for OnionRealm0HopData {
958         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
959                 w.size_hint(32);
960                 self.short_channel_id.write(w)?;
961                 self.amt_to_forward.write(w)?;
962                 self.outgoing_cltv_value.write(w)?;
963                 w.write_all(&[0;12])?;
964                 Ok(())
965         }
966 }
967
968 impl<R: Read> Readable<R> for OnionRealm0HopData {
969         fn read(r: &mut R) -> Result<Self, DecodeError> {
970                 Ok(OnionRealm0HopData {
971                         short_channel_id: Readable::read(r)?,
972                         amt_to_forward: Readable::read(r)?,
973                         outgoing_cltv_value: {
974                                 let v: u32 = Readable::read(r)?;
975                                 r.read_exact(&mut [0; 12])?;
976                                 v
977                         }
978                 })
979         }
980 }
981
982 impl Writeable for OnionHopData {
983         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
984                 w.size_hint(65);
985                 self.realm.write(w)?;
986                 self.data.write(w)?;
987                 self.hmac.write(w)?;
988                 Ok(())
989         }
990 }
991
992 impl<R: Read> Readable<R> for OnionHopData {
993         fn read(r: &mut R) -> Result<Self, DecodeError> {
994                 Ok(OnionHopData {
995                         realm: {
996                                 let r: u8 = Readable::read(r)?;
997                                 if r != 0 {
998                                         return Err(DecodeError::UnknownVersion);
999                                 }
1000                                 r
1001                         },
1002                         data: Readable::read(r)?,
1003                         hmac: Readable::read(r)?,
1004                 })
1005         }
1006 }
1007
1008 impl Writeable for Ping {
1009         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1010                 w.size_hint(self.byteslen as usize + 4);
1011                 self.ponglen.write(w)?;
1012                 vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
1013                 Ok(())
1014         }
1015 }
1016
1017 impl<R: Read> Readable<R> for Ping {
1018         fn read(r: &mut R) -> Result<Self, DecodeError> {
1019                 Ok(Ping {
1020                         ponglen: Readable::read(r)?,
1021                         byteslen: {
1022                                 let byteslen = Readable::read(r)?;
1023                                 r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
1024                                 byteslen
1025                         }
1026                 })
1027         }
1028 }
1029
1030 impl Writeable for Pong {
1031         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1032                 w.size_hint(self.byteslen as usize + 2);
1033                 vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
1034                 Ok(())
1035         }
1036 }
1037
1038 impl<R: Read> Readable<R> for Pong {
1039         fn read(r: &mut R) -> Result<Self, DecodeError> {
1040                 Ok(Pong {
1041                         byteslen: {
1042                                 let byteslen = Readable::read(r)?;
1043                                 r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
1044                                 byteslen
1045                         }
1046                 })
1047         }
1048 }
1049
1050 impl Writeable for UnsignedChannelAnnouncement {
1051         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1052                 w.size_hint(2 + 2*32 + 4*33 + self.features.byte_count() + self.excess_data.len());
1053                 self.features.write(w)?;
1054                 self.chain_hash.write(w)?;
1055                 self.short_channel_id.write(w)?;
1056                 self.node_id_1.write(w)?;
1057                 self.node_id_2.write(w)?;
1058                 self.bitcoin_key_1.write(w)?;
1059                 self.bitcoin_key_2.write(w)?;
1060                 w.write_all(&self.excess_data[..])?;
1061                 Ok(())
1062         }
1063 }
1064
1065 impl<R: Read> Readable<R> for UnsignedChannelAnnouncement {
1066         fn read(r: &mut R) -> Result<Self, DecodeError> {
1067                 Ok(Self {
1068                         features: Readable::read(r)?,
1069                         chain_hash: Readable::read(r)?,
1070                         short_channel_id: Readable::read(r)?,
1071                         node_id_1: Readable::read(r)?,
1072                         node_id_2: Readable::read(r)?,
1073                         bitcoin_key_1: Readable::read(r)?,
1074                         bitcoin_key_2: Readable::read(r)?,
1075                         excess_data: {
1076                                 let mut excess_data = vec![];
1077                                 r.read_to_end(&mut excess_data)?;
1078                                 excess_data
1079                         },
1080                 })
1081         }
1082 }
1083
1084 impl_writeable_len_match!(ChannelAnnouncement, {
1085                 { ChannelAnnouncement { contents: UnsignedChannelAnnouncement {ref features, ref excess_data, ..}, .. },
1086                         2 + 2*32 + 4*33 + features.byte_count() + excess_data.len() + 4*64 }
1087         }, {
1088         node_signature_1,
1089         node_signature_2,
1090         bitcoin_signature_1,
1091         bitcoin_signature_2,
1092         contents
1093 });
1094
1095 impl Writeable for UnsignedChannelUpdate {
1096         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1097                 w.size_hint(64 + self.excess_data.len());
1098                 self.chain_hash.write(w)?;
1099                 self.short_channel_id.write(w)?;
1100                 self.timestamp.write(w)?;
1101                 self.flags.write(w)?;
1102                 self.cltv_expiry_delta.write(w)?;
1103                 self.htlc_minimum_msat.write(w)?;
1104                 self.fee_base_msat.write(w)?;
1105                 self.fee_proportional_millionths.write(w)?;
1106                 w.write_all(&self.excess_data[..])?;
1107                 Ok(())
1108         }
1109 }
1110
1111 impl<R: Read> Readable<R> for UnsignedChannelUpdate {
1112         fn read(r: &mut R) -> Result<Self, DecodeError> {
1113                 Ok(Self {
1114                         chain_hash: Readable::read(r)?,
1115                         short_channel_id: Readable::read(r)?,
1116                         timestamp: Readable::read(r)?,
1117                         flags: Readable::read(r)?,
1118                         cltv_expiry_delta: Readable::read(r)?,
1119                         htlc_minimum_msat: Readable::read(r)?,
1120                         fee_base_msat: Readable::read(r)?,
1121                         fee_proportional_millionths: Readable::read(r)?,
1122                         excess_data: {
1123                                 let mut excess_data = vec![];
1124                                 r.read_to_end(&mut excess_data)?;
1125                                 excess_data
1126                         },
1127                 })
1128         }
1129 }
1130
1131 impl_writeable_len_match!(ChannelUpdate, {
1132                 { ChannelUpdate { contents: UnsignedChannelUpdate {ref excess_data, ..}, .. },
1133                         64 + excess_data.len() + 64 }
1134         }, {
1135         signature,
1136         contents
1137 });
1138
1139 impl Writeable for ErrorMessage {
1140         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1141                 w.size_hint(32 + 2 + self.data.len());
1142                 self.channel_id.write(w)?;
1143                 (self.data.len() as u16).write(w)?;
1144                 w.write_all(self.data.as_bytes())?;
1145                 Ok(())
1146         }
1147 }
1148
1149 impl<R: Read> Readable<R> for ErrorMessage {
1150         fn read(r: &mut R) -> Result<Self, DecodeError> {
1151                 Ok(Self {
1152                         channel_id: Readable::read(r)?,
1153                         data: {
1154                                 let mut sz: usize = <u16 as Readable<R>>::read(r)? as usize;
1155                                 let mut data = vec![];
1156                                 let data_len = r.read_to_end(&mut data)?;
1157                                 sz = cmp::min(data_len, sz);
1158                                 match String::from_utf8(data[..sz as usize].to_vec()) {
1159                                         Ok(s) => s,
1160                                         Err(_) => return Err(DecodeError::InvalidValue),
1161                                 }
1162                         }
1163                 })
1164         }
1165 }
1166
1167 impl Writeable for UnsignedNodeAnnouncement {
1168         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1169                 w.size_hint(64 + 76 + self.features.byte_count() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len());
1170                 self.features.write(w)?;
1171                 self.timestamp.write(w)?;
1172                 self.node_id.write(w)?;
1173                 w.write_all(&self.rgb)?;
1174                 self.alias.write(w)?;
1175
1176                 let mut addrs_to_encode = self.addresses.clone();
1177                 addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
1178                 addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
1179                 let mut addr_len = 0;
1180                 for addr in &addrs_to_encode {
1181                         addr_len += 1 + addr.len();
1182                 }
1183                 (addr_len + self.excess_address_data.len() as u16).write(w)?;
1184                 for addr in addrs_to_encode {
1185                         addr.write(w)?;
1186                 }
1187                 w.write_all(&self.excess_address_data[..])?;
1188                 w.write_all(&self.excess_data[..])?;
1189                 Ok(())
1190         }
1191 }
1192
1193 impl<R: Read> Readable<R> for UnsignedNodeAnnouncement {
1194         fn read(r: &mut R) -> Result<Self, DecodeError> {
1195                 let features: NodeFeatures = Readable::read(r)?;
1196                 let timestamp: u32 = Readable::read(r)?;
1197                 let node_id: PublicKey = Readable::read(r)?;
1198                 let mut rgb = [0; 3];
1199                 r.read_exact(&mut rgb)?;
1200                 let alias: [u8; 32] = Readable::read(r)?;
1201
1202                 let addr_len: u16 = Readable::read(r)?;
1203                 let mut addresses: Vec<NetAddress> = Vec::with_capacity(4);
1204                 let mut addr_readpos = 0;
1205                 let mut excess = false;
1206                 let mut excess_byte = 0;
1207                 loop {
1208                         if addr_len <= addr_readpos { break; }
1209                         match Readable::read(r) {
1210                                 Ok(Ok(addr)) => {
1211                                         match addr {
1212                                                 NetAddress::IPv4 { .. } => {
1213                                                         if addresses.len() > 0 {
1214                                                                 return Err(DecodeError::ExtraAddressesPerType);
1215                                                         }
1216                                                 },
1217                                                 NetAddress::IPv6 { .. } => {
1218                                                         if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
1219                                                                 return Err(DecodeError::ExtraAddressesPerType);
1220                                                         }
1221                                                 },
1222                                                 NetAddress::OnionV2 { .. } => {
1223                                                         if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
1224                                                                 return Err(DecodeError::ExtraAddressesPerType);
1225                                                         }
1226                                                 },
1227                                                 NetAddress::OnionV3 { .. } => {
1228                                                         if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
1229                                                                 return Err(DecodeError::ExtraAddressesPerType);
1230                                                         }
1231                                                 },
1232                                         }
1233                                         if addr_len < addr_readpos + 1 + addr.len() {
1234                                                 return Err(DecodeError::BadLengthDescriptor);
1235                                         }
1236                                         addr_readpos += (1 + addr.len()) as u16;
1237                                         addresses.push(addr);
1238                                 },
1239                                 Ok(Err(unknown_descriptor)) => {
1240                                         excess = true;
1241                                         excess_byte = unknown_descriptor;
1242                                         break;
1243                                 },
1244                                 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
1245                                 Err(e) => return Err(e),
1246                         }
1247                 }
1248
1249                 let mut excess_data = vec![];
1250                 let excess_address_data = if addr_readpos < addr_len {
1251                         let mut excess_address_data = vec![0; (addr_len - addr_readpos) as usize];
1252                         r.read_exact(&mut excess_address_data[if excess { 1 } else { 0 }..])?;
1253                         if excess {
1254                                 excess_address_data[0] = excess_byte;
1255                         }
1256                         excess_address_data
1257                 } else {
1258                         if excess {
1259                                 excess_data.push(excess_byte);
1260                         }
1261                         Vec::new()
1262                 };
1263                 r.read_to_end(&mut excess_data)?;
1264                 Ok(UnsignedNodeAnnouncement {
1265                         features,
1266                         timestamp,
1267                         node_id,
1268                         rgb,
1269                         alias,
1270                         addresses,
1271                         excess_address_data,
1272                         excess_data,
1273                 })
1274         }
1275 }
1276
1277 impl_writeable_len_match!(NodeAnnouncement, {
1278                 { NodeAnnouncement { contents: UnsignedNodeAnnouncement { ref features, ref addresses, ref excess_address_data, ref excess_data, ..}, .. },
1279                         64 + 76 + features.byte_count() + addresses.len()*38 + excess_address_data.len() + excess_data.len() }
1280         }, {
1281         signature,
1282         contents
1283 });
1284
1285 #[cfg(test)]
1286 mod tests {
1287         use hex;
1288         use ln::msgs;
1289         use ln::msgs::{ChannelFeatures, InitFeatures, NodeFeatures, OptionalField, OnionErrorPacket};
1290         use ln::channelmanager::{PaymentPreimage, PaymentHash};
1291         use util::ser::Writeable;
1292
1293         use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1294         use bitcoin_hashes::hex::FromHex;
1295         use bitcoin::util::address::Address;
1296         use bitcoin::network::constants::Network;
1297         use bitcoin::blockdata::script::Builder;
1298         use bitcoin::blockdata::opcodes;
1299
1300         use secp256k1::key::{PublicKey,SecretKey};
1301         use secp256k1::{Secp256k1, Message};
1302
1303         #[test]
1304         fn encoding_channel_reestablish_no_secret() {
1305                 let cr = msgs::ChannelReestablish {
1306                         channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
1307                         next_local_commitment_number: 3,
1308                         next_remote_commitment_number: 4,
1309                         data_loss_protect: OptionalField::Absent,
1310                 };
1311
1312                 let encoded_value = cr.encode();
1313                 assert_eq!(
1314                         encoded_value,
1315                         vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4]
1316                 );
1317         }
1318
1319         #[test]
1320         fn encoding_channel_reestablish_with_secret() {
1321                 let public_key = {
1322                         let secp_ctx = Secp256k1::new();
1323                         PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
1324                 };
1325
1326                 let cr = msgs::ChannelReestablish {
1327                         channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
1328                         next_local_commitment_number: 3,
1329                         next_remote_commitment_number: 4,
1330                         data_loss_protect: OptionalField::Present(msgs::DataLossProtect { your_last_per_commitment_secret: [9;32], my_current_per_commitment_point: public_key}),
1331                 };
1332
1333                 let encoded_value = cr.encode();
1334                 assert_eq!(
1335                         encoded_value,
1336                         vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143]
1337                 );
1338         }
1339
1340         macro_rules! get_keys_from {
1341                 ($slice: expr, $secp_ctx: expr) => {
1342                         {
1343                                 let privkey = SecretKey::from_slice(&hex::decode($slice).unwrap()[..]).unwrap();
1344                                 let pubkey = PublicKey::from_secret_key(&$secp_ctx, &privkey);
1345                                 (privkey, pubkey)
1346                         }
1347                 }
1348         }
1349
1350         macro_rules! get_sig_on {
1351                 ($privkey: expr, $ctx: expr, $string: expr) => {
1352                         {
1353                                 let sighash = Message::from_slice(&$string.into_bytes()[..]).unwrap();
1354                                 $ctx.sign(&sighash, &$privkey)
1355                         }
1356                 }
1357         }
1358
1359         #[test]
1360         fn encoding_announcement_signatures() {
1361                 let secp_ctx = Secp256k1::new();
1362                 let (privkey, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1363                 let sig_1 = get_sig_on!(privkey, secp_ctx, String::from("01010101010101010101010101010101"));
1364                 let sig_2 = get_sig_on!(privkey, secp_ctx, String::from("02020202020202020202020202020202"));
1365                 let announcement_signatures = msgs::AnnouncementSignatures {
1366                         channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
1367                         short_channel_id: 2316138423780173,
1368                         node_signature: sig_1,
1369                         bitcoin_signature: sig_2,
1370                 };
1371
1372                 let encoded_value = announcement_signatures.encode();
1373                 assert_eq!(encoded_value, hex::decode("040000000000000005000000000000000600000000000000070000000000000000083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073acf9953cef4700860f5967838eba2bae89288ad188ebf8b20bf995c3ea53a26df1876d0a3a0e13172ba286a673140190c02ba9da60a2e43a745188c8a83c7f3ef").unwrap());
1374         }
1375
1376         fn do_encoding_channel_announcement(unknown_features_bits: bool, non_bitcoin_chain_hash: bool, excess_data: bool) {
1377                 let secp_ctx = Secp256k1::new();
1378                 let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1379                 let (privkey_2, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1380                 let (privkey_3, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1381                 let (privkey_4, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1382                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1383                 let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
1384                 let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
1385                 let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
1386                 let mut features = ChannelFeatures::supported();
1387                 if unknown_features_bits {
1388                         features = ChannelFeatures::from_le_bytes(vec![0xFF, 0xFF]);
1389                 }
1390                 let unsigned_channel_announcement = msgs::UnsignedChannelAnnouncement {
1391                         features,
1392                         chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1393                         short_channel_id: 2316138423780173,
1394                         node_id_1: pubkey_1,
1395                         node_id_2: pubkey_2,
1396                         bitcoin_key_1: pubkey_3,
1397                         bitcoin_key_2: pubkey_4,
1398                         excess_data: if excess_data { vec![10, 0, 0, 20, 0, 0, 30, 0, 0, 40] } else { Vec::new() },
1399                 };
1400                 let channel_announcement = msgs::ChannelAnnouncement {
1401                         node_signature_1: sig_1,
1402                         node_signature_2: sig_2,
1403                         bitcoin_signature_1: sig_3,
1404                         bitcoin_signature_2: sig_4,
1405                         contents: unsigned_channel_announcement,
1406                 };
1407                 let encoded_value = channel_announcement.encode();
1408                 let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a1735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap();
1409                 if unknown_features_bits {
1410                         target_value.append(&mut hex::decode("0002ffff").unwrap());
1411                 } else {
1412                         target_value.append(&mut hex::decode("0000").unwrap());
1413                 }
1414                 if non_bitcoin_chain_hash {
1415                         target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
1416                 } else {
1417                         target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
1418                 }
1419                 target_value.append(&mut hex::decode("00083a840000034d031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b").unwrap());
1420                 if excess_data {
1421                         target_value.append(&mut hex::decode("0a00001400001e000028").unwrap());
1422                 }
1423                 assert_eq!(encoded_value, target_value);
1424         }
1425
1426         #[test]
1427         fn encoding_channel_announcement() {
1428                 do_encoding_channel_announcement(false, false, false);
1429                 do_encoding_channel_announcement(true, false, false);
1430                 do_encoding_channel_announcement(true, true, false);
1431                 do_encoding_channel_announcement(true, true, true);
1432                 do_encoding_channel_announcement(false, true, true);
1433                 do_encoding_channel_announcement(false, false, true);
1434                 do_encoding_channel_announcement(false, true, false);
1435                 do_encoding_channel_announcement(true, false, true);
1436         }
1437
1438         fn do_encoding_node_announcement(unknown_features_bits: bool, ipv4: bool, ipv6: bool, onionv2: bool, onionv3: bool, excess_address_data: bool, excess_data: bool) {
1439                 let secp_ctx = Secp256k1::new();
1440                 let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1441                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1442                 let features = if unknown_features_bits {
1443                         NodeFeatures::from_le_bytes(vec![0xFF, 0xFF])
1444                 } else {
1445                         // Set to some features we may support
1446                         NodeFeatures::from_le_bytes(vec![2 | 1 << 5])
1447                 };
1448                 let mut addresses = Vec::new();
1449                 if ipv4 {
1450                         addresses.push(msgs::NetAddress::IPv4 {
1451                                 addr: [255, 254, 253, 252],
1452                                 port: 9735
1453                         });
1454                 }
1455                 if ipv6 {
1456                         addresses.push(msgs::NetAddress::IPv6 {
1457                                 addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240],
1458                                 port: 9735
1459                         });
1460                 }
1461                 if onionv2 {
1462                         addresses.push(msgs::NetAddress::OnionV2 {
1463                                 addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246],
1464                                 port: 9735
1465                         });
1466                 }
1467                 if onionv3 {
1468                         addresses.push(msgs::NetAddress::OnionV3 {
1469                                 ed25519_pubkey: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224],
1470                                 checksum: 32,
1471                                 version: 16,
1472                                 port: 9735
1473                         });
1474                 }
1475                 let mut addr_len = 0;
1476                 for addr in &addresses {
1477                         addr_len += addr.len() + 1;
1478                 }
1479                 let unsigned_node_announcement = msgs::UnsignedNodeAnnouncement {
1480                         features,
1481                         timestamp: 20190119,
1482                         node_id: pubkey_1,
1483                         rgb: [32; 3],
1484                         alias: [16;32],
1485                         addresses,
1486                         excess_address_data: if excess_address_data { vec![33, 108, 40, 11, 83, 149, 162, 84, 110, 126, 75, 38, 99, 224, 79, 129, 22, 34, 241, 90, 79, 146, 232, 58, 162, 233, 43, 162, 165, 115, 193, 57, 20, 44, 84, 174, 99, 7, 42, 30, 193, 238, 125, 192, 192, 75, 222, 92, 132, 120, 6, 23, 42, 160, 92, 146, 194, 42, 232, 227, 8, 209, 210, 105] } else { Vec::new() },
1487                         excess_data: if excess_data { vec![59, 18, 204, 25, 92, 224, 162, 209, 189, 166, 168, 139, 239, 161, 159, 160, 127, 81, 202, 167, 92, 232, 56, 55, 242, 137, 101, 96, 11, 138, 172, 171, 8, 85, 255, 176, 231, 65, 236, 95, 124, 65, 66, 30, 152, 41, 169, 212, 134, 17, 200, 200, 49, 247, 27, 229, 234, 115, 230, 101, 148, 151, 127, 253] } else { Vec::new() },
1488                 };
1489                 addr_len += unsigned_node_announcement.excess_address_data.len() as u16;
1490                 let node_announcement = msgs::NodeAnnouncement {
1491                         signature: sig_1,
1492                         contents: unsigned_node_announcement,
1493                 };
1494                 let encoded_value = node_announcement.encode();
1495                 let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1496                 if unknown_features_bits {
1497                         target_value.append(&mut hex::decode("0002ffff").unwrap());
1498                 } else {
1499                         target_value.append(&mut hex::decode("000122").unwrap());
1500                 }
1501                 target_value.append(&mut hex::decode("013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010").unwrap());
1502                 target_value.append(&mut vec![(addr_len >> 8) as u8, addr_len as u8]);
1503                 if ipv4 {
1504                         target_value.append(&mut hex::decode("01fffefdfc2607").unwrap());
1505                 }
1506                 if ipv6 {
1507                         target_value.append(&mut hex::decode("02fffefdfcfbfaf9f8f7f6f5f4f3f2f1f02607").unwrap());
1508                 }
1509                 if onionv2 {
1510                         target_value.append(&mut hex::decode("03fffefdfcfbfaf9f8f7f62607").unwrap());
1511                 }
1512                 if onionv3 {
1513                         target_value.append(&mut hex::decode("04fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e00020102607").unwrap());
1514                 }
1515                 if excess_address_data {
1516                         target_value.append(&mut hex::decode("216c280b5395a2546e7e4b2663e04f811622f15a4f92e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d269").unwrap());
1517                 }
1518                 if excess_data {
1519                         target_value.append(&mut hex::decode("3b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
1520                 }
1521                 assert_eq!(encoded_value, target_value);
1522         }
1523
1524         #[test]
1525         fn encoding_node_announcement() {
1526                 do_encoding_node_announcement(true, true, true, true, true, true, true);
1527                 do_encoding_node_announcement(false, false, false, false, false, false, false);
1528                 do_encoding_node_announcement(false, true, false, false, false, false, false);
1529                 do_encoding_node_announcement(false, false, true, false, false, false, false);
1530                 do_encoding_node_announcement(false, false, false, true, false, false, false);
1531                 do_encoding_node_announcement(false, false, false, false, true, false, false);
1532                 do_encoding_node_announcement(false, false, false, false, false, true, false);
1533                 do_encoding_node_announcement(false, true, false, true, false, true, false);
1534                 do_encoding_node_announcement(false, false, true, false, true, false, false);
1535         }
1536
1537         fn do_encoding_channel_update(non_bitcoin_chain_hash: bool, direction: bool, disable: bool, htlc_maximum_msat: bool) {
1538                 let secp_ctx = Secp256k1::new();
1539                 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1540                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1541                 let unsigned_channel_update = msgs::UnsignedChannelUpdate {
1542                         chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1543                         short_channel_id: 2316138423780173,
1544                         timestamp: 20190119,
1545                         flags: if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 } | if htlc_maximum_msat { 1 << 8 } else { 0 },
1546                         cltv_expiry_delta: 144,
1547                         htlc_minimum_msat: 1000000,
1548                         fee_base_msat: 10000,
1549                         fee_proportional_millionths: 20,
1550                         excess_data: if htlc_maximum_msat { vec![0, 0, 0, 0, 59, 154, 202, 0] } else { Vec::new() }
1551                 };
1552                 let channel_update = msgs::ChannelUpdate {
1553                         signature: sig_1,
1554                         contents: unsigned_channel_update
1555                 };
1556                 let encoded_value = channel_update.encode();
1557                 let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1558                 if non_bitcoin_chain_hash {
1559                         target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
1560                 } else {
1561                         target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
1562                 }
1563                 target_value.append(&mut hex::decode("00083a840000034d013413a7").unwrap());
1564                 if htlc_maximum_msat {
1565                         target_value.append(&mut hex::decode("01").unwrap());
1566                 } else {
1567                         target_value.append(&mut hex::decode("00").unwrap());
1568                 }
1569                 target_value.append(&mut hex::decode("00").unwrap());
1570                 if direction {
1571                         let flag = target_value.last_mut().unwrap();
1572                         *flag = 1;
1573                 }
1574                 if disable {
1575                         let flag = target_value.last_mut().unwrap();
1576                         *flag = *flag | 1 << 1;
1577                 }
1578                 target_value.append(&mut hex::decode("009000000000000f42400000271000000014").unwrap());
1579                 if htlc_maximum_msat {
1580                         target_value.append(&mut hex::decode("000000003b9aca00").unwrap());
1581                 }
1582                 assert_eq!(encoded_value, target_value);
1583         }
1584
1585         #[test]
1586         fn encoding_channel_update() {
1587                 do_encoding_channel_update(false, false, false, false);
1588                 do_encoding_channel_update(true, false, false, false);
1589                 do_encoding_channel_update(false, true, false, false);
1590                 do_encoding_channel_update(false, false, true, false);
1591                 do_encoding_channel_update(false, false, false, true);
1592                 do_encoding_channel_update(true, true, true, true);
1593         }
1594
1595         fn do_encoding_open_channel(non_bitcoin_chain_hash: bool, random_bit: bool, shutdown: bool) {
1596                 let secp_ctx = Secp256k1::new();
1597                 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1598                 let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1599                 let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1600                 let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1601                 let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
1602                 let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
1603                 let open_channel = msgs::OpenChannel {
1604                         chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1605                         temporary_channel_id: [2; 32],
1606                         funding_satoshis: 1311768467284833366,
1607                         push_msat: 2536655962884945560,
1608                         dust_limit_satoshis: 3608586615801332854,
1609                         max_htlc_value_in_flight_msat: 8517154655701053848,
1610                         channel_reserve_satoshis: 8665828695742877976,
1611                         htlc_minimum_msat: 2316138423780173,
1612                         feerate_per_kw: 821716,
1613                         to_self_delay: 49340,
1614                         max_accepted_htlcs: 49340,
1615                         funding_pubkey: pubkey_1,
1616                         revocation_basepoint: pubkey_2,
1617                         payment_basepoint: pubkey_3,
1618                         delayed_payment_basepoint: pubkey_4,
1619                         htlc_basepoint: pubkey_5,
1620                         first_per_commitment_point: pubkey_6,
1621                         channel_flags: if random_bit { 1 << 5 } else { 0 },
1622                         shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
1623                 };
1624                 let encoded_value = open_channel.encode();
1625                 let mut target_value = Vec::new();
1626                 if non_bitcoin_chain_hash {
1627                         target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
1628                 } else {
1629                         target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
1630                 }
1631                 target_value.append(&mut hex::decode("02020202020202020202020202020202020202020202020202020202020202021234567890123456233403289122369832144668701144767633030896203198784335490624111800083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap());
1632                 if random_bit {
1633                         target_value.append(&mut hex::decode("20").unwrap());
1634                 } else {
1635                         target_value.append(&mut hex::decode("00").unwrap());
1636                 }
1637                 if shutdown {
1638                         target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
1639                 }
1640                 assert_eq!(encoded_value, target_value);
1641         }
1642
1643         #[test]
1644         fn encoding_open_channel() {
1645                 do_encoding_open_channel(false, false, false);
1646                 do_encoding_open_channel(true, false, false);
1647                 do_encoding_open_channel(false, true, false);
1648                 do_encoding_open_channel(false, false, true);
1649                 do_encoding_open_channel(true, true, true);
1650         }
1651
1652         fn do_encoding_accept_channel(shutdown: bool) {
1653                 let secp_ctx = Secp256k1::new();
1654                 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1655                 let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1656                 let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1657                 let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1658                 let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
1659                 let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
1660                 let accept_channel = msgs::AcceptChannel {
1661                         temporary_channel_id: [2; 32],
1662                         dust_limit_satoshis: 1311768467284833366,
1663                         max_htlc_value_in_flight_msat: 2536655962884945560,
1664                         channel_reserve_satoshis: 3608586615801332854,
1665                         htlc_minimum_msat: 2316138423780173,
1666                         minimum_depth: 821716,
1667                         to_self_delay: 49340,
1668                         max_accepted_htlcs: 49340,
1669                         funding_pubkey: pubkey_1,
1670                         revocation_basepoint: pubkey_2,
1671                         payment_basepoint: pubkey_3,
1672                         delayed_payment_basepoint: pubkey_4,
1673                         htlc_basepoint: pubkey_5,
1674                         first_per_commitment_point: pubkey_6,
1675                         shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
1676                 };
1677                 let encoded_value = accept_channel.encode();
1678                 let mut target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap();
1679                 if shutdown {
1680                         target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
1681                 }
1682                 assert_eq!(encoded_value, target_value);
1683         }
1684
1685         #[test]
1686         fn encoding_accept_channel() {
1687                 do_encoding_accept_channel(false);
1688                 do_encoding_accept_channel(true);
1689         }
1690
1691         #[test]
1692         fn encoding_funding_created() {
1693                 let secp_ctx = Secp256k1::new();
1694                 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1695                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1696                 let funding_created = msgs::FundingCreated {
1697                         temporary_channel_id: [2; 32],
1698                         funding_txid: Sha256dHash::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
1699                         funding_output_index: 255,
1700                         signature: sig_1,
1701                 };
1702                 let encoded_value = funding_created.encode();
1703                 let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202026e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c200ffd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1704                 assert_eq!(encoded_value, target_value);
1705         }
1706
1707         #[test]
1708         fn encoding_funding_signed() {
1709                 let secp_ctx = Secp256k1::new();
1710                 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1711                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1712                 let funding_signed = msgs::FundingSigned {
1713                         channel_id: [2; 32],
1714                         signature: sig_1,
1715                 };
1716                 let encoded_value = funding_signed.encode();
1717                 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1718                 assert_eq!(encoded_value, target_value);
1719         }
1720
1721         #[test]
1722         fn encoding_funding_locked() {
1723                 let secp_ctx = Secp256k1::new();
1724                 let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1725                 let funding_locked = msgs::FundingLocked {
1726                         channel_id: [2; 32],
1727                         next_per_commitment_point: pubkey_1,
1728                 };
1729                 let encoded_value = funding_locked.encode();
1730                 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
1731                 assert_eq!(encoded_value, target_value);
1732         }
1733
1734         fn do_encoding_shutdown(script_type: u8) {
1735                 let secp_ctx = Secp256k1::new();
1736                 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1737                 let script = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
1738                 let shutdown = msgs::Shutdown {
1739                         channel_id: [2; 32],
1740                         scriptpubkey: if script_type == 1 { Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else if script_type == 2 { Address::p2sh(&script, Network::Testnet).script_pubkey() } else if script_type == 3 { Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else { Address::p2wsh(&script, Network::Testnet).script_pubkey() },
1741                 };
1742                 let encoded_value = shutdown.encode();
1743                 let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
1744                 if script_type == 1 {
1745                         target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
1746                 } else if script_type == 2 {
1747                         target_value.append(&mut hex::decode("0017a914da1745e9b549bd0bfa1a569971c77eba30cd5a4b87").unwrap());
1748                 } else if script_type == 3 {
1749                         target_value.append(&mut hex::decode("0016001479b000887626b294a914501a4cd226b58b235983").unwrap());
1750                 } else if script_type == 4 {
1751                         target_value.append(&mut hex::decode("002200204ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260").unwrap());
1752                 }
1753                 assert_eq!(encoded_value, target_value);
1754         }
1755
1756         #[test]
1757         fn encoding_shutdown() {
1758                 do_encoding_shutdown(1);
1759                 do_encoding_shutdown(2);
1760                 do_encoding_shutdown(3);
1761                 do_encoding_shutdown(4);
1762         }
1763
1764         #[test]
1765         fn encoding_closing_signed() {
1766                 let secp_ctx = Secp256k1::new();
1767                 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1768                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1769                 let closing_signed = msgs::ClosingSigned {
1770                         channel_id: [2; 32],
1771                         fee_satoshis: 2316138423780173,
1772                         signature: sig_1,
1773                 };
1774                 let encoded_value = closing_signed.encode();
1775                 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1776                 assert_eq!(encoded_value, target_value);
1777         }
1778
1779         #[test]
1780         fn encoding_update_add_htlc() {
1781                 let secp_ctx = Secp256k1::new();
1782                 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1783                 let onion_routing_packet = msgs::OnionPacket {
1784                         version: 255,
1785                         public_key: Ok(pubkey_1),
1786                         hop_data: [1; 20*65],
1787                         hmac: [2; 32]
1788                 };
1789                 let update_add_htlc = msgs::UpdateAddHTLC {
1790                         channel_id: [2; 32],
1791                         htlc_id: 2316138423780173,
1792                         amount_msat: 3608586615801332854,
1793                         payment_hash: PaymentHash([1; 32]),
1794                         cltv_expiry: 821716,
1795                         onion_routing_packet
1796                 };
1797                 let encoded_value = update_add_htlc.encode();
1798                 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d32144668701144760101010101010101010101010101010101010101010101010101010101010101000c89d4ff031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202").unwrap();
1799                 assert_eq!(encoded_value, target_value);
1800         }
1801
1802         #[test]
1803         fn encoding_update_fulfill_htlc() {
1804                 let update_fulfill_htlc = msgs::UpdateFulfillHTLC {
1805                         channel_id: [2; 32],
1806                         htlc_id: 2316138423780173,
1807                         payment_preimage: PaymentPreimage([1; 32]),
1808                 };
1809                 let encoded_value = update_fulfill_htlc.encode();
1810                 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d0101010101010101010101010101010101010101010101010101010101010101").unwrap();
1811                 assert_eq!(encoded_value, target_value);
1812         }
1813
1814         #[test]
1815         fn encoding_update_fail_htlc() {
1816                 let reason = OnionErrorPacket {
1817                         data: [1; 32].to_vec(),
1818                 };
1819                 let update_fail_htlc = msgs::UpdateFailHTLC {
1820                         channel_id: [2; 32],
1821                         htlc_id: 2316138423780173,
1822                         reason
1823                 };
1824                 let encoded_value = update_fail_htlc.encode();
1825                 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d00200101010101010101010101010101010101010101010101010101010101010101").unwrap();
1826                 assert_eq!(encoded_value, target_value);
1827         }
1828
1829         #[test]
1830         fn encoding_update_fail_malformed_htlc() {
1831                 let update_fail_malformed_htlc = msgs::UpdateFailMalformedHTLC {
1832                         channel_id: [2; 32],
1833                         htlc_id: 2316138423780173,
1834                         sha256_of_onion: [1; 32],
1835                         failure_code: 255
1836                 };
1837                 let encoded_value = update_fail_malformed_htlc.encode();
1838                 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d010101010101010101010101010101010101010101010101010101010101010100ff").unwrap();
1839                 assert_eq!(encoded_value, target_value);
1840         }
1841
1842         fn do_encoding_commitment_signed(htlcs: bool) {
1843                 let secp_ctx = Secp256k1::new();
1844                 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1845                 let (privkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1846                 let (privkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1847                 let (privkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1848                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1849                 let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
1850                 let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
1851                 let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
1852                 let commitment_signed = msgs::CommitmentSigned {
1853                         channel_id: [2; 32],
1854                         signature: sig_1,
1855                         htlc_signatures: if htlcs { vec![sig_2, sig_3, sig_4] } else { Vec::new() },
1856                 };
1857                 let encoded_value = commitment_signed.encode();
1858                 let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1859                 if htlcs {
1860                         target_value.append(&mut hex::decode("00031735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
1861                 } else {
1862                         target_value.append(&mut hex::decode("0000").unwrap());
1863                 }
1864                 assert_eq!(encoded_value, target_value);
1865         }
1866
1867         #[test]
1868         fn encoding_commitment_signed() {
1869                 do_encoding_commitment_signed(true);
1870                 do_encoding_commitment_signed(false);
1871         }
1872
1873         #[test]
1874         fn encoding_revoke_and_ack() {
1875                 let secp_ctx = Secp256k1::new();
1876                 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1877                 let raa = msgs::RevokeAndACK {
1878                         channel_id: [2; 32],
1879                         per_commitment_secret: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
1880                         next_per_commitment_point: pubkey_1,
1881                 };
1882                 let encoded_value = raa.encode();
1883                 let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202020101010101010101010101010101010101010101010101010101010101010101031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
1884                 assert_eq!(encoded_value, target_value);
1885         }
1886
1887         #[test]
1888         fn encoding_update_fee() {
1889                 let update_fee = msgs::UpdateFee {
1890                         channel_id: [2; 32],
1891                         feerate_per_kw: 20190119,
1892                 };
1893                 let encoded_value = update_fee.encode();
1894                 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202013413a7").unwrap();
1895                 assert_eq!(encoded_value, target_value);
1896         }
1897
1898         #[test]
1899         fn encoding_init() {
1900                 assert_eq!(msgs::Init {
1901                         features: InitFeatures::from_le_bytes(vec![0xFF, 0xFF, 0xFF]),
1902                 }.encode(), hex::decode("00023fff0003ffffff").unwrap());
1903                 assert_eq!(msgs::Init {
1904                         features: InitFeatures::from_le_bytes(vec![0xFF]),
1905                 }.encode(), hex::decode("0001ff0001ff").unwrap());
1906                 assert_eq!(msgs::Init {
1907                         features: InitFeatures::from_le_bytes(vec![]),
1908                 }.encode(), hex::decode("00000000").unwrap());
1909         }
1910
1911         #[test]
1912         fn encoding_error() {
1913                 let error = msgs::ErrorMessage {
1914                         channel_id: [2; 32],
1915                         data: String::from("rust-lightning"),
1916                 };
1917                 let encoded_value = error.encode();
1918                 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67").unwrap();
1919                 assert_eq!(encoded_value, target_value);
1920         }
1921
1922         #[test]
1923         fn encoding_ping() {
1924                 let ping = msgs::Ping {
1925                         ponglen: 64,
1926                         byteslen: 64
1927                 };
1928                 let encoded_value = ping.encode();
1929                 let target_value = hex::decode("0040004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
1930                 assert_eq!(encoded_value, target_value);
1931         }
1932
1933         #[test]
1934         fn encoding_pong() {
1935                 let pong = msgs::Pong {
1936                         byteslen: 64
1937                 };
1938                 let encoded_value = pong.encode();
1939                 let target_value = hex::decode("004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
1940                 assert_eq!(encoded_value, target_value);
1941         }
1942 }