|
3 | 3 |
|
4 | 4 | use crate::types::Error; |
5 | 5 |
|
6 | | -use bee_message::{address::Address, constants::IOTA_SUPPLY}; |
| 6 | +use bee_message::{ |
| 7 | + address::Address, |
| 8 | + constants::IOTA_SUPPLY, |
| 9 | + output::{Output, DUST_THRESHOLD}, |
| 10 | +}; |
7 | 11 |
|
8 | 12 | use std::collections::{ |
9 | 13 | hash_map::{IntoIter, Iter, IterMut}, |
@@ -100,6 +104,49 @@ impl BalanceDiffs { |
100 | 104 | self.0.get(address) |
101 | 105 | } |
102 | 106 |
|
| 107 | + /// Negates a `BalanceDiffs`. |
| 108 | + pub fn negate(&mut self) { |
| 109 | + for (_, diff) in self.iter_mut() { |
| 110 | + diff.negate(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + pub fn output_add(&mut self, output: &Output) -> Result<(), Error> { |
| 115 | + match output { |
| 116 | + Output::SignatureLockedSingle(output) => { |
| 117 | + self.amount_add(*output.address(), output.amount())?; |
| 118 | + if output.amount() < DUST_THRESHOLD { |
| 119 | + self.dust_outputs_inc(*output.address())?; |
| 120 | + } |
| 121 | + } |
| 122 | + Output::SignatureLockedDustAllowance(output) => { |
| 123 | + self.amount_add(*output.address(), output.amount())?; |
| 124 | + self.dust_allowance_add(*output.address(), output.amount())?; |
| 125 | + } |
| 126 | + output => return Err(Error::UnsupportedOutputKind(output.kind())), |
| 127 | + } |
| 128 | + |
| 129 | + Ok(()) |
| 130 | + } |
| 131 | + |
| 132 | + pub fn output_sub(&mut self, output: &Output) -> Result<(), Error> { |
| 133 | + match output { |
| 134 | + Output::SignatureLockedSingle(output) => { |
| 135 | + self.amount_sub(*output.address(), output.amount())?; |
| 136 | + if output.amount() < DUST_THRESHOLD { |
| 137 | + self.dust_outputs_dec(*output.address())?; |
| 138 | + } |
| 139 | + } |
| 140 | + Output::SignatureLockedDustAllowance(output) => { |
| 141 | + self.amount_sub(*output.address(), output.amount())?; |
| 142 | + self.dust_allowance_sub(*output.address(), output.amount())?; |
| 143 | + } |
| 144 | + output => return Err(Error::UnsupportedOutputKind(output.kind())), |
| 145 | + } |
| 146 | + |
| 147 | + Ok(()) |
| 148 | + } |
| 149 | + |
103 | 150 | /// Adds a given amount to a given address. |
104 | 151 | pub fn amount_add(&mut self, address: Address, amount: u64) -> Result<(), Error> { |
105 | 152 | let entry = self.0.entry(address).or_default(); |
@@ -169,13 +216,6 @@ impl BalanceDiffs { |
169 | 216 | pub fn iter_mut(&mut self) -> IterMut<'_, Address, BalanceDiff> { |
170 | 217 | self.0.iter_mut() |
171 | 218 | } |
172 | | - |
173 | | - /// Negates a `BalanceDiffs`. |
174 | | - pub fn negate(&mut self) { |
175 | | - for (_, diff) in self.iter_mut() { |
176 | | - diff.negate(); |
177 | | - } |
178 | | - } |
179 | 219 | } |
180 | 220 |
|
181 | 221 | impl IntoIterator for BalanceDiffs { |
|
0 commit comments