From ed96131c4f3b68af8fa2a7874ec8aaa2c5597fc8 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Thu, 10 Nov 2022 20:29:50 -0700 Subject: [PATCH] Add missing `std::fmt::Display` implementations for error types. --- .../src/data_api/wallet/input_selection.rs | 20 +++++++++++++ zcash_client_backend/src/fees.rs | 29 +++++++++++++++++++ .../src/transaction/components/amount.rs | 14 +++++++++ .../src/transaction/fees/zip317.rs | 13 +++++++++ 4 files changed, 76 insertions(+) diff --git a/zcash_client_backend/src/data_api/wallet/input_selection.rs b/zcash_client_backend/src/data_api/wallet/input_selection.rs index d95fed8c9..c8ecce7a6 100644 --- a/zcash_client_backend/src/data_api/wallet/input_selection.rs +++ b/zcash_client_backend/src/data_api/wallet/input_selection.rs @@ -189,6 +189,26 @@ pub enum GreedyInputSelectorError { Change(ChangeError), } +impl fmt::Display for GreedyInputSelectorError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match &self { + GreedyInputSelectorError::Balance(e) => write!( + f, + "A balance calculation violated amount validity bounds: {:?}.", + e + ), + GreedyInputSelectorError::UnsupportedAddress(_) => { + // we can't encode the UA to its string representation because we + // don't have network parameters here + write!(f, "Unified address contains no supported receivers.") + } + GreedyInputSelectorError::Change(err) => { + write!(f, "An error occurred computing change and fees: {}", err) + } + } + } +} + impl From> for InputSelectorError> diff --git a/zcash_client_backend/src/fees.rs b/zcash_client_backend/src/fees.rs index aaaca4ed7..9e34948ed 100644 --- a/zcash_client_backend/src/fees.rs +++ b/zcash_client_backend/src/fees.rs @@ -1,3 +1,5 @@ +use std::fmt; + use zcash_primitives::{ consensus::{self, BlockHeight}, transaction::{ @@ -95,6 +97,33 @@ pub enum ChangeError { StrategyError(E), } +impl fmt::Display for ChangeError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match &self { + ChangeError::InsufficientFunds { + available, + required, + } => write!( + f, + "Insufficient funds: required {} ZAT, but only {} ZAT were available.", + i64::from(required), + i64::from(available) + ), + ChangeError::DustInputs { + transparent, + sapling, + } => { + // we can't encode the UA to its string representation because we + // don't have network parameters here + write!(f, "Insufficient funds: {} dust inputs were present, but would cost more to spend than they are worth.", transparent.len() + sapling.len()) + } + ChangeError::StrategyError(err) => { + write!(f, "{}", err) + } + } + } +} + impl From for ChangeError { fn from(err: BalanceError) -> ChangeError { ChangeError::StrategyError(err) diff --git a/zcash_primitives/src/transaction/components/amount.rs b/zcash_primitives/src/transaction/components/amount.rs index 532e225f8..c98d7223c 100644 --- a/zcash_primitives/src/transaction/components/amount.rs +++ b/zcash_primitives/src/transaction/components/amount.rs @@ -261,6 +261,20 @@ pub enum BalanceError { Underflow, } +impl std::fmt::Display for BalanceError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match &self { + BalanceError::Overflow => { + write!(f, "Amount addition resulted in a value the valid range.") + } + BalanceError::Underflow => write!( + f, + "Amount subtraction resulted in a value outside the valid range." + ), + } + } +} + #[cfg(any(test, feature = "test-dependencies"))] pub mod testing { use proptest::prelude::prop_compose; diff --git a/zcash_primitives/src/transaction/fees/zip317.rs b/zcash_primitives/src/transaction/fees/zip317.rs index 6f5218129..8cc9360f6 100644 --- a/zcash_primitives/src/transaction/fees/zip317.rs +++ b/zcash_primitives/src/transaction/fees/zip317.rs @@ -98,6 +98,19 @@ impl From for FeeError { } } +impl std::fmt::Display for FeeError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match &self { + FeeError::Balance(e) => write!( + f, + "A balance calculation violated amount validity bounds: {}.", + e + ), + FeeError::NonP2pkhInputs(_) => write!(f, "Only P2PKH inputs are supported."), + } + } +} + impl super::FeeRule for FeeRule { type Error = FeeError;