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 10 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.

2 changes: 1 addition & 1 deletion 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
3 changes: 1 addition & 2 deletions primitives/src/sentry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
targeting::Rules,
validator::{ApproveState, Heartbeat, MessageTypes, NewState, Type as MessageType},
Address, BalancesMap, BigNum, Channel, ChannelId, ValidatorId, IPFS,
Address, BigNum, Channel, ChannelId, ValidatorId, IPFS,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
Expand Down
31 changes: 3 additions & 28 deletions primitives/src/sentry/accounting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ impl<S: BalancesState> Balances<S> {
}
}

#[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 @@ -196,30 +198,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"),
})
}
}
}
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")
);
22 changes: 5 additions & 17 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,14 +37,7 @@ 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) {
Expand Down Expand Up @@ -94,11 +87,7 @@ pub async fn check_access(
)
}));

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 +177,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
Loading