Skip to content
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

Merged
merged 5 commits into from Aug 22, 2018

Conversation

dongcarl
Copy link
Member

@dongcarl dongcarl commented Aug 17, 2018

  • Separate serialize::Error and network::Error from util::Error
  • Remove unneeded propagate_err and consume_err

Closes #131

Copy link
Contributor

@jeandudey jeandudey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK.

@@ -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> {
Copy link
Member

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).

@@ -20,6 +20,7 @@

use network::constants;
use network::address::Address;
use network::serialize;
Copy link
Member

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)

@@ -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};
Copy link
Member

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)

/// Not connected to peer
SocketNotConnectedToPeer,
/// Error propagated from subsystem
Detail(String, Box<Error>),
Copy link
Member

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)

@@ -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))));
}
Copy link
Member

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.

/// Parsing error
ParseFailed,
/// Error propagated from subsystem
Detail(String, Box<Error>),
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

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 RawDecoders also use a dynamic String, I do believe however, that with this overall change, we can eliminate that method and directly return serialize::Errors, does that sound good?

Copy link
Member Author

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?

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),
Copy link
Member

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.

@@ -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?
Copy link
Member

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),
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here.

@apoelstra apoelstra added this to the 0.14 milestone Aug 20, 2018
@dongcarl
Copy link
Member Author

@apoelstra Is there any reason we're not using e.fmt(f) in fmt::Display impls?

@apoelstra
Copy link
Member

apoelstra commented Aug 21, 2018

@dongcarl I assumed that wouldn't work unless fmt::Display was imported, and I prefer to use the full path fmt::Display everywhere rather than doing so.

In fact if we used e.fmt(f) why would rustc choose fmt::Display::fmt over fmt::Debug::fmt, say?

- 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
@dongcarl
Copy link
Member Author

All concerns should have been addressed.

@apoelstra apoelstra merged commit a61ad5d into rust-bitcoin:master Aug 22, 2018
Davidson-Souza pushed a commit to Davidson-Souza/rust-bitcoin that referenced this pull request Jul 12, 2023
casey pushed a commit to casey/rust-bitcoin that referenced this pull request Nov 28, 2023
yancyribbens pushed a commit to yancyribbens/rust-bitcoin that referenced this pull request Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants