Skip to content

Commit 6f0d7fd

Browse files
authored
Merge pull request #179 from samparsky/fix-get-auth
Fix get auth
2 parents 093bbdd + 0cb18cd commit 6f0d7fd

File tree

11 files changed

+58
-156
lines changed

11 files changed

+58
-156
lines changed

Cargo.lock

Lines changed: 0 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

adapter/src/dummy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl Adapter for DummyAdapter {
8888
}
8989
}
9090

91-
fn get_auth(&mut self, _validator: &ValidatorId) -> AdapterResult<String> {
91+
fn get_auth(&self, _validator: &ValidatorId) -> AdapterResult<String> {
9292
let who = self
9393
.session_tokens
9494
.iter()

adapter/src/ethereum.rs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use primitives::{
1111
};
1212
use serde::{Deserialize, Serialize};
1313
use serde_json::Value;
14-
use std::collections::HashMap;
1514
use std::convert::TryFrom;
1615
use std::error::Error;
1716
use std::fs;
@@ -38,8 +37,6 @@ pub struct EthereumAdapter {
3837
keystore_json: Value,
3938
keystore_pwd: Password,
4039
config: Config,
41-
// Auth tokens that we've generated to authenticate with someone (address => token)
42-
authorization_tokens: HashMap<String, String>,
4340
wallet: Option<SafeAccount>,
4441
}
4542

@@ -70,7 +67,6 @@ impl EthereumAdapter {
7067
address,
7168
keystore_json,
7269
keystore_pwd: opts.keystore_pwd.into(),
73-
authorization_tokens: HashMap::new(),
7470
wallet: None,
7571
config: config.to_owned(),
7672
})
@@ -227,33 +223,22 @@ impl Adapter for EthereumAdapter {
227223
Ok(sess)
228224
}
229225

230-
fn get_auth(&mut self, validator_id: &ValidatorId) -> AdapterResult<String> {
231-
let validator = validator_id.to_owned();
232-
match (
233-
&self.wallet,
234-
self.authorization_tokens.get(&validator.to_string()),
235-
) {
236-
(Some(_), Some(token)) => Ok(token.to_owned()),
237-
(Some(wallet), None) => {
238-
let era = Utc::now().timestamp_millis() as f64 / 60000.0;
239-
let payload = Payload {
240-
id: validator.to_hex_checksummed_string(),
241-
era: era.floor() as i64,
242-
identity: None,
243-
address: self.whoami().to_hex_checksummed_string(),
244-
};
245-
let token = ewt_sign(wallet, &self.keystore_pwd, &payload)
246-
.map_err(|_| map_error("Failed to sign token"))?;
247-
248-
self.authorization_tokens
249-
.insert(validator.to_string(), token.clone());
250-
251-
Ok(token)
252-
}
253-
(_, _) => Err(AdapterError::Configuration(
254-
"failed to unlock wallet".to_string(),
255-
)),
256-
}
226+
fn get_auth(&self, validator: &ValidatorId) -> AdapterResult<String> {
227+
let wallet = self
228+
.wallet
229+
.as_ref()
230+
.ok_or_else(|| AdapterError::Configuration("unlock wallet".to_string()))?;
231+
232+
let era = Utc::now().timestamp_millis() as f64 / 60000.0;
233+
let payload = Payload {
234+
id: validator.to_hex_checksummed_string(),
235+
era: era.floor() as i64,
236+
identity: None,
237+
address: self.whoami().to_hex_checksummed_string(),
238+
};
239+
240+
ewt_sign(&wallet, &self.keystore_pwd, &payload)
241+
.map_err(|_| map_error("Failed to sign token"))
257242
}
258243
}
259244

primitives/src/adapter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct Session {
5959
pub uid: ValidatorId,
6060
}
6161

62-
pub trait Adapter: ChannelValidator + Send + Clone + Debug {
62+
pub trait Adapter: ChannelValidator + Send + Sync + Clone + Debug {
6363
/// Unlock adapter
6464
fn unlock(&mut self) -> AdapterResult<()>;
6565

@@ -84,5 +84,5 @@ pub trait Adapter: ChannelValidator + Send + Clone + Debug {
8484
fn session_from_token(&self, token: &str) -> AdapterResult<Session>;
8585

8686
/// Gets authentication for specific validator
87-
fn get_auth(&mut self, validator_id: &ValidatorId) -> AdapterResult<String>;
87+
fn get_auth(&self, validator_id: &ValidatorId) -> AdapterResult<String>;
8888
}

validator_worker/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ futures-preview = { version = "=0.3.0-alpha.19", features = ["compat"]}
2323
futures_legacy = { version = "0.1.20", package = "futures" }
2424
# Concurrency
2525
tokio = { version = "=0.1.19" }
26-
async-std = "^0.99.10"
2726
# API client
2827
reqwest = "0.9.18"
2928
# Configuration

validator_worker/src/follower.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ async fn on_new_state<'a, A: Adapter + 'static>(
6464
return Ok(on_error(&iface, &new_state, InvalidNewState::RootHash).await);
6565
}
6666

67-
let adapter = iface.adapter.read().await.clone();
68-
69-
if !adapter.verify(
67+
if !iface.adapter.verify(
7068
&iface.channel.spec.validators.leader().id,
7169
&proposed_state_root,
7270
&new_state.signature,
@@ -84,7 +82,7 @@ async fn on_new_state<'a, A: Adapter + 'static>(
8482
return Ok(on_error(&iface, &new_state, InvalidNewState::Transition).await);
8583
}
8684

87-
let signature = adapter.sign(&new_state.state_root)?;
85+
let signature = iface.adapter.sign(&new_state.state_root)?;
8886
let health_threshold = u64::from(iface.config.health_threshold_promilles).into();
8987
let health = is_healthy(
9088
&iface.channel,

validator_worker/src/heartbeat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async fn send_heartbeat<A: Adapter + 'static>(iface: &SentryApi<A>) -> Result<()
2323
let state_root_raw = get_signable_state_root(&iface.channel.id, &merkle_tree.root())?;
2424
let state_root = hex::encode(state_root_raw);
2525

26-
let signature = iface.adapter.read().await.sign(&state_root)?;
26+
let signature = iface.adapter.sign(&state_root)?;
2727

2828
let message_types = MessageTypes::Heartbeat(Heartbeat {
2929
signature,

validator_worker/src/leader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async fn on_new_accounting<A: Adapter + 'static>(
2525
let state_root_raw = get_state_root_hash(&iface, &balances)?;
2626
let state_root = hex::encode(state_root_raw);
2727

28-
let signature = iface.adapter.read().await.sign(&state_root)?;
28+
let signature = iface.adapter.sign(&state_root)?;
2929

3030
iface
3131
.propagate(&[&MessageTypes::NewState(NewState {

validator_worker/src/lib.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ mod test {
4444
use super::*;
4545

4646
use adapter::DummyAdapter;
47-
use async_std::sync::RwLock;
4847
use primitives::adapter::DummyAdapterOptions;
4948
use primitives::config::configuration;
5049
use primitives::util::tests::prep_db::{AUTH, DUMMY_CHANNEL, IDS};
5150
use primitives::{BalancesMap, Channel};
52-
use std::sync::Arc;
5351

5452
fn setup_iface(channel: &Channel) -> SentryApi<DummyAdapter> {
5553
let adapter_options = DummyAdapterOptions {
@@ -61,14 +59,7 @@ mod test {
6159
let dummy_adapter = DummyAdapter::init(adapter_options, &config);
6260
let whoami = dummy_adapter.whoami().clone();
6361

64-
SentryApi::init(
65-
Arc::new(RwLock::new(dummy_adapter)),
66-
&channel,
67-
&config,
68-
false,
69-
&whoami,
70-
)
71-
.expect("should succeed")
62+
SentryApi::init(dummy_adapter, &channel, &config, false, &whoami).expect("should succeed")
7263
}
7364

7465
#[test]

validator_worker/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use clap::{App, Arg};
55

66
use adapter::{AdapterTypes, DummyAdapter, EthereumAdapter};
7-
use async_std::sync::RwLock;
87
use futures::compat::Future01CompatExt;
98
use futures::future::try_join_all;
109
use futures::future::{join, FutureExt, TryFutureExt};
@@ -15,7 +14,6 @@ use primitives::{Channel, SpecValidator, ValidatorId};
1514
use std::convert::TryFrom;
1615
use std::error::Error;
1716
use std::ops::Add;
18-
use std::sync::Arc;
1917
use std::time::{Duration, Instant};
2018
use tokio::timer::Delay;
2119
use tokio::util::FutureExt as TokioFutureExt;
@@ -26,7 +24,7 @@ use validator_worker::{all_channels, follower, leader, SentryApi};
2624
struct Args<A: Adapter> {
2725
sentry_url: String,
2826
config: Config,
29-
adapter: Arc<RwLock<A>>,
27+
adapter: A,
3028
whoami: ValidatorId,
3129
}
3230

@@ -71,7 +69,7 @@ fn main() -> Result<(), Box<dyn Error>> {
7169
Arg::with_name("singleTick")
7270
.short("t")
7371
.takes_value(false)
74-
.help("runs the validator in single-tick mode and exis"),
72+
.help("runs the validator in single-tick mode and exit"),
7573
)
7674
.get_matches();
7775

@@ -126,7 +124,9 @@ fn run<A: Adapter + 'static>(
126124
config: &Config,
127125
adapter: A,
128126
) -> Result<(), Box<dyn Error>> {
129-
let sentry_adapter = Arc::new(RwLock::new(adapter.clone()));
127+
let mut sentry_adapter = adapter.clone();
128+
// unlock adapter
129+
sentry_adapter.unlock()?;
130130
let whoami = adapter.whoami().to_owned();
131131

132132
let args = Args {
@@ -188,7 +188,7 @@ async fn iterate_channels<A: Adapter + 'static>(args: Args<A>) -> Result<(), ()>
188188
}
189189

190190
async fn validator_tick<A: Adapter + 'static>(
191-
adapter: Arc<RwLock<A>>,
191+
adapter: A,
192192
channel: Channel,
193193
config: &Config,
194194
whoami: &ValidatorId,

0 commit comments

Comments
 (0)