Skip to content

Added DeserializeFailure to decode error enum for better error mapping #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fuzz/fuzz_targets/channel_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub fn do_test(data: &[u8]) {
msgs::DecodeError::ExtraAddressesPerType => return,
msgs::DecodeError::BadLengthDescriptor => return,
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
msgs::DecodeError::DeserializeFailure => return,
}
}
}
Expand All @@ -154,6 +155,7 @@ pub fn do_test(data: &[u8]) {
msgs::DecodeError::ExtraAddressesPerType => return,
msgs::DecodeError::BadLengthDescriptor => return,
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
msgs::DecodeError::DeserializeFailure => return,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/router_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn do_test(data: &[u8]) {
msgs::DecodeError::ExtraAddressesPerType => return,
msgs::DecodeError::BadLengthDescriptor => return,
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
msgs::DecodeError::DeserializeFailure => return,
}
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use secp256k1;
use bitcoin::util::hash::Sha256dHash;
use bitcoin::network::serialize::{deserialize,serialize};
use bitcoin::blockdata::script::Script;
use bitcoin::network::serialize::Error as SerializeError;

use std::error::Error;
use std::{cmp, fmt};
Expand Down Expand Up @@ -43,6 +44,9 @@ pub enum DecodeError {
/// A length descriptor in the packet didn't describe the later data correctly
/// (currently only generated in node_announcement)
BadLengthDescriptor,
///This is a bitcoin::network::serialize::Error converted to decode error
///This happens when the chain_hash or temporary_channel_id fails deserialization
DeserializeFailure,
}
pub trait MsgDecodable: Sized {
fn decode(v: &[u8]) -> Result<Self, DecodeError>;
Expand Down Expand Up @@ -519,9 +523,17 @@ impl Error for DecodeError {
DecodeError::ShortRead => "Packet extended beyond the provided bytes",
DecodeError::ExtraAddressesPerType => "More than one address of a single type",
DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
DecodeError::DeserializeFailure => "Deserialization failed of packet, chain_hash or temporary_channel_id invalid",
}
}
}

impl From<SerializeError> for DecodeError{
fn from(_e: SerializeError) -> Self {
DecodeError::DeserializeFailure
}
}

impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.description())
Expand Down Expand Up @@ -681,10 +693,11 @@ impl MsgDecodable for OpenChannel {
}
shutdown_scriptpubkey = Some(Script::from(v[321..321+len].to_vec()));
}

let mut temp_channel_id = [0; 32];
temp_channel_id[..].copy_from_slice(&v[32..64]);
Ok(OpenChannel {
chain_hash: deserialize(&v[0..32]).unwrap(),
temporary_channel_id: deserialize(&v[32..64]).unwrap(),
chain_hash: deserialize(&v[0..32])?,
temporary_channel_id: temp_channel_id,
funding_satoshis: byte_utils::slice_to_be64(&v[64..72]),
push_msat: byte_utils::slice_to_be64(&v[72..80]),
dust_limit_satoshis: byte_utils::slice_to_be64(&v[80..88]),
Expand Down
2 changes: 2 additions & 0 deletions src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
continue;
},
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
msgs::DecodeError::DeserializeFailure => return Err(PeerHandleError{ no_connection_possible: false }),

}
}
};
Expand Down