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

Change error type of Encodable to io::Error like bitcoin does #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Serialize for ExtData {
}

impl Encodable for ExtData {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
Ok(match *self {
ExtData::Proof {
ref challenge,
Expand Down Expand Up @@ -299,7 +299,7 @@ impl BlockHeader {
}

impl Encodable for BlockHeader {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let version = if let ExtData::Dynafed { .. } = self.ext {
self.version | 0x8000_0000
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/confidential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Default for Value {
}

impl Encodable for Value {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
match *self {
Value::Null => 0u8.consensus_encode(s),
Value::Explicit(n) => {
Expand All @@ -153,7 +153,7 @@ impl Encodable for Value {
}

impl Encodable for PedersenCommitment {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
e.write_all(&self.serialize())?;
Ok(33)
}
Expand Down Expand Up @@ -368,7 +368,7 @@ impl Default for Asset {


impl Encodable for Asset {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
match *self {
Asset::Null => 0u8.consensus_encode(s),
Asset::Explicit(n) => {
Expand All @@ -381,7 +381,7 @@ impl Encodable for Asset {
}

impl Encodable for Generator {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
e.write_all(&self.serialize())?;
Ok(33)
}
Expand Down Expand Up @@ -625,7 +625,7 @@ impl Default for Nonce {
}

impl Encodable for Nonce {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
match *self {
Nonce::Null => 0u8.consensus_encode(s),
Nonce::Explicit(n) => {
Expand All @@ -638,7 +638,7 @@ impl Encodable for Nonce {
}

impl Encodable for PublicKey {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
e.write_all(&self.serialize())?;
Ok(33)
}
Expand Down
4 changes: 2 additions & 2 deletions src/dynafed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl fmt::Debug for FullParams {
}

impl Encodable for FullParams {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let ret = Encodable::consensus_encode(&self.signblockscript, &mut s)? +
Encodable::consensus_encode(&self.signblock_witness_limit, &mut s)? +
Encodable::consensus_encode(&self.fedpeg_program, &mut s)? +
Expand Down Expand Up @@ -594,7 +594,7 @@ impl Serialize for Params {
}

impl Encodable for Params {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
Ok(match *self {
Params::Null => Encodable::consensus_encode(&0u8, &mut s)?,
Params::Compact {
Expand Down
26 changes: 13 additions & 13 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub trait Encodable {
/// Encode an object with a well-defined format, should only ever error if
/// the underlying `Write` errors. Returns the number of bytes written on
/// success
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error>;
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, io::Error>;
}

/// Data which can be encoded in a consensus-consistent way
Expand Down Expand Up @@ -189,7 +189,7 @@ pub fn deserialize_partial<T: Decodable>(data: &[u8]) -> Result<(T, usize), Erro
}

impl Encodable for sha256::Midstate {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, io::Error> {
self.to_byte_array().consensus_encode(e)
}
}
Expand All @@ -200,7 +200,7 @@ impl Decodable for sha256::Midstate {
}
}

pub(crate) fn consensus_encode_with_size<S: io::Write>(data: &[u8], mut s: S) -> Result<usize, Error> {
pub(crate) fn consensus_encode_with_size<S: io::Write>(data: &[u8], mut s: S) -> Result<usize, io::Error> {
let vi_len = bitcoin::VarInt(data.len() as u64).consensus_encode(&mut s)?;
s.emit_slice(data)?;
Ok(vi_len + data.len())
Expand All @@ -210,7 +210,7 @@ pub(crate) fn consensus_encode_with_size<S: io::Write>(data: &[u8], mut s: S) ->
macro_rules! impl_upstream {
($type: ty) => {
impl Encodable for $type {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, Error> {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
Ok(btcenc::Encodable::consensus_encode(self, &mut e)?)
}
}
Expand Down Expand Up @@ -239,7 +239,7 @@ impl_upstream!(crate::hashes::sha256d::Hash);

// Specific locktime types (which appear in PSET/PSBT2 but not in rust-bitcoin PSBT)
impl Encodable for crate::locktime::Height {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, io::Error> {
crate::LockTime::from(*self).consensus_encode(s)
}
}
Expand All @@ -254,7 +254,7 @@ impl Decodable for crate::locktime::Height {


impl Encodable for crate::locktime::Time {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, io::Error> {
crate::LockTime::from(*self).consensus_encode(s)
}
}
Expand All @@ -273,7 +273,7 @@ macro_rules! impl_vec {
($type: ty) => {
impl Encodable for Vec<$type> {
#[inline]
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
len += btcenc::VarInt(self.len() as u64).consensus_encode(&mut s)?;
for c in self.iter() {
Expand Down Expand Up @@ -315,7 +315,7 @@ macro_rules! impl_box_option {
($type: ty) => {
impl Encodable for Option<Box<$type>> {
#[inline]
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, io::Error> {
match self {
None => Vec::<u8>::new().consensus_encode(e),
Some(v) => v.serialize().consensus_encode(e),
Expand All @@ -338,7 +338,7 @@ macro_rules! impl_box_option {
}
// special implementations for elements only fields
impl Encodable for Tweak {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, io::Error> {
self.as_ref().consensus_encode(e)
}
}
Expand All @@ -350,7 +350,7 @@ impl Decodable for Tweak {
}

impl Encodable for RangeProof {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, io::Error> {
self.serialize().consensus_encode(e)
}
}
Expand All @@ -362,7 +362,7 @@ impl Decodable for RangeProof {
}

impl Encodable for SurjectionProof {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, io::Error> {
self.serialize().consensus_encode(e)
}
}
Expand All @@ -375,7 +375,7 @@ impl Decodable for SurjectionProof {


impl Encodable for sha256::Hash {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, io::Error> {
self.to_byte_array().consensus_encode(s)
}
}
Expand All @@ -387,7 +387,7 @@ impl Decodable for sha256::Hash {
}

impl Encodable for TapLeafHash {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, io::Error> {
self.to_byte_array().consensus_encode(s)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/hash_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use bitcoin::secp256k1::ThirtyTwoByteHash;
macro_rules! impl_hashencode {
($hashtype:ident) => {
impl $crate::encode::Encodable for $hashtype {
fn consensus_encode<W: std::io::Write>(&self, w: W) -> Result<usize, crate::encode::Error> {
fn consensus_encode<W: std::io::Write>(&self, w: W) -> Result<usize, std::io::Error> {
self.0.consensus_encode(w)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ macro_rules! impl_consensus_encoding {
($thing:ident, $($field:ident),+) => (
impl $crate::encode::Encodable for $thing {
#[inline]
fn consensus_encode<S: std::io::Write>(&self, mut s: S) -> Result<usize, $crate::encode::Error> {
fn consensus_encode<S: std::io::Write>(&self, mut s: S) -> Result<usize, std::io::Error> {
let mut ret = 0;
$( ret += self.$field.consensus_encode(&mut s)?; )+
Ok(ret)
Expand Down
2 changes: 1 addition & 1 deletion src/issuance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl FromStr for AssetId {
}

impl Encodable for AssetId {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, encode::Error> {
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, io::Error> {
self.0.consensus_encode(e)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/locktime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! whether `LockTime < LOCKTIME_THRESHOLD`.
//!

use std::{mem, fmt};
use std::{io, mem, fmt};
use std::cmp::{PartialOrd, Ordering};
use std::convert::TryFrom;
use std::str::FromStr;
Expand Down Expand Up @@ -284,7 +284,7 @@ impl fmt::Display for LockTime {

impl Encodable for LockTime {
#[inline]
fn consensus_encode<W: Write>(&self, w: W) -> Result<usize, encode::Error> {
fn consensus_encode<W: Write>(&self, w: W) -> Result<usize, io::Error> {
let v = self.to_consensus_u32();
v.consensus_encode(w)
}
Expand Down
2 changes: 1 addition & 1 deletion src/pset/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ macro_rules! impl_psetmap_consensus_encoding {
fn consensus_encode<S: ::std::io::Write>(
&self,
mut s: S,
) -> Result<usize, $crate::encode::Error> {
) -> Result<usize, std::io::Error> {
let mut len = 0;
for pair in $crate::pset::Map::get_pairs(self)? {
len += $crate::encode::Encodable::consensus_encode(&pair, &mut s)?;
Expand Down
2 changes: 1 addition & 1 deletion src/pset/map/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl Map for Global {
Ok(())
}

fn get_pairs(&self) -> Result<Vec<raw::Pair>, encode::Error> {
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error> {
let mut rv: Vec<raw::Pair> = Default::default();

let TxData {
Expand Down
2 changes: 1 addition & 1 deletion src/pset/map/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ impl Map for Input {
Ok(())
}

fn get_pairs(&self) -> Result<Vec<raw::Pair>, encode::Error> {
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error> {
let mut rv: Vec<raw::Pair> = Default::default();

impl_pset_get_pair! {
Expand Down
4 changes: 3 additions & 1 deletion src/pset/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

use std::io;

use crate::encode;
use crate::pset::{self, raw};

Expand All @@ -21,7 +23,7 @@ pub(crate) trait Map {
fn insert_pair(&mut self, pair: raw::Pair) -> Result<(), encode::Error>;

/// Attempt to get all key-value pairs.
fn get_pairs(&self) -> Result<Vec<raw::Pair>, encode::Error>;
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error>;

/// Attempt to merge with another key-value map of the same type.
fn merge(&mut self, other: Self) -> Result<(), pset::Error>;
Expand Down
2 changes: 1 addition & 1 deletion src/pset/map/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ impl Map for Output {
Ok(())
}

fn get_pairs(&self) -> Result<Vec<raw::Pair>, encode::Error> {
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error> {
let mut rv: Vec<raw::Pair> = Default::default();

impl_pset_get_pair! {
Expand Down
2 changes: 1 addition & 1 deletion src/pset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ impl PartiallySignedTransaction {
}

impl Encodable for PartiallySignedTransaction {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
len += b"pset".consensus_encode(&mut s)?;

Expand Down
6 changes: 3 additions & 3 deletions src/pset/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Decodable for Key {
}

impl Encodable for Key {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
len += VarInt((self.key.len() + 1) as u64).consensus_encode(&mut s)?;

Expand All @@ -151,7 +151,7 @@ impl Encodable for Key {
}

impl Encodable for Pair {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let len = self.key.consensus_encode(&mut s)?;
Ok(len + self.value.consensus_encode(s)?)
}
Expand All @@ -170,7 +170,7 @@ impl<Subtype> Encodable for ProprietaryKey<Subtype>
where
Subtype: Copy + From<u8> + Into<u8>,
{
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
let mut len = self.prefix.consensus_encode(&mut e)? + 1;
e.emit_u8(self.subtype.into())?;
len += e.write(&self.key)?;
Expand Down
2 changes: 1 addition & 1 deletion src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ impl Encodable for Script {
fn consensus_encode<S: io::Write>(
&self,
s: S,
) -> Result<usize, encode::Error> {
) -> Result<usize, io::Error> {
self.0.consensus_encode(s)
}
}
Expand Down
Loading
Loading