Skip to content

Commit 3392de3

Browse files
authored
Merge pull request #420 from AdExNetwork/generic-validator-changes
Generic validator changes
2 parents c441b99 + ae49bac commit 3392de3

File tree

6 files changed

+47
-34
lines changed

6 files changed

+47
-34
lines changed

primitives/src/campaign.rs

Lines changed: 7 additions & 24 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,12 +186,10 @@ 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
}
@@ -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)

primitives/src/campaign_validator.rs

Lines changed: 1 addition & 1 deletion
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

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/util/tests/prep_db.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,17 @@ lazy_static! {
3939
addresses.insert("publisher2".into(), Address::try_from("0x2054b0c1339309597ad04ba47f4590f8cdb4e305").expect("failed to parse id"));
4040
addresses.insert("creator".into(), Address::try_from("0x033ed90e0fec3f3ea1c9b005c724d704501e0196").expect("failed to parse id"));
4141
addresses.insert("tester".into(), Address::try_from("0x2892f6C41E0718eeeDd49D98D648C789668cA67d").expect("failed to parse id"));
42-
// These are the real Addresses of these stablecoins, however, they are only used for testing!
43-
addresses.insert("DAI".into(), Address::try_from("0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359").expect("failed to parse id"));
44-
addresses.insert("USDT".into(), Address::try_from("0xdac17f958d2ee523a2206206994597c13d831ec7").expect("failed to parse id"));
45-
addresses.insert("USDC".into(), Address::try_from("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").expect("failed to parse id"));
4642

4743
addresses
4844
};
4945

46+
// These are the real Addresses of these stablecoins, however, they are only used for testing!
5047
pub static ref TOKENS: HashMap<String, Address> = {
5148
let mut tokens = HashMap::new();
5249

53-
tokens.insert("DAI".into(), "0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359".parse::<Address>().expect("Should parse"));
54-
50+
tokens.insert("DAI".into(), "0x6b175474e89094c44da98b954eedeac495271d0f".parse::<Address>().expect("Should parse"));
51+
tokens.insert("USDT".into(), "0xdac17f958d2ee523a2206206994597c13d831ec7".parse::<Address>().expect("failed to parse id"));
52+
tokens.insert("USDC".into(), "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".parse::<Address>().expect("failed to parse id"));
5553
tokens
5654
};
5755

primitives/src/validator.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::{Deserialize, Serialize};
2-
use std::{convert::TryFrom, fmt, str::FromStr};
2+
use std::{borrow::Borrow, convert::TryFrom, fmt, str::FromStr};
33

44
use crate::{
55
address::Error, targeting::Value, Address, DomainError, ToETHChecksum, ToHex, UnifiedNum,
@@ -107,6 +107,30 @@ pub struct ValidatorDesc {
107107
pub url: String,
108108
}
109109

110+
#[derive(Debug, PartialEq, Eq, Clone)]
111+
pub enum Validator<T> {
112+
Leader(T),
113+
Follower(T),
114+
}
115+
116+
impl<T> Validator<T> {
117+
pub fn into_inner(self) -> T {
118+
match self {
119+
Self::Leader(validator) => validator,
120+
Self::Follower(validator) => validator,
121+
}
122+
}
123+
}
124+
125+
impl<T> Borrow<T> for Validator<T> {
126+
fn borrow(&self) -> &T {
127+
match self {
128+
Self::Leader(validator) => validator,
129+
Self::Follower(validator) => validator,
130+
}
131+
}
132+
}
133+
110134
/// Validator Message Types
111135
pub mod messages {
112136
use std::{any::type_name, convert::TryFrom, fmt, marker::PhantomData};

0 commit comments

Comments
 (0)