Skip to content

Commit

Permalink
Merge pull request #1736 from golemfactory/0.9_poriver_mn_net
Browse files Browse the repository at this point in the history
[ya-provider] Allow multiple payment networks
  • Loading branch information
nieznanysprawiciel authored Nov 30, 2021
2 parents 1a480f4 + 48bbdc7 commit 89a2e19
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 31 deletions.
42 changes: 22 additions & 20 deletions agent/provider/src/provider_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct ProviderAgent {
hardware: hardware::Manager,
accounts: Vec<AccountView>,
log_handler: LoggerHandle,
network: NetworkName,
networks: Vec<NetworkName>,
}

impl ProviderAgent {
Expand Down Expand Up @@ -128,15 +128,17 @@ impl ProviderAgent {
args.runner.session_id = args.market.session_id.clone();
args.payment.session_id = args.market.session_id.clone();

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));
let networks = args.node.account.networks.clone();
for n in networks.iter() {
let net_color = match n {
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(&n));
}
let mut globals = GlobalsManager::try_new(&config.globals_file, args.node)?;
globals.spawn_monitor(&config.globals_file)?;
let mut presets = PresetManager::load_or_create(&config.presets_file)?;
Expand All @@ -159,7 +161,7 @@ impl ProviderAgent {
hardware,
accounts,
log_handler,
network,
networks,
})
}

Expand Down Expand Up @@ -249,18 +251,18 @@ impl ProviderAgent {
}
}

fn accounts(&self, network: &NetworkName) -> anyhow::Result<Vec<AccountView>> {
fn accounts(&self, networks: &Vec<NetworkName>) -> anyhow::Result<Vec<AccountView>> {
let globals = self.globals.get_state();
if let Some(address) = &globals.account {
log::info!(
"Filtering payment accounts by address={} and network={}",
"Filtering payment accounts by address={} and networks={:?}",
address,
network
networks,
);
let accounts: Vec<AccountView> = self
.accounts
.iter()
.filter(|acc| &acc.address == address && &acc.network == network)
.filter(|acc| &acc.address == address && networks.contains(&acc.network))
.cloned()
.collect();

Expand All @@ -270,21 +272,21 @@ impl ProviderAgent {
\t`yagna payment init --receiver --network {} --account {}`\n\
for all drivers you want to use.",
address,
network,
networks[0],
address,
)
}

Ok(accounts)
} else {
log::debug!("Filtering payment accounts by network={}", network);
log::debug!("Filtering payment accounts by networks={:?}", networks);
let accounts: Vec<AccountView> = self
.accounts
.iter()
// FIXME: this is dirty fix -- we can get more that one address from this filter
// FIXME: use /me endpoint and filter out only accounts bound to given app-key
// FIXME: or introduce param to getProviderAccounts to filter out external account above
.filter(|acc| &acc.network == network)
.filter(|acc| networks.contains(&acc.network))
.cloned()
.collect();

Expand All @@ -293,7 +295,7 @@ impl ProviderAgent {
"Default payment account not initialized. Please run\n\
\t`yagna payment init --receiver --network {}`\n\
for all drivers you want to use.",
network,
networks[0],
)
}

Expand Down Expand Up @@ -471,7 +473,7 @@ impl Handler<CreateOffers> for ProviderAgent {
let runner = self.runner.clone();
let market = self.market.clone();
let node_info = self.create_node_info();
let accounts = match self.accounts(&self.network) {
let accounts = match self.accounts(&self.networks) {
Ok(acc) => acc,
Err(e) => return future::err(e).boxed_local(),
};
Expand Down
6 changes: 3 additions & 3 deletions agent/provider/src/startup_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ impl ProviderConfig {

#[derive(StructOpt, Clone, Debug, Serialize, Deserialize, derive_more::Display)]
#[display(
fmt = "{}Network: {}",
fmt = "{}Networks: {:?}",
"account.map(|a| format!(\"Address: {}\n\", a)).unwrap_or(\"\".into())",
network
networks
)]
pub struct ReceiverAccount {
/// Account for payments.
#[structopt(long, env = "YA_ACCOUNT")]
pub account: Option<NodeId>,
/// Payment network.
#[structopt(long = "payment-network", env = "YA_PAYMENT_NETWORK", possible_values = NetworkName::VARIANTS, default_value = NetworkName::Mainnet.into())]
pub network: NetworkName,
pub networks: Vec<NetworkName>,
}

#[derive(StructOpt, Clone, Debug)]
Expand Down
4 changes: 4 additions & 0 deletions golem_cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl YaCommand {
cmd.env("EXE_UNIT_PATH", plugins_dir.join("ya-*.json"));
}
}
// YA_PAYMENT_NETWORK is used in different context in golemsp
// and in ya-provider. golemsp always passes commandline
// --payment-network arg, so it's safe to just remove it here.
cmd.env_remove("YA_PAYMENT_NETWORK");

Ok(YaProviderCommand { cmd })
}
Expand Down
7 changes: 4 additions & 3 deletions golem_cli/src/command/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl YaProviderCommand {
pub async fn set_config(
self,
config: &ProviderConfig,
network: &NetworkName,
networks: &Vec<NetworkName>,
) -> anyhow::Result<()> {
let mut cmd = self.cmd;

Expand All @@ -66,7 +66,9 @@ impl YaProviderCommand {
if let Some(account) = &config.account {
cmd.args(&["--account", &account.to_string()]);
}
cmd.args(&["--payment-network", &network.to_string()]);
for n in networks.iter() {
cmd.args(&["--payment-network", &n.to_string()]);
}

log::debug!("executing: {:?}", cmd);

Expand Down Expand Up @@ -273,7 +275,6 @@ impl YaProviderCommand {

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
2 changes: 2 additions & 0 deletions golem_cli/src/command/yagna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ impl PaymentDriver {
PartialEq,
Serialize,
)]
#[strum(serialize_all = "lowercase")]
#[serde(rename_all = "lowercase")]
pub enum NetworkGroup {
Mainnet,
Testnet,
Expand Down
4 changes: 2 additions & 2 deletions golem_cli/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub async fn run(settings: Settings) -> Result</*exit code*/ i32> {
node_name: settings.node_name,
..ProviderConfig::default()
},
&settings.account.network,
&settings.account.networks,
)
.await?;
}
Expand All @@ -62,7 +62,7 @@ pub async fn run(settings: Settings) -> Result</*exit code*/ i32> {
account: settings.account.account,
..ProviderConfig::default()
},
&settings.account.network,
&settings.account.networks,
)
.await?;
}
Expand Down
6 changes: 3 additions & 3 deletions golem_cli/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ pub async fn setup(run_config: &RunConfig, force: bool) -> Result<i32> {

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

let is_configured = {
Expand Down

0 comments on commit 89a2e19

Please sign in to comment.