bee_ledger/types/
balance.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::types::{error::Error, BalanceDiff};
5
6use bee_common::packable::{Packable, Read, Write};
7use bee_message::{constants::IOTA_SUPPLY, output::dust_outputs_max};
8
9/// Holds the balance of an address.
10#[derive(Clone, Debug, Default, Eq, PartialEq)]
11pub struct Balance {
12    amount: u64,
13    dust_allowance: u64,
14    dust_outputs: u64,
15}
16
17impl Balance {
18    /// Creates a new `Balance`.
19    pub fn new(amount: u64, dust_allowance: u64, dust_outputs: u64) -> Result<Self, Error> {
20        if amount > IOTA_SUPPLY {
21            Err(Error::InvalidBalance(amount))
22        } else if dust_allowance > IOTA_SUPPLY {
23            Err(Error::InvalidBalance(dust_allowance))
24        } else if dust_outputs > IOTA_SUPPLY {
25            Err(Error::InvalidBalance(dust_outputs))
26        } else {
27            Ok(Self {
28                amount,
29                dust_allowance,
30                dust_outputs,
31            })
32        }
33    }
34
35    /// Returns the amount of the `Balance`.
36    pub fn amount(&self) -> u64 {
37        self.amount
38    }
39
40    /// Returns the dust allowance of the `Balance`.
41    pub fn dust_allowance(&self) -> u64 {
42        self.dust_allowance
43    }
44
45    /// Returns the number of dust outputs of the `Balance`.
46    pub fn dust_outputs(&self) -> u64 {
47        self.dust_outputs
48    }
49
50    /// Returns whether more dust is allowed on the `Balance`.
51    pub fn dust_allowed(&self) -> bool {
52        self.dust_outputs() < dust_outputs_max(self.dust_allowance())
53    }
54
55    /// Safely applies a `BalanceDiff` to the `Balance`.
56    pub fn apply_diff(self, diff: &BalanceDiff) -> Result<Self, Error> {
57        let amount = (self.amount as i64)
58            .checked_add(diff.amount())
59            .ok_or_else(|| Error::BalanceOverflow(self.amount as i128 + diff.amount() as i128))?;
60        let dust_allowance = (self.dust_allowance() as i64)
61            .checked_add(diff.dust_allowance())
62            .ok_or_else(|| Error::BalanceOverflow(self.dust_allowance() as i128 + diff.dust_allowance() as i128))?;
63        let dust_outputs = (self.dust_outputs as i64)
64            .checked_add(diff.dust_outputs())
65            .ok_or_else(|| Error::BalanceOverflow(self.dust_outputs as i128 + diff.dust_outputs() as i128))?;
66
67        // Given the nature of Utxo, this is not supposed to happen.
68        if amount < 0 {
69            return Err(Error::NegativeBalance(amount));
70        }
71        if dust_allowance < 0 {
72            return Err(Error::NegativeDustAllowance(dust_allowance));
73        }
74        if dust_outputs < 0 {
75            return Err(Error::NegativeDustOutputs(dust_outputs));
76        }
77
78        Ok(Self {
79            amount: amount as u64,
80            dust_allowance: dust_allowance as u64,
81            dust_outputs: dust_outputs as u64,
82        })
83    }
84}
85
86impl Packable for Balance {
87    type Error = Error;
88
89    fn packed_len(&self) -> usize {
90        self.amount.packed_len() + self.dust_allowance.packed_len() + self.dust_outputs.packed_len()
91    }
92
93    fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
94        self.amount.pack(writer)?;
95        self.dust_allowance.pack(writer)?;
96        self.dust_outputs.pack(writer)?;
97
98        Ok(())
99    }
100
101    fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error> {
102        let amount = u64::unpack_inner::<R, CHECK>(reader)?;
103        let dust_allowance = u64::unpack_inner::<R, CHECK>(reader)?;
104        let dust_outputs = u64::unpack_inner::<R, CHECK>(reader)?;
105
106        Balance::new(amount, dust_allowance, dust_outputs)
107    }
108}