Skip to content

Commit 4c4d987

Browse files
authored
Merge branch 'dev' into issue-243-contrafactual-accounts
2 parents 3fd7815 + 8efc08c commit 4c4d987

File tree

17 files changed

+497
-47
lines changed

17 files changed

+497
-47
lines changed

Cargo.lock

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

primitives/src/channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{AdUnit, EventSubmission, TargetingTag, ValidatorDesc, ValidatorId};
1111
use hex::{FromHex, FromHexError};
1212
use std::ops::Deref;
1313

14-
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Copy, Clone)]
14+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Copy, Clone, Hash)]
1515
#[serde(transparent)]
1616
pub struct ChannelId(#[serde(with = "SerHex::<StrictPfx>")] [u8; 32]);
1717

primitives/src/util/tests/prep_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ lazy_static! {
2020
ids.insert("user".into(), ValidatorId::try_from("0x20754168c00a6e58116ccfd0a5f7d1bb66c5de9d").expect("failed to parse id"));
2121
ids.insert("publisher".into(), ValidatorId::try_from("0xb7d3f81e857692d13e9d63b232a90f4a1793189e").expect("failed to parse id"));
2222
ids.insert("publisher2".into(), ValidatorId::try_from("0x2054b0c1339309597ad04ba47f4590f8cdb4e305").expect("failed to parse id"));
23-
ids.insert("creator".into(), ValidatorId::try_from("0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359").expect("failed to parse id"));
23+
ids.insert("creator".into(), ValidatorId::try_from("0x033ed90e0fec3f3ea1c9b005c724d704501e0196").expect("failed to parse id"));
2424
ids.insert("tester".into(), ValidatorId::try_from("0x2892f6C41E0718eeeDd49D98D648C789668cA67d").expect("failed to parse id"));
2525

2626
ids

sentry/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2018"
77
[dependencies]
88
# Futures
99
futures = "0.3.1"
10+
async-std = "1.4.0"
1011
# Primitives
1112
primitives = { path = "../primitives", features = ["postgres"] }
1213
adapter = { version = "0.1", path = "../adapter" }
@@ -15,17 +16,18 @@ hex = "0.3.2"
1516
# CLI
1617
clap = "2.33.0"
1718
# Server
18-
tokio = { version = "0.2", features = ["macros"] }
19+
tokio = { version = "0.2.9", features = ["macros", "rt-core"] }
1920
hyper = { version = "0.13", features = ["stream"] }
2021
regex = "1"
2122
# Database
2223
redis = { version = "0.14", features = ["tokio-rt-core"] }
2324
bb8 = { git = "https://github.com/khuey/bb8" }
2425
bb8-postgres = { git = "https://github.com/khuey/bb8", features = ["with-chrono-0_4", "with-serde_json-1"] }
26+
2527
# Migrations
2628
migrant_lib = { version = "0.27", features = ["d-postgres"] }
2729
# Logger
28-
slog = { version = "^2.2.3" , features = ["max_level_trace"] }
30+
slog = { version = "^2.2.3", features = ["max_level_trace"] }
2931
# Serde
3032
serde = { version = "^1.0", features = ['derive'] }
3133
serde_json = "^1.0"
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
-- This file should undo anything in `up.sql`
2-
DROP TABLE event_aggregates, validator_messages, channels;
2+
DROP AGGREGATE jsonb_object_agg(jsonb);
3+
DROP TABLE event_aggregates, validator_messages, channels;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DELETE FROM event_aggregates;
2+
DELETE FROM validator_messages;
3+
DELETE FROM channels;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
INSERT INTO
2+
channels
3+
VALUES
4+
(
5+
'0x061d5e2a67d0a9a10f1c732bca12a676d83f79663a396f7d87b3e30b9b411088',
6+
'0x033ed90e0fec3f3ea1c9b005c724d704501e0196',
7+
'0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359',
8+
'1000',
9+
to_timestamp(4102444800000),
10+
'{"minPerImpression":"1","maxPerImpression":"10","pricingBounds":{"CLICK":{"min":"0","max":"0"}},"withdrawPeriodStart":4073414400000,"validators":[{"id":"0xce07CbB7e054514D590a0262C93070D838bFBA2e","url":"http://localhost:8005","fee":"100"},{"id":"0xC91763D7F14ac5c5dDfBCD012e0D2A61ab9bDED3","url":"http://localhost:8006","fee":"100"}]}'
11+
);

sentry/migrations/20190806011140_initial-tables/up.sql

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,19 @@ CREATE INDEX idx_validator_messages_msg_state_root ON validator_messages ((msg -
2828
CREATE TABLE event_aggregates
2929
(
3030
channel_id VARCHAR(66) NOT NULL REFERENCES channels (id) ON DELETE RESTRICT,
31-
created TIMESTAMP WITH TIME ZONE NOT NULL,
32-
events JSONB NOT NULL
31+
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
32+
event_type VARCHAR(255) NOT NULL,
33+
earner VARCHAR(255),
34+
count VARCHAR NOT NULL,
35+
payout VARCHAR NOT NULL
3336
);
3437

3538
CREATE INDEX idx_event_aggregates_created ON event_aggregates (created);
39+
CREATE INDEX idx_event_aggregates_channel ON event_aggregates (channel_id);
40+
CREATE INDEX idx_event_aggregates_event_type ON event_aggregates (event_type);
41+
42+
CREATE AGGREGATE jsonb_object_agg(jsonb) (
43+
SFUNC = 'jsonb_concat',
44+
STYPE = jsonb,
45+
INITCOND = '{}'
46+
);

sentry/src/access.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use chrono::Utc;
22
use futures::future::try_join_all;
33
use redis::aio::MultiplexedConnection;
44

5+
use crate::Session;
56
use primitives::event_submission::{RateLimit, Rule};
67
use primitives::sentry::Event;
78
use primitives::Channel;
89
use std::cmp::PartialEq;
9-
10-
use crate::Session;
10+
use std::error;
11+
use std::fmt;
1112

1213
#[derive(Debug, PartialEq, Eq)]
1314
pub enum Error {
@@ -17,6 +18,19 @@ pub enum Error {
1718
RulesError(String),
1819
}
1920

21+
impl error::Error for Error {}
22+
23+
impl fmt::Display for Error {
24+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25+
match self {
26+
Error::OnlyCreatorCanCloseChannel => write!(f, "only creator can create channel"),
27+
Error::ChannelIsExpired => write!(f, "channel has expired"),
28+
Error::ChannelIsInWithdrawPeriod => write!(f, "channel is in withdraw period"),
29+
Error::RulesError(error) => write!(f, "{}", error),
30+
}
31+
}
32+
}
33+
2034
// @TODO: Make pub(crate)
2135
pub async fn check_access(
2236
redis: &MultiplexedConnection,

sentry/src/db.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use lazy_static::lazy_static;
99

1010
pub mod analytics;
1111
mod channel;
12-
mod event_aggregate;
12+
pub mod event_aggregate;
1313
mod validator_message;
1414

1515
pub use self::channel::*;
@@ -55,7 +55,7 @@ pub async fn postgres_connection() -> Result<DbPool, bb8_postgres::tokio_postgre
5555
Pool::builder().build(pg_mgr).await
5656
}
5757

58-
pub async fn setup_migrations() {
58+
pub async fn setup_migrations(environment: &str) {
5959
use migrant_lib::{Config, Direction, Migrator, Settings};
6060

6161
let settings = Settings::configure_postgres()
@@ -82,9 +82,16 @@ pub async fn setup_migrations() {
8282
};
8383
}
8484

85+
let mut migrations = vec![make_migration!("20190806011140_initial-tables")];
86+
87+
if environment == "development" {
88+
// seeds database tables for testing
89+
migrations.push(make_migration!("20190806011140_initial-tables/seed"))
90+
}
91+
8592
// Define Migrations
8693
config
87-
.use_migrations(&[make_migration!("20190806011140_initial-tables")])
94+
.use_migrations(&migrations)
8895
.expect("Loading migrations failed");
8996

9097
// Reload config, ping the database for applied migrations

0 commit comments

Comments
 (0)