Skip to content

Commit 9cc28db

Browse files
authored
Removed hardcoded 'good gateways' in favour of pseusorandom choice from the active set (#450)
1 parent 21d2749 commit 9cc28db

File tree

2 files changed

+52
-48
lines changed

2 files changed

+52
-48
lines changed

clients/native/src/commands/init.rs

+26-24
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ use std::sync::Arc;
2727
use std::time::Duration;
2828
use topology::{gateway, NymTopology};
2929

30-
const GOOD_GATEWAYS: [&str; 2] = [
31-
"DiYR9o8KgeQ81woKPYVAu4LNaAEg8SWkiufDCahNnPov",
32-
"5nrYxPR8gt2Gzo2BbHtsGf66KAEQY91WmM1eW78EphNy",
33-
];
34-
3530
pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
3631
App::new("init")
3732
.about("Initialise a Nym client. Do this first!")
@@ -100,28 +95,30 @@ async fn register_with_gateway(
10095
.expect("failed to register with the gateway!")
10196
}
10297

103-
async fn gateway_details(validator_server: &str, gateway_id: &str) -> gateway::Node {
98+
async fn gateway_details(validator_server: &str, chosen_gateway_id: Option<&str>) -> gateway::Node {
10499
let validator_client_config = validator_client::Config::new(validator_server.to_string());
105100
let validator_client = validator_client::Client::new(validator_client_config);
106101
let topology = validator_client.get_active_topology().await.unwrap();
107102
let nym_topology: NymTopology = topology.into();
108103
let version_filtered_topology = nym_topology.filter_system_version(env!("CARGO_PKG_VERSION"));
109104

110-
version_filtered_topology
111-
.gateways()
112-
.iter()
113-
.find(|gateway| gateway.identity_key.to_base58_string() == gateway_id)
114-
.expect(&*format!("no gateway with id {} exists!", gateway_id))
115-
.clone()
116-
}
117-
118-
fn select_gateway(arg: Option<&str>) -> &str {
119-
if let Some(gateway_id) = arg {
120-
gateway_id
105+
// if we have chosen particular gateway - use it, otherwise choose a random one.
106+
// (remember that in active topology all gateways have at least 100 reputation so should
107+
// be working correctly)
108+
109+
if let Some(gateway_id) = chosen_gateway_id {
110+
version_filtered_topology
111+
.gateways()
112+
.iter()
113+
.find(|gateway| gateway.identity_key.to_base58_string() == gateway_id)
114+
.expect(&*format!("no gateway with id {} exists!", gateway_id))
115+
.clone()
121116
} else {
122-
// TODO1: this should only be done on testnet
123-
// TODO2: it should probably check if chosen gateway is actually online
124-
GOOD_GATEWAYS.choose(&mut rand::thread_rng()).unwrap()
117+
version_filtered_topology
118+
.gateways()
119+
.choose(&mut rand::thread_rng())
120+
.expect("there are no gateways on the network!")
121+
.clone()
125122
}
126123
}
127124

@@ -154,12 +151,17 @@ pub fn execute(matches: &ArgMatches) {
154151
// create identity, encryption and ack keys.
155152
let mut key_manager = KeyManager::new(&mut rng);
156153

157-
let gateway_id = select_gateway(matches.value_of("gateway"));
158-
config.get_base_mut().with_gateway_id(gateway_id);
154+
let chosen_gateway_id = matches.value_of("gateway");
159155

160156
let registration_fut = async {
161-
let gate_details =
162-
gateway_details(&config.get_base().get_validator_rest_endpoint(), gateway_id).await;
157+
let gate_details = gateway_details(
158+
&config.get_base().get_validator_rest_endpoint(),
159+
chosen_gateway_id,
160+
)
161+
.await;
162+
config
163+
.get_base_mut()
164+
.with_gateway_id(gate_details.identity_key.to_base58_string());
163165
let shared_keys =
164166
register_with_gateway(&gate_details, key_manager.identity_keypair()).await;
165167
(shared_keys, gate_details.client_listener)

clients/socks5/src/commands/init.rs

+26-24
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ use std::sync::Arc;
2626
use std::time::Duration;
2727
use topology::{gateway, NymTopology};
2828

29-
const GOOD_GATEWAYS: [&str; 2] = [
30-
"DiYR9o8KgeQ81woKPYVAu4LNaAEg8SWkiufDCahNnPov",
31-
"5nrYxPR8gt2Gzo2BbHtsGf66KAEQY91WmM1eW78EphNy",
32-
];
33-
3429
pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
3530
App::new("init")
3631
.about("Initialise a Nym client. Do this first!")
@@ -101,28 +96,30 @@ async fn register_with_gateway(
10196
.expect("failed to register with the gateway!")
10297
}
10398

104-
async fn gateway_details(validator_server: &str, gateway_id: &str) -> gateway::Node {
99+
async fn gateway_details(validator_server: &str, chosen_gateway_id: Option<&str>) -> gateway::Node {
105100
let validator_client_config = validator_client::Config::new(validator_server.to_string());
106101
let validator_client = validator_client::Client::new(validator_client_config);
107102
let topology = validator_client.get_active_topology().await.unwrap();
108103
let nym_topology: NymTopology = topology.into();
109104
let version_filtered_topology = nym_topology.filter_system_version(env!("CARGO_PKG_VERSION"));
110105

111-
version_filtered_topology
112-
.gateways()
113-
.iter()
114-
.find(|gateway| gateway.identity_key.to_base58_string() == gateway_id)
115-
.expect(&*format!("no gateway with id {} exists!", gateway_id))
116-
.clone()
117-
}
118-
119-
fn select_gateway(arg: Option<&str>) -> &str {
120-
if let Some(gateway_id) = arg {
121-
gateway_id
106+
// if we have chosen particular gateway - use it, otherwise choose a random one.
107+
// (remember that in active topology all gateways have at least 100 reputation so should
108+
// be working correctly)
109+
110+
if let Some(gateway_id) = chosen_gateway_id {
111+
version_filtered_topology
112+
.gateways()
113+
.iter()
114+
.find(|gateway| gateway.identity_key.to_base58_string() == gateway_id)
115+
.expect(&*format!("no gateway with id {} exists!", gateway_id))
116+
.clone()
122117
} else {
123-
// TODO1: this should only be done on testnet
124-
// TODO2: it should probably check if chosen gateway is actually online
125-
GOOD_GATEWAYS.choose(&mut rand::thread_rng()).unwrap()
118+
version_filtered_topology
119+
.gateways()
120+
.choose(&mut rand::thread_rng())
121+
.expect("there are no gateways on the network!")
122+
.clone()
126123
}
127124
}
128125

@@ -156,12 +153,17 @@ pub fn execute(matches: &ArgMatches) {
156153
// create identity, encryption and ack keys.
157154
let mut key_manager = KeyManager::new(&mut rng);
158155

159-
let gateway_id = select_gateway(matches.value_of("gateway"));
160-
config.get_base_mut().with_gateway_id(gateway_id);
156+
let chosen_gateway_id = matches.value_of("gateway");
161157

162158
let registration_fut = async {
163-
let gate_details =
164-
gateway_details(&config.get_base().get_validator_rest_endpoint(), gateway_id).await;
159+
let gate_details = gateway_details(
160+
&config.get_base().get_validator_rest_endpoint(),
161+
chosen_gateway_id,
162+
)
163+
.await;
164+
config
165+
.get_base_mut()
166+
.with_gateway_id(gate_details.identity_key.to_base58_string());
165167
let shared_keys =
166168
register_with_gateway(&gate_details, key_manager.identity_keypair()).await;
167169
(shared_keys, gate_details.client_listener)

0 commit comments

Comments
 (0)