Skip to content

Commit ce122e1

Browse files
authored
Merge pull request #388 from AdExNetwork/issue-381-spender-aggregates
Issue #381 Spender aggregates - structs and setup
2 parents c817f56 + 910971a commit ce122e1

File tree

15 files changed

+489
-125
lines changed

15 files changed

+489
-125
lines changed

Cargo.lock

+26-80
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

primitives/Makefile.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[tasks.dev-test-flow]
2+
description = "Development testing flow will first format the code, and than run cargo build and test"
3+
category = "Development"
4+
dependencies = [
5+
"format-flow",
6+
"format-toml-conditioned-flow",
7+
"pre-build",
8+
"build",
9+
"post-build",
10+
"services-up",
11+
"test-flow",
12+
"services-down",
13+
]
14+
15+
[tasks.test]
16+
env = { "POSTGRES_DB" = "sentry_leader" }
17+
18+
[tasks.services-up]
19+
script = "docker-compose -f ../docker-compose.ci.yml up -d postgres-leader"
20+
21+
[tasks.services-down]
22+
script = "docker-compose -f ../docker-compose.ci.yml down"

primitives/src/campaign.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
channel_v5::Channel, targeting::Rules, AdUnit, Address, EventSubmission, UnifiedNum,
3-
ValidatorDesc,
3+
ValidatorDesc, ValidatorId,
44
};
55

66
use chrono::{
@@ -184,6 +184,14 @@ pub struct Campaign {
184184
}
185185

186186
impl Campaign {
187+
pub fn find_validator(&self, validator: ValidatorId) -> Option<&'_ ValidatorDesc> {
188+
match (self.leader(), self.follower()) {
189+
(Some(leader), _) if leader.id == validator => Some(leader),
190+
(_, Some(follower)) if follower.id == validator => Some(follower),
191+
_ => None,
192+
}
193+
}
194+
187195
/// Matches the Channel.leader to the Campaign.spec.leader
188196
/// If they match it returns `Some`, otherwise, it returns `None`
189197
pub fn leader(&self) -> Option<&'_ ValidatorDesc> {

primitives/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod ipfs;
3535
pub mod market;
3636
pub mod merkle_tree;
3737
pub mod sentry;
38+
pub mod spender;
3839
pub mod supermarket;
3940
pub mod targeting;
4041
mod unified_num;

primitives/src/sentry.rs

-7
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,6 @@ impl fmt::Display for Event {
172172
}
173173
}
174174

175-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
176-
pub struct Earner {
177-
#[serde(rename = "publisher")]
178-
pub address: String,
179-
pub promilles: u64,
180-
}
181-
182175
#[derive(Debug, Serialize, Deserialize)]
183176
#[serde(rename_all = "camelCase")]
184177
pub struct EventAggregate {

primitives/src/spender.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::{channel_v5::Channel, Address, BalancesMap, UnifiedNum};
2+
use chrono::{DateTime, Utc};
3+
use serde::{Deserialize, Serialize};
4+
5+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
6+
#[serde(rename_all = "camelCase")]
7+
pub struct Deposit {
8+
pub total: UnifiedNum,
9+
pub still_on_create2: UnifiedNum,
10+
}
11+
12+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
13+
pub struct Spendable {
14+
pub spender: Address,
15+
pub channel: Channel,
16+
#[serde(flatten)]
17+
pub deposit: Deposit,
18+
}
19+
20+
#[derive(Debug, Clone, Serialize, Deserialize)]
21+
#[serde(rename_all = "camelCase")]
22+
pub struct Aggregate {
23+
pub spender: Address,
24+
pub channel: Channel,
25+
pub balances: BalancesMap,
26+
pub created: DateTime<Utc>,
27+
}
28+
#[cfg(feature = "postgres")]
29+
mod postgres {
30+
use std::convert::TryFrom;
31+
use tokio_postgres::{Error, Row};
32+
33+
use super::*;
34+
35+
impl TryFrom<Row> for Spendable {
36+
type Error = Error;
37+
38+
fn try_from(row: Row) -> Result<Self, Self::Error> {
39+
Ok(Spendable {
40+
spender: row.try_get("spender")?,
41+
channel: row.try_get("channel")?,
42+
deposit: Deposit {
43+
total: row.try_get("total")?,
44+
still_on_create2: row.try_get("still_on_create2")?,
45+
},
46+
})
47+
}
48+
}
49+
}

primitives/src/validator.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ impl TryFrom<Value> for ValidatorId {
106106
#[serde(rename_all = "camelCase")]
107107
pub struct ValidatorDesc {
108108
pub id: ValidatorId,
109+
/// The validator fee in pro milles (per 1000)
110+
pub fee: UnifiedNum,
109111
#[serde(default, skip_serializing_if = "Option::is_none")]
112+
/// The address which will receive the fees
110113
pub fee_addr: Option<Address>,
114+
/// The url of the Validator on which is the API
111115
pub url: String,
112-
pub fee: UnifiedNum,
113116
}
114117

115118
/// Validator Message Types

sentry/Cargo.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "sentry"
33
version = "0.1.0"
4-
authors = ["Omidiora Samuel <[email protected]>"]
4+
authors = ["Lachezar Lechev <[email protected]>", "Omidiora Samuel <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
@@ -27,7 +27,7 @@ bb8 = "0.7"
2727
bb8-postgres = { version = "0.7", features = ["with-chrono-0_4", "with-serde_json-1"] }
2828

2929
# Migrations
30-
migrant_lib = { version = "^0.30", features = ["d-postgres"] }
30+
migrant_lib = { version = "^0.32", features = ["d-postgres"] }
3131
# Logger
3232
slog = { version = "^2.2.3", features = ["max_level_trace"] }
3333
# Serde
@@ -37,5 +37,10 @@ serde_urlencoded = "^0.7"
3737
# Other
3838
lazy_static = "1.4.0"
3939
thiserror = "^1.0"
40+
tokio-postgres = { version = "0.7.0", features = ["with-chrono-0_4", "with-serde_json-1"] }
41+
42+
[dev-dependencies]
43+
# todo: Replace `bb8` once we update all places.
4044
deadpool = "0.7.0"
45+
deadpool-postgres = "0.7.0"
4146
once_cell = "1.5.2"

sentry/Makefile.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ dependencies = [
1212
"services-down",
1313
]
1414

15+
[tasks.test]
16+
env = { "POSTGRES_DB" = "sentry_leader" }
17+
1518
[tasks.services-up]
16-
script = "docker-compose -f ../docker-compose.ci.yml up -d redis-leader"
19+
script = "docker-compose -f ../docker-compose.ci.yml up -d redis-leader postgres-leader"
1720

1821
[tasks.services-down]
1922
script = "docker-compose -f ../docker-compose.ci.yml down"

0 commit comments

Comments
 (0)