Skip to content

Commit ad5cd8f

Browse files
committed
Merge branch 'dev' into issue-9-get-event-aggregates
2 parents d9db398 + bd4044d commit ad5cd8f

File tree

17 files changed

+589
-193
lines changed

17 files changed

+589
-193
lines changed

Cargo.lock

Lines changed: 29 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

adapter/src/ethereum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl Adapter for EthereumAdapter {
145145
));
146146
}
147147

148-
let contract_address = Address::from_slice(&self.config.ethereum_core_address);
148+
let contract_address: Address = self.config.ethereum_core_address.into();
149149

150150
let (_eloop, transport) = web3::transports::Http::new(&self.config.ethereum_network)
151151
.map_err(|_| map_error("failed to init http transport"))?;
@@ -209,7 +209,7 @@ impl Adapter for EthereumAdapter {
209209

210210
let sess = match &verified.payload.identity {
211211
Some(identity) => {
212-
let contract_address = Address::from_slice(identity);
212+
let contract_address: Address = identity.into();
213213
let (_eloop, transport) =
214214
web3::transports::Http::new(&self.config.ethereum_network)
215215
.map_err(|_| map_error("failed to init http transport"))?;

adapter/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ impl TryFrom<&Channel> for EthereumChannel {
8787
hash.input(spec);
8888
let spec_hash: [u8; 32] = hash.result().into();
8989

90-
let validators: Vec<ValidatorId> = channel
90+
let validators = channel
9191
.spec
9292
.validators
93-
.into_iter()
94-
.map(|v| v.id.clone())
95-
.collect();
93+
.iter()
94+
.map(|v| &v.id)
95+
.collect::<Vec<_>>();
9696

9797
let creator = channel.creator.inner();
9898
let deposit_asset = <[u8; 20]>::from_hex(&channel.deposit_asset[2..])
@@ -103,7 +103,7 @@ impl TryFrom<&Channel> for EthereumChannel {
103103
&deposit_asset,
104104
&channel.deposit_amount.to_string(),
105105
channel.valid_until,
106-
&validators.as_slice(),
106+
&validators,
107107
&spec_hash,
108108
)
109109
}
@@ -115,7 +115,7 @@ impl EthereumChannel {
115115
token_addr: &[u8; 20],
116116
token_amount: &str, // big num string
117117
valid_until: DateTime<Utc>,
118-
validators: &[ValidatorId],
118+
validators: &[&ValidatorId],
119119
spec: &[u8; 32],
120120
) -> Result<Self, ChannelError> {
121121
if BigNum::try_from(token_amount).is_err() {
@@ -171,7 +171,7 @@ impl EthereumChannel {
171171
}
172172

173173
pub fn hash_hex(&self, contract_addr: &[u8; 20]) -> Result<String, Box<dyn Error>> {
174-
let result = self.hash(&contract_addr)?;
174+
let result = self.hash(contract_addr)?;
175175
Ok(format!("0x{}", hex::encode(result)))
176176
}
177177

adview-manager/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ pub fn get_html(
234234
}],
235235
};
236236

237-
let on_load: String = validators.into_iter().map(|validator| {
237+
let on_load: String = validators.iter().map(|validator| {
238238
let fetch_opts = "{ method: 'POST', headers: { 'content-type': 'application/json' }, body: this.dataset.eventBody }";
239239
let fetch_url = format!("{}/channel/{}/events", validator.url, channel_id);
240240

primitives/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ rand = "^0.6"
3737
# postgres feature
3838
postgres-types = { version = "0.1.0-alpha.2", optional = true }
3939
bytes = { version = "0.5", optional = true }
40-
tokio-postgres = { version = "0.5.0-alpha.2", optional = true, features = ["with-chrono-0_4", "with-serde_json-1"] }
40+
tokio-postgres = { version = "0.5.1", optional = true, features = ["with-chrono-0_4", "with-serde_json-1"] }
4141
# Futures
4242
futures = "0.3.1"
4343
# Other

primitives/src/analytics.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use crate::DomainError;
2+
use serde::{Deserialize, Serialize};
3+
4+
pub const ANALYTICS_QUERY_LIMIT: u32 = 200;
5+
6+
#[derive(Debug, Serialize, Deserialize)]
7+
pub struct AnalyticsResponse {
8+
time: u32,
9+
value: String,
10+
}
11+
12+
#[cfg(feature = "postgres")]
13+
pub mod postgres {
14+
use super::AnalyticsResponse;
15+
use tokio_postgres::Row;
16+
17+
impl From<&Row> for AnalyticsResponse {
18+
fn from(row: &Row) -> Self {
19+
Self {
20+
time: row.get("time"),
21+
value: row.get("value"),
22+
}
23+
}
24+
}
25+
}
26+
27+
#[derive(Debug, Deserialize)]
28+
pub struct AnalyticsQuery {
29+
#[serde(default = "default_limit")]
30+
pub limit: u32,
31+
#[serde(default = "default_event_type")]
32+
pub event_type: String,
33+
#[serde(default = "default_metric")]
34+
pub metric: String,
35+
#[serde(default = "default_timeframe")]
36+
pub timeframe: String,
37+
}
38+
39+
impl AnalyticsQuery {
40+
pub fn is_valid(&self) -> Result<(), DomainError> {
41+
let valid_event_types = ["IMPRESSION", "CLICK"];
42+
let valid_metric = ["eventPayouts", "eventCounts"];
43+
let valid_timeframe = ["year", "month", "week", "day", "hour"];
44+
45+
if !valid_event_types.contains(&self.event_type.as_str()) {
46+
Err(DomainError::InvalidArgument(format!(
47+
"invalid event_type, possible values are: {}",
48+
valid_event_types.join(" ,")
49+
)))
50+
} else if !valid_metric.contains(&self.metric.as_str()) {
51+
Err(DomainError::InvalidArgument(format!(
52+
"invalid metric, possible values are: {}",
53+
valid_metric.join(" ,")
54+
)))
55+
} else if !valid_timeframe.contains(&self.timeframe.as_str()) {
56+
Err(DomainError::InvalidArgument(format!(
57+
"invalid timeframe, possible values are: {}",
58+
valid_timeframe.join(" ,")
59+
)))
60+
} else if self.limit > ANALYTICS_QUERY_LIMIT {
61+
Err(DomainError::InvalidArgument(format!(
62+
"invalid limit {}, maximum value 200",
63+
self.limit
64+
)))
65+
} else {
66+
Ok(())
67+
}
68+
}
69+
}
70+
71+
fn default_limit() -> u32 {
72+
100
73+
}
74+
75+
fn default_event_type() -> String {
76+
"IMPRESSION".into()
77+
}
78+
79+
fn default_metric() -> String {
80+
"eventCounts".into()
81+
}
82+
83+
fn default_timeframe() -> String {
84+
"hour".into()
85+
}

primitives/src/channel.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ impl SpecValidators {
148148
SpecValidator::None
149149
}
150150
}
151+
152+
pub fn iter(&self) -> Iter<'_> {
153+
Iter::new(&self)
154+
}
151155
}
152156

153157
impl From<(ValidatorDesc, ValidatorDesc)> for SpecValidators {
@@ -159,10 +163,44 @@ impl From<(ValidatorDesc, ValidatorDesc)> for SpecValidators {
159163
/// Fixed size iterator of 2, as we need an iterator in couple of occasions
160164
impl<'a> IntoIterator for &'a SpecValidators {
161165
type Item = &'a ValidatorDesc;
162-
type IntoIter = ::std::vec::IntoIter<Self::Item>;
166+
type IntoIter = Iter<'a>;
163167

164168
fn into_iter(self) -> Self::IntoIter {
165-
vec![self.leader(), self.follower()].into_iter()
169+
self.iter()
170+
}
171+
}
172+
173+
pub struct Iter<'a> {
174+
validators: &'a SpecValidators,
175+
index: u8,
176+
}
177+
178+
impl<'a> Iter<'a> {
179+
fn new(validators: &'a SpecValidators) -> Self {
180+
Self {
181+
validators,
182+
index: 0,
183+
}
184+
}
185+
}
186+
187+
impl<'a> Iterator for Iter<'a> {
188+
type Item = &'a ValidatorDesc;
189+
190+
fn next(&mut self) -> Option<Self::Item> {
191+
match self.index {
192+
0 => {
193+
self.index += 1;
194+
195+
Some(self.validators.leader())
196+
}
197+
1 => {
198+
self.index += 1;
199+
200+
Some(self.validators.follower())
201+
}
202+
_ => None,
203+
}
166204
}
167205
}
168206

primitives/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod util {
2323

2424
pub mod logging;
2525
}
26+
pub mod analytics;
2627
pub mod validator;
2728

2829
pub use self::ad_unit::AdUnit;
@@ -42,7 +43,10 @@ pub enum DomainError {
4243

4344
impl fmt::Display for DomainError {
4445
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45-
write!(f, "Domain error",)
46+
match self {
47+
DomainError::InvalidArgument(err) => write!(f, "{}", err),
48+
DomainError::RuleViolation(err) => write!(f, "{}", err),
49+
}
4650
}
4751
}
4852

primitives/src/util/tests/prep_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ lazy_static! {
3535
auth.insert("user".into(), "x8c9v1b2".into());
3636
auth.insert("publisher".into(), "testing".into());
3737
auth.insert("publisher2".into(), "testing2".into());
38-
auth.insert("creator".into(), "awesomeCreator".into());
38+
auth.insert("creator".into(), "0x033ed90e0fec3f3ea1c9b005c724d704501e0196".into());
3939
auth.insert("tester".into(), "AUTH_awesomeTester".into());
4040

4141
auth

primitives/src/validator.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ impl ValidatorId {
3232
}
3333
}
3434

35-
impl AsRef<[u8]> for ValidatorId {
36-
fn as_ref(&self) -> &[u8] {
37-
&self.0
38-
}
39-
}
40-
4135
impl From<&[u8; 20]> for ValidatorId {
4236
fn from(bytes: &[u8; 20]) -> Self {
4337
Self(*bytes)
4438
}
4539
}
4640

41+
impl AsRef<[u8]> for ValidatorId {
42+
fn as_ref(&self) -> &[u8] {
43+
&self.0
44+
}
45+
}
46+
4747
impl TryFrom<&str> for ValidatorId {
4848
type Error = DomainError;
4949
fn try_from(value: &str) -> Result<Self, Self::Error> {

0 commit comments

Comments
 (0)