Skip to content

Issue 382 campaign insert events #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions adapter/src/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl Adapter for DummyAdapter {
Ok(())
}

fn whoami(&self) -> &ValidatorId {
&self.identity
fn whoami(&self) -> ValidatorId {
self.identity
}

fn sign(&self, state_root: &str) -> AdapterResult<String, Self::AdapterError> {
Expand Down
14 changes: 7 additions & 7 deletions adapter/src/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ impl Adapter for EthereumAdapter {
Ok(())
}

fn whoami(&self) -> &ValidatorId {
&self.address
fn whoami(&self) -> ValidatorId {
self.address
}

fn sign(&self, state_root: &str) -> AdapterResult<String, Self::AdapterError> {
Expand Down Expand Up @@ -263,7 +263,7 @@ impl Adapter for EthereumAdapter {
address: self.whoami().to_checksum(),
};

ewt_sign(&wallet, &self.keystore_pwd, &payload)
ewt_sign(wallet, &self.keystore_pwd, &payload)
.map_err(|err| AdapterError::Adapter(Error::SignMessage(err).into()))
}

Expand Down Expand Up @@ -401,8 +401,8 @@ fn hash_message(message: &[u8]) -> [u8; 32] {
let message_length = message.len();

let mut result = Keccak::new_keccak256();
result.update(&format!("{}{}", eth, message_length).as_bytes());
result.update(&message);
result.update(format!("{}{}", eth, message_length).as_bytes());
result.update(message);

let mut res: [u8; 32] = [0; 32];
result.finalize(&mut res);
Expand Down Expand Up @@ -453,7 +453,7 @@ pub fn ewt_sign(
base64::URL_SAFE_NO_PAD,
);
let message = Message::from(hash_message(
&format!("{}.{}", header_encoded, payload_encoded).as_bytes(),
format!("{}.{}", header_encoded, payload_encoded).as_bytes(),
));
let signature: Signature = signer
.sign(password, &message)
Expand All @@ -475,7 +475,7 @@ pub fn ewt_verify(
token: &str,
) -> Result<VerifyPayload, EwtVerifyError> {
let message = Message::from(hash_message(
&format!("{}.{}", header_encoded, payload_encoded).as_bytes(),
format!("{}.{}", header_encoded, payload_encoded).as_bytes(),
));

let decoded_signature = base64::decode_config(&token, base64::URL_SAFE_NO_PAD)
Expand Down
2 changes: 1 addition & 1 deletion adview-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ fn get_unit_html(
) -> String {
let image_url = normalize_url(&ad_unit.media_url);

let element_html = if is_video(&ad_unit) {
let element_html = if is_video(ad_unit) {
video_html(on_load, size, &image_url, &ad_unit.media_mime)
} else {
image_html(on_load, size, &image_url)
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub trait Adapter: Send + Sync + fmt::Debug + Clone {
fn unlock(&mut self) -> AdapterResult<(), Self::AdapterError>;

/// Get Adapter whoami
fn whoami(&self) -> &ValidatorId;
fn whoami(&self) -> ValidatorId;

/// Signs the provided state_root
fn sign(&self, state_root: &str) -> AdapterResult<String, Self::AdapterError>;
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/big_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl TryFrom<&str> for BigNum {
type Error = super::DomainError;

fn try_from(num: &str) -> Result<Self, Self::Error> {
let big_uint = BigUint::from_str(&num)
let big_uint = BigUint::from_str(num)
.map_err(|err| super::DomainError::InvalidArgument(err.to_string()))?;

Ok(Self(big_uint))
Expand Down
4 changes: 2 additions & 2 deletions primitives/src/campaign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl Campaign {
}
}

/// Matches the Channel.leader to the Campaign.spec.leader
/// Matches the Channel.leader to the Campaign.validators.leader
/// If they match it returns `Some`, otherwise, it returns `None`
pub fn leader(&self) -> Option<&'_ ValidatorDesc> {
self.validators.find(&self.channel.leader)
Expand Down Expand Up @@ -318,7 +318,7 @@ pub mod validators {
}

pub fn iter(&self) -> Iter<'_> {
Iter::new(&self)
Iter::new(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/src/campaign_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Validator for Campaign {
return Err(Validation::UnlistedValidator.into());
}

if !creator_listed(&self, &config.creators_whitelist) {
if !creator_listed(self, &config.creators_whitelist) {
return Err(Validation::UnlistedCreator.into());
}

Expand Down
6 changes: 3 additions & 3 deletions primitives/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ impl SpecValidators {

pub fn find(&self, validator_id: &ValidatorId) -> Option<SpecValidator<'_>> {
if &self.leader().id == validator_id {
Some(SpecValidator::Leader(&self.leader()))
Some(SpecValidator::Leader(self.leader()))
} else if &self.follower().id == validator_id {
Some(SpecValidator::Follower(&self.follower()))
Some(SpecValidator::Follower(self.follower()))
} else {
None
}
Expand All @@ -257,7 +257,7 @@ impl SpecValidators {
}

pub fn iter(&self) -> Iter<'_> {
Iter::new(&self)
Iter::new(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/src/sentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ pub mod campaign_create {
}
}
}

// All editable fields stored in one place, used for checking when a budget is changed
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ModifyCampaign {
Expand Down
43 changes: 11 additions & 32 deletions primitives/src/sentry/accounting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,26 @@ impl<S: BalancesState> Balances<S> {
Ok(())
}

/// Adds the spender to the Balances with `UnifiedNum::from(0)` if he does not exist
/// Adds the spender to the Balances with `0` if he does not exist
pub fn add_spender(&mut self, spender: Address) {
self.spenders.entry(spender).or_insert(UnifiedNum::from(0));
self.spenders
.entry(spender)
.or_insert_with(UnifiedNum::default);
}

/// Adds the earner to the Balances with `UnifiedNum::from(0)` if he does not exist
/// Adds the earner to the Balances with `0` if he does not exist
pub fn add_earner(&mut self, earner: Address) {
self.earners.entry(earner).or_insert(UnifiedNum::from(0));
self.earners
.entry(earner)
.or_insert_with(UnifiedNum::default);
}
}

#[derive(Debug)]
#[derive(Debug, Error)]
pub enum OverflowError {
#[error("Spender {0} amount overflowed")]
Spender(Address),
#[error("Earner {0} amount overflowed")]
Earner(Address),
}

Expand Down Expand Up @@ -206,30 +212,3 @@ mod de {
}
}
}

#[cfg(feature = "postgres")]
mod postgres {
use super::*;
use postgres_types::Json;
use tokio_postgres::Row;

impl TryFrom<&Row> for Accounting<CheckedState> {
type Error = Error;

fn try_from(row: &Row) -> Result<Self, Self::Error> {
let balances = Balances::<UncheckedState> {
earners: row.get::<_, Json<_>>("earners").0,
spenders: row.get::<_, Json<_>>("spenders").0,
state: PhantomData::default(),
}
.check()?;

Ok(Self {
channel: row.get("channel"),
balances,
updated: row.get("updated"),
created: row.get("created"),
})
}
}
}
2 changes: 1 addition & 1 deletion primitives/src/util/tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ pub fn past_datetime(from: Option<&DateTime<Utc>>) -> DateTime<Utc> {

let from = from.unwrap_or(&default_from);

datetime_between(&from, Some(&to))
datetime_between(from, Some(&to))
}
4 changes: 2 additions & 2 deletions primitives/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl ValidatorId {
}

pub fn inner(&self) -> &[u8; 20] {
&self.0.as_bytes()
self.0.as_bytes()
}
}

Expand All @@ -53,7 +53,7 @@ impl From<&[u8; 20]> for ValidatorId {

impl AsRef<[u8]> for ValidatorId {
fn as_ref(&self) -> &[u8] {
&self.0.as_ref()
self.0.as_ref()
}
}

Expand Down
1 change: 1 addition & 0 deletions sentry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ redis = { version = "0.20", features = ["aio", "tokio-comp"] }
deadpool = "0.8.0"
deadpool-postgres = "0.9.0"
tokio-postgres = { version = "0.7.0", features = ["with-chrono-0_4", "with-serde_json-1"] }
postgres-types = { version = "0.2.1", features = ["derive", "with-chrono-0_4", "with-serde_json-1"] }

# Migrations
migrant_lib = { version = "^0.32", features = ["d-postgres"] }
Expand Down
13 changes: 8 additions & 5 deletions sentry/migrations/20190806011140_initial-tables/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ CREATE AGGREGATE jsonb_object_agg (jsonb) (
INITCOND = '{}'
);

CREATE TYPE AccountingSide AS ENUM ('Earner', 'Spender');

CREATE TABLE accounting (
channel_id varchar(66) NOT NULL,
channel jsonb NOT NULL,
earners jsonb DEFAULT '{}' NULL,
spenders jsonb DEFAULT '{}' NULL,
side AccountingSide NOT NULL,
"address" varchar(42) NOT NULL,
amount bigint NOT NULL,
updated timestamp(2) with time zone DEFAULT NULL NULL,
created timestamp(2) with time zone NOT NULL,

PRIMARY KEY (channel_id)
)
-- Do not rename the Primary key constraint (`accounting_pkey`)!
PRIMARY KEY (channel_id, side, "address")
);
39 changes: 11 additions & 28 deletions sentry/src/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use thiserror::Error;

#[derive(Debug, PartialEq, Eq, Error)]
pub enum Error {
#[error("channel is expired")]
#[error("Campaign is expired")]
CampaignIsExpired,
#[error("event submission restricted")]
ForbiddenReferrer,
Expand All @@ -37,17 +37,10 @@ pub async fn check_access(
if current_time > campaign.active.to {
return Err(Error::CampaignIsExpired);
}

let (is_creator, auth_uid) = match auth {
Some(auth) => (
auth.uid.to_address() == campaign.creator,
auth.uid.to_string(),
),
None => (false, Default::default()),
};
let auth_uid = auth.map(|auth| auth.uid.to_string()).unwrap_or_default();

// Rules for events
if forbidden_country(&session) || forbidden_referrer(&session) {
if forbidden_country(session) || forbidden_referrer(session) {
return Err(Error::ForbiddenReferrer);
}

Expand Down Expand Up @@ -83,22 +76,13 @@ pub async fn check_access(
return Ok(());
}

let apply_all_rules = try_join_all(rules.iter().map(|rule| {
apply_rule(
redis.clone(),
&rule,
&events,
&campaign,
&auth_uid,
&session,
)
}));
let apply_all_rules = try_join_all(
rules
.iter()
.map(|rule| apply_rule(redis.clone(), rule, events, campaign, &auth_uid, session)),
);

if let Err(rule_error) = apply_all_rules.await {
Err(Error::RulesError(rule_error))
} else {
Ok(())
}
apply_all_rules.await.map_err(Error::RulesError).map(|_| ())
}

async fn apply_rule(
Expand Down Expand Up @@ -188,9 +172,8 @@ mod test {
config::configuration,
event_submission::{RateLimit, Rule},
sentry::Event,
targeting::Rules,
util::tests::prep_db::{ADDRESSES, DUMMY_CAMPAIGN, DUMMY_CHANNEL, IDS},
Channel, Config, EventSubmission,
util::tests::prep_db::{ADDRESSES, DUMMY_CAMPAIGN, IDS},
Config, EventSubmission,
};

use deadpool::managed::Object;
Expand Down
1 change: 0 additions & 1 deletion sentry/src/analytics_recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ pub async fn record(
.ignore();
}
}
_ => {}
});

if let Err(err) = db.query_async::<_, Option<String>>(&mut conn).await {
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub async fn setup_migrations(environment: &str) {
.database_password(POSTGRES_PASSWORD.as_str())
.database_host(POSTGRES_HOST.as_str())
.database_port(*POSTGRES_PORT)
.database_name(&POSTGRES_DB.as_ref().unwrap_or(&POSTGRES_USER))
.database_name(POSTGRES_DB.as_ref().unwrap_or(&POSTGRES_USER))
.build()
.expect("Should build migration settings");

Expand Down
Loading