diff --git a/zebra-chain/src/amount.rs b/zebra-chain/src/amount.rs index 38c82753b..23314272c 100644 --- a/zebra-chain/src/amount.rs +++ b/zebra-chain/src/amount.rs @@ -326,12 +326,14 @@ where } } -impl std::ops::Neg for Amount { - type Output = Self; - +impl std::ops::Neg for Amount +where + C: Constraint, +{ + type Output = Amount; fn neg(self) -> Self::Output { - Amount::try_from(-self.0) - .expect("a change in sign to any value inside Amount is always valid") + Amount::::try_from(-self.0) + .expect("a negation of any Amount into NegativeAllowed is always valid") } } diff --git a/zebra-chain/src/transaction.rs b/zebra-chain/src/transaction.rs index a4fec3edb..5afad812f 100644 --- a/zebra-chain/src/transaction.rs +++ b/zebra-chain/src/transaction.rs @@ -743,7 +743,7 @@ impl Transaction { .sum::>()?; Ok(ValueBalance::from_transparent_amount( - (input_value_balance - output_value_balance)?, + (output_value_balance - input_value_balance)?, )) } diff --git a/zebra-chain/src/value_balance.rs b/zebra-chain/src/value_balance.rs index 164d7a427..497047891 100644 --- a/zebra-chain/src/value_balance.rs +++ b/zebra-chain/src/value_balance.rs @@ -28,10 +28,11 @@ where /// /// [Consensus rule]: https://zips.z.cash/protocol/protocol.pdf#transactions pub fn remaining_transaction_value(&self) -> Result, 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::() + // 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::() } /// Creates a [`ValueBalance`] from the given transparent amount.