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
Fix Error type for SimpleDecoder and SimpleEncoder #137
Conversation
ab1f4f6
to
09bdda0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK.
src/internal_macros.rs
Outdated
@@ -16,15 +16,15 @@ macro_rules! impl_consensus_encoding { | |||
($thing:ident, $($field:ident),+) => ( | |||
impl<S: ::network::serialize::SimpleEncoder> ::network::encodable::ConsensusEncodable<S> for $thing { | |||
#[inline] | |||
fn consensus_encode(&self, s: &mut S) -> Result<(), S::Error> { | |||
fn consensus_encode(&self, s: &mut S) -> Result<(), serialize::Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use ::network::serialize::Error
here? It is annoying to type out the full path but it means that users of the macro don't need mysterious imports (e.g. the one you added to block.rs
).
src/network/message_network.rs
Outdated
@@ -20,6 +20,7 @@ | |||
|
|||
use network::constants; | |||
use network::address::Address; | |||
use network::serialize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be dropped (see comment on internal_macros.rs)
src/blockdata/block.rs
Outdated
@@ -25,7 +25,7 @@ use util::Error::{SpvBadTarget, SpvBadProofOfWork}; | |||
use util::hash::Sha256dHash; | |||
use util::uint::Uint256; | |||
use network::encodable::VarInt; | |||
use network::serialize::BitcoinHash; | |||
use network::serialize::{self, BitcoinHash}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be dropped (see comment on internal_macros.rs)
src/network/mod.rs
Outdated
/// Not connected to peer | ||
SocketNotConnectedToPeer, | ||
/// Error propagated from subsystem | ||
Detail(String, Box<Error>), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variant can be dropped (see comments on network/listener.rs which is the only place this was used)
src/network/listener.rs
Outdated
@@ -40,7 +40,7 @@ pub trait Listener { | |||
// Open socket | |||
let mut ret_sock = Socket::new(self.network()); | |||
if let Err(e) = ret_sock.connect(self.peer(), self.port()) { | |||
return Err(util::Error::Detail("listener".to_owned(), Box::new(e))); | |||
return Err(util::Error::Network(network::Error::Detail("listener".to_owned(), Box::new(e)))); | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole if
should be replaced by ret_sock.connect(self.peer(), self.port())?;
because ret_sock.connect
already returns a network::Error
.
Then you can drop the new self
import at the top of this file.
src/network/serialize.rs
Outdated
/// Parsing error | ||
ParseFailed, | ||
/// Error propagated from subsystem | ||
Detail(String, Box<Error>), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should drop the Box<Error>
from this; the only thing ever packaged in it is Error::ParseFailed
. In fact I think we should just make ParseFailed
take a String
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact it should take a &'static str
rather than a String
. The only place we dynamically generate a string is in util::hex_bytes
, which (a) is only used in unit tests, (b) will be replaced alongside the hashes in the medium-term future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that the error(&mut self, err: String) -> Error
method of RawDecoder
s also use a dynamic String
, I do believe however, that with this overall change, we can eliminate that method and directly return serialize::Error
s, does that sound good?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@apoelstra I have replaced those instanced with their own serialize::Error
vairants... What do we do with the util::hex_bytes
case? Do I do the same?
src/network/serialize.rs
Outdated
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
Error::Io(ref e) => fmt::Display::fmt(e, f), | ||
Error::Detail(ref s, ref e) => write!(f, "{}: {}", s, e), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think every variant (once we merge Detail
into ParseFailed
) should do something here, and drop the ref x
match.
src/network/message.rs
Outdated
@@ -174,30 +174,30 @@ impl<S: SimpleEncoder> ConsensusEncodable<S> for RawNetworkMessage { | |||
|
|||
// TODO: restriction on D::Error is so that `propagate_err` will work; | |||
// is there a more generic way to handle this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This TODO comment should be deleted.
src/util/mod.rs
Outdated
Error::Secp256k1(ref e) => fmt::Display::fmt(e, f), | ||
Error::Serialize(ref e) => fmt::Display::fmt(e, f), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should add case for Error::Network
... and might as well replace ref x
in the catchall case with Error::SpvBadTarget | Error::SpvBadProofofWork
since there are only the two cases and it'll trigger exhaustiveness checking if we add new ones.
src/util/mod.rs
Outdated
Error::Secp256k1(ref e) => Some(e), | ||
Error::Serialize(ref e) => Some(e), | ||
_ => None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto here.
@apoelstra Is there any reason we're not using |
@dongcarl I assumed that wouldn't work unless In fact if we used |
- Separate serialize::Error and network::Error from util::Error - Remove unneeded propagate_err and consume_err - Change fuzzing code to ignore Err type
- Add serialize::Error::ParseFailed(&'static str) variant for serialization errors without context - Add appropriate variants to replace network::Error::Detail for serialization error with context - Remove error method from SimpleDecoders
09bdda0
to
bccdd06
Compare
All concerns should have been addressed. |
Fix broken CI on master
psbt: satisfy any ToPublicKey key
Closes #131