Skip to content

Commit

Permalink
Payments: Mainnet/testnet preparation
Browse files Browse the repository at this point in the history
Introduced changes to the payment service and payment driver API
necessary for supporting both mainnet and testnet in a single Yagna
daemon process. This changes require drivers to be network-aware.

Signed-off-by: Adam Wierzbicki <awierzbicki@golem.network>
  • Loading branch information
Wiezzel committed Jan 13, 2021
1 parent ba0418f commit 9ed8557
Show file tree
Hide file tree
Showing 28 changed files with 324 additions and 141 deletions.
23 changes: 16 additions & 7 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ ya-sb-router = { path = "service-bus/router" }
ya-sb-util = { path = "service-bus/util" }

## CLIENT
ya-client = { git = "https://github.com/golemfactory/ya-client.git", rev = "87f106498a7a7b1519d82d0981fc21b260a89cf2"}
ya-client-model = { git = "https://github.com/golemfactory/ya-client.git", rev = "87f106498a7a7b1519d82d0981fc21b260a89cf2"}
ya-client = { git = "https://github.com/golemfactory/ya-client.git", rev = "94f4cc5844477374f97dacfa22a53a15f3a59ada"}
ya-client-model = { git = "https://github.com/golemfactory/ya-client.git", rev = "94f4cc5844477374f97dacfa22a53a15f3a59ada"}

#ya-client = { path = "../ya-client" }
#ya-client-model = { path = "../ya-client/model" }
Expand Down
2 changes: 1 addition & 1 deletion agent/provider/src/payments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ mod pricing;

pub use factory::PaymentModelFactory;
pub use payments::Payments;
pub use pricing::{LinearPricing, LinearPricingOffer, PricingOffer};
pub use pricing::{AccountView, LinearPricing, LinearPricingOffer, PricingOffer};
19 changes: 17 additions & 2 deletions agent/provider/src/payments/pricing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ use ya_client_model::payment::Account;
use super::model::{PaymentDescription, PaymentModel};
use crate::market::presets::{Coefficient, Preset};

#[derive(Clone, Debug)]
pub struct AccountView {
pub address: String,
pub platform: String,
}

impl From<Account> for AccountView {
fn from(account: Account) -> Self {
Self {
address: account.address,
platform: account.platform,
}
}
}

pub trait PricingOffer {
fn prices(&self, preset: &Preset) -> Vec<(Coefficient, f64)>;
fn build(
&self,
accounts: &Vec<Account>,
accounts: &Vec<AccountView>,
initial_price: f64,
prices: Vec<(String, f64)>,
) -> Result<ComInfo>;
Expand Down Expand Up @@ -89,7 +104,7 @@ impl PricingOffer for LinearPricingOffer {

fn build(
&self,
accounts: &Vec<Account>,
accounts: &Vec<AccountView>,
initial_price: f64,
prices: Vec<(String, f64)>,
) -> Result<ComInfo> {
Expand Down
23 changes: 14 additions & 9 deletions agent/provider/src/provider_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::execution::{
use crate::hardware;
use crate::market::provider_market::{OfferKind, Shutdown as MarketShutdown, Unsubscribe};
use crate::market::{CreateOffer, Preset, PresetManager, ProviderMarket};
use crate::payments::{LinearPricingOffer, Payments, PricingOffer};
use crate::payments::{AccountView, LinearPricingOffer, Payments, PricingOffer};
use crate::startup_config::{FileMonitor, NodeConfig, ProviderConfig, RecvAccount, RunConfig};
use crate::tasks::task_manager::{InitializeTaskManager, TaskManager};

Expand All @@ -24,7 +24,6 @@ use std::{fs, io};
use ya_agreement_utils::agreement::TypedArrayPointer;
use ya_agreement_utils::*;
use ya_client::cli::ProviderApi;
use ya_client_model::payment::Account;
use ya_file_logging::{start_logger, ReconfigurationHandle};
use ya_utils_actix::actix_handler::send_message;
use ya_utils_path::SwapSave;
Expand All @@ -36,7 +35,7 @@ pub struct ProviderAgent {
task_manager: Addr<TaskManager>,
presets: PresetManager,
hardware: hardware::Manager,
accounts: Vec<Account>,
accounts: Vec<AccountView>,
log_handler: ReconfigurationHandle,
}

Expand Down Expand Up @@ -146,7 +145,13 @@ impl ProviderAgent {
let api = ProviderApi::try_from(&args.api)?;

log::info!("Loading payment accounts...");
let accounts: Vec<Account> = api.payment.get_provider_accounts().await?;
let accounts: Vec<AccountView> = api
.payment
.get_provider_accounts()
.await?
.into_iter()
.map(Into::into)
.collect();
log::info!("Payment accounts: {:#?}", accounts);
let registry = config.registry()?;
registry.validate()?;
Expand Down Expand Up @@ -187,7 +192,7 @@ impl ProviderAgent {
inf_node_info: InfNodeInfo,
runner: Addr<TaskRunner>,
market: Addr<ProviderMarket>,
accounts: Vec<Account>,
accounts: Vec<AccountView>,
) -> anyhow::Result<()> {
if presets.is_empty() {
return Err(anyhow!("No Presets were selected. Can't create offers."));
Expand Down Expand Up @@ -268,19 +273,19 @@ impl ProviderAgent {
node_info
}

fn accounts(&self) -> Vec<Account> {
fn accounts(&self) -> Vec<AccountView> {
let globals = self.globals.get_state();
if let Some(account) = &globals.account {
let mut accounts = Vec::new();
if account.platform.is_some() {
let zkaddr = Account {
let zkaddr = AccountView {
platform: account.platform.clone().unwrap(),
address: account.address.to_lowercase(),
};
accounts.push(zkaddr);
} else {
for &platform in &["NGNT", "ZK-NGNT"] {
accounts.push(Account {
for &platform in &["erc20-rinkeby-tglm", "zksync-rinkeby-tglm"] {
accounts.push(AccountView {
platform: platform.to_string(),
address: account.address.to_lowercase(),
})
Expand Down
4 changes: 2 additions & 2 deletions agent/provider/src/startup_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl FromStr for RecvAccount {
(Some(driver), Some(addr), None) => {
let platform = Some(
match driver {
"zksync" | "zk" => "ZK-NGNT",
"eth" | "l1" => "NGTN",
"zksync" | "zk" => "zksync-rinkeby-tglm",
"eth" | "l1" => "erc20-rinkeby-tglm",
_ => anyhow::bail!("unknown driver: {}", driver),
}
.to_string(),
Expand Down
16 changes: 14 additions & 2 deletions core/model/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,24 @@ impl RpcMessage for VerifyPayment {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Init {
address: String,
network: Option<String>,
token: Option<String>,
mode: AccountMode,
}

impl Init {
pub fn new(address: String, mode: AccountMode) -> Init {
Init { address, mode }
pub fn new(
address: String,
network: Option<String>,
token: Option<String>,
mode: AccountMode,
) -> Init {
Init {
address,
network,
token,
mode,
}
}
pub fn address(&self) -> String {
self.address.clone()
Expand Down
44 changes: 31 additions & 13 deletions core/model/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod local {
use crate::driver::{AccountMode, PaymentConfirmation};
use bigdecimal::{BigDecimal, Zero};
use chrono::{DateTime, Utc};
use std::collections::HashMap;
use std::fmt::Display;
use ya_client_model::NodeId;

Expand Down Expand Up @@ -129,17 +130,37 @@ pub mod local {
#[error("")]
pub struct NoError {} // This is needed because () doesn't implement Display

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Network {
pub default_token: String,
pub tokens: HashMap<String, String>, // token -> platform
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DriverDetails {
pub default_network: String,
pub networks: HashMap<String, Network>,
pub recv_init_required: bool, // Is account initialization required for receiving payments
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RegisterDriver {
pub driver_name: String,
pub platform: String,
pub recv_init_required: bool, // Is account initialization required for receiving payments
pub details: DriverDetails,
}

#[derive(Clone, Debug, Serialize, Deserialize, thiserror::Error)]
pub enum RegisterDriverError {
#[error("Invalid default token specified: token={0}, network={1}")]
InvalidDefaultToken(String, String),
#[error("Invalid default network specified: {0}")]
InvalidDefaultNetwork(String),
}

impl RpcMessage for RegisterDriver {
const ID: &'static str = "RegisterDriver";
type Item = ();
type Error = NoError;
type Error = RegisterDriverError;
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -153,9 +174,10 @@ pub mod local {

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RegisterAccount {
pub platform: String,
pub address: String,
pub driver: String,
pub network: String,
pub token: String,
pub mode: AccountMode,
}

Expand All @@ -165,6 +187,10 @@ pub mod local {
AlreadyRegistered(String, String),
#[error("Driver not registered: {0}")]
DriverNotRegistered(String),
#[error("Network not supported by driver: network={0}, driver={1}")]
UnsupportedNetwork(String, String),
#[error("Token not supported by driver: token={0}, network={1}, driver={2}")]
UnsupportedToken(String, String, String),
#[error("Error while registering account: {0}")]
Other(String),
}
Expand All @@ -190,6 +216,7 @@ pub mod local {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NotifyPayment {
pub driver: String,
pub platform: String,
pub amount: BigDecimal,
pub sender: String,
pub recipient: String,
Expand Down Expand Up @@ -287,15 +314,6 @@ pub mod local {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GetAccounts {}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Account {
pub platform: String,
pub address: String,
pub driver: String,
pub send: bool,
pub receive: bool,
}

impl RpcMessage for GetAccounts {
const ID: &'static str = "GetAccounts";
type Item = Vec<Account>;
Expand Down
Loading

0 comments on commit 9ed8557

Please sign in to comment.