Skip to content

Commit 6704530

Browse files
committed
Make fees work again
1 parent 9a2cf4d commit 6704530

File tree

4 files changed

+66
-38
lines changed

4 files changed

+66
-38
lines changed

Cargo.lock

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

validator_worker/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ path = "src/lib.rs"
1515
primitives = { path = "../primitives" }
1616
adapter = { version = "0.1", path = "../adapter" }
1717
chrono = { version = "0.4", features = ["serde"] }
18+
num = "0.2.0"
1819
# Futures
1920
futures-preview = { version = "=0.3.0-alpha.16", features = ["compat", "io-compat"] }
2021
futures_legacy = { version = "0.1", package = "futures" }

validator_worker/src/core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub mod fees;
12
pub mod follower_rules;

validator_worker/src/core/fees.rs

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use primitives::channel::Channel;
2-
use primitves::{BigNum};
3-
41
use std::collections::BTreeMap;
52

6-
use crate::{BigNum, Channel, DomainError, ValidatorDesc};
73
use num::rational::Ratio;
4+
use primitives::{BigNum, Channel, DomainError, ValidatorDesc};
85

96
pub type BalancesMap = BTreeMap<String, BigNum>;
107

@@ -53,7 +50,7 @@ fn distribute_fee<'a>(
5350

5451
if fee_rounded > 0.into() {
5552
let entry = balances
56-
.entry(validator.id.clone().into())
53+
.entry(validator.id.clone())
5754
.or_insert_with(|| 0.into());
5855

5956
*entry += &fee_rounded;
@@ -137,8 +134,9 @@ impl Distribution {
137134
#[cfg(test)]
138135
mod test {
139136
use super::*;
140-
use crate::channel::fixtures::{get_channel, get_channel_spec, ValidatorsOption};
141-
use crate::validator::fixtures::get_validator;
137+
use primitives::util::tests::prep_db::{
138+
DUMMY_CHANNEL, DUMMY_VALIDATOR_FOLLOWER, DUMMY_VALIDATOR_LEADER, IDS,
139+
};
142140

143141
mod applying_fee_returns_the_same_tree_with_zero_fees {
144142
use super::*;
@@ -199,27 +197,47 @@ mod test {
199197
}
200198

201199
fn get_zero_fee_channel() -> Channel {
202-
let leader = get_validator("one", Some(0.into()));
203-
let follower = get_validator("two", Some(0.into()));
204-
205-
let spec = get_channel_spec(ValidatorsOption::Pair { leader, follower });
206-
let mut channel = get_channel("zero fees", &None, Some(spec));
207-
channel.deposit_amount = 100_000.into();
208-
209-
channel
200+
let leader = ValidatorDesc {
201+
fee: 0.into(),
202+
..DUMMY_VALIDATOR_LEADER.clone()
203+
};
204+
let follower = ValidatorDesc {
205+
fee: 0.into(),
206+
..DUMMY_VALIDATOR_FOLLOWER.clone()
207+
};
208+
209+
let mut spec = DUMMY_CHANNEL.spec.clone();
210+
spec.validators = [leader, follower].into();
211+
212+
Channel {
213+
deposit_amount: 100_000.into(),
214+
spec,
215+
..DUMMY_CHANNEL.clone()
216+
}
210217
}
211218
}
212219

213220
mod applying_fee_correctly {
214221
use super::*;
215222

216223
fn setup_balances_after_fee(balances_map: BalancesMap) -> BalancesMap {
217-
let leader = get_validator("one", Some(50.into()));
218-
let follower = get_validator("two", Some(50.into()));
219-
220-
let spec = get_channel_spec(ValidatorsOption::Pair { leader, follower });
221-
let mut channel = get_channel("apply fees", &None, Some(spec));
222-
channel.deposit_amount = 10_000.into();
224+
let leader = ValidatorDesc {
225+
fee: 50.into(),
226+
..DUMMY_VALIDATOR_LEADER.clone()
227+
};
228+
let follower = ValidatorDesc {
229+
fee: 50.into(),
230+
..DUMMY_VALIDATOR_FOLLOWER.clone()
231+
};
232+
233+
let mut spec = DUMMY_CHANNEL.spec.clone();
234+
spec.validators = [leader, follower].into();
235+
236+
let channel = Channel {
237+
deposit_amount: 10_000.into(),
238+
spec,
239+
..DUMMY_CHANNEL.clone()
240+
};
223241

224242
let balances_after_fee = get_balances_after_fees_tree(&balances_map, &channel)
225243
.expect("Calculation of fees failed");
@@ -239,8 +257,8 @@ mod test {
239257
let expected_balances: BalancesMap = vec![
240258
("a".to_string(), 990.into()),
241259
("b".to_string(), 1_188.into()),
242-
("one".to_string(), 11.into()),
243-
("two".into(), 11.into()),
260+
(IDS.get("leader").unwrap().to_owned(), 11.into()),
261+
(IDS.get("follower").unwrap().to_owned(), 11.into()),
244262
]
245263
.into_iter()
246264
.collect();
@@ -263,16 +281,16 @@ mod test {
263281
let balances_map = vec![
264282
("a".to_string(), 100.into()),
265283
("b".to_string(), 2_000.into()),
266-
("one".to_string(), 200.into()),
284+
(IDS.get("leader").unwrap().to_owned(), 200.into()),
267285
]
268286
.into_iter()
269287
.collect();
270288

271289
let expected_balances: BalancesMap = vec![
272290
("a".to_string(), 99.into()),
273291
("b".to_string(), 1_980.into()),
274-
("one".to_string(), 209.into()),
275-
("two".into(), 11.into()),
292+
(IDS.get("leader").unwrap().to_owned(), 209.into()),
293+
(IDS.get("follower").unwrap().to_owned(), 11.into()),
276294
]
277295
.into_iter()
278296
.collect();
@@ -309,8 +327,8 @@ mod test {
309327
("c".to_string(), 693.into()),
310328
("d".to_string(), 4_950.into()),
311329
("e".to_string(), 3_960.into()),
312-
("one".to_string(), 51.into()),
313-
("two".to_string(), 50.into()),
330+
(IDS.get("leader").unwrap().to_owned(), 51.into()),
331+
(IDS.get("follower").unwrap().to_owned(), 50.into()),
314332
]
315333
.into_iter()
316334
.collect();
@@ -335,11 +353,23 @@ mod test {
335353
.into_iter()
336354
.collect();
337355

338-
let leader = get_validator("one", Some(600.into()));
339-
let follower = get_validator("two", Some(600.into()));
340-
let spec = get_channel_spec(ValidatorsOption::Pair { leader, follower });
341-
let mut channel = get_channel("zero fees", &None, Some(spec));
342-
channel.deposit_amount = 1_000.into();
356+
let leader = ValidatorDesc {
357+
fee: 600.into(),
358+
..DUMMY_VALIDATOR_LEADER.clone()
359+
};
360+
let follower = ValidatorDesc {
361+
fee: 600.into(),
362+
..DUMMY_VALIDATOR_FOLLOWER.clone()
363+
};
364+
365+
let mut spec = DUMMY_CHANNEL.spec.clone();
366+
spec.validators = [leader, follower].into();
367+
368+
let channel = Channel {
369+
deposit_amount: 1_000.into(),
370+
spec,
371+
..DUMMY_CHANNEL.clone()
372+
};
343373

344374
let domain_error = get_balances_after_fees_tree(&balances_map, &channel)
345375
.expect_err("Should be DomainError not allow fees sum to exceed the deposit");
@@ -351,9 +381,4 @@ mod test {
351381
domain_error
352382
);
353383
}
354-
355384
}
356-
357-
// pub fn get_balances_after_fees_tree(mut balances: &HashMap<String, String>, channel: &Channel) {
358-
// unimplemented!()
359-
// }

0 commit comments

Comments
 (0)