Skip to content

Commit 17acadd

Browse files
authored
Revert "Issue #102 mergeAggrs and mergePayoutsIntoBalances (#110)" (#116)
This reverts commit 27f50c8.
1 parent 27f50c8 commit 17acadd

File tree

6 files changed

+91
-337
lines changed

6 files changed

+91
-337
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

domain/src/balances_map.rs

Lines changed: 89 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,68 @@
11
use std::collections::BTreeMap;
22

3+
use serde::{Deserialize, Serialize};
4+
35
use crate::{BigNum, Channel, DomainError, ValidatorDesc};
46
use num::rational::Ratio;
57

6-
pub type BalancesMap = BTreeMap<String, BigNum>;
8+
type InnerBTreeMap = BTreeMap<String, BigNum>;
79

8-
pub fn get_balances_after_fees_tree(
9-
balances: &BalancesMap,
10-
channel: &Channel,
11-
) -> Result<BalancesMap, DomainError> {
12-
let distribution = Distribution::new(balances, &channel)?;
10+
#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq)]
11+
#[serde(transparent)]
12+
pub struct BalancesMap(InnerBTreeMap);
1313

14-
let mut balances_after_fees = BTreeMap::default();
15-
let mut total = BigNum::from(0);
14+
impl BalancesMap {
15+
pub fn apply_fees(&self, on_channel: &Channel) -> Result<Self, DomainError> {
16+
let distribution = Distribution::new(&self.0, &on_channel)?;
1617

17-
for (key, value) in balances.iter() {
18-
let adjusted_balance = value * &distribution.ratio;
18+
let mut balances_after_fees = BTreeMap::default();
19+
let mut total = BigNum::from(0);
1920

20-
total += &adjusted_balance;
21-
balances_after_fees.insert(key.clone(), adjusted_balance);
22-
}
21+
for (key, value) in self.0.iter() {
22+
let adjusted_balance = value * &distribution.ratio;
2323

24-
let rounding_error = distribution.rounding_error(&total)?;
24+
total += &adjusted_balance;
25+
balances_after_fees.insert(key.clone(), adjusted_balance);
26+
}
2527

26-
let balances_after_fees = distribute_fee(
27-
balances_after_fees,
28-
rounding_error,
29-
distribution.fee_ratio,
30-
channel.spec.validators.into_iter(),
31-
);
28+
let rounding_error = distribution.rounding_error(&total)?;
3229

33-
Ok(balances_after_fees)
34-
}
35-
36-
fn distribute_fee<'a>(
37-
mut balances: BalancesMap,
38-
rounding_error: BigNum,
39-
fee_ratio: Ratio<BigNum>,
40-
validators: impl Iterator<Item = &'a ValidatorDesc>,
41-
) -> BalancesMap {
42-
for (index, validator) in validators.enumerate() {
43-
let fee = &validator.fee * &fee_ratio;
44-
45-
let fee_rounded = if index == 0 {
46-
&fee + &rounding_error
47-
} else {
48-
fee
49-
};
30+
let balances_after_fees = Self::distribute_fee(
31+
balances_after_fees,
32+
rounding_error,
33+
distribution.fee_ratio,
34+
on_channel.spec.validators.into_iter(),
35+
);
5036

51-
if fee_rounded > 0.into() {
52-
let entry = balances
53-
.entry(validator.id.clone().into())
54-
.or_insert_with(|| 0.into());
37+
Ok(Self(balances_after_fees))
38+
}
5539

56-
*entry += &fee_rounded;
40+
fn distribute_fee<'a>(
41+
mut balances: InnerBTreeMap,
42+
rounding_error: BigNum,
43+
fee_ratio: Ratio<BigNum>,
44+
validators: impl Iterator<Item = &'a ValidatorDesc>,
45+
) -> InnerBTreeMap {
46+
for (index, validator) in validators.enumerate() {
47+
let fee = &validator.fee * &fee_ratio;
48+
49+
let fee_rounded = if index == 0 {
50+
&fee + &rounding_error
51+
} else {
52+
fee
53+
};
54+
55+
if fee_rounded > 0.into() {
56+
let entry = balances
57+
.entry(validator.id.clone().into())
58+
.or_insert_with(|| 0.into());
59+
60+
*entry += &fee_rounded;
61+
}
5762
}
58-
}
5963

60-
balances
64+
balances
65+
}
6166
}
6267

6368
#[derive(Debug)]
@@ -77,7 +82,7 @@ struct Distribution {
7782
}
7883

7984
impl Distribution {
80-
pub fn new(for_balances: &BalancesMap, on_channel: &Channel) -> Result<Self, DomainError> {
85+
pub fn new(for_balances: &InnerBTreeMap, on_channel: &Channel) -> Result<Self, DomainError> {
8186
let deposit = on_channel.deposit_amount.clone();
8287

8388
let total_distributed: BigNum = for_balances.iter().map(|(_, balance)| balance).sum();
@@ -139,60 +144,63 @@ mod test {
139144

140145
mod applying_fee_returns_the_same_tree_with_zero_fees {
141146
use super::*;
142-
fn setup_balances_map(balances_map: &BalancesMap) -> BalancesMap {
147+
fn setup_balances_map(tree: &InnerBTreeMap) -> BalancesMap {
143148
let channel = get_zero_fee_channel();
144149

145-
let balances_after_fee = get_balances_after_fees_tree(balances_map, &channel)
150+
let balances_map = BalancesMap(tree.clone());
151+
152+
let balances_after_fee = balances_map
153+
.apply_fees(&channel)
146154
.expect("Calculation of fees failed");
147155

148156
balances_after_fee
149157
}
150158

151159
#[test]
152160
fn case_1_three_values() {
153-
let balances_map: BalancesMap = vec![
161+
let tree: InnerBTreeMap = vec![
154162
("a".to_string(), 1001.into()),
155163
("b".to_string(), 3124.into()),
156164
("c".to_string(), 122.into()),
157165
]
158166
.into_iter()
159167
.collect();
160168

161-
assert_eq!(setup_balances_map(&balances_map), balances_map);
169+
assert_eq!(setup_balances_map(&tree).0, tree);
162170
}
163171

164172
#[test]
165173
fn case_2_three_simple_values() {
166-
let balances_map: BalancesMap = vec![
174+
let tree: InnerBTreeMap = vec![
167175
("a".to_string(), 1.into()),
168176
("b".to_string(), 2.into()),
169177
("c".to_string(), 3.into()),
170178
]
171179
.into_iter()
172180
.collect();
173181

174-
assert_eq!(setup_balances_map(&balances_map), balances_map);
182+
assert_eq!(setup_balances_map(&tree).0, tree);
175183
}
176184

177185
#[test]
178186
fn case_3_one_value() {
179-
let balances_map = vec![("a".to_string(), BigNum::from(1))]
187+
let tree: InnerBTreeMap = vec![("a".to_string(), BigNum::from(1))]
180188
.into_iter()
181189
.collect();
182190

183-
assert_eq!(setup_balances_map(&balances_map), balances_map);
191+
assert_eq!(setup_balances_map(&tree).0, tree);
184192
}
185193

186194
#[test]
187195
fn case_4_two_values() {
188-
let balances_map = vec![
196+
let tree: InnerBTreeMap = vec![
189197
("a".to_string(), 1.into()),
190198
("b".to_string(), 99_999.into()),
191199
]
192200
.into_iter()
193201
.collect();
194202

195-
assert_eq!(setup_balances_map(&balances_map), balances_map);
203+
assert_eq!(setup_balances_map(&tree).0, tree);
196204
}
197205

198206
fn get_zero_fee_channel() -> Channel {
@@ -210,30 +218,33 @@ mod test {
210218
mod applying_fee_correctly {
211219
use super::*;
212220

213-
fn setup_balances_after_fee(balances_map: BalancesMap) -> BalancesMap {
221+
fn setup_balances_after_fee(tree: InnerBTreeMap) -> BalancesMap {
214222
let leader = get_validator("one", Some(50.into()));
215223
let follower = get_validator("two", Some(50.into()));
216224

217225
let spec = get_channel_spec(ValidatorsOption::Pair { leader, follower });
218226
let mut channel = get_channel("apply fees", &None, Some(spec));
219227
channel.deposit_amount = 10_000.into();
220228

221-
let balances_after_fee = get_balances_after_fees_tree(&balances_map, &channel)
229+
let balances_map = BalancesMap(tree);
230+
231+
let balances_after_fee = balances_map
232+
.apply_fees(&channel)
222233
.expect("Calculation of fees failed");
223234

224235
balances_after_fee
225236
}
226237

227238
#[test]
228239
fn case_1_partially_distributed() {
229-
let balances_map = vec![
240+
let tree = vec![
230241
("a".to_string(), 1_000.into()),
231242
("b".to_string(), 1_200.into()),
232243
]
233244
.into_iter()
234245
.collect();
235246

236-
let expected_balances: BalancesMap = vec![
247+
let expected_tree: InnerBTreeMap = vec![
237248
("a".to_string(), 990.into()),
238249
("b".to_string(), 1_188.into()),
239250
("one".to_string(), 11.into()),
@@ -242,30 +253,27 @@ mod test {
242253
.into_iter()
243254
.collect();
244255

245-
let balances_after_fee = setup_balances_after_fee(balances_map);
256+
let balances_after_fee = setup_balances_after_fee(tree).0;
246257
let actual_sum: BigNum = balances_after_fee.iter().map(|(_, v)| v).sum();
247258

248259
assert_eq!(
249-
expected_balances
250-
.iter()
251-
.map(|(_, value)| value)
252-
.sum::<BigNum>(),
260+
expected_tree.iter().map(|(_, value)| value).sum::<BigNum>(),
253261
actual_sum
254262
);
255-
assert_eq!(expected_balances, balances_after_fee);
263+
assert_eq!(expected_tree, balances_after_fee);
256264
}
257265

258266
#[test]
259-
fn case_2_partially_distributed_with_validator_in_the_input_balances_map() {
260-
let balances_map = vec![
267+
fn case_2_partially_distributed_with_validator_in_the_input_tree() {
268+
let tree = vec![
261269
("a".to_string(), 100.into()),
262270
("b".to_string(), 2_000.into()),
263271
("one".to_string(), 200.into()),
264272
]
265273
.into_iter()
266274
.collect();
267275

268-
let expected_balances: BalancesMap = vec![
276+
let expected_tree: InnerBTreeMap = vec![
269277
("a".to_string(), 99.into()),
270278
("b".to_string(), 1_980.into()),
271279
("one".to_string(), 209.into()),
@@ -274,23 +282,20 @@ mod test {
274282
.into_iter()
275283
.collect();
276284

277-
let balances_after_fee = setup_balances_after_fee(balances_map);
285+
let balances_after_fee = setup_balances_after_fee(tree).0;
278286
let actual_sum: BigNum = balances_after_fee.iter().map(|(_, v)| v).sum();
279287

280288
assert_eq!(
281-
expected_balances
282-
.iter()
283-
.map(|(_, value)| value)
284-
.sum::<BigNum>(),
289+
expected_tree.iter().map(|(_, value)| value).sum::<BigNum>(),
285290
actual_sum
286291
);
287-
assert_eq!(expected_balances, balances_after_fee);
292+
assert_eq!(expected_tree, balances_after_fee);
288293
}
289294

290295
#[test]
291296
/// also testing the rounding error correction
292297
fn case_3_fully_distributed() {
293-
let balances_map = vec![
298+
let tree = vec![
294299
("a".to_string(), 105.into()),
295300
("b".to_string(), 195.into()),
296301
("c".to_string(), 700.into()),
@@ -300,7 +305,7 @@ mod test {
300305
.into_iter()
301306
.collect();
302307

303-
let expected_balances: BalancesMap = vec![
308+
let expected_tree: InnerBTreeMap = vec![
304309
("a".to_string(), 103.into()),
305310
("b".to_string(), 193.into()),
306311
("c".to_string(), 693.into()),
@@ -312,23 +317,20 @@ mod test {
312317
.into_iter()
313318
.collect();
314319

315-
let balances_after_fee = setup_balances_after_fee(balances_map);
320+
let balances_after_fee = setup_balances_after_fee(tree).0;
316321
let actual_sum: BigNum = balances_after_fee.iter().map(|(_, v)| v).sum();
317322

318323
assert_eq!(
319-
expected_balances
320-
.iter()
321-
.map(|(_, value)| value)
322-
.sum::<BigNum>(),
324+
expected_tree.iter().map(|(_, value)| value).sum::<BigNum>(),
323325
actual_sum
324326
);
325-
assert_eq!(expected_balances, balances_after_fee);
327+
assert_eq!(expected_tree, balances_after_fee);
326328
}
327329
}
328330

329331
#[test]
330332
fn errors_when_fees_larger_that_deposit() {
331-
let balances_map = vec![("a".to_string(), 10.into()), ("b".to_string(), 10.into())]
333+
let tree: InnerBTreeMap = vec![("a".to_string(), 10.into()), ("b".to_string(), 10.into())]
332334
.into_iter()
333335
.collect();
334336

@@ -338,7 +340,10 @@ mod test {
338340
let mut channel = get_channel("zero fees", &None, Some(spec));
339341
channel.deposit_amount = 1_000.into();
340342

341-
let domain_error = get_balances_after_fees_tree(&balances_map, &channel)
343+
let balances_map = BalancesMap(tree.clone());
344+
345+
let domain_error = balances_map
346+
.apply_fees(&channel)
342347
.expect_err("Should be DomainError not allow fees sum to exceed the deposit");
343348

344349
assert_eq!(

domain/src/big_num.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std::ops::{Add, AddAssign, Div, Mul, Sub};
55
use std::str::FromStr;
66

77
use num::rational::Ratio;
8-
use num::{BigUint, CheckedSub, Integer};
8+
use num::Integer;
9+
use num_bigint::BigUint;
910
use num_derive::{Num, NumOps, One, Zero};
1011
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1112

@@ -150,12 +151,6 @@ impl<'a> Sum<&'a BigNum> for BigNum {
150151
}
151152
}
152153

153-
impl CheckedSub for BigNum {
154-
fn checked_sub(&self, v: &Self) -> Option<Self> {
155-
self.0.checked_sub(&v.0).map(Self)
156-
}
157-
}
158-
159154
impl Mul<&Ratio<BigNum>> for &BigNum {
160155
type Output = BigNum;
161156

validator/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ domain = { version = "0.1", path = "../domain", features = ["repositories", "fix
1414
adapter = { version = "0.1", path = "../adapter", features = ["dummy-adapter"] }
1515
memory-repository = { version = "0.1", path = "../memory-repository" }
1616
chrono = { version = "0.4", features = ["serde"] }
17-
num-traits = "0.2"
1817
# Futures
1918
futures-preview = { version = "=0.3.0-alpha.16", features = ["compat", "io-compat"] }
2019
futures_legacy = { version = "0.1", package = "futures" }

validator/src/domain.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ pub use self::validator::{Validator, ValidatorError, ValidatorFuture};
44
pub use self::worker::{Worker, WorkerFuture};
55

66
pub mod channel;
7-
pub mod event;
87
pub mod validator;
98
pub mod worker;

0 commit comments

Comments
 (0)