Skip to content

Commit c441b99

Browse files
authored
Merge pull request #411 from AdExNetwork/issue-382-campaign-routes
Issue 382 campaign routes
2 parents 5ed4c43 + 30b375d commit c441b99

40 files changed

+2696
-744
lines changed

Cargo.lock

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

adapter/src/dummy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ impl Adapter for DummyAdapter {
9292
Ok(())
9393
}
9494

95-
fn whoami(&self) -> &ValidatorId {
96-
&self.identity
95+
fn whoami(&self) -> ValidatorId {
96+
self.identity
9797
}
9898

9999
fn sign(&self, state_root: &str) -> AdapterResult<String, Self::AdapterError> {

adapter/src/ethereum.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ impl Adapter for EthereumAdapter {
149149
Ok(())
150150
}
151151

152-
fn whoami(&self) -> &ValidatorId {
153-
&self.address
152+
fn whoami(&self) -> ValidatorId {
153+
self.address
154154
}
155155

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

266-
ewt_sign(&wallet, &self.keystore_pwd, &payload)
266+
ewt_sign(wallet, &self.keystore_pwd, &payload)
267267
.map_err(|err| AdapterError::Adapter(Error::SignMessage(err).into()))
268268
}
269269

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

403403
let mut result = Keccak::new_keccak256();
404-
result.update(&format!("{}{}", eth, message_length).as_bytes());
405-
result.update(&message);
404+
result.update(format!("{}{}", eth, message_length).as_bytes());
405+
result.update(message);
406406

407407
let mut res: [u8; 32] = [0; 32];
408408
result.finalize(&mut res);
@@ -453,7 +453,7 @@ pub fn ewt_sign(
453453
base64::URL_SAFE_NO_PAD,
454454
);
455455
let message = Message::from(hash_message(
456-
&format!("{}.{}", header_encoded, payload_encoded).as_bytes(),
456+
format!("{}.{}", header_encoded, payload_encoded).as_bytes(),
457457
));
458458
let signature: Signature = signer
459459
.sign(password, &message)
@@ -475,7 +475,7 @@ pub fn ewt_verify(
475475
token: &str,
476476
) -> Result<VerifyPayload, EwtVerifyError> {
477477
let message = Message::from(hash_message(
478-
&format!("{}.{}", header_encoded, payload_encoded).as_bytes(),
478+
format!("{}.{}", header_encoded, payload_encoded).as_bytes(),
479479
));
480480

481481
let decoded_signature = base64::decode_config(&token, base64::URL_SAFE_NO_PAD)

adview-manager/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn get_unit_html(
172172
) -> String {
173173
let image_url = normalize_url(&ad_unit.media_url);
174174

175-
let element_html = if is_video(&ad_unit) {
175+
let element_html = if is_video(ad_unit) {
176176
video_html(on_load, size, &image_url, &ad_unit.media_mime)
177177
} else {
178178
image_html(on_load, size, &image_url)

lib/protocol-eth

primitives/src/adapter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub trait Adapter: Send + Sync + fmt::Debug + Clone {
8181
fn unlock(&mut self) -> AdapterResult<(), Self::AdapterError>;
8282

8383
/// Get Adapter whoami
84-
fn whoami(&self) -> &ValidatorId;
84+
fn whoami(&self) -> ValidatorId;
8585

8686
/// Signs the provided state_root
8787
fn sign(&self, state_root: &str) -> AdapterResult<String, Self::AdapterError>;

primitives/src/big_num.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl TryFrom<&str> for BigNum {
243243
type Error = super::DomainError;
244244

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

249249
Ok(Self(big_uint))

primitives/src/campaign.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl Campaign {
195195
}
196196
}
197197

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

320320
pub fn iter(&self) -> Iter<'_> {
321-
Iter::new(&self)
321+
Iter::new(self)
322322
}
323323
}
324324

primitives/src/campaign_validator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl Validator for Campaign {
5757
return Err(Validation::UnlistedValidator.into());
5858
}
5959

60-
if !creator_listed(&self, &config.creators_whitelist) {
60+
if !creator_listed(self, &config.creators_whitelist) {
6161
return Err(Validation::UnlistedCreator.into());
6262
}
6363

primitives/src/channel.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ impl SpecValidators {
238238

239239
pub fn find(&self, validator_id: &ValidatorId) -> Option<SpecValidator<'_>> {
240240
if &self.leader().id == validator_id {
241-
Some(SpecValidator::Leader(&self.leader()))
241+
Some(SpecValidator::Leader(self.leader()))
242242
} else if &self.follower().id == validator_id {
243-
Some(SpecValidator::Follower(&self.follower()))
243+
Some(SpecValidator::Follower(self.follower()))
244244
} else {
245245
None
246246
}
@@ -257,7 +257,7 @@ impl SpecValidators {
257257
}
258258

259259
pub fn iter(&self) -> Iter<'_> {
260-
Iter::new(&self)
260+
Iter::new(self)
261261
}
262262
}
263263

primitives/src/sentry.rs

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{
2-
targeting::Rules,
32
validator::{ApproveState, Heartbeat, MessageTypes, NewState, Type as MessageType},
4-
Address, BalancesMap, BigNum, Channel, ChannelId, ValidatorId, IPFS,
3+
Address, BigNum, Channel, ChannelId, ValidatorId, IPFS,
54
};
65
use chrono::{DateTime, Utc};
76
use serde::{Deserialize, Serialize};
@@ -127,19 +126,6 @@ pub enum Event {
127126
ad_slot: Option<IPFS>,
128127
referrer: Option<String>,
129128
},
130-
/// only the creator can send this event
131-
#[serde(rename_all = "camelCase")]
132-
UpdateTargeting { targeting_rules: Rules },
133-
/// Closes the `Campaign`
134-
/// only the creator can send this event
135-
#[serde(rename_all = "camelCase")]
136-
Close,
137-
/// TODO: AIP#61 Check and explain who can send this event as well as when it can be received
138-
/// A map of earners which gets merged in the `spender::Aggregate`
139-
/// NOTE: Does **not** contain any fees!
140-
/// This even can be used to pay to yourself, but this is irrelevant as it's your funds you are paying yourself.
141-
#[serde(rename_all = "camelCase")]
142-
Pay { payout: BalancesMap },
143129
}
144130

145131
impl Event {
@@ -161,9 +147,6 @@ impl AsRef<str> for Event {
161147
match *self {
162148
Event::Impression { .. } => "IMPRESSION",
163149
Event::Click { .. } => "CLICK",
164-
Event::UpdateTargeting { .. } => "UPDATE_TARGETING",
165-
Event::Close => "CLOSE",
166-
Event::Pay { .. } => "PAY",
167150
}
168151
}
169152
}
@@ -377,6 +360,85 @@ pub mod campaign_create {
377360
}
378361
}
379362
}
363+
364+
/// This implementation helps with test setup
365+
/// **NOTE:** It erases the CampaignId, since the creation of the campaign gives it's CampaignId
366+
impl From<Campaign> for CreateCampaign {
367+
fn from(campaign: Campaign) -> Self {
368+
Self {
369+
channel: campaign.channel,
370+
creator: campaign.creator,
371+
budget: campaign.budget,
372+
validators: campaign.validators,
373+
title: campaign.title,
374+
pricing_bounds: campaign.pricing_bounds,
375+
event_submission: campaign.event_submission,
376+
ad_units: campaign.ad_units,
377+
targeting_rules: campaign.targeting_rules,
378+
created: campaign.created,
379+
active: campaign.active,
380+
}
381+
}
382+
}
383+
384+
// All editable fields stored in one place, used for checking when a budget is changed
385+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
386+
pub struct ModifyCampaign {
387+
pub budget: Option<UnifiedNum>,
388+
pub validators: Option<Validators>,
389+
pub title: Option<String>,
390+
pub pricing_bounds: Option<PricingBounds>,
391+
pub event_submission: Option<EventSubmission>,
392+
pub ad_units: Option<Vec<AdUnit>>,
393+
pub targeting_rules: Option<Rules>,
394+
}
395+
396+
impl ModifyCampaign {
397+
pub fn from_campaign(campaign: Campaign) -> Self {
398+
ModifyCampaign {
399+
budget: Some(campaign.budget),
400+
validators: Some(campaign.validators),
401+
title: campaign.title,
402+
pricing_bounds: campaign.pricing_bounds,
403+
event_submission: campaign.event_submission,
404+
ad_units: Some(campaign.ad_units),
405+
targeting_rules: Some(campaign.targeting_rules),
406+
}
407+
}
408+
409+
pub fn apply(self, mut campaign: Campaign) -> Campaign {
410+
if let Some(new_budget) = self.budget {
411+
campaign.budget = new_budget;
412+
}
413+
414+
if let Some(new_validators) = self.validators {
415+
campaign.validators = new_validators;
416+
}
417+
418+
// check if it was passed otherwise not sending a Title will result in clearing of the current one
419+
if let Some(new_title) = self.title {
420+
campaign.title = Some(new_title);
421+
}
422+
423+
if let Some(new_pricing_bounds) = self.pricing_bounds {
424+
campaign.pricing_bounds = Some(new_pricing_bounds);
425+
}
426+
427+
if let Some(new_event_submission) = self.event_submission {
428+
campaign.event_submission = Some(new_event_submission);
429+
}
430+
431+
if let Some(new_ad_units) = self.ad_units {
432+
campaign.ad_units = new_ad_units;
433+
}
434+
435+
if let Some(new_targeting_rules) = self.targeting_rules {
436+
campaign.targeting_rules = new_targeting_rules;
437+
}
438+
439+
campaign
440+
}
441+
}
380442
}
381443

382444
#[cfg(feature = "postgres")]

primitives/src/sentry/accounting.rs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,27 @@ impl<S: BalancesState> Balances<S> {
6868

6969
Ok(())
7070
}
71+
72+
/// Adds the spender to the Balances with `0` if he does not exist
73+
pub fn add_spender(&mut self, spender: Address) {
74+
self.spenders
75+
.entry(spender)
76+
.or_insert_with(UnifiedNum::default);
77+
}
78+
79+
/// Adds the earner to the Balances with `0` if he does not exist
80+
pub fn add_earner(&mut self, earner: Address) {
81+
self.earners
82+
.entry(earner)
83+
.or_insert_with(UnifiedNum::default);
84+
}
7185
}
7286

73-
#[derive(Debug)]
87+
#[derive(Debug, Error)]
7488
pub enum OverflowError {
89+
#[error("Spender {0} amount overflowed")]
7590
Spender(Address),
91+
#[error("Earner {0} amount overflowed")]
7692
Earner(Address),
7793
}
7894

@@ -196,30 +212,3 @@ mod de {
196212
}
197213
}
198214
}
199-
200-
#[cfg(feature = "postgres")]
201-
mod postgres {
202-
use super::*;
203-
use postgres_types::Json;
204-
use tokio_postgres::Row;
205-
206-
impl TryFrom<&Row> for Accounting<CheckedState> {
207-
type Error = Error;
208-
209-
fn try_from(row: &Row) -> Result<Self, Self::Error> {
210-
let balances = Balances::<UncheckedState> {
211-
earners: row.get::<_, Json<_>>("earners").0,
212-
spenders: row.get::<_, Json<_>>("spenders").0,
213-
state: PhantomData::default(),
214-
}
215-
.check()?;
216-
217-
Ok(Self {
218-
channel: row.get("channel"),
219-
balances,
220-
updated: row.get("updated"),
221-
created: row.get("created"),
222-
})
223-
}
224-
}
225-
}

primitives/src/util/tests/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ pub fn past_datetime(from: Option<&DateTime<Utc>>) -> DateTime<Utc> {
2525

2626
let from = from.unwrap_or(&default_from);
2727

28-
datetime_between(&from, Some(&to))
28+
datetime_between(from, Some(&to))
2929
}

primitives/src/validator.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl ValidatorId {
2727
}
2828

2929
pub fn inner(&self) -> &[u8; 20] {
30-
&self.0.as_bytes()
30+
self.0.as_bytes()
3131
}
3232
}
3333

@@ -39,6 +39,12 @@ impl From<&Address> for ValidatorId {
3939
}
4040
}
4141

42+
impl From<Address> for ValidatorId {
43+
fn from(address: Address) -> Self {
44+
Self(address)
45+
}
46+
}
47+
4248
impl From<&[u8; 20]> for ValidatorId {
4349
fn from(bytes: &[u8; 20]) -> Self {
4450
Self(Address::from(bytes))
@@ -47,7 +53,7 @@ impl From<&[u8; 20]> for ValidatorId {
4753

4854
impl AsRef<[u8]> for ValidatorId {
4955
fn as_ref(&self) -> &[u8] {
50-
&self.0.as_ref()
56+
self.0.as_ref()
5157
}
5258
}
5359

0 commit comments

Comments
 (0)