Use the same value balance sign for transparent and shielded outputs (#2569)

* change signs

* make impl of Neg generic

* change implementation of Neg for Amount

Co-authored-by: teor <teor@riseup.net>
This commit is contained in:
Alfredo Garcia 2021-08-08 19:41:34 -03:00 committed by GitHub
parent 8f4c3b09ea
commit 751185d4ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 10 deletions

View File

@ -326,12 +326,14 @@ where
}
}
impl std::ops::Neg for Amount<NegativeAllowed> {
type Output = Self;
impl<C> std::ops::Neg for Amount<C>
where
C: Constraint,
{
type Output = Amount<NegativeAllowed>;
fn neg(self) -> Self::Output {
Amount::try_from(-self.0)
.expect("a change in sign to any value inside Amount<NegativeAllowed> is always valid")
Amount::<NegativeAllowed>::try_from(-self.0)
.expect("a negation of any Amount into NegativeAllowed is always valid")
}
}

View File

@ -743,7 +743,7 @@ impl Transaction {
.sum::<Result<Amount, AmountError>>()?;
Ok(ValueBalance::from_transparent_amount(
(input_value_balance - output_value_balance)?,
(output_value_balance - input_value_balance)?,
))
}

View File

@ -28,10 +28,11 @@ where
///
/// [Consensus rule]: https://zips.z.cash/protocol/protocol.pdf#transactions
pub fn remaining_transaction_value(&self) -> Result<Amount<NonNegative>, Error> {
// This rule checks the transparent value balance minus the sum of the sprout,
// sapling, and orchard value balances in a transaction is nonnegative.
(self.transparent - (self.sprout + self.sapling + self.orchard)?)?
.constrain::<NonNegative>()
// Calculated in Zebra by negating the sum of the transparent, sprout,
// sapling, and orchard value balances as specified in
// https://zebra.zfnd.org/dev/rfcs/0012-value-pools.html#definitions
let value = (self.transparent + self.sprout + self.sapling + self.orchard)?;
(-(value)).constrain::<NonNegative>()
}
/// Creates a [`ValueBalance`] from the given transparent amount.