From 7cf26421c5b9e203405f9bf68b232ee0c8f61b43 Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Wed, 2 Aug 2023 07:20:57 +0100 Subject: [PATCH] Change error type of Encodable to io::Error like bitcoin does --- src/block.rs | 4 ++-- src/confidential.rs | 12 ++++++------ src/dynafed.rs | 4 ++-- src/encode.rs | 26 +++++++++++++------------- src/hash_types.rs | 2 +- src/internal_macros.rs | 2 +- src/issuance.rs | 2 +- src/locktime.rs | 4 ++-- src/pset/macros.rs | 2 +- src/pset/map/global.rs | 2 +- src/pset/map/input.rs | 2 +- src/pset/map/mod.rs | 4 +++- src/pset/map/output.rs | 2 +- src/pset/mod.rs | 2 +- src/pset/raw.rs | 6 +++--- src/script.rs | 2 +- src/sighash.rs | 22 +++++++++++----------- src/transaction.rs | 10 +++++----- 18 files changed, 56 insertions(+), 54 deletions(-) diff --git a/src/block.rs b/src/block.rs index 63175d52..4829ac2c 100644 --- a/src/block.rs +++ b/src/block.rs @@ -169,7 +169,7 @@ impl Serialize for ExtData { } impl Encodable for ExtData { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { Ok(match *self { ExtData::Proof { ref challenge, @@ -299,7 +299,7 @@ impl BlockHeader { } impl Encodable for BlockHeader { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { let version = if let ExtData::Dynafed { .. } = self.ext { self.version | 0x8000_0000 } else { diff --git a/src/confidential.rs b/src/confidential.rs index 9332d643..c8aed772 100644 --- a/src/confidential.rs +++ b/src/confidential.rs @@ -140,7 +140,7 @@ impl Default for Value { } impl Encodable for Value { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { match *self { Value::Null => 0u8.consensus_encode(s), Value::Explicit(n) => { @@ -153,7 +153,7 @@ impl Encodable for Value { } impl Encodable for PedersenCommitment { - fn consensus_encode(&self, mut e: W) -> Result { + fn consensus_encode(&self, mut e: W) -> Result { e.write_all(&self.serialize())?; Ok(33) } @@ -368,7 +368,7 @@ impl Default for Asset { impl Encodable for Asset { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { match *self { Asset::Null => 0u8.consensus_encode(s), Asset::Explicit(n) => { @@ -381,7 +381,7 @@ impl Encodable for Asset { } impl Encodable for Generator { - fn consensus_encode(&self, mut e: W) -> Result { + fn consensus_encode(&self, mut e: W) -> Result { e.write_all(&self.serialize())?; Ok(33) } @@ -625,7 +625,7 @@ impl Default for Nonce { } impl Encodable for Nonce { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { match *self { Nonce::Null => 0u8.consensus_encode(s), Nonce::Explicit(n) => { @@ -638,7 +638,7 @@ impl Encodable for Nonce { } impl Encodable for PublicKey { - fn consensus_encode(&self, mut e: W) -> Result { + fn consensus_encode(&self, mut e: W) -> Result { e.write_all(&self.serialize())?; Ok(33) } diff --git a/src/dynafed.rs b/src/dynafed.rs index 41f80b75..85c53633 100644 --- a/src/dynafed.rs +++ b/src/dynafed.rs @@ -176,7 +176,7 @@ impl fmt::Debug for FullParams { } impl Encodable for FullParams { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { 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)? + @@ -594,7 +594,7 @@ impl Serialize for Params { } impl Encodable for Params { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { Ok(match *self { Params::Null => Encodable::consensus_encode(&0u8, &mut s)?, Params::Compact { diff --git a/src/encode.rs b/src/encode.rs index b80a88b1..e5a7a271 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -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(&self, e: W) -> Result; + fn consensus_encode(&self, e: W) -> Result; } /// Data which can be encoded in a consensus-consistent way @@ -189,7 +189,7 @@ pub fn deserialize_partial(data: &[u8]) -> Result<(T, usize), Erro } impl Encodable for sha256::Midstate { - fn consensus_encode(&self, e: W) -> Result { + fn consensus_encode(&self, e: W) -> Result { self.to_byte_array().consensus_encode(e) } } @@ -200,7 +200,7 @@ impl Decodable for sha256::Midstate { } } -pub(crate) fn consensus_encode_with_size(data: &[u8], mut s: S) -> Result { +pub(crate) fn consensus_encode_with_size(data: &[u8], mut s: S) -> Result { let vi_len = bitcoin::VarInt(data.len() as u64).consensus_encode(&mut s)?; s.emit_slice(data)?; Ok(vi_len + data.len()) @@ -210,7 +210,7 @@ pub(crate) fn consensus_encode_with_size(data: &[u8], mut s: S) -> macro_rules! impl_upstream { ($type: ty) => { impl Encodable for $type { - fn consensus_encode(&self, mut e: W) -> Result { + fn consensus_encode(&self, mut e: W) -> Result { Ok(btcenc::Encodable::consensus_encode(self, &mut e)?) } } @@ -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(&self, s: S) -> Result { + fn consensus_encode(&self, s: S) -> Result { crate::LockTime::from(*self).consensus_encode(s) } } @@ -254,7 +254,7 @@ impl Decodable for crate::locktime::Height { impl Encodable for crate::locktime::Time { - fn consensus_encode(&self, s: S) -> Result { + fn consensus_encode(&self, s: S) -> Result { crate::LockTime::from(*self).consensus_encode(s) } } @@ -273,7 +273,7 @@ macro_rules! impl_vec { ($type: ty) => { impl Encodable for Vec<$type> { #[inline] - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { let mut len = 0; len += btcenc::VarInt(self.len() as u64).consensus_encode(&mut s)?; for c in self.iter() { @@ -315,7 +315,7 @@ macro_rules! impl_box_option { ($type: ty) => { impl Encodable for Option> { #[inline] - fn consensus_encode(&self, e: W) -> Result { + fn consensus_encode(&self, e: W) -> Result { match self { None => Vec::::new().consensus_encode(e), Some(v) => v.serialize().consensus_encode(e), @@ -338,7 +338,7 @@ macro_rules! impl_box_option { } // special implementations for elements only fields impl Encodable for Tweak { - fn consensus_encode(&self, e: W) -> Result { + fn consensus_encode(&self, e: W) -> Result { self.as_ref().consensus_encode(e) } } @@ -350,7 +350,7 @@ impl Decodable for Tweak { } impl Encodable for RangeProof { - fn consensus_encode(&self, e: W) -> Result { + fn consensus_encode(&self, e: W) -> Result { self.serialize().consensus_encode(e) } } @@ -362,7 +362,7 @@ impl Decodable for RangeProof { } impl Encodable for SurjectionProof { - fn consensus_encode(&self, e: W) -> Result { + fn consensus_encode(&self, e: W) -> Result { self.serialize().consensus_encode(e) } } @@ -375,7 +375,7 @@ impl Decodable for SurjectionProof { impl Encodable for sha256::Hash { - fn consensus_encode(&self, s: S) -> Result { + fn consensus_encode(&self, s: S) -> Result { self.to_byte_array().consensus_encode(s) } } @@ -387,7 +387,7 @@ impl Decodable for sha256::Hash { } impl Encodable for TapLeafHash { - fn consensus_encode(&self, s: S) -> Result { + fn consensus_encode(&self, s: S) -> Result { self.to_byte_array().consensus_encode(s) } } diff --git a/src/hash_types.rs b/src/hash_types.rs index 050c43b3..3e1fff33 100644 --- a/src/hash_types.rs +++ b/src/hash_types.rs @@ -22,7 +22,7 @@ use bitcoin::secp256k1::ThirtyTwoByteHash; macro_rules! impl_hashencode { ($hashtype:ident) => { impl $crate::encode::Encodable for $hashtype { - fn consensus_encode(&self, w: W) -> Result { + fn consensus_encode(&self, w: W) -> Result { self.0.consensus_encode(w) } } diff --git a/src/internal_macros.rs b/src/internal_macros.rs index 1936c6c2..304b690d 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -16,7 +16,7 @@ macro_rules! impl_consensus_encoding { ($thing:ident, $($field:ident),+) => ( impl $crate::encode::Encodable for $thing { #[inline] - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { let mut ret = 0; $( ret += self.$field.consensus_encode(&mut s)?; )+ Ok(ret) diff --git a/src/issuance.rs b/src/issuance.rs index a52158fe..aa80e6db 100644 --- a/src/issuance.rs +++ b/src/issuance.rs @@ -174,7 +174,7 @@ impl FromStr for AssetId { } impl Encodable for AssetId { - fn consensus_encode(&self, e: W) -> Result { + fn consensus_encode(&self, e: W) -> Result { self.0.consensus_encode(e) } } diff --git a/src/locktime.rs b/src/locktime.rs index 5ffddf8c..5f69063d 100644 --- a/src/locktime.rs +++ b/src/locktime.rs @@ -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; @@ -284,7 +284,7 @@ impl fmt::Display for LockTime { impl Encodable for LockTime { #[inline] - fn consensus_encode(&self, w: W) -> Result { + fn consensus_encode(&self, w: W) -> Result { let v = self.to_consensus_u32(); v.consensus_encode(w) } diff --git a/src/pset/macros.rs b/src/pset/macros.rs index edea5b17..8450ffc8 100644 --- a/src/pset/macros.rs +++ b/src/pset/macros.rs @@ -62,7 +62,7 @@ macro_rules! impl_psetmap_consensus_encoding { fn consensus_encode( &self, mut s: S, - ) -> Result { + ) -> Result { let mut len = 0; for pair in $crate::pset::Map::get_pairs(self)? { len += $crate::encode::Encodable::consensus_encode(&pair, &mut s)?; diff --git a/src/pset/map/global.rs b/src/pset/map/global.rs index b0989758..2dffc9b0 100644 --- a/src/pset/map/global.rs +++ b/src/pset/map/global.rs @@ -205,7 +205,7 @@ impl Map for Global { Ok(()) } - fn get_pairs(&self) -> Result, encode::Error> { + fn get_pairs(&self) -> Result, io::Error> { let mut rv: Vec = Default::default(); let TxData { diff --git a/src/pset/map/input.rs b/src/pset/map/input.rs index 93956860..3b9b8a82 100644 --- a/src/pset/map/input.rs +++ b/src/pset/map/input.rs @@ -725,7 +725,7 @@ impl Map for Input { Ok(()) } - fn get_pairs(&self) -> Result, encode::Error> { + fn get_pairs(&self) -> Result, io::Error> { let mut rv: Vec = Default::default(); impl_pset_get_pair! { diff --git a/src/pset/map/mod.rs b/src/pset/map/mod.rs index 8d8f191c..1688a8cd 100644 --- a/src/pset/map/mod.rs +++ b/src/pset/map/mod.rs @@ -12,6 +12,8 @@ // If not, see . // +use std::io; + use crate::encode; use crate::pset::{self, raw}; @@ -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, encode::Error>; + fn get_pairs(&self) -> Result, io::Error>; /// Attempt to merge with another key-value map of the same type. fn merge(&mut self, other: Self) -> Result<(), pset::Error>; diff --git a/src/pset/map/output.rs b/src/pset/map/output.rs index ad8df6ff..18d9b6b0 100644 --- a/src/pset/map/output.rs +++ b/src/pset/map/output.rs @@ -406,7 +406,7 @@ impl Map for Output { Ok(()) } - fn get_pairs(&self) -> Result, encode::Error> { + fn get_pairs(&self) -> Result, io::Error> { let mut rv: Vec = Default::default(); impl_pset_get_pair! { diff --git a/src/pset/mod.rs b/src/pset/mod.rs index e8cbc244..c784ef46 100644 --- a/src/pset/mod.rs +++ b/src/pset/mod.rs @@ -682,7 +682,7 @@ impl PartiallySignedTransaction { } impl Encodable for PartiallySignedTransaction { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { let mut len = 0; len += b"pset".consensus_encode(&mut s)?; diff --git a/src/pset/raw.rs b/src/pset/raw.rs index b9e3fdf6..8a955ea5 100644 --- a/src/pset/raw.rs +++ b/src/pset/raw.rs @@ -136,7 +136,7 @@ impl Decodable for Key { } impl Encodable for Key { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { let mut len = 0; len += VarInt((self.key.len() + 1) as u64).consensus_encode(&mut s)?; @@ -151,7 +151,7 @@ impl Encodable for Key { } impl Encodable for Pair { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { let len = self.key.consensus_encode(&mut s)?; Ok(len + self.value.consensus_encode(s)?) } @@ -170,7 +170,7 @@ impl Encodable for ProprietaryKey where Subtype: Copy + From + Into, { - fn consensus_encode(&self, mut e: W) -> Result { + fn consensus_encode(&self, mut e: W) -> Result { let mut len = self.prefix.consensus_encode(&mut e)? + 1; e.emit_u8(self.subtype.into())?; len += e.write(&self.key)?; diff --git a/src/script.rs b/src/script.rs index b3dadf33..8ff5e2c7 100644 --- a/src/script.rs +++ b/src/script.rs @@ -919,7 +919,7 @@ impl Encodable for Script { fn consensus_encode( &self, s: S, - ) -> Result { + ) -> Result { self.0.consensus_encode(s) } } diff --git a/src/sighash.rs b/src/sighash.rs index cc3edfe9..7ef32c9f 100644 --- a/src/sighash.rs +++ b/src/sighash.rs @@ -113,7 +113,7 @@ pub struct ScriptPath<'s> { pub enum Error { /// Could happen only by using `*_encode_signing_*` methods with custom writers, engines writers /// like the ones used in methods `*_signature_hash` don't error - Encode(encode::Error), + Io(io::Error), /// Requested index is greater or equal than the number of inputs in the transaction IndexOutOfInputsBounds { @@ -153,7 +153,7 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Error::Encode(ref e) => write!(f, "Writer errored: {:?}", e), + Error::Io(ref e) => write!(f, "Writer errored: {:?}", e), Error::IndexOutOfInputsBounds { index, inputs_size } => write!(f, "Requested index ({}) is greater or equal than the number of transaction inputs ({})", index, inputs_size), Error::SingleWithoutCorrespondingOutput { index, outputs_size } => write!(f, "SIGHASH_SINGLE for input ({}) haven't a corresponding output (#outputs:{})", index, outputs_size), Error::PrevoutsSize => write!(f, "Number of supplied prevouts differs from the number of inputs in transaction"), @@ -167,6 +167,12 @@ impl fmt::Display for Error { impl ::std::error::Error for Error {} +impl From for Error { + fn from(e: io::Error) -> Error { + Error::Io(e) + } +} + impl<'u, T> Prevouts<'u, T> where T: Borrow { fn check_all(&self, tx: &Transaction) -> Result<(), Error> { if let Prevouts::All(prevouts) = self { @@ -508,7 +514,7 @@ impl> SighashCache { script_code: &Script, value: confidential::Value, sighash_type: EcdsaSighashType, - ) -> Result<(), encode::Error> { + ) -> Result<(), io::Error> { let zero_hash = sha256d::Hash::all_zeros(); let (sighash, anyone_can_pay) = sighash_type.split_anyonecanpay_flag(); @@ -603,7 +609,7 @@ impl> SighashCache { input_index: usize, script_pubkey: &Script, sighash_type: EcdsaSighashType, - ) -> Result<(), encode::Error> { + ) -> Result<(), io::Error> { assert!(input_index < self.tx.input.len()); // Panic on OOB let (sighash, anyone_can_pay) = sighash_type.split_anyonecanpay_flag(); @@ -840,12 +846,6 @@ impl> SighashCache { } } -impl From for Error { - fn from(e: encode::Error) -> Self { - Error::Encode(e) - } -} - #[derive(Clone, PartialEq, Eq, Hash, Debug)] /// The `Annex` struct is a slice wrapper enforcing first byte to be `0x50` pub struct Annex<'a>(&'a [u8]); @@ -867,7 +867,7 @@ impl<'a> Annex<'a> { } impl<'a> Encodable for Annex<'a> { - fn consensus_encode(&self, writer: W) -> Result { + fn consensus_encode(&self, writer: W) -> Result { encode::consensus_encode_with_size(self.0, writer) } } diff --git a/src/transaction.rs b/src/transaction.rs index e04fdc40..53df6e68 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -103,7 +103,7 @@ impl Default for OutPoint { } impl Encodable for OutPoint { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { Ok(self.txid.consensus_encode(&mut s)? + self.vout.consensus_encode(&mut s)?) } @@ -481,7 +481,7 @@ impl Default for TxIn { serde_struct_impl!(TxIn, previous_output, is_pegin, script_sig, sequence, asset_issuance, witness); impl Encodable for TxIn { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { let mut ret = 0; let mut vout = self.previous_output.vout; if self.is_pegin { @@ -669,7 +669,7 @@ pub struct TxOut { serde_struct_impl!(TxOut, asset, value, nonce, script_pubkey, witness); impl Encodable for TxOut { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { Ok(self.asset.consensus_encode(&mut s)? + self.value.consensus_encode(&mut s)? + self.nonce.consensus_encode(&mut s)? + @@ -991,7 +991,7 @@ impl Transaction { } impl Encodable for Sequence { - fn consensus_encode(&self, w: W) -> Result { + fn consensus_encode(&self, w: W) -> Result { self.0.consensus_encode(w) } } @@ -1003,7 +1003,7 @@ impl Decodable for Sequence { } impl Encodable for Transaction { - fn consensus_encode(&self, mut s: S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { let mut ret = 0; ret += self.version.consensus_encode(&mut s)?;