Skip to content

V5: Authenticated POST route to modify/submit campaigns #408

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 38 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3661ca2
committing current progress
simzzz May 28, 2021
413149f
Merge branch 'aip-61-adex-v5' into v5-modify-submit-campaigns
simzzz May 28, 2021
8c4d687
fixed some more errors
simzzz May 31, 2021
58c61aa
more error fixes
simzzz May 31, 2021
70ff87c
Progress w/out tests
simzzz Jun 8, 2021
f7dae5b
fixed typo
simzzz Jun 10, 2021
0f40016
Refactoring + changes to the way the remaining for all campaigns is r…
simzzz Jun 10, 2021
33f0e3e
added CampaignCreateResponse
simzzz Jun 10, 2021
084f6e6
Merge branch 'aip-61-adex-v5' into v5-modify-submit-campaigns
simzzz Jun 11, 2021
475b8db
More changes to budget/redis operations + fixed problems from PR review
simzzz Jun 11, 2021
60efa4a
more changes to updating remaining
simzzz Jun 11, 2021
1b8cd75
added regex for campaign update route
simzzz Jun 14, 2021
efb30f9
Merge branch 'issue-381-accounting-db' into v5-modify-submit-campaigns
simzzz Jun 15, 2021
57116cc
Accounting messages + First integration tests
simzzz Jun 15, 2021
f4e0949
fixed some unwraps
simzzz Jun 16, 2021
825d6cb
more fixes
simzzz Jun 22, 2021
5fd05db
Merge branch 'issue-382-campaign-routes' into v5-modify-submit-campaigns
simzzz Jun 22, 2021
067b3af
fixed remaining issues in PR
simzzz Jun 29, 2021
9fd4f4a
Included MGET for getting multiple remainings at once
simzzz Jun 30, 2021
9380089
fixed failing test
simzzz Jun 30, 2021
219e3f0
cargofmt + some refactoring + used MutatedCampaign object in more places
simzzz Jun 30, 2021
187d198
Merge branch 'issue-382-campaign-routes' into v5-modify-submit-campaigns
simzzz Jul 2, 2021
ad2d585
Finished with PR changes requested
simzzz Jul 6, 2021
4f23861
PR changes
simzzz Jul 9, 2021
0082a35
Merge branch 'issue-382-campaign-routes' into v5-modify-submit-campaigns
simzzz Jul 12, 2021
8fcef13
sentry - routes - modify campaign
elpiel Jul 13, 2021
b664dd1
sentry - db - campaign - fix doc comment
elpiel Jul 13, 2021
10deba9
sentry - mod test_util - setup_dummy_app helper fn
elpiel Aug 4, 2021
88c3243
sentry - db - campaign - CampaignRemaining redis struct
elpiel Aug 4, 2021
3ca6ab5
primitives - sentry - impl From<Campaign> for CreateCampaign
elpiel Aug 4, 2021
11dc715
primitives - validator - From<Address> for ValidatorID
elpiel Aug 4, 2021
07d9c74
primitives - sentry - accounting - Balances - add spender/earner
elpiel Aug 9, 2021
d80e109
sentry - Applicaiton - add CampaignRemaining to app
elpiel Aug 9, 2021
e1a245e
sentry - db - CampaignRemaining - MGET guard against empty vec of cam…
elpiel Aug 9, 2021
9a94c5f
sentry - routes - campaign create/modify - more checks & tests
elpiel Aug 9, 2021
5177217
sentry - db - make pub insert_accounting
elpiel Aug 9, 2021
75a5a5f
sentry - routes - campaign - get_delta_budget - fix logic & naming
elpiel Aug 9, 2021
19ccd17
Merge pull request #418 from AdExNetwork/redis-and-tests
elpiel Aug 9, 2021
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
2 changes: 1 addition & 1 deletion lib/protocol-eth
82 changes: 80 additions & 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 Expand Up @@ -361,6 +360,85 @@ pub mod campaign_create {
}
}
}

/// This implementation helps with test setup
/// **NOTE:** It erases the CampaignId, since the creation of the campaign gives it's CampaignId
impl From<Campaign> for CreateCampaign {
fn from(campaign: Campaign) -> Self {
Self {
channel: campaign.channel,
creator: campaign.creator,
budget: campaign.budget,
validators: campaign.validators,
title: campaign.title,
pricing_bounds: campaign.pricing_bounds,
event_submission: campaign.event_submission,
ad_units: campaign.ad_units,
targeting_rules: campaign.targeting_rules,
created: campaign.created,
active: campaign.active,
}
}
}

// All editable fields stored in one place, used for checking when a budget is changed
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ModifyCampaign {
pub budget: Option<UnifiedNum>,
pub validators: Option<Validators>,
pub title: Option<String>,
pub pricing_bounds: Option<PricingBounds>,
pub event_submission: Option<EventSubmission>,
pub ad_units: Option<Vec<AdUnit>>,
pub targeting_rules: Option<Rules>,
}

impl ModifyCampaign {
pub fn from_campaign(campaign: Campaign) -> Self {
ModifyCampaign {
budget: Some(campaign.budget),
validators: Some(campaign.validators),
title: campaign.title,
pricing_bounds: campaign.pricing_bounds,
event_submission: campaign.event_submission,
ad_units: Some(campaign.ad_units),
targeting_rules: Some(campaign.targeting_rules),
}
}

pub fn apply(self, mut campaign: Campaign) -> Campaign {
if let Some(new_budget) = self.budget {
campaign.budget = new_budget;
}

if let Some(new_validators) = self.validators {
campaign.validators = new_validators;
}

// check if it was passed otherwise not sending a Title will result in clearing of the current one
if let Some(new_title) = self.title {
campaign.title = Some(new_title);
}

if let Some(new_pricing_bounds) = self.pricing_bounds {
campaign.pricing_bounds = Some(new_pricing_bounds);
}

if let Some(new_event_submission) = self.event_submission {
campaign.event_submission = Some(new_event_submission);
}

if let Some(new_ad_units) = self.ad_units {
campaign.ad_units = new_ad_units;
}

if let Some(new_targeting_rules) = self.targeting_rules {
campaign.targeting_rules = new_targeting_rules;
}

campaign
}
}
}

#[cfg(feature = "postgres")]
Expand Down
10 changes: 10 additions & 0 deletions primitives/src/sentry/accounting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ impl<S: BalancesState> Balances<S> {

Ok(())
}

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

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

#[derive(Debug)]
Expand Down
6 changes: 6 additions & 0 deletions primitives/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ impl From<&Address> for ValidatorId {
}
}

impl From<Address> for ValidatorId {
fn from(address: Address) -> Self {
Self(address)
}
}

impl From<&[u8; 20]> for ValidatorId {
fn from(bytes: &[u8; 20]) -> Self {
Self(Address::from(bytes))
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use lazy_static::lazy_static;

pub mod accounting;
pub mod analytics;
mod campaign;
pub mod campaign;
mod channel;
pub mod event_aggregate;
pub mod spendable;
Expand Down
4 changes: 1 addition & 3 deletions sentry/src/db/accounting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ pub async fn get_accounting_spent(
Ok(row.get("spent"))
}

// TODO This is still WIP
#[allow(dead_code)]
async fn insert_accounting(
pub async fn insert_accounting(
pool: DbPool,
channel: Channel,
balances: Balances<CheckedState>,
Expand Down
Loading