Skip to content
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

getting default configuration from rpc-node.dev.golem.network #2678

Merged
merged 5 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 8 additions & 2 deletions .env-template
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ YAGNA_DATADIR="."
#ACCOUNT_LIST="${YAGNA_DATADIR}/accounts.json"
#PAYMENT_SHUTDOWN_TIMEOUT_SECS=10

## All drivers
#RINKEBY_GETH_ADDR=http://1.geth.testnet.golem.network:55555
### All drivers

## Set list of Ethereum nodes used by payment driver for specified networks.
## By default payment driver will query nodes addresses from DNS record `{network_name}.rpc-node.dev.golem.network`.
## Setting any of these variables will disable DNS lookup mechanism and use custom list of nodes instead for chosen network.
#MAINNET_GETH_ADDR=https://geth.golem.network:55555
#GOERLI_GETH_ADDR=https://rpc.ankr.com/eth_goerli
#POLYGON_GETH_ADDR=https://bor.golem.network,https://polygon-rpc.com
#MUMBAI_GETH_ADDR=https://matic-mumbai.chainstacklabs.com

## ERC20 driver.
#ETH_FAUCET_ADDRESS=http://faucet.testnet.golem.network:4000/donate
Expand Down
71 changes: 9 additions & 62 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ members = [
"test-utils/test-framework/framework-macro"
]

[workspace.dependencies]
rand = "0.8.5"
url = "2.3.1"
trust-dns-resolver = "0.22"

[patch.crates-io]
## SERVICES
ya-identity = { path = "core/identity" }
Expand Down
14 changes: 13 additions & 1 deletion core/payment-driver/base/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,19 @@ pub struct PaymentEntity {
pub network: Network,
}

#[derive(AsExpression, FromSqlRow, PartialEq, Eq, Debug, Clone, Copy, FromPrimitive, Default)]
#[derive(
AsExpression,
FromSqlRow,
PartialEq,
Eq,
PartialOrd,
Ord,
Debug,
Clone,
Copy,
FromPrimitive,
Default,
)]
#[sql_type = "Integer"]
pub enum Network {
Mainnet = 1, //Main Ethereum chain
Expand Down
6 changes: 5 additions & 1 deletion core/payment-driver/erc20/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

[features]
default = []
integration = []

[dependencies]
async-trait = "0.1"
Expand Down Expand Up @@ -33,7 +34,9 @@ tiny-keccak = { version = "2.0", features = ["keccak"] }
tokio = { version = "1", features = ["full"] }
uuid = { version = "0.8", features = ["v4"] }
web3 = { version = "0.16", default-features = false, features = [ "http-tls", "signing", "ws-tls-tokio" ] }

rand = { workspace = true }
url = { workspace=true }
trust-dns-resolver = { workspace=true, default-features = false }
## yagna dependencies
ya-payment-driver = "0.3"
ya-client-model = "0.5"
Expand All @@ -46,3 +49,4 @@ actix-rt = "2.7"
dotenv = "0.15.0"
env_logger = "0.7.1"
structopt = "0.3"
test-case = "3.1.0"
71 changes: 8 additions & 63 deletions core/payment-driver/erc20/src/erc20/ethereum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![allow(clippy::too_many_arguments)]

use futures::prelude::*;
use std::collections::HashMap;
use std::pin::pin;
use std::sync::Arc;

use bigdecimal::BigDecimal;
Expand Down Expand Up @@ -201,10 +203,14 @@ where
F: FnMut(Web3<Http>) -> R,
R: futures::Future<Output = Result<T, ClientError>>,
{
let clients = get_clients(network).await?;
lazy_static! {
static ref RESOLVER: super::rpc_resolv::RpcResolver = super::rpc_resolv::RpcResolver::new();
};

let mut clients = pin!(RESOLVER.clients_for(network).await);
let mut last_err: Option<ClientError> = None;

for client in clients {
while let Some(client) = clients.next().await {
match f(client).await {
Ok(result) => return Ok(result),
Err(ClientError::Web3(e)) => match e {
Expand Down Expand Up @@ -526,67 +532,6 @@ async fn get_tx_receipt_with(
.map_err(Into::into)
}

fn get_rpc_addr_from_env(network: Network) -> Vec<String> {
match network {
Network::Mainnet => {
collect_rpc_addr_from("MAINNET_GETH_ADDR", "https://geth.golem.network:55555")
}
Network::Rinkeby => collect_rpc_addr_from(
"RINKEBY_GETH_ADDR",
"http://geth.testnet.golem.network:55555",
),
Network::Goerli => {
collect_rpc_addr_from("GOERLI_GETH_ADDR", "https://rpc.ankr.com/eth_goerli")
}
Network::Polygon => collect_rpc_addr_from(
"POLYGON_GETH_ADDR",
"https://bor.golem.network,https://polygon-rpc.com",
),
Network::Mumbai => collect_rpc_addr_from(
"MUMBAI_GETH_ADDR",
"https://matic-mumbai.chainstacklabs.com",
),
}
}

fn collect_rpc_addr_from(env: &str, default: &str) -> Vec<String> {
std::env::var(env)
.ok()
.unwrap_or_else(|| default.to_string())
.split(',')
.map(|path| path.to_string())
.collect()
}

async fn get_clients(network: Network) -> Result<Vec<Web3<Http>>, GenericError> {
let geth_addrs = get_rpc_addr_from_env(network);
let mut clients: Vec<Web3<Http>> = Default::default();

for geth_addr in geth_addrs {
{
let client_map = WEB3_CLIENT_MAP.read().await;
if let Some(client) = client_map.get(&geth_addr).cloned() {
clients.push(client);
continue;
}
}

let transport = match web3::transports::Http::new(&geth_addr) {
Ok(t) => t,
Err(_) => continue,
};

let client = Web3::new(transport);

let mut client_map = WEB3_CLIENT_MAP.write().await;
client_map.insert(geth_addr, client.clone());

clients.push(client);
}

Ok(clients)
}

fn get_env(network: Network) -> config::EnvConfiguration {
match network {
Network::Mainnet => *config::MAINNET_CONFIG,
Expand Down
1 change: 1 addition & 0 deletions core/payment-driver/erc20/src/erc20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ pub mod wallet;
mod config;
pub mod eth_utils;
mod gasless_transfer;
mod rpc_resolv;
pub mod transaction;
Loading
Loading