Skip to content

Commit b363bb6

Browse files
authored
Merge pull request #127 from AdExNetwork/issue-119-migrate-is_healthy-and-is_valid_transaction
Issue 119 migrate is healthy and is valid transaction
2 parents 3af3f3b + 36a6513 commit b363bb6

File tree

8 files changed

+235
-159
lines changed

8 files changed

+235
-159
lines changed

primitives/src/channel.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
use std::error::Error;
12
use std::fmt;
2-
//
3+
34
use chrono::serde::{ts_milliseconds, ts_seconds};
45
use chrono::{DateTime, Utc};
56
use serde::{Deserialize, Serialize};
6-
//
7+
78
use crate::big_num::BigNum;
89
use crate::util::serde::ts_milliseconds_option;
910
use crate::{AdUnit, EventSubmission, TargetingTag, ValidatorDesc};
10-
use std::error::Error;
11+
#[path = "./channel_fixtures.rs"]
12+
pub mod fixtures;
1113

1214
#[derive(Serialize, Deserialize, Debug, Clone)]
1315
#[serde(rename_all = "camelCase")]
@@ -21,7 +23,6 @@ pub struct Channel {
2123
pub spec: ChannelSpec,
2224
}
2325

24-
//
2526
#[derive(Serialize, Deserialize, Debug, Clone)]
2627
#[serde(rename_all = "camelCase")]
2728
pub struct ChannelSpec {

primitives/src/channel_fixtures.rs

Lines changed: 126 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
//use chrono::{DateTime, Utc};
2-
//use fake::faker::*;
3-
//use time::Duration;
4-
//
5-
//use crate::asset::fixtures::get_asset;
6-
//use crate::fixtures::{get_targeting_tags, get_validator};
7-
//use crate::test_util;
8-
//use crate::BigNum;
9-
//
10-
//use super::{Channel, ChannelId, ChannelSpec, SpecValidators, ValidatorDesc};
11-
//
1+
use chrono::{DateTime, TimeZone, Utc};
2+
use fake::faker::*;
3+
use time::Duration;
4+
5+
use crate::targeting_tag::fixtures::get_targeting_tags;
6+
use crate::validator::fixtures::get_validator;
7+
use crate::{BigNum, EventSubmission};
8+
9+
use super::{Channel, ChannelSpec, SpecValidators, ValidatorDesc};
10+
use crate::util::tests::take_one;
11+
12+
const ASSETS_LIST: [&str; 8] = ["DAI", "BGN", "EUR", "USD", "ADX", "BTC", "LIT", "ETH"];
13+
1214
///// It will get the length of channel_id bytes and will fill enough bytes in front
1315
///// If > 32 bytes &str is passed it will `panic!`
1416
//pub fn get_channel_id(channel_id: &str) -> ChannelId {
@@ -24,35 +26,72 @@
2426
//
2527
// ChannelId { bytes: id }
2628
//}
27-
//
28-
//pub fn get_channel(
29-
// id: &str,
30-
// valid_until: &Option<DateTime<Utc>>,
31-
// spec: Option<ChannelSpec>,
32-
//) -> Channel {
33-
// let channel_id = get_channel_id(id);
34-
// let deposit_amount = BigNum::from(<Faker as Number>::between(100, 5000));
35-
// let valid_until: DateTime<Utc> = valid_until.unwrap_or_else(|| {
36-
// let future_from = Utc::now() + Duration::days(7);
37-
// test_util::time::datetime_between(&future_from, None)
38-
// });
39-
// let creator = <Faker as Name>::name();
40-
// let deposit_asset = get_asset();
41-
// let spec = spec.unwrap_or_else(|| {
42-
// get_channel_spec(ValidatorsOption::Generate {
43-
// validators_prefix: id,
44-
// })
45-
// });
46-
//
47-
// Channel {
48-
// id: channel_id,
49-
// creator,
50-
// deposit_asset,
51-
// deposit_amount,
52-
// valid_until,
53-
// spec,
54-
// }
55-
//}
29+
30+
pub fn get_dummy_channel() -> Channel {
31+
let leader = ValidatorDesc {
32+
id: "awesomeLeader".to_string(),
33+
url: "http://localhost:8005".to_string(),
34+
fee: 100.into(),
35+
};
36+
let follower = ValidatorDesc {
37+
id: "awesomeFollower".to_string(),
38+
url: "http://localhost:8006".to_string(),
39+
fee: 100.into(),
40+
};
41+
let nonce = BigNum::from(<Faker as Number>::between(100_000_000, 999_999_999));
42+
43+
Channel {
44+
id: "awesomeTestChannel".to_string(),
45+
creator: "awesomeCreator".to_string(),
46+
deposit_asset: "DAI".to_string(),
47+
deposit_amount: 1_000.into(),
48+
// UNIX timestamp for 2100-01-01
49+
valid_until: Utc.timestamp(4_102_444_800, 0),
50+
spec: ChannelSpec {
51+
title: None,
52+
validators: SpecValidators([leader, follower]),
53+
max_per_impression: 10.into(),
54+
min_per_impression: 10.into(),
55+
targeting: vec![],
56+
min_targeting_score: None,
57+
event_submission: EventSubmission { allow: vec![] },
58+
// July 29, 2019 7:00:00 AM
59+
created: Utc.timestamp(1_564_383_600, 0),
60+
active_from: None,
61+
nonce,
62+
withdraw_period_start: Utc.timestamp_millis(4_073_414_400_000),
63+
ad_units: vec![],
64+
},
65+
}
66+
}
67+
68+
pub fn get_channel(
69+
id: &str,
70+
valid_until: &Option<DateTime<Utc>>,
71+
spec: Option<ChannelSpec>,
72+
) -> Channel {
73+
let deposit_amount = BigNum::from(<Faker as Number>::between(100, 5000));
74+
let valid_until: DateTime<Utc> = valid_until.unwrap_or_else(|| {
75+
let future_from = Utc::now() + Duration::days(7);
76+
crate::util::tests::time::datetime_between(&future_from, None)
77+
});
78+
let creator = <Faker as Name>::name();
79+
let deposit_asset = take_one(&ASSETS_LIST).into();
80+
let spec = spec.unwrap_or_else(|| {
81+
get_channel_spec(ValidatorsOption::Generate {
82+
validators_prefix: id,
83+
})
84+
});
85+
86+
Channel {
87+
id: id.into(),
88+
creator,
89+
deposit_asset,
90+
deposit_amount,
91+
valid_until,
92+
spec,
93+
}
94+
}
5695
//
5796
//pub fn get_channels(count: usize, valid_until_ge: Option<DateTime<Utc>>) -> Vec<Channel> {
5897
// (1..=count)
@@ -67,53 +106,50 @@
67106
// .collect()
68107
//}
69108
//
70-
//pub enum ValidatorsOption<'a> {
71-
// Pair {
72-
// leader: ValidatorDesc,
73-
// follower: ValidatorDesc,
74-
// },
75-
// SpecValidators(SpecValidators),
76-
// Generate {
77-
// validators_prefix: &'a str,
78-
// },
79-
//}
80-
//
81-
//pub fn get_channel_spec(validators_option: ValidatorsOption<'_>) -> ChannelSpec {
82-
// use crate::EventSubmission;
83-
// use test_util::take_one;
84-
//
85-
// let validators = match validators_option {
86-
// ValidatorsOption::Pair { leader, follower } => [leader, follower].into(),
87-
// ValidatorsOption::SpecValidators(spec_validators) => spec_validators,
88-
// ValidatorsOption::Generate { validators_prefix } => [
89-
// get_validator(&format!("{} leader", validators_prefix), None),
90-
// get_validator(&format!("{} follower", validators_prefix), None),
91-
// ]
92-
// .into(),
93-
// };
94-
//
95-
// let title_string = Some(<Faker as Lorem>::sentence(3, 4));
96-
//
97-
// let title = take_one(&[&title_string, &None]).to_owned();
98-
// let max_per_impression = BigNum::from(<Faker as Number>::between(250, 500));
99-
// let min_per_impression = BigNum::from(<Faker as Number>::between(1, 250));
100-
// let nonce = BigNum::from(<Faker as Number>::between(100_000_000, 999_999_999));
101-
// let min_targeting_score =
102-
// take_one(&[&None, &Some(<Faker as Number>::between(1, 500))]).to_owned();
103-
//
104-
// ChannelSpec {
105-
// validators,
106-
// title,
107-
// max_per_impression,
108-
// min_per_impression,
109-
// targeting: get_targeting_tags(<Faker as Number>::between(0, 5)),
110-
// min_targeting_score,
111-
// // @TODO: `EventSubmission` fixture issue #27
112-
// event_submission: EventSubmission { allow: vec![] },
113-
// created: Utc::now(),
114-
// active_from: Some(Utc::now()),
115-
// nonce,
116-
// withdraw_period_start: Utc::now(),
117-
// ad_units: Vec::new(),
118-
// }
119-
//}
109+
pub enum ValidatorsOption<'a> {
110+
Pair {
111+
leader: ValidatorDesc,
112+
follower: ValidatorDesc,
113+
},
114+
SpecValidators(SpecValidators),
115+
Generate {
116+
validators_prefix: &'a str,
117+
},
118+
}
119+
120+
pub fn get_channel_spec(validators_option: ValidatorsOption<'_>) -> ChannelSpec {
121+
let validators = match validators_option {
122+
ValidatorsOption::Pair { leader, follower } => [leader, follower].into(),
123+
ValidatorsOption::SpecValidators(spec_validators) => spec_validators,
124+
ValidatorsOption::Generate { validators_prefix } => [
125+
get_validator(&format!("{} leader", validators_prefix), None),
126+
get_validator(&format!("{} follower", validators_prefix), None),
127+
]
128+
.into(),
129+
};
130+
131+
let title_string = Some(<Faker as Lorem>::sentence(3, 4));
132+
133+
let title = take_one(&[&title_string, &None]).to_owned();
134+
let max_per_impression = BigNum::from(<Faker as Number>::between(250, 500));
135+
let min_per_impression = BigNum::from(<Faker as Number>::between(1, 250));
136+
let nonce = BigNum::from(<Faker as Number>::between(100_000_000, 999_999_999));
137+
let min_targeting_score =
138+
take_one(&[&None, &Some(<Faker as Number>::between(1_f64, 500_f64))]).to_owned();
139+
140+
ChannelSpec {
141+
validators,
142+
title,
143+
max_per_impression,
144+
min_per_impression,
145+
targeting: get_targeting_tags(<Faker as Number>::between(0, 5)),
146+
min_targeting_score,
147+
// @TODO: `EventSubmission` fixture issue #27
148+
event_submission: EventSubmission { allow: vec![] },
149+
created: Utc::now(),
150+
active_from: Some(Utc::now()),
151+
nonce,
152+
withdraw_period_start: Utc::now(),
153+
ad_units: Vec::new(),
154+
}
155+
}

primitives/src/targeting_tag.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ where
5353
}
5454
}
5555

56-
#[cfg(any(test, feature = "fixtures"))]
5756
pub mod fixtures {
5857
use fake::faker::*;
5958

primitives/src/util.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#[cfg(any(test, feature = "fixtures"))]
21
pub mod tests;
32

43
pub mod serde {

primitives/src/validator.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use crate::Channel;
2-
use crate::{BalancesMap, BigNum};
1+
use std::pin::Pin;
2+
33
use chrono::{DateTime, Utc};
44
use futures::prelude::*;
55
use serde::{Deserialize, Serialize};
6-
use std::pin::Pin;
6+
7+
use crate::Channel;
8+
use crate::{BalancesMap, BigNum};
79

810
pub type ValidatorFuture<T> = Pin<Box<dyn Future<Output = Result<T, ValidatorError>> + Send>>;
911

@@ -104,3 +106,22 @@ pub enum MessageTypes {
104106
Heartbeat(Heartbeat),
105107
Accounting(Accounting),
106108
}
109+
110+
pub mod fixtures {
111+
use fake::faker::*;
112+
113+
use crate::BigNum;
114+
115+
use super::ValidatorDesc;
116+
117+
pub fn get_validator<V: AsRef<str>>(validator_id: V, fee: Option<BigNum>) -> ValidatorDesc {
118+
let fee = fee.unwrap_or_else(|| BigNum::from(<Faker as Number>::between(1, 13)));
119+
let url = format!(
120+
"http://{}-validator-url.com/validator",
121+
validator_id.as_ref()
122+
);
123+
let id = validator_id.as_ref().to_string();
124+
125+
ValidatorDesc { id, url, fee }
126+
}
127+
}

validator_worker/src/core.rs

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

0 commit comments

Comments
 (0)