Skip to content

Commit c45dd0f

Browse files
committed
fix clippy & run rustfmt
1 parent 20415c1 commit c45dd0f

File tree

11 files changed

+97
-88
lines changed

11 files changed

+97
-88
lines changed

adapter/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ pub fn get_signable_state_root(
4242
Ok(res)
4343
}
4444

45-
pub fn get_balance_leaf(is_spender: bool, acc: &Address, amnt: &BigNum) -> Result<[u8; 32], Box<dyn Error>> {
45+
pub fn get_balance_leaf(
46+
is_spender: bool,
47+
acc: &Address,
48+
amnt: &BigNum,
49+
) -> Result<[u8; 32], Box<dyn Error>> {
4650
let address = Token::Address(EthAddress::from_slice(acc.as_bytes()));
4751
let amount = Token::Uint(
4852
U256::from_dec_str(&amnt.to_str_radix(10))
4953
.map_err(|_| ChannelError::InvalidArgument("failed to parse amt".into()))?,
5054
);
5155

5256
let tokens = if is_spender {
53-
vec![Token::String("spender".into()), address, amount]
57+
vec![Token::String("spender".into()), address, amount]
5458
} else {
5559
vec![address, amount]
5660
};

primitives/src/balances.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ impl<S: BalancesState> Balances<S> {
8989

9090
/// Returns a tuple of the sum of `(earners, spenders)`
9191
pub fn sum(&self) -> Option<(UnifiedNum, UnifiedNum)> {
92-
self.spenders.values().sum::<Option<UnifiedNum>>().map(|spenders| {
93-
let earners = self.earners.values().sum::<Option<UnifiedNum>>()?;
92+
self.spenders
93+
.values()
94+
.sum::<Option<UnifiedNum>>()
95+
.map(|spenders| {
96+
let earners = self.earners.values().sum::<Option<UnifiedNum>>()?;
9497

95-
Some((earners, spenders))
96-
})
97-
.flatten()
98+
Some((earners, spenders))
99+
})
100+
.flatten()
98101
}
99102
}
100103

primitives/src/util/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::{convert::TryFrom, fmt, str::FromStr};
33
use parse_display::Display;
44
use serde::{Deserialize, Serialize};
55
use thiserror::Error;
6-
use url::Url;
76
pub use url::ParseError;
7+
use url::Url;
88

99
// `url::Url::scheme()` returns lower-cased ASCII string without `:`
1010
const SCHEMES: [&str; 2] = ["http", "https"];

sentry/src/routes/channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ async fn create_or_update_spendable_document(
246246
};
247247

248248
let spendable = Spendable {
249-
channel: channel.clone(),
249+
channel: *channel,
250250
deposit: Deposit {
251251
total,
252252
still_on_create2,

validator_worker/src/channel.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use crate::{
44
sentry_interface::{campaigns::all_campaigns, Validator, Validators},
55
SentryApi,
66
};
7-
use primitives::{
8-
adapter::Adapter, channel_v5::Channel, config::Config, util::ApiUrl, ChannelId, UnifiedNum,
9-
};
7+
use primitives::{adapter::Adapter, channel_v5::Channel, config::Config, util::ApiUrl, ChannelId};
108
// use slog::{error, info, Logger};
119
use slog::Logger;
1210
use std::collections::{hash_map::Entry, HashSet};
@@ -20,7 +18,7 @@ pub async fn channel_tick<A: Adapter + 'static>(
2018
) -> Result<ChannelId, Error<A::AdapterError>> {
2119
let tick = channel
2220
.find_validator(adapter.whoami())
23-
.ok_or_else(|| Error::ChannelNotIntendedForUs)?;
21+
.ok_or(Error::ChannelNotIntendedForUs)?;
2422

2523
let sentry = SentryApi::init(
2624
adapter,
@@ -51,13 +49,14 @@ pub async fn channel_tick<A: Adapter + 'static>(
5149
}
5250

5351
// TODO: Add timeout
54-
let _tick_result =
55-
match tick {
56-
primitives::Validator::Leader(_v) => todo!(),
57-
primitives::Validator::Follower(_v) => follower::tick(&sentry, channel, accounting.balances)
52+
let _tick_result = match tick {
53+
primitives::Validator::Leader(_v) => todo!(),
54+
primitives::Validator::Follower(_v) => {
55+
follower::tick(&sentry, channel, accounting.balances)
5856
.await
59-
.map_err(|err| Error::FollowerTick(channel.id(), TickError::Tick(err)))?,
60-
};
57+
.map_err(|err| Error::FollowerTick(channel.id(), TickError::Tick(err)))?
58+
}
59+
};
6160

6261
// Validation #3
6362
// Accounting.balances != NewState.balances

validator_worker/src/core/follower_rules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn get_health(
6969
// TODO: Check if taking the difference between the two sums (earners & spenders) is good enough as a health check!
7070

7171
// Because Accounting should always have >= values than the NewState ones
72-
let health_penalty = diff_earners * &UnifiedNum::from(1_000) / &diff_spenders;
72+
let health_penalty = diff_earners * UnifiedNum::from(1_000) / diff_spenders;
7373

7474
Some(1_000 - health_penalty.to_u64())
7575
}

validator_worker/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ pub enum Error<AE: AdapterErrorKind + 'static> {
3232
Overflow,
3333
#[error("Whoami is neither a Leader or follower in channel")]
3434
// TODO: Add channel, validatorId, etc.
35-
ChannelNotIntendedForUs
36-
}
35+
ChannelNotIntendedForUs,
36+
}

validator_worker/src/follower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ pub fn is_valid_transition(
218218
// sum(accounting.balances.spenders) > sum(new_state.balances.spenders)
219219
// sum(accounting.balances.earners) > sum(new_state.balances.earners)
220220
let is_valid =
221-
!(accounting_spenders > new_state_spenders) || !(accounting_earners > new_state_earners);
221+
accounting_spenders <= new_state_spenders || accounting_earners <= new_state_earners;
222222

223223
Ok(is_valid)
224224
}

validator_worker/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
use std::error::Error;
55

66
use adapter::{get_balance_leaf, get_signable_state_root};
7-
use primitives::{
8-
balances::CheckedState, merkle_tree::MerkleTree, Balances,
9-
ChannelId,
10-
};
7+
use primitives::{balances::CheckedState, merkle_tree::MerkleTree, Balances, ChannelId};
118

129
pub use self::sentry_interface::{all_channels, SentryApi};
1310

@@ -48,7 +45,7 @@ pub(crate) fn get_state_root_hash(
4845
mod test {
4946
use super::*;
5047

51-
use primitives::{util::tests::prep_db::{ADDRESSES, DUMMY_CAMPAIGN, DUMMY_CHANNEL}};
48+
use primitives::util::tests::prep_db::{ADDRESSES, DUMMY_CAMPAIGN, DUMMY_CHANNEL};
5249

5350
#[test]
5451
// TODO: Double check this test - encoded value! after introducing `spenders` ("spender", address, amount)
@@ -85,7 +82,6 @@ mod test {
8582
let actual_hash =
8683
get_state_root_hash(channel, &balances, 18).expect("should get state root hash");
8784

88-
8985
assert_eq!(
9086
"4fad5375c3ef5f8a9d23a8276fed0151164dea72a5891cec8b43e1d190ed430e",
9187
hex::encode(actual_hash)

validator_worker/src/main.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![deny(rust_2018_idioms)]
22
#![deny(clippy::all)]
33

4-
use std::{collections::HashSet, convert::TryFrom, error::Error, time::Duration};
4+
use std::{convert::TryFrom, error::Error, time::Duration};
55

66
use clap::{crate_version, App, Arg};
77
use futures::future::{join, join_all};
@@ -18,21 +18,12 @@ use primitives::{
1818
config::{configuration, Config},
1919
util::tests::prep_db::{AUTH, IDS},
2020
util::ApiUrl,
21-
Channel,
2221
ValidatorId,
23-
// Campaign, ChannelId, SpecValidator,
22+
// Campaign, ChannelId, SpecValidator,
2423
};
2524
use slog::{error, info, Logger};
2625
use std::fmt::Debug;
27-
use validator_worker::{
28-
channel::{channel_tick, collect_channels},
29-
// all_channels,
30-
// error::{Error as ValidatorWorkerError, TickError},
31-
// follower, leader, sentry_interface,
32-
// sentry_interface::{campaigns::all_campaigns, Validators},
33-
// SentryApi,
34-
sentry_interface::Validators,
35-
};
26+
use validator_worker::channel::{channel_tick, collect_channels};
3627

3728
#[derive(Debug, Clone)]
3829
struct Args<A: Adapter> {
@@ -152,7 +143,7 @@ fn run<A: Adapter + 'static>(
152143
adapter.unlock()?;
153144

154145
let args = Args {
155-
sentry_url: sentry_url.to_owned(),
146+
sentry_url,
156147
config: config.to_owned(),
157148
adapter,
158149
};

validator_worker/src/sentry_interface.rs

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,20 @@ use futures::future::{join_all, try_join_all, TryFutureExt};
55
use reqwest::{Client, Response};
66
use slog::Logger;
77

8-
use primitives::{{ChannelId, Config, ToETHChecksum, ValidatorId}, Address, adapter::{Adapter, AdapterErrorKind, Error as AdapterError}, balances::{CheckedState, UncheckedState}, channel::Channel as ChannelOld, channel_v5::Channel, sentry::{AccountingResponse, ChannelListResponse, EventAggregateResponse, LastApprovedResponse, SuccessResponse, ValidatorMessageResponse}, spender::Spender, util::ApiUrl, validator::MessageTypes};
8+
use primitives::{
9+
adapter::{Adapter, AdapterErrorKind, Error as AdapterError},
10+
balances::{CheckedState, UncheckedState},
11+
channel::Channel as ChannelOld,
12+
channel_v5::Channel,
13+
sentry::{
14+
AccountingResponse, ChannelListResponse, EventAggregateResponse, LastApprovedResponse,
15+
SuccessResponse, ValidatorMessageResponse,
16+
},
17+
spender::Spender,
18+
util::ApiUrl,
19+
validator::MessageTypes,
20+
Address, {ChannelId, Config, ToETHChecksum, ValidatorId},
21+
};
922
use thiserror::Error;
1023

1124
pub type PropagationResult<AE> = Result<ValidatorId, (ValidatorId, Error<AE>)>;
@@ -63,14 +76,13 @@ impl<A: Adapter + 'static> SentryApi<A> {
6376
.build()
6477
.map_err(Error::BuildingClient)?;
6578

66-
let whoami =
67-
propagate_to
68-
.get(&adapter.whoami())
69-
.cloned()
70-
.ok_or_else(|| Error::WhoamiMissing {
71-
channel: channel.id(),
72-
whoami: adapter.whoami(),
73-
})?;
79+
let whoami = propagate_to
80+
.get(&adapter.whoami())
81+
.cloned()
82+
.ok_or_else(|| Error::WhoamiMissing {
83+
channel: channel.id(),
84+
whoami: adapter.whoami(),
85+
})?;
7486

7587
Ok(Self {
7688
adapter,
@@ -87,18 +99,14 @@ impl<A: Adapter + 'static> SentryApi<A> {
8799
&self,
88100
messages: &[&MessageTypes],
89101
) -> Vec<PropagationResult<A::AdapterError>> {
90-
join_all(
91-
self.propagate_to
92-
.iter()
93-
.map(|(validator_id, validator)| {
94-
propagate_to::<A>(
95-
&self.client,
96-
self.channel.id(),
97-
(*validator_id, validator),
98-
messages,
99-
)
100-
}),
101-
)
102+
join_all(self.propagate_to.iter().map(|(validator_id, validator)| {
103+
propagate_to::<A>(
104+
&self.client,
105+
self.channel.id(),
106+
(*validator_id, validator),
107+
messages,
108+
)
109+
}))
102110
.await
103111
}
104112

@@ -109,11 +117,15 @@ impl<A: Adapter + 'static> SentryApi<A> {
109117
) -> Result<Option<MessageTypes>, Error<A::AdapterError>> {
110118
let message_type = message_types.join("+");
111119

112-
let endpoint = self.whoami.url.join(&format!(
113-
"/validator-messages/{}/{}?limit=1",
114-
from.to_checksum(),
115-
message_type
116-
)).expect("Should parse endpoint");
120+
let endpoint = self
121+
.whoami
122+
.url
123+
.join(&format!(
124+
"/validator-messages/{}/{}?limit=1",
125+
from.to_checksum(),
126+
message_type
127+
))
128+
.expect("Should parse endpoint");
117129

118130
let result = self
119131
.client
@@ -140,7 +152,8 @@ impl<A: Adapter + 'static> SentryApi<A> {
140152
) -> Result<LastApprovedResponse<UncheckedState>, Error<A::AdapterError>> {
141153
self.client
142154
.get(
143-
self.whoami.url
155+
self.whoami
156+
.url
144157
.join(&format!("v5/channel/{}/last-approved", channel))
145158
.expect("Should not error while creating endpoint"),
146159
)
@@ -156,7 +169,8 @@ impl<A: Adapter + 'static> SentryApi<A> {
156169
) -> Result<LastApprovedResponse<UncheckedState>, Error<A::AdapterError>> {
157170
self.client
158171
.get(
159-
self.whoami.url
172+
self.whoami
173+
.url
160174
.join("last-approved?withHeartbeat=true")
161175
.expect("Should not error while creating endpoint"),
162176
)
@@ -167,13 +181,13 @@ impl<A: Adapter + 'static> SentryApi<A> {
167181
}
168182

169183
// TODO: Pagination & use of `AllSpendersResponse`
170-
pub async fn get_all_spenders(&self) -> Result<HashMap<Address, Spender>, Error<A::AdapterError>> {
184+
pub async fn get_all_spenders(
185+
&self,
186+
) -> Result<HashMap<Address, Spender>, Error<A::AdapterError>> {
171187
let url = self
172-
.whoami.url
173-
.join(&format!(
174-
"v5/channel/{}/spender/all",
175-
self.channel.id()
176-
))
188+
.whoami
189+
.url
190+
.join(&format!("v5/channel/{}/spender/all", self.channel.id()))
177191
.expect("Should not error when creating endpoint");
178192

179193
self.client
@@ -189,13 +203,14 @@ impl<A: Adapter + 'static> SentryApi<A> {
189203

190204
/// Get the accounting from Sentry
191205
/// `Balances` should always be in `CheckedState`
192-
pub async fn get_accounting(&self, channel: ChannelId) -> Result<AccountingResponse<CheckedState>, Error<A::AdapterError>> {
206+
pub async fn get_accounting(
207+
&self,
208+
channel: ChannelId,
209+
) -> Result<AccountingResponse<CheckedState>, Error<A::AdapterError>> {
193210
let url = self
194-
.whoami.url
195-
.join(&format!(
196-
"v5/channel/{}/accounting",
197-
channel
198-
))
211+
.whoami
212+
.url
213+
.join(&format!("v5/channel/{}/accounting", channel))
199214
.expect("Should not error when creating endpoint");
200215

201216
self.client
@@ -214,7 +229,8 @@ impl<A: Adapter + 'static> SentryApi<A> {
214229
after: DateTime<Utc>,
215230
) -> Result<EventAggregateResponse, Error<A::AdapterError>> {
216231
let url = self
217-
.whoami.url
232+
.whoami
233+
.url
218234
.join(&format!(
219235
"events-aggregates?after={}",
220236
after.timestamp_millis()
@@ -238,10 +254,10 @@ async fn propagate_to<A: Adapter>(
238254
(validator_id, validator): (ValidatorId, &Validator),
239255
messages: &[&MessageTypes],
240256
) -> PropagationResult<A::AdapterError> {
241-
let endpoint = validator.url.join(&format!(
242-
"v5/channel/{}/validator-messages",
243-
channel_id
244-
)).expect("Should not error when creating endpoint url");
257+
let endpoint = validator
258+
.url
259+
.join(&format!("v5/channel/{}/validator-messages", channel_id))
260+
.expect("Should not error when creating endpoint url");
245261

246262
let mut body = HashMap::new();
247263
body.insert("messages", messages);
@@ -317,7 +333,7 @@ pub mod campaigns {
317333
sentry_url: &ApiUrl,
318334
whoami: ValidatorId,
319335
) -> Result<Vec<Campaign>, reqwest::Error> {
320-
let first_page = fetch_page(sentry_url, 0, whoami).await?;
336+
let first_page = fetch_page(sentry_url, 0, whoami).await?;
321337

322338
if first_page.pagination.total_pages < 2 {
323339
Ok(first_page.campaigns)

0 commit comments

Comments
 (0)