Skip to content

Commit bafbd02

Browse files
committed
ValidatorDesc fees are now per 1
1 parent 1d27a27 commit bafbd02

File tree

5 files changed

+35
-56
lines changed

5 files changed

+35
-56
lines changed

primitives/examples/create_campaign_request.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ fn main() {
2424
"validators":[
2525
{
2626
"id":"0x80690751969B234697e9059e04ed72195c3507fa",
27-
"fee":"3000000",
27+
"fee":"3000",
2828
"url":"http://localhost:8005"
2929
},
3030
{
3131
"id":"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7",
32-
"fee":"2000000",
32+
"fee":"2000",
3333
"url":"http://localhost:8006"
3434
}
3535
],
@@ -75,12 +75,12 @@ fn main() {
7575
"validators":[
7676
{
7777
"id":"0x80690751969B234697e9059e04ed72195c3507fa",
78-
"fee":"3000000",
78+
"fee":"3000",
7979
"url":"http://localhost:8005"
8080
},
8181
{
8282
"id":"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7",
83-
"fee":"2000000",
83+
"fee":"2000",
8484
"url":"http://localhost:8006"
8585
}
8686
],

primitives/src/test_util.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ mod logger {
149149
pub static DUMMY_VALIDATOR_LEADER: Lazy<ValidatorDesc> = Lazy::new(|| ValidatorDesc {
150150
id: IDS[&LEADER],
151151
url: "http://localhost:8005".to_string(),
152-
fee: UnifiedNum::from_whole(0.03),
152+
fee: UnifiedNum::from_whole(0.00003),
153153
fee_addr: None,
154154
});
155155

156156
pub static DUMMY_VALIDATOR_FOLLOWER: Lazy<ValidatorDesc> = Lazy::new(|| ValidatorDesc {
157157
id: IDS[&FOLLOWER],
158158
url: "http://localhost:8006".to_string(),
159-
fee: UnifiedNum::from_whole(0.02),
159+
fee: UnifiedNum::from_whole(0.00002),
160160
fee_addr: None,
161161
});
162162

@@ -315,12 +315,12 @@ pub static CAMPAIGNS: Lazy<[ChainOf<Campaign>; 3]> = Lazy::new(|| {
315315
};
316316

317317
let leader_desc = ValidatorDesc {
318-
fee: UnifiedNum::from_whole(0.5),
318+
fee: UnifiedNum::from_whole(0.0005),
319319
..DUMMY_VALIDATOR_LEADER.clone()
320320
};
321321

322322
let follower_desc = ValidatorDesc {
323-
fee: UnifiedNum::from_whole(0.4),
323+
fee: UnifiedNum::from_whole(0.0004),
324324
..DUMMY_VALIDATOR_FOLLOWER.clone()
325325
};
326326

@@ -378,12 +378,12 @@ pub static CAMPAIGNS: Lazy<[ChainOf<Campaign>; 3]> = Lazy::new(|| {
378378
};
379379

380380
let leader_desc = ValidatorDesc {
381-
fee: UnifiedNum::from_whole(0.1),
381+
fee: UnifiedNum::from_whole(0.0001),
382382
..DUMMY_VALIDATOR_FOLLOWER.clone()
383383
};
384384

385385
let follower_desc = ValidatorDesc {
386-
fee: UnifiedNum::from_whole(0.05),
386+
fee: UnifiedNum::from_whole(0.00005),
387387
..DUMMY_VALIDATOR_LEADER.clone()
388388
};
389389
let validators = Validators::new((leader_desc, follower_desc));
@@ -440,12 +440,12 @@ pub static CAMPAIGNS: Lazy<[ChainOf<Campaign>; 3]> = Lazy::new(|| {
440440
};
441441

442442
let leader_desc = ValidatorDesc {
443-
fee: UnifiedNum::from_whole(0.02),
443+
fee: UnifiedNum::from_whole(0.00002),
444444
..DUMMY_VALIDATOR_LEADER.clone()
445445
};
446446

447447
let follower_desc = ValidatorDesc {
448-
fee: UnifiedNum::from_whole(0.0175),
448+
fee: UnifiedNum::from_whole(0.0000175),
449449
..DUMMY_VALIDATOR_FOLLOWER.clone()
450450
};
451451

primitives/src/validator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ impl TryFrom<Value> for ValidatorId {
113113

114114
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
115115
#[serde(rename_all = "camelCase")]
116-
/// A Validator description which includes the identity, fee (pro milles) and the Sentry URL.
116+
/// A Validator description which includes the identity, fee (per event) and the Sentry URL.
117117
pub struct ValidatorDesc {
118118
pub id: ValidatorId,
119-
/// The validator fee in pro milles (per 1000)
119+
/// The validator fee in per event
120120
///
121121
/// Each fee is calculated based on the payout for an event.
122122
///
123-
/// payout * fee / 1000 = event fee payout
123+
/// payout * fee = event fee payout
124124
pub fee: UnifiedNum,
125125
#[serde(default, skip_serializing_if = "Option::is_none")]
126126
/// The address which will receive the fees

sentry/src/spender.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ pub mod fee {
22

33
use primitives::{Address, DomainError, UnifiedNum, ValidatorDesc};
44

5-
/// Pro mile (per thousands, `1000`) in `UnifiedNum` precision, i.e. `1_000.00_000_000`.
6-
pub const PRO_MILLE: UnifiedNum = UnifiedNum::from_u64(1_000 * UnifiedNum::MULTIPLIER);
7-
85
/// Calculates the fee for a given payout of the specified validator
96
/// This function will return None if the provided validator is not part of the Campaign / Channel
107
/// In the case of overflow when calculating the payout, an error will be returned
@@ -13,17 +10,16 @@ pub mod fee {
1310
validator: &ValidatorDesc,
1411
) -> Result<UnifiedNum, DomainError> {
1512
// should never overflow, but we guard against overflow
16-
// `UnifiedNum::checked_div` will accurately floor the inner `u64` for the `UnifiedNum::PRECISION`
1713
payout
1814
.checked_mul(&validator.fee)
19-
.and_then(|pro_mille_fee| pro_mille_fee.checked_div(&PRO_MILLE))
2015
.ok_or_else(|| DomainError::InvalidArgument("payout calculation overflow".to_string()))
2116
}
2217

2318
#[cfg(test)]
2419
mod test {
2520
use primitives::{
2621
test_util::{DUMMY_VALIDATOR_LEADER, PUBLISHER},
22+
unified_num::FromWhole,
2723
UnifiedNum, ValidatorDesc,
2824
};
2925

@@ -32,36 +28,26 @@ pub mod fee {
3228
#[test]
3329
fn test_calculation_of_fee() {
3430
let mut dummy_leader = DUMMY_VALIDATOR_LEADER.clone();
35-
dummy_leader.fee = UnifiedNum::from(10_000_000);
31+
dummy_leader.fee = UnifiedNum::from_whole(0.1);
3632

3733
// normal payout - no flooring
3834
{
39-
// 30 000 * 10 000 000 / 1 000 00 000 000 = 3
35+
// 30 000 * 10 000 000 / 100 000 000 = 3000
4036

41-
// 0.0003 * 0.1 / 1000.0 = 0.00000003 = UnifiedNum(3)
42-
// 0.00 030 000 * 0.10 000 000 / 1 000 = 0.00 000 003
43-
let payout = (*PUBLISHER, UnifiedNum::from(30_000));
37+
// 0.0003 * 0.1 = 0.00000003 = UnifiedNum(3)
38+
// 0.00 030 000 * 0.10 000 000 = 0.00003
39+
let payout = (*PUBLISHER, UnifiedNum::from_whole(0.0003));
4440

4541
let validator_fee =
4642
calculate_fee(payout, &dummy_leader).expect("Should not overflow");
4743

4844
assert_eq!(
49-
UnifiedNum::from(3),
45+
UnifiedNum::from_whole(0.00003),
5046
validator_fee,
51-
"fee should be 0.00000003 in UnifiedNum"
47+
"fee should be 0.00003 in UnifiedNum"
5248
);
5349
}
5450

55-
// payout with flooring
56-
{
57-
// 66 000 * 10 000 000 / 100 000 000 000 = 6.6 = 6
58-
let payout = (*PUBLISHER, UnifiedNum::from(66_000));
59-
let validator_fee =
60-
calculate_fee(payout, &dummy_leader).expect("Should not overflow");
61-
62-
assert_eq!(UnifiedNum::from(6), validator_fee);
63-
}
64-
6551
// Overflow - even using `Ratio` for `UnifiedNum`, this should overflow
6652
{
6753
let very_high_fee = ValidatorDesc {
@@ -79,13 +65,12 @@ pub mod fee {
7965
// e.g. 3 TOKENs
8066
let payout = (*PUBLISHER, UnifiedNum::from(300_000_000_u64));
8167

82-
// 300 000 000 × 10 000 000 / 100 000 000 000
68+
// 300 000 000 × 10 000 000 / 100 000 000 = 30 000 000
8369
let validator_fee =
8470
calculate_fee(payout, &dummy_leader).expect("Should not overflow");
8571

86-
// : 3 000 000 000 000
87-
// 0.000003
88-
assert_eq!(UnifiedNum::from(30_000), validator_fee);
72+
// 0.3
73+
assert_eq!(UnifiedNum::from_whole(0.3), validator_fee);
8974
}
9075
}
9176
}

test_harness/src/lib.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,17 @@ mod tests {
299299
id: VALIDATORS[&LEADER].address.into(),
300300
url: VALIDATORS[&LEADER].sentry_url.to_string(),
301301
// min_validator_fee for token: 0.000_010
302-
// fee per 1000 (pro mille) = 5.00000000 (UnifiedNum)
303-
// fee per 1 payout: payout * fee / 1000 = payout * 0.00500000
304-
fee: 500_000_000.into(),
302+
// fee per 1 payout: payout * fee = payout * 0.00500000
303+
fee: UnifiedNum::from_whole(0.005),
305304
fee_addr: None,
306305
};
307306

308307
let follower_desc = ValidatorDesc {
309308
id: VALIDATORS[&FOLLOWER].address.into(),
310309
url: VALIDATORS[&FOLLOWER].sentry_url.to_string(),
311310
// min_validator_fee for token: 0.000_010
312-
// fee per 1000 (pro mille) = 4.00000000 (UnifiedNum)
313-
// fee per 1 payout: payout * fee / 1000 = payout * 0.00400000
314-
fee: 400_000_000.into(),
311+
// fee per 1 payout: payout * fee = payout * 0.00400000
312+
fee: UnifiedNum::from_whole(0.004),
315313
fee_addr: None,
316314
};
317315

@@ -391,9 +389,8 @@ mod tests {
391389
let leader_desc = ValidatorDesc {
392390
id: VALIDATORS[&FOLLOWER].address.into(),
393391
url: VALIDATORS[&FOLLOWER].sentry_url.to_string(),
394-
// fee per 1000 (pro mille) = 0.10000000 (UnifiedNum)
395392
// fee per 1 = 0.00010000
396-
fee: UnifiedNum::from_whole(0.1),
393+
fee: UnifiedNum::from_whole(0.0001),
397394
fee_addr: None,
398395
};
399396

@@ -402,9 +399,8 @@ mod tests {
402399
let follower_desc = ValidatorDesc {
403400
id: VALIDATORS[&LEADER].address.into(),
404401
url: VALIDATORS[&LEADER].sentry_url.to_string(),
405-
// fee per 1000 (pro mille) = 0.05000000 (UnifiedNum)
406402
// fee per 1 = 0.00005000
407-
fee: UnifiedNum::from_whole(0.05),
403+
fee: UnifiedNum::from_whole(0.00005),
408404
fee_addr: None,
409405
};
410406

@@ -481,19 +477,17 @@ mod tests {
481477
id: VALIDATORS[&LEADER].address.into(),
482478
url: VALIDATORS[&LEADER].sentry_url.to_string(),
483479
// min_validator_fee for token: 0.000_010
484-
// fee per 1000 (pro mille) = 2.00000000
485480
// fee per 1 payout: payout * fee / 1000 = payout * 0.00200000
486-
fee: UnifiedNum::from_whole(2),
481+
fee: UnifiedNum::from_whole(0.002),
487482
fee_addr: None,
488483
};
489484

490485
let follower_desc = ValidatorDesc {
491486
id: VALIDATORS[&FOLLOWER].address.into(),
492487
url: VALIDATORS[&FOLLOWER].sentry_url.to_string(),
493488
// min_validator_fee for token: 0.000_010
494-
// fee per 1000 (pro mille) = 1.75000000
495-
// fee per 1 payout: payout * fee / 1000 = payout * 0.00175000
496-
fee: UnifiedNum::from_whole(1.75),
489+
// fee per 1 payout: payout * fee = payout * 0.00175000
490+
fee: UnifiedNum::from_whole(0.00175),
497491
fee_addr: None,
498492
};
499493

0 commit comments

Comments
 (0)