Skip to content

Commit 81a3b3d

Browse files
authored
Merge pull request #223 from AdExNetwork/issue-213-redundant-whoami-where-we-have-adapter
Issue #213 Redundant `whoami` where we have `Adapter` + #210 #212
2 parents f1c8fd4 + ac55c33 commit 81a3b3d

File tree

4 files changed

+32
-36
lines changed

4 files changed

+32
-36
lines changed

validator_worker/src/follower.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ enum NewStateResult {
2323
}
2424

2525
pub async fn tick<A: Adapter + 'static>(iface: &SentryApi<A>) -> Result<(), Box<dyn Error>> {
26-
let from = iface.channel.spec.validators.leader().id.clone();
27-
let new_msg_response = iface
28-
.get_latest_msg(from.to_string(), &["NewState"])
29-
.await?;
26+
let from = &iface.channel.spec.validators.leader().id;
27+
let new_msg_response = iface.get_latest_msg(from, &["NewState"]).await?;
3028
let new_msg = match new_msg_response {
3129
Some(MessageTypes::NewState(new_state)) => Some(new_state),
3230
_ => None,

validator_worker/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ mod test {
5757
};
5858
let config = configuration("development", None).expect("Dev config should be available");
5959
let dummy_adapter = DummyAdapter::init(adapter_options, &config);
60-
let whoami = dummy_adapter.whoami().clone();
6160

62-
SentryApi::init(dummy_adapter, &channel, &config, false, &whoami).expect("should succeed")
61+
SentryApi::init(dummy_adapter, &channel, &config, false).expect("should succeed")
6362
}
6463

6564
#[test]

validator_worker/src/main.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ struct Args<A: Adapter> {
2525
sentry_url: String,
2626
config: Config,
2727
adapter: A,
28-
whoami: ValidatorId,
2928
}
3029

3130
fn main() -> Result<(), Box<dyn Error>> {
@@ -127,13 +126,11 @@ fn run<A: Adapter + 'static>(
127126
let mut sentry_adapter = adapter.clone();
128127
// unlock adapter
129128
sentry_adapter.unlock()?;
130-
let whoami = adapter.whoami().to_owned();
131129

132130
let args = Args {
133131
sentry_url: sentry_url.to_owned(),
134132
config: config.to_owned(),
135133
adapter: sentry_adapter,
136-
whoami,
137134
};
138135

139136
if is_single_tick {
@@ -158,7 +155,7 @@ async fn infinite<A: Adapter + 'static>(args: Args<A>) -> Result<(), ()> {
158155
}
159156

160157
async fn iterate_channels<A: Adapter + 'static>(args: Args<A>) -> Result<(), ()> {
161-
let result = all_channels(&args.sentry_url, args.whoami.to_string()).await;
158+
let result = all_channels(&args.sentry_url, args.adapter.whoami()).await;
162159

163160
if let Err(e) = result {
164161
eprintln!("Failed to get channels {}", e);
@@ -168,11 +165,12 @@ async fn iterate_channels<A: Adapter + 'static>(args: Args<A>) -> Result<(), ()>
168165
let channels = result.unwrap();
169166
let channels_size = channels.len();
170167

171-
let tick =
172-
try_join_all(channels.into_iter().map(|channel| {
173-
validator_tick(args.adapter.clone(), channel, &args.config, &args.whoami)
174-
}))
175-
.await;
168+
let tick = try_join_all(
169+
channels
170+
.into_iter()
171+
.map(|channel| validator_tick(args.adapter.clone(), channel, &args.config)),
172+
)
173+
.await;
176174

177175
if let Err(e) = tick {
178176
eprintln!("An occurred while processing channels {}", e);
@@ -191,9 +189,9 @@ async fn validator_tick<A: Adapter + 'static>(
191189
adapter: A,
192190
channel: Channel,
193191
config: &Config,
194-
whoami: &ValidatorId,
195192
) -> Result<(), ValidatorWorkerError> {
196-
let sentry = SentryApi::init(adapter, &channel, &config, true, whoami)?;
193+
let whoami = adapter.whoami().clone();
194+
let sentry = SentryApi::init(adapter, &channel, &config, true)?;
197195
let duration = Duration::from_secs(config.validator_tick_timeout as u64);
198196

199197
match channel.spec.validators.find(&whoami) {

validator_worker/src/sentry_interface.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub struct SentryApi<T: Adapter> {
2323
pub logging: bool,
2424
pub channel: Channel,
2525
pub config: Config,
26-
pub whoami: ValidatorId,
2726
pub propagate_to: Vec<(ValidatorDesc, String)>,
2827
}
2928

@@ -33,15 +32,14 @@ impl<T: Adapter + 'static> SentryApi<T> {
3332
channel: &Channel,
3433
config: &Config,
3534
logging: bool,
36-
whoami: &ValidatorId,
3735
) -> Result<Self, ValidatorWorker> {
3836
let client = Client::builder()
3937
.timeout(Duration::from_secs(config.fetch_timeout.into()))
4038
.build()
4139
.unwrap();
4240

4341
// validate that we are to validate the channel
44-
match channel.spec.validators.find(&whoami) {
42+
match channel.spec.validators.find(adapter.whoami()) {
4543
SpecValidator::Leader(v) | SpecValidator::Follower(v) => {
4644
let channel_id = format!("0x{}", hex::encode(&channel.id));
4745
let validator_url = format!("{}/channel/{}", v.url, channel_id);
@@ -70,7 +68,6 @@ impl<T: Adapter + 'static> SentryApi<T> {
7068
propagate_to,
7169
channel: channel.to_owned(),
7270
config: config.to_owned(),
73-
whoami: whoami.to_owned(),
7471
})
7572
}
7673
SpecValidator::None => Err(ValidatorWorker::Failed(
@@ -92,13 +89,15 @@ impl<T: Adapter + 'static> SentryApi<T> {
9289

9390
pub async fn get_latest_msg(
9491
&self,
95-
from: String,
92+
from: &ValidatorId,
9693
message_types: &[&str],
9794
) -> Result<Option<MessageTypes>, reqwest::Error> {
9895
let message_type = message_types.join("+");
9996
let url = format!(
10097
"{}/validator-messages/{}/{}?limit=1",
101-
self.validator_url, from, message_type
98+
self.validator_url,
99+
from.to_string(),
100+
message_type
102101
);
103102
let result = self
104103
.client
@@ -115,7 +114,7 @@ impl<T: Adapter + 'static> SentryApi<T> {
115114
&self,
116115
message_types: &[&str],
117116
) -> Result<Option<MessageTypes>, reqwest::Error> {
118-
self.get_latest_msg(self.whoami.to_string(), message_types)
117+
self.get_latest_msg(self.adapter.whoami(), message_types)
119118
.await
120119
}
121120

@@ -133,7 +132,7 @@ impl<T: Adapter + 'static> SentryApi<T> {
133132
let future = self
134133
.client
135134
.get(&format!(
136-
"{}/last-approved?withHearbeat=true",
135+
"{}/last-approved?withHeartbeat=true",
137136
self.validator_url
138137
))
139138
.send()
@@ -148,7 +147,7 @@ impl<T: Adapter + 'static> SentryApi<T> {
148147
) -> Result<EventAggregateResponse, Box<ValidatorWorker>> {
149148
let auth_token = self
150149
.adapter
151-
.get_auth(&self.whoami)
150+
.get_auth(self.adapter.whoami())
152151
.map_err(|e| Box::new(ValidatorWorker::Failed(e.to_string())))?;
153152

154153
let url = format!(
@@ -222,18 +221,17 @@ fn handle_http_error(e: reqwest::Error) {
222221

223222
pub async fn all_channels(
224223
sentry_url: &str,
225-
whoami: String,
224+
whoami: &ValidatorId,
226225
) -> Result<Vec<Channel>, reqwest::Error> {
227226
let url = sentry_url.to_owned();
228-
let first_page = fetch_page(url.clone(), 0, whoami.clone()).await?;
227+
let first_page = fetch_page(url.clone(), 0, &whoami).await?;
229228

230229
if first_page.total_pages < 2 {
231230
Ok(first_page.channels)
232231
} else {
233-
let mut all: Vec<ChannelListResponse> = try_join_all(
234-
(1..first_page.total_pages).map(|i| fetch_page(url.clone(), i, whoami.clone())),
235-
)
236-
.await?;
232+
let mut all: Vec<ChannelListResponse> =
233+
try_join_all((1..first_page.total_pages).map(|i| fetch_page(url.clone(), i, &whoami)))
234+
.await?;
237235

238236
all.push(first_page);
239237
let result_all: Vec<Channel> = all
@@ -247,15 +245,18 @@ pub async fn all_channels(
247245
async fn fetch_page(
248246
sentry_url: String,
249247
page: u64,
250-
validator: String,
248+
validator: &ValidatorId,
251249
) -> Result<ChannelListResponse, reqwest::Error> {
252250
let client = Client::new();
253251

254-
let mut query = vec![format!("page={}", page)];
255-
query.push(format!("validator={}", validator.to_string()));
252+
let query = [
253+
format!("page={}", page),
254+
format!("validator={}", validator.to_hex_checksummed_string()),
255+
]
256+
.join("&");
256257

257258
let future = client
258-
.get(format!("{}/channel/list?{}", sentry_url, query.join("&")).as_str())
259+
.get(&format!("{}/channel/list?{}", sentry_url, query))
259260
.send()
260261
.and_then(|mut res: Response| res.json::<ChannelListResponse>());
261262

0 commit comments

Comments
 (0)