Associate io::Error with psbt::Error

In order to associate the error, psbt::Error must not derive so many
traits. Tests are also adjusted for the new error type.
This commit is contained in:
DanGould 2023-02-02 12:34:49 -05:00
parent f52301151c
commit 126cbb00ef
No known key found for this signature in database
GPG Key ID: B07290B28996AFE0
3 changed files with 28 additions and 8 deletions

View File

@ -11,6 +11,7 @@ use crate::consensus::encode;
use crate::psbt::raw;
use crate::hashes;
use crate::io;
use crate::bip32::ExtendedPubKey;
/// Enum for marking psbt hash error.
@ -22,7 +23,7 @@ pub enum PsbtHash {
Hash256,
}
/// Ways that a Partially Signed Transaction might fail.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// Magic bytes for a PSBT must be the ASCII for "psbt" serialized in most
@ -100,6 +101,8 @@ pub enum Error {
Version(&'static str),
/// PSBT data is not consumed entirely
PartialDataConsumption,
/// I/O error.
Io(io::Error),
}
impl fmt::Display for Error {
@ -140,6 +143,7 @@ impl fmt::Display for Error {
Error::XPubKey(s) => write!(f, "xpub key error - {}", s),
Error::Version(s) => write!(f, "version error {}", s),
Error::PartialDataConsumption => f.write_str("data not consumed entirely when explicitly deserializing"),
Error::Io(ref e) => write_err!(f, "I/O error"; e),
}
}
}
@ -152,6 +156,7 @@ impl std::error::Error for Error {
match self {
HashParse(e) => Some(e),
Io(e) => Some(e),
| InvalidMagic
| MissingUtxo
| InvalidSeparator
@ -180,7 +185,7 @@ impl std::error::Error for Error {
| Taproot(_)
| XPubKey(_)
| Version(_)
| PartialDataConsumption=> None
| PartialDataConsumption=> None,
}
}
}
@ -197,3 +202,9 @@ impl From<encode::Error> for Error {
Error::ConsensusEncoding
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}

View File

@ -1732,15 +1732,24 @@ mod tests {
// no previous output
let mut t2 = t.clone();
t2.inputs[0].non_witness_utxo = None;
assert_eq!(t2.fee(), Err(Error::MissingUtxo));
match t2.fee().unwrap_err() {
Error::MissingUtxo => {},
e => panic!("unexpected error: {:?}", e)
}
// negative fee
let mut t3 = t.clone();
t3.unsigned_tx.output[0].value = prev_output_val;
assert_eq!(t3.fee(), Err(Error::NegativeFee));
match t3.fee().unwrap_err() {
Error::NegativeFee => {},
e => panic!("unexpected error: {:?}", e)
}
// overflow
t.unsigned_tx.output[0].value = u64::max_value();
t.unsigned_tx.output[1].value = u64::max_value();
assert_eq!(t.fee(), Err(Error::FeeOverflow));
match t.fee().unwrap_err() {
Error::FeeOverflow => {},
e => panic!("unexpected error: {:?}", e)
}
}
#[test]

View File

@ -225,7 +225,7 @@ impl Serialize for KeySource {
impl Deserialize for KeySource {
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() < 4 {
return Err(encode::Error::from(io::Error::from(io::ErrorKind::UnexpectedEof)).into())
return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into())
}
let fprint: Fingerprint = bytes[0..4].try_into().expect("4 is the fingerprint length");
@ -319,7 +319,7 @@ impl Serialize for (XOnlyPublicKey, TapLeafHash) {
impl Deserialize for (XOnlyPublicKey, TapLeafHash) {
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() < 32 {
return Err(encode::Error::from(io::Error::from(io::ErrorKind::UnexpectedEof)).into())
return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into())
}
let a: XOnlyPublicKey = Deserialize::deserialize(&bytes[..32])?;
let b: TapLeafHash = Deserialize::deserialize(&bytes[32..])?;
@ -353,7 +353,7 @@ impl Serialize for (ScriptBuf, LeafVersion) {
impl Deserialize for (ScriptBuf, LeafVersion) {
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
if bytes.is_empty() {
return Err(encode::Error::from(io::Error::from(io::ErrorKind::UnexpectedEof)).into())
return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into())
}
// The last byte is LeafVersion.
let script = ScriptBuf::deserialize(&bytes[..bytes.len() - 1])?;