Skip to content

Commit

Permalink
[golemsp] mainnet vs testnet (#1732)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiivan authored Nov 29, 2021
1 parent 37a8643 commit 5bc9c78
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 59 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions agent/provider/src/provider_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ impl ProviderAgent {
let network = args.node.account.network.clone();
let net_color = match network {
NetworkName::Mainnet => yansi::Color::Magenta,
NetworkName::Polygon => yansi::Color::Magenta,
NetworkName::Rinkeby => yansi::Color::Cyan,
NetworkName::Mumbai => yansi::Color::Cyan,
_ => yansi::Color::Red,
};
log::info!("Using payment network: {}", net_color.paint(&network));
Expand Down
1 change: 1 addition & 0 deletions golem_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ serde_json = "1.0"
strip-ansi-escapes = "0.1"
structopt = "0.3"
strum = "0.20.0"
strum_macros = "0.20.0"
tokio = { version = "0.2", features = ["process", "rt-core", "signal", "time", "io-util", "io-std"] }
url = "2.1"

Expand Down
13 changes: 6 additions & 7 deletions golem_cli/src/command/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use tokio::process::{Child, Command};
use ya_core_model::payment::local::NetworkName;
pub use ya_provider::GlobalsState as ProviderConfig;

use crate::command::NETWORK_GROUP_MAP;
use crate::setup::RunConfig;

const CLASSIC_RUNTIMES: &'static [&'static str] = &["wasm", "vw"];
Expand Down Expand Up @@ -268,14 +269,12 @@ impl YaProviderCommand {
}

pub async fn spawn(mut self, app_key: &str, run_cfg: &RunConfig) -> anyhow::Result<Child> {
self.cmd
.args(&[
"run",
"--payment-network",
&run_cfg.account.network.to_string(),
])
.env("YAGNA_APPKEY", app_key);
self.cmd.args(&["run"]).env("YAGNA_APPKEY", app_key);

for nn in NETWORK_GROUP_MAP[&run_cfg.account.network].iter() {
self.cmd.arg("--payment-network").arg(nn.to_string());
break; // ya-provider doesn't support many payment networks yet
}
if let Some(node_name) = &run_cfg.node_name {
self.cmd.arg("--node-name").arg(node_name);
}
Expand Down
40 changes: 39 additions & 1 deletion golem_cli/src/command/yagna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use chrono::{DateTime, Utc};
use futures::prelude::*;
use lazy_static::lazy_static;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::process::Stdio;
use strum_macros::{Display, EnumString, EnumVariantNames, IntoStaticStr};

use crate::setup::RunConfig;
use tokio::process::{Child, Command};
Expand Down Expand Up @@ -100,6 +101,43 @@ impl PaymentDriver {
}
}

#[derive(
Clone,
Debug,
Deserialize,
Display,
EnumVariantNames,
EnumString,
Eq,
Hash,
IntoStaticStr,
PartialEq,
Serialize,
)]
pub enum NetworkGroup {
Mainnet,
Testnet,
}

lazy_static! {
pub static ref NETWORK_GROUP_MAP: HashMap<NetworkGroup, Vec<NetworkName>> = {
let mut ngm = HashMap::new();
ngm.insert(
NetworkGroup::Mainnet,
vec![NetworkName::Mainnet, NetworkName::Polygon],
);
ngm.insert(
NetworkGroup::Testnet,
vec![
NetworkName::Rinkeby,
NetworkName::Mumbai,
NetworkName::Goerli,
],
);
ngm
};
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Id {
Expand Down
24 changes: 13 additions & 11 deletions golem_cli/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::appkey;
use crate::command::{YaCommand, ERC20_DRIVER, ZKSYNC_DRIVER};
use crate::command::{YaCommand, ERC20_DRIVER, NETWORK_GROUP_MAP, ZKSYNC_DRIVER};
use crate::setup::RunConfig;
use crate::utils::payment_account;
use anyhow::{Context, Result};
Expand Down Expand Up @@ -118,16 +118,18 @@ pub async fn run(mut config: RunConfig) -> Result</*exit code*/ i32> {
let provider_config = cmd.ya_provider()?.get_config().await?;
let address =
payment_account(&cmd, &config.account.account.or(provider_config.account)).await?;
cmd.yagna()?
.payment_init(&address, &config.account.network, &ERC20_DRIVER)
.await?;
if let Err(e) = cmd
.yagna()?
.payment_init(&address, &config.account.network, &ZKSYNC_DRIVER)
.await
{
log::debug!("Failed to initialize zkSync driver. e:{}", e);
};
for nn in NETWORK_GROUP_MAP[&config.account.network].iter() {
cmd.yagna()?
.payment_init(&address, &nn, &ERC20_DRIVER)
.await?;
if let Err(e) = cmd
.yagna()?
.payment_init(&address, &nn, &ZKSYNC_DRIVER)
.await
{
log::debug!("Failed to initialize zkSync driver. e:{}", e);
};
}

let provider = cmd.ya_provider()?.spawn(&app_key, &config).await?;
let ctrl_c = tokio::signal::ctrl_c();
Expand Down
23 changes: 17 additions & 6 deletions golem_cli/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@ use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::PathBuf;
use structopt::clap;
use structopt::StructOpt;
use strum::VariantNames;

use structopt::clap;
use ya_core_model::NodeId;
use ya_provider::ReceiverAccount;

use crate::command::UsageDef;
use crate::command::{NetworkGroup, NETWORK_GROUP_MAP};
use crate::terminal::clear_stdin;

const OLD_DEFAULT_SUBNETS: &[&'static str] = &["community", "community.3", "community.4"];
const DEFAULT_SUBNET: &str = "public-beta";

#[derive(StructOpt, Clone, Debug, Deserialize, Serialize)]
pub struct ConfigAccount {
/// Account for payments.
#[structopt(long, env = "YA_ACCOUNT")]
pub account: Option<NodeId>,
/// Payment network.
#[structopt(long = "payment-network", env = "YA_PAYMENT_NETWORK", possible_values = NetworkGroup::VARIANTS, default_value = NetworkGroup::Mainnet.into())]
pub network: NetworkGroup,
}

#[derive(StructOpt, Debug, Clone, Serialize, Deserialize)]
pub struct RunConfig {
#[structopt(env = "NODE_NAME", hidden = true)]
Expand All @@ -24,7 +35,7 @@ pub struct RunConfig {
pub subnet: Option<String>,

#[structopt(flatten)]
pub account: ReceiverAccount,
pub account: ConfigAccount,

/// changes log level from info to debug
#[structopt(long)]
Expand Down Expand Up @@ -135,9 +146,9 @@ pub async fn setup(run_config: &RunConfig, force: bool) -> Result<i32> {

config.node_name = Some(node_name);
config.subnet = Some(subnet);
cmd.ya_provider()?
.set_config(&config, &run_config.account.network)
.await?;
for nn in NETWORK_GROUP_MAP[&run_config.account.network].iter() {
cmd.ya_provider()?.set_config(&config, &nn).await?;
}
}

let is_configured = {
Expand Down
91 changes: 57 additions & 34 deletions golem_cli/src/status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::collections::BTreeMap;

use ansi_term::{Colour, Style};
use anyhow::{anyhow, Result};
use bigdecimal::BigDecimal;
use futures::prelude::*;
use prettytable::{cell, format, row, Table};
use strum::VariantNames;
Expand All @@ -8,37 +11,58 @@ use ya_core_model::payment::local::{NetworkName, StatusResult};
use ya_core_model::NodeId;

use crate::appkey;
use crate::command::{PaymentSummary, YaCommand, ERC20_DRIVER, ZKSYNC_DRIVER};
use crate::command::{
NetworkGroup, PaymentSummary, YaCommand, ERC20_DRIVER, NETWORK_GROUP_MAP, ZKSYNC_DRIVER,
};
use crate::platform::Status as KvmStatus;
use crate::utils::{is_yagna_running, payment_account};

async fn payment_status(
cmd: &YaCommand,
network: &NetworkName,
account: &Option<NodeId>,
) -> anyhow::Result<(StatusResult, StatusResult)> {
) -> anyhow::Result<BTreeMap<String, StatusResult>> {
let address = payment_account(&cmd, account).await?;

let (status_zk, status_erc20) = future::join(
cmd.yagna()?
.payment_status(&address, network, &ZKSYNC_DRIVER),
cmd.yagna()?
.payment_status(&address, network, &ERC20_DRIVER),
)
.await;

match (status_zk, status_erc20) {
(Ok(zk), Ok(eth)) => Ok((zk, eth)),
(Ok(zk), Err(e)) => {
log::warn!("yagna payment status for ERC-20 {} failed: {}", network, e);
Ok((zk, StatusResult::default()))
let network_group = {
if NETWORK_GROUP_MAP[&NetworkGroup::Mainnet].contains(network) {
NetworkGroup::Mainnet
} else {
NetworkGroup::Testnet
}
(Err(e), Ok(erc20)) => {
log::debug!("yagna payment status for zkSync {} failed: {}", network, e);
Ok((StatusResult::default(), erc20))
};

let mut result = BTreeMap::new();
let (futures, labels) = {
let mut f = vec![];
let mut l = vec![];
for nn in NETWORK_GROUP_MAP[&network_group].iter() {
if let Ok(_) = ZKSYNC_DRIVER.platform(&nn) {
l.push("zksync".to_string());
f.push(cmd.yagna()?.payment_status(&address, nn, &ZKSYNC_DRIVER));
}
if nn == &NetworkName::Mainnet {
l.push("on-chain".to_string());
} else {
l.push(nn.to_string().to_lowercase());
};
f.push(cmd.yagna()?.payment_status(&address, nn, &ERC20_DRIVER));
}
(_, Err(e)) => Err(e),
(f, l)
};
let fr = future::join_all(futures).await;
let mut n = 0;
for r in fr {
result.insert(
labels[n].clone(),
r.unwrap_or_else(|e| {
log::warn!("yagna payment status for {} failed: {}", labels[n], e);
StatusResult::default()
}),
);
n += 1;
}
Ok(result)
}

pub async fn run() -> Result</*exit code*/ i32> {
Expand Down Expand Up @@ -111,13 +135,13 @@ pub async fn run() -> Result</*exit code*/ i32> {
let payments = {
let (id, invoice_status) =
future::try_join(cmd.yagna()?.default_id(), cmd.yagna()?.invoice_status()).await?;
let (zk_payment_status, erc20_payment_status) =
payment_status(&cmd, &network, &config.account).await?;
let payment_statuses = payment_status(&cmd, &network, &config.account).await?;

let token = match zk_payment_status.token.len() {
0 => erc20_payment_status.token,
_ => zk_payment_status.token,
};
let token = &payment_statuses
.values()
.cloned()
.collect::<Vec<StatusResult>>()[0]
.token;

let mut table = Table::new();
let format = format::FormatBuilder::new().padding(1, 1).build();
Expand All @@ -140,19 +164,18 @@ pub async fn run() -> Result</*exit code*/ i32> {
"network",
Style::new().fg(net_color).paint(network.to_string())
]);
let total_amount = &zk_payment_status.amount + &erc20_payment_status.amount;
let total_amount: BigDecimal =
payment_statuses.values().cloned().map(|ps| ps.amount).sum();
table.add_row(row![
"amount (total)",
format!("{} {}", total_amount, token)
]);
table.add_row(row![
" (on-chain)",
format!("{} {}", &erc20_payment_status.amount, token)
]);
table.add_row(row![
" (zk-sync)",
format!("{} {}", &zk_payment_status.amount, token)
]);
for (label, status) in payment_statuses {
table.add_row(row![
format!(" ({})", label),
format!("{} {}", status.amount, token)
]);
}
table.add_empty_row();
{
let (pending, pending_cnt) = invoice_status.provider.total_pending();
Expand Down

0 comments on commit 5bc9c78

Please sign in to comment.