Skip to content

Commit 36fc543

Browse files
authored
Merge pull request #135 from AdExNetwork/issue-120
Issue #120 merge_aggrs & merge_payouts_into_balances
2 parents dbed5cb + dfd0216 commit 36fc543

File tree

7 files changed

+63
-38
lines changed

7 files changed

+63
-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.

primitives/src/balances_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::collections::HashMap;
1+
use std::collections::BTreeMap;
22

33
use crate::BigNum;
44

5-
pub type BalancesMap = HashMap<String, BigNum>;
5+
pub type BalancesMap = BTreeMap<String, BigNum>;

primitives/src/big_num.rs

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

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

@@ -151,6 +150,12 @@ impl<'a> Sum<&'a BigNum> for BigNum {
151150
}
152151
}
153152

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+
154159
impl Mul<&Ratio<BigNum>> for &BigNum {
155160
type Output = BigNum;
156161

validator_worker/Cargo.toml

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

validator_worker/src/core/events.rs

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use chrono::{DateTime, Utc};
44
use num_traits::CheckedSub;
55
use serde::{Deserialize, Serialize};
66

7-
use domain::balances_map::get_balances_after_fees_tree;
8-
use domain::validator::message::Accounting;
9-
use domain::{BalancesMap, BigNum, Channel, ChannelId, DomainError};
10-
7+
use crate::core::fees::get_balances_after_fees_tree;
8+
use primitives::sentry::{AggregateEvents, EventAggregate};
9+
use primitives::validator::Accounting;
10+
use primitives::{BalancesMap, BigNum, Channel, DomainError};
1111

1212
#[allow(dead_code)]
1313
fn merge_aggrs(
@@ -25,7 +25,7 @@ fn merge_aggrs(
2525
.to_owned();
2626

2727
// Build an intermediary balances representation
28-
let mut balances_before_fees = accounting.pre_fees.clone();
28+
let mut balances_before_fees = accounting.balances_before_fees.clone();
2929

3030
// Merge in all the aggrs
3131
for aggr in aggregates {
@@ -38,7 +38,7 @@ fn merge_aggrs(
3838

3939
let new_accounting = Accounting {
4040
last_event_aggregate,
41-
pre_fees: balances_before_fees,
41+
balances_before_fees,
4242
balances: balances.clone(),
4343
};
4444

@@ -83,20 +83,29 @@ fn merge_payouts_into_balances<'a, T: Iterator<Item = &'a AggregateEvents>>(
8383
#[cfg(test)]
8484
mod test {
8585
use super::*;
86-
use domain::channel::fixtures::{get_channel, get_channel_spec, ValidatorsOption};
87-
use domain::fixtures::get_channel_id;
88-
use domain::validator::fixtures::get_validator;
86+
use primitives::util::tests::prep_db::{
87+
DUMMY_CHANNEL, DUMMY_VALIDATOR_FOLLOWER, DUMMY_VALIDATOR_LEADER,
88+
};
89+
use primitives::{Channel, ChannelSpec, ValidatorDesc};
8990

9091
#[test]
9192
fn should_merge_event_aggrs_and_apply_fees() {
9293
// fees: 100
93-
// deposit: 10000
94-
let leader = get_validator("one", Some(50.into()));
95-
let follower = get_validator("two", Some(50.into()));
94+
// deposit: 10 000
95+
let leader = ValidatorDesc {
96+
fee: 50.into(),
97+
..DUMMY_VALIDATOR_LEADER.clone()
98+
};
99+
let follower = ValidatorDesc {
100+
fee: 50.into(),
101+
..DUMMY_VALIDATOR_FOLLOWER.clone()
102+
};
96103

97-
let spec = get_channel_spec(ValidatorsOption::Pair { leader, follower });
98-
let mut channel = get_channel("channel", &None, Some(spec));
99-
channel.deposit_amount = 10_000.into();
104+
let mut channel = Channel {
105+
deposit_amount: 10_000.into(),
106+
..DUMMY_CHANNEL.clone()
107+
};
108+
channel.spec.validators = [leader, follower].into();
100109

101110
let balances_before_fees: BalancesMap =
102111
vec![("a".to_string(), 100.into()), ("b".to_string(), 200.into())]
@@ -105,7 +114,7 @@ mod test {
105114

106115
let acc = Accounting {
107116
last_event_aggregate: Utc::now(),
108-
pre_fees: balances_before_fees,
117+
balances_before_fees,
109118
balances: BalancesMap::default(),
110119
};
111120

@@ -114,7 +123,7 @@ mod test {
114123

115124
assert_eq!(balances, new_accounting.balances, "balances is the same");
116125
assert_eq!(
117-
new_accounting.pre_fees["a"],
126+
new_accounting.balances_before_fees["a"],
118127
150.into(),
119128
"balance of recipient incremented accordingly"
120129
);
@@ -127,12 +136,24 @@ mod test {
127136

128137
#[test]
129138
fn should_never_allow_exceeding_the_deposit() {
130-
let leader = get_validator("one", Some(50.into()));
131-
let follower = get_validator("two", Some(50.into()));
139+
let leader = ValidatorDesc {
140+
fee: 50.into(),
141+
..DUMMY_VALIDATOR_LEADER.clone()
142+
};
143+
let follower = ValidatorDesc {
144+
fee: 50.into(),
145+
..DUMMY_VALIDATOR_FOLLOWER.clone()
146+
};
132147

133-
let spec = get_channel_spec(ValidatorsOption::Pair { leader, follower });
134-
let mut channel = get_channel("channel", &None, Some(spec));
135-
channel.deposit_amount = 10_000.into();
148+
let spec = ChannelSpec {
149+
validators: [leader, follower].into(),
150+
..DUMMY_CHANNEL.spec.clone()
151+
};
152+
let channel = Channel {
153+
deposit_amount: 10_000.into(),
154+
spec,
155+
..DUMMY_CHANNEL.clone()
156+
};
136157

137158
let balances_before_fees: BalancesMap =
138159
vec![("a".to_string(), 100.into()), ("b".to_string(), 200.into())]
@@ -141,7 +162,7 @@ mod test {
141162

142163
let acc = Accounting {
143164
last_event_aggregate: Utc::now(),
144-
pre_fees: balances_before_fees,
165+
balances_before_fees,
145166
balances: BalancesMap::default(),
146167
};
147168

@@ -150,12 +171,12 @@ mod test {
150171

151172
assert_eq!(balances, new_accounting.balances, "balances is the same");
152173
assert_eq!(
153-
new_accounting.pre_fees["a"],
174+
new_accounting.balances_before_fees["a"],
154175
9_800.into(),
155176
"balance of recipient incremented accordingly"
156177
);
157178
assert_eq!(
158-
new_accounting.pre_fees["b"],
179+
new_accounting.balances_before_fees["b"],
159180
200.into(),
160181
"balances of non-recipient remains the same"
161182
);
@@ -165,7 +186,7 @@ mod test {
165186
"balanceAfterFees is ok"
166187
);
167188
assert_eq!(
168-
&new_accounting.pre_fees.values().sum::<BigNum>(),
189+
&new_accounting.balances_before_fees.values().sum::<BigNum>(),
169190
&channel.deposit_amount,
170191
"sum(balancesBeforeFees) == depositAmount"
171192
);
@@ -187,11 +208,11 @@ mod test {
187208
};
188209

189210
EventAggregate {
190-
channel_id: get_channel_id("one"),
211+
channel_id: DUMMY_CHANNEL.id.to_owned(),
191212
created: Utc::now(),
192213
events: vec![("IMPRESSION".to_string(), aggregate_events)]
193214
.into_iter()
194215
.collect(),
195216
}
196217
}
197-
}
218+
}

validator_worker/src/core/fees.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
use std::collections::BTreeMap;
2-
31
use num::rational::Ratio;
4-
use primitives::{BigNum, Channel, DomainError, ValidatorDesc};
5-
6-
pub type BalancesMap = BTreeMap<String, BigNum>;
2+
use primitives::{BalancesMap, BigNum, Channel, DomainError, ValidatorDesc};
73

84
pub fn get_balances_after_fees_tree(
95
balances: &BalancesMap,
106
channel: &Channel,
117
) -> Result<BalancesMap, DomainError> {
128
let distribution = Distribution::new(balances, &channel)?;
139

14-
let mut balances_after_fees = BTreeMap::default();
10+
let mut balances_after_fees = BalancesMap::default();
1511
let mut total = BigNum::from(0);
1612

1713
for (key, value) in balances.iter() {

validator_worker/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use self::leader::Leader;
1313
pub use self::sentry_interface::all_channels;
1414

1515
pub mod core {
16+
pub mod events;
1617
pub mod fees;
1718
pub mod follower_rules;
1819
}

0 commit comments

Comments
 (0)