Skip to content

Commit c0bdaa1

Browse files
committed
primitives - Validator - Clean up and simplify
1 parent 8f0c8d3 commit c0bdaa1

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

primitives/src/campaign.rs

Lines changed: 7 additions & 14 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_");
@@ -186,12 +186,10 @@ pub struct Campaign {
186186
}
187187

188188
impl Campaign {
189-
pub fn find_validator(&self, validator: &ValidatorId) -> Option<ValidatorRole<'_>> {
189+
pub fn find_validator(&self, validator: &ValidatorId) -> Option<Validator<&ValidatorDesc>> {
190190
match (self.leader(), self.follower()) {
191-
(Some(leader), _) if &leader.id == validator => Some(ValidatorRole::Leader(leader)),
192-
(_, Some(follower)) if &follower.id == validator => {
193-
Some(ValidatorRole::Follower(follower))
194-
}
191+
(Some(leader), _) if &leader.id == validator => Some(Validator::Leader(leader)),
192+
(_, Some(follower)) if &follower.id == validator => Some(Validator::Follower(follower)),
195193
_ => None,
196194
}
197195
}
@@ -278,20 +276,15 @@ mod pricing {
278276
}
279277
}
280278
}
281-
// TODO AIP#61: Double check if we require all the methods and enums, as some parts are now in the `Campaign`
282-
// This includes the matching of the Channel leader & follower to the Validators
279+
/// Campaign Validators
283280
pub mod validators {
284-
use crate::{Validator, ValidatorDesc, ValidatorId};
281+
use crate::{ValidatorDesc, ValidatorId};
285282
use serde::{Deserialize, Serialize};
286283

287284
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
288285
/// Unordered list of the validators representing the leader & follower
289286
pub struct Validators(ValidatorDesc, ValidatorDesc);
290287

291-
pub type ValidatorRole<'a> = Validator<&'a ValidatorDesc>;
292-
293-
294-
295288
impl Validators {
296289
pub fn new(validators: (ValidatorDesc, ValidatorDesc)) -> Self {
297290
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/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, Validator},
17+
validator::{Validator, ValidatorDesc, ValidatorId},
1818
};
1919

2020
mod ad_slot;

primitives/src/validator.rs

Lines changed: 11 additions & 2 deletions
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,
@@ -114,7 +114,16 @@ pub enum Validator<T> {
114114
}
115115

116116
impl<T> Validator<T> {
117-
pub fn validator<'a>(&'a self) -> &'a 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 {
118127
match self {
119128
Self::Leader(validator) => validator,
120129
Self::Follower(validator) => validator,

0 commit comments

Comments
 (0)