Skip to content

Commit

Permalink
fix chain
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-aranha-cw committed Jul 25, 2024
1 parent eeea4cc commit cf5eec2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
56 changes: 30 additions & 26 deletions src/infra/blockchain_client/blockchain_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,48 @@ use crate::log_and_err;
#[derive(Debug)]
pub struct BlockchainClient {
http: HttpClient,
pub http_url: String,
pub http_url: Option<String>,
ws: Option<RwLock<WsClient>>,
ws_url: Option<String>,
timeout: Duration,
}

impl BlockchainClient {
/// Creates a new RPC client connected only to HTTP.
pub async fn new_http(http_url: &str, timeout: Duration) -> anyhow::Result<Self> {
pub async fn new_http(http_url: Option<&str>, timeout: Duration) -> anyhow::Result<Self> {
Self::new_http_ws(http_url, None, timeout).await
}

/// Creates a new RPC client connected to HTTP and optionally to WS.
pub async fn new_http_ws(http_url: &str, ws_url: Option<&str>, timeout: Duration) -> anyhow::Result<Self> {
tracing::info!(%http_url, "creating blockchain client");

// build http provider
let http = Self::build_http_client(http_url, timeout)?;

// build ws provider
let ws = if let Some(ws_url) = ws_url {
Some(RwLock::new(Self::build_ws_client(ws_url, timeout).await?))
pub async fn new_http_ws(http_url: Option<&str>, ws_url: Option<&str>, timeout: Duration) -> anyhow::Result<Self> {
if let Some(http_url) = http_url {
tracing::info!(%http_url, "creating blockchain client");

// build http provider
let http = Self::build_http_client(http_url, timeout)?;

// build ws provider
let ws = if let Some(ws_url) = ws_url {
Some(RwLock::new(Self::build_ws_client(ws_url, timeout).await?))
} else {
None
};

let client = Self {
http,
http_url: Some(http_url.to_owned()),
ws,
ws_url: ws_url.map(|x| x.to_owned()),
timeout,
};

// check health before assuming it is ok
client.fetch_listening().await?;

Ok(client)
} else {
None
};

let client = Self {
http,
http_url: http_url.to_owned(),
ws,
ws_url: ws_url.map(|x| x.to_owned()),
timeout,
};

// check health before assuming it is ok
client.fetch_listening().await?;

Ok(client)
log_and_err!("HTTP URL must be provided")
}
}

fn build_http_client(url: &str, timeout: Duration) -> anyhow::Result<HttpClient> {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async fn run(config: StratusConfig) -> anyhow::Result<()> {
let (http_url, ws_url) = consensus.get_chain_url().await.expect("chain url not found");

Some(Arc::new(
BlockchainClient::new_http_ws(&http_url, ws_url.as_deref(), config.importer.external_rpc_timeout).await?,
BlockchainClient::new_http_ws(http_url, ws_url.as_deref(), config.importer.external_rpc_timeout).await?,
))
}
_ => None,
Expand Down

0 comments on commit cf5eec2

Please sign in to comment.