58 lines
1.8 KiB
Rust
Raw Normal View History

2026-01-08 09:10:00 +02:00
use anyhow::Result;
2026-01-27 08:13:53 +02:00
use futures::{Stream, TryFutureExt};
use log::warn;
use logos_blockchain_chain_broadcast_service::BlockInfo;
pub use logos_blockchain_common_http_client::{BasicAuthCredentials, CommonHttpClient, Error};
2026-01-27 08:13:53 +02:00
use logos_blockchain_core::{block::Block, header::HeaderId, mantle::SignedMantleTx};
2026-01-21 11:47:28 -03:00
use reqwest::{Client, Url};
2026-01-27 08:13:53 +02:00
use tokio_retry::Retry;
2026-01-07 16:29:37 +02:00
2026-01-08 09:10:00 +02:00
// Simple wrapper
// maybe extend in the future for our purposes
2026-01-21 11:47:28 -03:00
pub struct BedrockClient {
http_client: CommonHttpClient,
node_url: Url,
}
2026-01-07 16:29:37 +02:00
2026-01-08 09:10:00 +02:00
impl BedrockClient {
2026-01-21 11:47:28 -03:00
pub fn new(auth: Option<BasicAuthCredentials>, node_url: Url) -> Result<Self> {
2026-01-08 09:10:00 +02:00
let client = Client::builder()
2026-01-20 10:26:22 +02:00
//Add more fields if needed
2026-01-08 09:10:00 +02:00
.timeout(std::time::Duration::from_secs(60))
.build()?;
2026-01-07 16:29:37 +02:00
2026-01-21 11:47:28 -03:00
let http_client = CommonHttpClient::new_with_client(client, auth);
Ok(Self {
http_client,
node_url,
})
}
pub async fn post_transaction(&self, tx: SignedMantleTx) -> Result<(), Error> {
self.http_client
.post_transaction(self.node_url.clone(), tx)
.await
2026-01-07 16:29:37 +02:00
}
2026-01-21 14:50:29 +02:00
2026-01-27 08:13:53 +02:00
pub async fn get_lib_stream(&self) -> Result<impl Stream<Item = BlockInfo>, Error> {
self.http_client.get_lib_stream(self.node_url.clone()).await
2026-01-21 14:50:29 +02:00
}
pub async fn get_block_by_id(
&self,
header_id: HeaderId,
start_delay_millis: u64,
max_retries: usize,
) -> Result<Option<Block<SignedMantleTx>>, Error> {
let strategy = tokio_retry::strategy::FibonacciBackoff::from_millis(start_delay_millis)
.take(max_retries);
Retry::spawn(strategy, || {
2026-01-27 08:13:53 +02:00
self.http_client
.get_block_by_id(self.node_url.clone(), header_id)
2026-01-21 14:50:29 +02:00
.inspect_err(|err| warn!("Block fetching failed with err: {err:#?}"))
})
.await
}
2026-01-07 16:29:37 +02:00
}