Skip to content

Commit 14560fa

Browse files
authored
Merge pull request #222 from AdExNetwork/issue-215-ethereum-channel-redundant-validator_id-check
Issue #215 `EthereumChannel` redundant validator id check
2 parents 794793b + fbc8b3e commit 14560fa

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

adapter/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ impl TryFrom<&Channel> for EthereumChannel {
8787
hash.input(spec);
8888
let spec_hash: [u8; 32] = hash.result().into();
8989

90-
let validators: Vec<ValidatorId> = channel
90+
let validators = channel
9191
.spec
9292
.validators
93-
.into_iter()
94-
.map(|v| v.id.clone())
95-
.collect();
93+
.iter()
94+
.map(|v| &v.id)
95+
.collect::<Vec<_>>();
9696

9797
let creator = <[u8; 20]>::from_hex(&channel.creator[2..])
9898
.map_err(|_| ChannelError::InvalidArgument("failed to parse creator".into()))?;
@@ -104,7 +104,7 @@ impl TryFrom<&Channel> for EthereumChannel {
104104
&deposit_asset,
105105
&channel.deposit_amount.to_string(),
106106
channel.valid_until,
107-
&validators.as_slice(),
107+
&validators,
108108
&spec_hash,
109109
)
110110
}
@@ -116,7 +116,7 @@ impl EthereumChannel {
116116
token_addr: &[u8; 20],
117117
token_amount: &str, // big num string
118118
valid_until: DateTime<Utc>,
119-
validators: &[ValidatorId],
119+
validators: &[&ValidatorId],
120120
spec: &[u8; 32],
121121
) -> Result<Self, ChannelError> {
122122
if BigNum::try_from(token_amount).is_err() {

adview-manager/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ pub fn get_html(
234234
}],
235235
};
236236

237-
let on_load: String = validators.into_iter().map(|validator| {
237+
let on_load: String = validators.iter().map(|validator| {
238238
let fetch_opts = "{ method: 'POST', headers: { 'content-type': 'application/json' }, body: this.dataset.eventBody }";
239239
let fetch_url = format!("{}/channel/{}/events", validator.url, channel_id);
240240

primitives/src/channel.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ impl SpecValidators {
148148
SpecValidator::None
149149
}
150150
}
151+
152+
pub fn iter(&self) -> Iter<'_> {
153+
Iter::new(&self)
154+
}
151155
}
152156

153157
impl From<(ValidatorDesc, ValidatorDesc)> for SpecValidators {
@@ -159,10 +163,44 @@ impl From<(ValidatorDesc, ValidatorDesc)> for SpecValidators {
159163
/// Fixed size iterator of 2, as we need an iterator in couple of occasions
160164
impl<'a> IntoIterator for &'a SpecValidators {
161165
type Item = &'a ValidatorDesc;
162-
type IntoIter = ::std::vec::IntoIter<Self::Item>;
166+
type IntoIter = Iter<'a>;
163167

164168
fn into_iter(self) -> Self::IntoIter {
165-
vec![self.leader(), self.follower()].into_iter()
169+
self.iter()
170+
}
171+
}
172+
173+
pub struct Iter<'a> {
174+
validators: &'a SpecValidators,
175+
index: u8,
176+
}
177+
178+
impl<'a> Iter<'a> {
179+
fn new(validators: &'a SpecValidators) -> Self {
180+
Self {
181+
validators,
182+
index: 0,
183+
}
184+
}
185+
}
186+
187+
impl<'a> Iterator for Iter<'a> {
188+
type Item = &'a ValidatorDesc;
189+
190+
fn next(&mut self) -> Option<Self::Item> {
191+
match self.index {
192+
0 => {
193+
self.index += 1;
194+
195+
Some(self.validators.leader())
196+
}
197+
1 => {
198+
self.index += 1;
199+
200+
Some(self.validators.follower())
201+
}
202+
_ => None,
203+
}
166204
}
167205
}
168206

validator_worker/src/core/fees.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn get_balances_after_fees_tree(
1010

1111
let total_distributed = balances.iter().map(|(_, balance)| balance).sum::<BigNum>();
1212

13-
let validators_iter = channel.spec.validators.into_iter();
13+
let validators_iter = channel.spec.validators.iter();
1414
let total_validators_fee = validators_iter
1515
.map(|validator| &validator.fee)
1616
.sum::<BigNum>();
@@ -54,7 +54,7 @@ pub fn get_balances_after_fees_tree(
5454
balances_after_fees,
5555
rounding_error,
5656
fee_ratio,
57-
channel.spec.validators.into_iter(),
57+
channel.spec.validators.iter(),
5858
);
5959

6060
Ok(balances_after_fees)

validator_worker/src/sentry_interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<T: Adapter + 'static> SentryApi<T> {
4646
let propagate_to = channel
4747
.spec
4848
.validators
49-
.into_iter()
49+
.iter()
5050
.map(|validator| {
5151
adapter
5252
.get_auth(&validator.id)

0 commit comments

Comments
 (0)