Skip to content

Commit ce5af79

Browse files
authored
refactor: use Url in the config (#136)
* refactor: use Url in the config * refactor: asyncronous memoizing :)
1 parent f885ad9 commit ce5af79

File tree

5 files changed

+24
-34
lines changed

5 files changed

+24
-34
lines changed

src/config.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,27 @@ pub struct BuilderConfig {
6666
/// URL for Host RPC node.
6767
#[from_env(
6868
var = "HOST_RPC_URL",
69-
desc = "URL for Host RPC node. This MUST be a valid HTTP or WS URL, starting with http://, https://, ws:// or wss://",
70-
infallible
69+
desc = "URL for Host RPC node. This MUST be a valid HTTP or WS URL, starting with http://, https://, ws:// or wss://"
7170
)]
72-
pub host_rpc_url: Cow<'static, str>,
71+
pub host_rpc_url: url::Url,
7372

7473
/// URL for the Rollup RPC node.
7574
#[from_env(
7675
var = "ROLLUP_RPC_URL",
77-
desc = "URL for Rollup RPC node. This MUST be a valid WS url starting with ws:// or wss://. Http providers are not supported.",
78-
infallible
76+
desc = "URL for Rollup RPC node. This MUST be a valid WS url starting with ws:// or wss://. Http providers are not supported."
7977
)]
80-
pub ru_rpc_url: Cow<'static, str>,
78+
pub ru_rpc_url: url::Url,
8179

8280
/// URL of the tx pool to poll for incoming transactions.
83-
#[from_env(
84-
var = "TX_POOL_URL",
85-
desc = "URL of the tx pool to poll for incoming transactions",
86-
infallible
87-
)]
88-
pub tx_pool_url: Cow<'static, str>,
81+
#[from_env(var = "TX_POOL_URL", desc = "URL of the tx pool to poll for incoming transactions")]
82+
pub tx_pool_url: url::Url,
8983

9084
/// Additional RPC URLs to which the builder should broadcast transactions.
9185
/// * Should not include the `HOST_RPC_URL` value, as that is already sent to by default.
9286
/// * Setting this can incur `already known` errors.
9387
#[from_env(
9488
var = "TX_BROADCAST_URLS",
9589
desc = "Additional RPC URLs to which the builder broadcasts transactions",
96-
infallible,
9790
optional
9891
)]
9992
pub tx_broadcast_urls: Vec<Cow<'static, str>>,
@@ -190,15 +183,13 @@ impl BuilderConfig {
190183
tokio::sync::OnceCell::const_new();
191184

192185
ONCE.get_or_try_init(|| async {
193-
let url = url::Url::parse(&self.ru_rpc_url)?;
194-
195-
let scheme = url.scheme();
186+
let scheme = self.ru_rpc_url.scheme();
196187
eyre::ensure!(
197188
scheme == "ws" || scheme == "wss",
198189
"Invalid Rollup RPC URL scheme: {scheme}. Expected ws:// or wss://"
199190
);
200191

201-
RootProvider::connect_with(BuiltInConnectionString::Ws(url, None))
192+
RootProvider::connect_with(BuiltInConnectionString::Ws(self.ru_rpc_url.clone(), None))
202193
.await
203194
.map_err(Into::into)
204195
})
@@ -216,7 +207,7 @@ impl BuilderConfig {
216207
.with_nonce_management(SimpleNonceManager::default())
217208
.fetch_chain_id()
218209
.wallet(EthereumWallet::from(builder_signer))
219-
.connect(&self.host_rpc_url)
210+
.connect(self.host_rpc_url.as_str())
220211
.await
221212
.map_err(Into::into)
222213
}
@@ -225,8 +216,8 @@ impl BuilderConfig {
225216
pub fn connect_additional_broadcast(&self) -> Vec<RootProvider> {
226217
self.tx_broadcast_urls
227218
.iter()
228-
.map(|url_str| {
229-
let url = url::Url::parse(url_str).expect("failed to parse URL");
219+
.map(|url| {
220+
let url = url.parse::<url::Url>().expect("Invalid URL in tx_broadcast_urls");
230221
RootProvider::new_http(url)
231222
})
232223
.collect::<Vec<_>>()

src/tasks/cache/bundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl BundlePoller {
5151

5252
/// Fetches bundles from the transaction cache and returns them.
5353
pub async fn check_bundle_cache(&mut self) -> eyre::Result<Vec<TxCacheBundle>> {
54-
let bundle_url: Url = Url::parse(&self.config.tx_pool_url)?.join("bundles")?;
54+
let bundle_url: Url = self.config.tx_pool_url.join("bundles")?;
5555
let token =
5656
self.token.secret().await.map_err(|e| eyre::eyre!("Failed to read token: {e}"))?;
5757

src/tasks/cache/tx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl TxPoller {
5050

5151
/// Polls the transaction cache for transactions.
5252
pub async fn check_tx_cache(&mut self) -> Result<Vec<TxEnvelope>, Error> {
53-
let url: Url = Url::parse(&self.config.tx_pool_url)?.join("transactions")?;
53+
let url: Url = self.config.tx_pool_url.join("transactions")?;
5454
self.client
5555
.get(url)
5656
.send()

src/tasks/submit/prep.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use signet_constants::SignetSystemConstants;
1616
use signet_sim::BuiltBlock;
1717
use signet_types::{SignRequest, SignResponse};
1818
use signet_zenith::BundleHelper;
19-
use std::sync::OnceLock;
2019
use tracing::Instrument;
2120

2221
/// Preparation logic for transactions issued to the host chain by the
@@ -35,8 +34,8 @@ pub struct SubmitPrep<'a> {
3534
constants: SignetSystemConstants,
3635

3736
// Memoized quincey request and response
38-
sig_request: OnceLock<SignRequest>,
39-
quincey_resp: OnceLock<SignResponse>,
37+
sig_request: std::sync::OnceLock<SignRequest>,
38+
quincey_resp: tokio::sync::OnceCell<SignResponse>,
4039
}
4140

4241
impl<'a> SubmitPrep<'a> {
@@ -78,12 +77,12 @@ impl<'a> SubmitPrep<'a> {
7877

7978
/// Get the quincey signature response for the block.
8079
async fn quincey_resp(&self) -> eyre::Result<&SignResponse> {
81-
if let Some(resp) = self.quincey_resp.get() {
82-
return Ok(resp);
83-
}
84-
85-
let sig = self.quincey.get_signature(self.sig_request()).await?;
86-
Ok(self.quincey_resp.get_or_init(|| sig))
80+
self.quincey_resp
81+
.get_or_try_init(|| async {
82+
let sig_request = self.sig_request();
83+
self.quincey.get_signature(sig_request).await
84+
})
85+
.await
8786
}
8887

8988
/// Get the signature components from the response.

src/test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub fn setup_test_config() -> Result<BuilderConfig> {
2121
let config = BuilderConfig {
2222
host_chain_id: signet_constants::pecorino::HOST_CHAIN_ID,
2323
ru_chain_id: signet_constants::pecorino::RU_CHAIN_ID,
24-
host_rpc_url: "https://host-rpc.pecorino.signet.sh".into(),
25-
ru_rpc_url: "https://rpc.pecorino.signet.sh".into(),
24+
host_rpc_url: "https://host-rpc.pecorino.signet.sh".parse().unwrap(),
25+
ru_rpc_url: "https://rpc.pecorino.signet.sh".parse().unwrap(),
2626
tx_broadcast_urls: vec!["http://localhost:9000".into()],
2727
zenith_address: Address::default(),
2828
quincey_url: "http://localhost:8080".into(),
@@ -31,7 +31,7 @@ pub fn setup_test_config() -> Result<BuilderConfig> {
3131
builder_key: "0000000000000000000000000000000000000000000000000000000000000000".into(),
3232
builder_rewards_address: Address::default(),
3333
rollup_block_gas_limit: 3_000_000_000,
34-
tx_pool_url: "http://localhost:9000/".into(),
34+
tx_pool_url: "http://localhost:9000/".parse().unwrap(),
3535
oauth: OAuthConfig {
3636
oauth_client_id: "some_client_id".into(),
3737
oauth_client_secret: "some_client_secret".into(),

0 commit comments

Comments
 (0)