Skip to content

Commit e5b35a0

Browse files
committed
Merge branch 'issue-391-get-spender-info' into issue-392-get-all-spenders-info
2 parents 7a54cbb + 79f02cc commit e5b35a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2117
-690
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)

docs/config/dev.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ validators_whitelist = []
3232

3333
[[token_address_whitelist]]
3434
# DAI
35-
address = '0x73967c6a0904aa032c103b4104747e88c566b1a2'
35+
address = '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359'
3636
# 1 * 10^-10 = 0.0_000_000_001
3737
min_token_units_for_deposit = '100000000'
3838
min_validator_fee = '100000000'

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: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
channel_v5::Channel, targeting::Rules, AdUnit, Address, EventSubmission, UnifiedNum,
2+
channel_v5::Channel, targeting::Rules, AdUnit, Address, EventSubmission, UnifiedNum, Validator,
33
ValidatorDesc, ValidatorId,
44
};
55

@@ -13,7 +13,7 @@ use serde_with::with_prefix;
1313
pub use {
1414
campaign_id::CampaignId,
1515
pricing::{Pricing, PricingBounds},
16-
validators::{ValidatorRole, Validators},
16+
validators::Validators,
1717
};
1818

1919
with_prefix!(pub prefix_active "active_");
@@ -156,6 +156,7 @@ mod campaign_id {
156156
}
157157

158158
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
159+
#[serde(rename_all = "camelCase")]
159160
pub struct Campaign {
160161
pub id: CampaignId,
161162
pub channel: Channel,
@@ -185,17 +186,15 @@ pub struct Campaign {
185186
}
186187

187188
impl Campaign {
188-
pub fn find_validator(&self, validator: &ValidatorId) -> Option<ValidatorRole<'_>> {
189+
pub fn find_validator(&self, validator: &ValidatorId) -> Option<Validator<&ValidatorDesc>> {
189190
match (self.leader(), self.follower()) {
190-
(Some(leader), _) if &leader.id == validator => Some(ValidatorRole::Leader(leader)),
191-
(_, Some(follower)) if &follower.id == validator => {
192-
Some(ValidatorRole::Follower(follower))
193-
}
191+
(Some(leader), _) if &leader.id == validator => Some(Validator::Leader(leader)),
192+
(_, Some(follower)) if &follower.id == validator => Some(Validator::Follower(follower)),
194193
_ => None,
195194
}
196195
}
197196

198-
/// Matches the Channel.leader to the Campaign.spec.leader
197+
/// Matches the Channel.leader to the Campaign.validators.leader
199198
/// If they match it returns `Some`, otherwise, it returns `None`
200199
pub fn leader(&self) -> Option<&'_ ValidatorDesc> {
201200
self.validators.find(&self.channel.leader)
@@ -277,8 +276,7 @@ mod pricing {
277276
}
278277
}
279278
}
280-
// TODO AIP#61: Double check if we require all the methods and enums, as some parts are now in the `Campaign`
281-
// This includes the matching of the Channel leader & follower to the Validators
279+
/// Campaign Validators
282280
pub mod validators {
283281
use crate::{ValidatorDesc, ValidatorId};
284282
use serde::{Deserialize, Serialize};
@@ -287,21 +285,6 @@ pub mod validators {
287285
/// Unordered list of the validators representing the leader & follower
288286
pub struct Validators(ValidatorDesc, ValidatorDesc);
289287

290-
#[derive(Debug, PartialEq, Eq, Clone)]
291-
pub enum ValidatorRole<'a> {
292-
Leader(&'a ValidatorDesc),
293-
Follower(&'a ValidatorDesc),
294-
}
295-
296-
impl<'a> ValidatorRole<'a> {
297-
pub fn validator(&self) -> &'a ValidatorDesc {
298-
match self {
299-
ValidatorRole::Leader(validator) => validator,
300-
ValidatorRole::Follower(validator) => validator,
301-
}
302-
}
303-
}
304-
305288
impl Validators {
306289
pub fn new(validators: (ValidatorDesc, ValidatorDesc)) -> Self {
307290
Self(validators.0, validators.1)
@@ -318,7 +301,7 @@ pub mod validators {
318301
}
319302

320303
pub fn iter(&self) -> Iter<'_> {
321-
Iter::new(&self)
304+
Iter::new(self)
322305
}
323306
}
324307

primitives/src/campaign_validator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Validator for Campaign {
4545
fn validate(&self, config: &Config, validator_identity: &ValidatorId) -> Result<(), Error> {
4646
// check if the channel validators include our adapter identity
4747
let whoami_validator = match self.find_validator(validator_identity) {
48-
Some(role) => role.validator(),
48+
Some(role) => role.into_inner(),
4949
None => return Err(Validation::AdapterNotIncluded.into()),
5050
};
5151

@@ -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/channel_v5.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use ethereum_types::U256;
22
use serde::{Deserialize, Serialize};
33
use std::fmt;
44

5-
use crate::{Address, ChannelId, ValidatorId};
5+
use crate::{Address, ChannelId, Validator, ValidatorId};
66

77
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
88
#[serde(rename_all = "camelCase")]
@@ -34,6 +34,14 @@ impl Channel {
3434

3535
ChannelId::from(channel_id)
3636
}
37+
38+
pub fn find_validator(&self, validator: ValidatorId) -> Option<Validator<ValidatorId>> {
39+
match (self.leader, self.follower) {
40+
(leader, _) if leader == validator => Some(Validator::Leader(leader)),
41+
(_, follower) if follower == validator => Some(Validator::Follower(follower)),
42+
_ => None,
43+
}
44+
}
3745
}
3846

3947
/// The nonce is an Unsigned 256 number

primitives/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use self::{
1414
event_submission::EventSubmission,
1515
ipfs::IPFS,
1616
unified_num::UnifiedNum,
17-
validator::{ValidatorDesc, ValidatorId},
17+
validator::{Validator, ValidatorDesc, ValidatorId},
1818
};
1919

2020
mod ad_slot;

primitives/src/sentry.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
2+
spender::Spender,
23
validator::{ApproveState, Heartbeat, MessageTypes, NewState, Type as MessageType},
3-
Address, BigNum, Channel, ChannelId, UnifiedNum, ValidatorId, IPFS,
4+
Address, BigNum, Channel, ChannelId, ValidatorId, IPFS,
45
};
56
use chrono::{DateTime, Utc};
67
use serde::{Deserialize, Serialize};
@@ -198,21 +199,9 @@ pub struct SuccessResponse {
198199
}
199200

200201
#[derive(Serialize, Deserialize, Debug)]
202+
#[serde(rename_all = "camelCase")]
201203
pub struct SpenderResponse {
202-
pub total_deposited: Option<UnifiedNum>,
203-
pub spender_leaf: Option<SpenderLeaf>,
204-
}
205-
206-
#[derive(Serialize, Deserialize, Debug)]
207-
pub struct AllSpendersResponse {
208-
pub spenders: Option<HashMap<Address, SpenderResponse>>,
209-
}
210-
211-
// TODO: Maybe there is a better place for this struct
212-
#[derive(Serialize, Deserialize, Debug)]
213-
pub struct SpenderLeaf {
214-
pub total_spent: UnifiedNum,
215-
// merkle_proof: [u8; 32], // TODO
204+
pub spender: Spender,
216205
}
217206

218207
#[derive(Serialize, Deserialize, Debug)]
@@ -379,6 +368,26 @@ pub mod campaign_create {
379368
}
380369
}
381370

371+
/// This implementation helps with test setup
372+
/// **NOTE:** It erases the CampaignId, since the creation of the campaign gives it's CampaignId
373+
impl From<Campaign> for CreateCampaign {
374+
fn from(campaign: Campaign) -> Self {
375+
Self {
376+
channel: campaign.channel,
377+
creator: campaign.creator,
378+
budget: campaign.budget,
379+
validators: campaign.validators,
380+
title: campaign.title,
381+
pricing_bounds: campaign.pricing_bounds,
382+
event_submission: campaign.event_submission,
383+
ad_units: campaign.ad_units,
384+
targeting_rules: campaign.targeting_rules,
385+
created: campaign.created,
386+
active: campaign.active,
387+
}
388+
}
389+
}
390+
382391
// All editable fields stored in one place, used for checking when a budget is changed
383392
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
384393
pub struct ModifyCampaign {

0 commit comments

Comments
 (0)