Apply suggestions from code review

Co-authored-by: str4d <thestr4d@gmail.com>
This commit is contained in:
Kris Nuttycombe 2022-09-07 11:10:34 -06:00 committed by Kris Nuttycombe
parent 167bcd86ce
commit a93c9d334e
3 changed files with 8 additions and 11 deletions

View File

@ -25,9 +25,7 @@ impl Bundle {
pub fn value_balance(&self) -> Option<Amount> {
self.joinsplits
.iter()
.try_fold(Amount::zero(), |total, js| {
js.value_balance().and_then(|b| total + b)
})
.try_fold(Amount::zero(), |total, js| total + js.net_value())
}
}
@ -187,11 +185,11 @@ impl JsDescription {
writer.write_all(&self.ciphertexts[1])
}
/// The value balance for the JoinSplit. When this is positive,
/// The net value for the JoinSplit. When this is positive,
/// its value is added to the transparent value pool; when it
/// is negative, its value is subtracted from the transparent
/// value pool.
pub fn value_balance(&self) -> Option<Amount> {
self.vpub_new - self.vpub_old
pub fn net_value(&self) -> Amount {
(self.vpub_new - self.vpub_old).expect("difference is in range [-MAX_MONEY..=MAX_MONEY]")
}
}

View File

@ -68,12 +68,10 @@ impl<A: Authorization> Bundle<A> {
/// transferred out of the transparent pool into shielded pools or to fees; a negative value
/// means that the containing transaction has funds being transferred into the transparent pool
/// from the shielded pools.
pub fn value_balance<E, F: FnMut(&OutPoint) -> Result<Amount, E>>(
&self,
mut get_prevout_value: F,
) -> Result<Amount, E>
pub fn value_balance<E, F>(&self, mut get_prevout_value: F) -> Result<Amount, E>
where
E: From<BalanceError>,
F: FnMut(&OutPoint) -> Result<Amount, E>,
{
let input_sum = self.vin.iter().try_fold(Amount::zero(), |total, txin| {
get_prevout_value(&txin.prevout)

View File

@ -420,7 +420,8 @@ impl<A: Authorization> TransactionData<A> {
.map_or_else(Amount::zero, |b| *b.value_balance()),
];
IntoIterator::into_iter(&value_balances)
value_balances
.iter()
.sum::<Option<_>>()
.ok_or_else(|| BalanceError::Overflow.into())
}