Skip to content

Get faucet url from envvar or resolve srv record #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ya-sgx = "0.1"
ya-utils-path = "0.1"
ya-utils-futures = "0.1"
ya-utils-process = { version = "0.1", features = ["lock"] }
ya-utils-networking = "0.1"
ya-version = "0.1"

gftp = { version = "0.1", optional = true } # just to enable gftp build for cargo-deb
Expand Down Expand Up @@ -132,6 +133,7 @@ members = [
"utils/agreement-utils",
"utils/compile-time-utils",
"utils/futures",
"utils/networking",
"utils/path",
"utils/process",
"utils/std-utils",
Expand Down Expand Up @@ -188,8 +190,9 @@ ya-exe-unit = { path = "exe-unit" }
ya-file-logging = { path = "utils/file-logging" }
ya-transfer = { path = "utils/transfer" }
ya-utils-actix = { path = "utils/actix_utils"}
ya-utils-futures = { path = "utils/futures"}
ya-utils-path = { path = "utils/path"}
ya-utils-futures = { path = "utils/futures" }
ya-utils-networking = { path = "utils/networking" }
ya-utils-path = { path = "utils/path" }
ya-utils-process = { path = "utils/process"}
ya-diesel-utils = { path = "utils/diesel-utils"}
ya-metrics = { path = "core/metrics" }
Expand Down
2 changes: 1 addition & 1 deletion core/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ya-core-model = { path = "../model", features=["net", "identity"] }
ya-service-api = "0.1"
ya-service-api-interfaces = "0.1"
ya-service-bus = "0.3"
ya-utils-networking = "0.1"

actix-rt = "1.0"
anyhow = "1.0"
Expand All @@ -22,7 +23,6 @@ log = "0.4"
serde_json = "1.0"
thiserror = "1.0"
tokio = { version = "0.2", features = ["time"] }
trust-dns-resolver = { version = "0.19", features = ["mdns"] }

[dev-dependencies]
ya-sb-proto = "0.1"
Expand Down
27 changes: 2 additions & 25 deletions core/net/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ use actix_rt::Arbiter;
use anyhow::anyhow;
use futures::channel::oneshot;
use futures::prelude::*;
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::net::{SocketAddr, ToSocketAddrs};
use std::rc::Rc;
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
use trust_dns_resolver::TokioAsyncResolver;

use ya_core_model::identity::{self, IdentityInfo};
use ya_core_model::net;
Expand All @@ -16,43 +13,23 @@ use ya_service_bus::connection::ClientInfo;
use ya_service_bus::{
connection, serialization, typed as bus, untyped as local_bus, Error, RpcEndpoint, RpcMessage,
};
use ya_utils_networking::srv_resolver;

use crate::api::{net_service, parse_from_addr};
use crate::handler::{auto_rebind, CentralBusHandler};

pub const CENTRAL_ADDR_ENV_VAR: &str = "CENTRAL_NET_HOST";
const DEFAULT_LOOKUP_DOMAIN: &'static str = "dev.golem.network";

async fn central_net_addr() -> std::io::Result<SocketAddr> {
Ok(match std::env::var(CENTRAL_ADDR_ENV_VAR) {
Ok(v) => v,
Err(_) => resolve_net_addr().await?,
Err(_) => srv_resolver::resolve_record("_net._tcp.").await?,
}
.to_socket_addrs()?
.next()
.expect("central net hub addr needed"))
}

async fn resolve_net_addr() -> std::io::Result<String> {
let resolver: TokioAsyncResolver =
TokioAsyncResolver::tokio(ResolverConfig::google(), ResolverOpts::default()).await?;
let lookup = resolver
.srv_lookup(format!("_net._tcp.{}", DEFAULT_LOOKUP_DOMAIN))
.await?;
let srv = lookup
.iter()
.next()
.ok_or_else(|| IoError::from(IoErrorKind::NotFound))?;
let addr = format!(
"{}:{}",
srv.target().to_string().trim_end_matches('.'),
srv.port()
);

log::debug!("Central net address: {}", addr);
Ok(addr)
}

/// Initialize net module on a hub.
pub async fn bind_remote(
client_info: ClientInfo,
Expand Down
1 change: 1 addition & 0 deletions core/payment-driver/zksync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ya-payment-driver = "0.1.0"
ya-client-model = "0.2.0"
ya-service-api-interfaces = "0.1"
ya-utils-futures = "0.1"
ya-utils-networking = "0.1"

[dev-dependencies]
actix-rt = "1.0"
Expand Down
18 changes: 14 additions & 4 deletions core/payment-driver/zksync/src/zksync/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ use tokio::time::delay_for;

// Workspace uses
use ya_payment_driver::{db::models::Network, model::GenericError};
use ya_utils_networking::srv_resolver;

// Local uses
use crate::zksync::wallet::account_balance;

const DEFAULT_FAUCET_ADDR: &str = "http://3.249.139.167:5778/zk/donatex";
const DEFAULT_FAUCET_SRV_PREFIX: &str = "_zk-faucet._tcp.";
const FAUCET_ADDR_ENVAR: &str = "ZKSYNC_FAUCET_ADDR";
const MAX_FAUCET_REQUESTS: u32 = 6;

lazy_static! {
static ref FAUCET_ADDR: String =
env::var("ZKSYNC_FAUCET_ADDR").unwrap_or(DEFAULT_FAUCET_ADDR.to_string());
static ref MIN_BALANCE: BigDecimal = BigDecimal::from(50);
static ref MAX_WAIT: Duration = Duration::minutes(1);
}
Expand Down Expand Up @@ -81,8 +81,9 @@ async fn wait_for_ngnt(address: &str, network: Network) -> Result<(), GenericErr

async fn faucet_donate(address: &str, _network: Network) -> Result<(), GenericError> {
let client = awc::Client::new();
let faucet_url = resolve_faucet_url().await?;
let response = client
.get(format!("{}/{}", *FAUCET_ADDR, address))
.get(format!("{}/{}", faucet_url, address))
.send()
.await
.map_err(GenericError::new)?
Expand All @@ -94,3 +95,12 @@ async fn faucet_donate(address: &str, _network: Network) -> Result<(), GenericEr
// TODO: Verify tx hash
Ok(())
}

async fn resolve_faucet_url() -> Result<String, GenericError> {
match env::var(FAUCET_ADDR_ENVAR) {
Ok(addr) => Ok(addr),
_ => srv_resolver::resolve_record(DEFAULT_FAUCET_SRV_PREFIX)
.await
.map_err(|_| GenericError::new("Faucet SRV record cannot be resolved")),
}
}
12 changes: 12 additions & 0 deletions utils/networking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "ya-utils-networking"
version = "0.1.0"
authors = ["Golem Factory <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "0.4"
trust-dns-resolver = { version = "0.19", features = ["mdns"] }

1 change: 1 addition & 0 deletions utils/networking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod srv_resolver;
29 changes: 29 additions & 0 deletions utils/networking/src/srv_resolver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
use trust_dns_resolver::TokioAsyncResolver;

const DEFAULT_LOOKUP_DOMAIN: &'static str = "dev.golem.network";

pub async fn resolve_record(record: &str) -> std::io::Result<String> {
let resolver: TokioAsyncResolver =
TokioAsyncResolver::tokio(ResolverConfig::google(), ResolverOpts::default()).await?;
let lookup = resolver
.srv_lookup(format!(
"{}.{}",
record.trim_end_matches('.'),
DEFAULT_LOOKUP_DOMAIN
))
.await?;
let srv = lookup
.iter()
.next()
.ok_or_else(|| IoError::from(IoErrorKind::NotFound))?;
let addr = format!(
"{}:{}",
srv.target().to_string().trim_end_matches('.'),
srv.port()
);

log::debug!("Central net address: {}", addr);
Ok(addr)
}