Amount::{zero, is_positive, is_negative}

This commit is contained in:
Jack Grigg 2019-07-25 20:50:17 +01:00
parent fa50d551c8
commit 0ea4408d46
No known key found for this signature in database
GPG Key ID: 9E8255172BBF9898
3 changed files with 21 additions and 4 deletions

View File

@ -79,7 +79,7 @@ impl SaplingOutput {
Some(g_d) => g_d,
None => return Err(Error(ErrorKind::InvalidAddress)),
};
if value.0 < 0 {
if value.is_negative() {
return Err(Error(ErrorKind::InvalidAmount));
}
@ -288,7 +288,7 @@ impl<R: RngCore + CryptoRng> Builder<R> {
to: &TransparentAddress,
value: Amount,
) -> Result<(), Error> {
if value.0 < 0 {
if value.is_negative() {
return Err(Error(ErrorKind::InvalidAmount));
}

View File

@ -8,6 +8,11 @@ const MAX_MONEY: i64 = 21_000_000 * COIN;
pub struct Amount(pub i64);
impl Amount {
/// Returns a zero-valued Amount.
pub const fn zero() -> Self {
Amount(0)
}
// Read an Amount from a signed 64-bit little-endian integer.
pub fn read_i64<R: Read>(mut reader: R, allow_negative: bool) -> io::Result<Self> {
let amount = reader.read_i64::<LittleEndian>()?;
@ -39,6 +44,18 @@ impl Amount {
))
}
}
/// Returns `true` if `self` is positive and `false` if the number is zero or
/// negative.
pub const fn is_positive(self) -> bool {
self.0.is_positive()
}
/// Returns `true` if `self` is negative and `false` if the number is zero or
/// positive.
pub const fn is_negative(self) -> bool {
self.0.is_negative()
}
}
#[cfg(test)]

View File

@ -112,7 +112,7 @@ impl TransactionData {
vout: vec![],
lock_time: 0,
expiry_height: 0,
value_balance: Amount(0),
value_balance: Amount::zero(),
shielded_spends: vec![],
shielded_outputs: vec![],
joinsplits: vec![],
@ -190,7 +190,7 @@ impl Transaction {
let so = Vector::read(&mut reader, OutputDescription::read)?;
(vb, ss, so)
} else {
(Amount(0), vec![], vec![])
(Amount::zero(), vec![], vec![])
};
let (joinsplits, joinsplit_pubkey, joinsplit_sig) = if version >= 2 {