Skip to content

Commit

Permalink
Add options for ignoring legacy network when listing drivers (#3337)
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 authored Oct 1, 2024
1 parent e08b0cd commit 1418de5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 35 deletions.
4 changes: 3 additions & 1 deletion core/model/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ pub mod local {
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GetDrivers {}
pub struct GetDrivers {
pub ignore_legacy_networks: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize, thiserror::Error)]
pub enum GetDriversError {
Expand Down
72 changes: 44 additions & 28 deletions core/payment/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
mod rpc;

use std::collections::HashMap;
// External crates
use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
use serde_json::to_value;
use serde_json::{to_value, Value};
use std::str::FromStr;
use std::time::UNIX_EPOCH;
use structopt::*;
use ya_client_model::payment::DriverStatusProperty;
use ya_client_model::payment::{DriverDetails, DriverStatusProperty};
use ya_core_model::payment::local::NetworkName;

// Workspace uses
Expand Down Expand Up @@ -553,41 +554,56 @@ Typically operation should take less than 1 minute.
)))
}
DriverSubcommand::List => {
let drivers = bus::service(pay::BUS_ID).call(pay::GetDrivers {}).await??;
let drivers: HashMap<String, DriverDetails> = bus::service(pay::BUS_ID)
.call(pay::GetDrivers {
ignore_legacy_networks: false,
})
.await??;
if ctx.json_output {
return CommandOutput::object(drivers);
}
Ok(ResponseTable {
columns: vec![
"driver".to_owned(),
"network".to_owned(),
"default?".to_owned(),
"token".to_owned(),
"platform".to_owned(),
],
values: drivers
.iter()
.flat_map(|(driver, dd)| {
dd.networks
.iter()
.flat_map(|(network, n)| {
n.tokens
.iter()
.map(|(token, platform)|
serde_json::json! {[

let mut values: Vec<Value> = drivers
.iter()
.flat_map(|(driver, dd)| {
dd.networks
.iter()
.flat_map(|(network, n)| {
n.tokens
.iter()
.map(|(token, platform)|
serde_json::json! {[
driver,
network,
if &dd.default_network == network { "X" } else { "" },
token,
platform,
]}
)
.collect::<Vec<serde_json::Value>>()
})
.collect::<Vec<serde_json::Value>>()
})
.collect(),
}.into())
)
.collect::<Vec<serde_json::Value>>()
})
.collect::<Vec<serde_json::Value>>()
})
.collect();

values.sort_by(|a, b| {
//sort by index 4 (which means platform, be careful when changing these values)
let left_str = a.as_array().unwrap()[4].as_str().unwrap();
let right_str = b.as_array().unwrap()[4].as_str().unwrap();
left_str.cmp(right_str)
});

Ok(ResponseTable {
columns: vec![
"driver".to_owned(), //index 0
"network".to_owned(), //index 1
"default?".to_owned(), //index 2
"token".to_owned(), //index 3
"platform".to_owned(), //index 4 - we are sorting by platform, be careful when changing these values
],
values,
}
.into())
}
},
PaymentCli::ReleaseAllocations => {
Expand Down
21 changes: 17 additions & 4 deletions core/payment/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,18 @@ impl DriverRegistry {
}
}

pub fn get_drivers(&self) -> HashMap<String, DriverDetails> {
self.drivers.clone()
pub fn get_drivers(&self, ignore_legacy_networks: bool) -> HashMap<String, DriverDetails> {
let mut drivers = self.drivers.clone();

let legacy_networks = ["mumbai", "goerli", "rinkeby"];
if ignore_legacy_networks {
drivers.values_mut().for_each(|driver| {
driver
.networks
.retain(|name, _| !legacy_networks.contains(&name.as_str()))
})
}
drivers
}

pub fn get_network(
Expand Down Expand Up @@ -381,11 +391,14 @@ impl PaymentProcessor {
.map_err(|_| GetAccountsError::InternalTimeout)
}

pub async fn get_drivers(&self) -> Result<HashMap<String, DriverDetails>, GetDriversError> {
pub async fn get_drivers(
&self,
ignore_legacy_networks: bool,
) -> Result<HashMap<String, DriverDetails>, GetDriversError> {
self.registry
.timeout_read(REGISTRY_LOCK_TIMEOUT)
.await
.map(|registry| registry.get_drivers())
.map(|registry| registry.get_drivers(ignore_legacy_networks))
.map_err(|_| GetDriversError::InternalTimeout)
}

Expand Down
9 changes: 7 additions & 2 deletions core/payment/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ mod local {
_caller: String,
msg: GetDrivers,
) -> Result<HashMap<String, DriverDetails>, GetDriversError> {
processor.get_drivers().await
processor.get_drivers(msg.ignore_legacy_networks).await
}

async fn payment_driver_status(
Expand All @@ -506,7 +506,12 @@ mod local {
None => {
#[allow(clippy::iter_kv_map)]
// Unwrap is provably safe because NoError can't be instanciated
match service(PAYMENT_BUS_ID).call(GetDrivers {}).await {
match service(PAYMENT_BUS_ID)
.call(GetDrivers {
ignore_legacy_networks: false,
})
.await
{
Ok(drivers) => drivers,
Err(e) => return Err(PaymentDriverStatusError::Internal(e.to_string())),
}
Expand Down

0 comments on commit 1418de5

Please sign in to comment.