From 7295c285934ea94634d3d12ba4abc4fc35058c6e Mon Sep 17 00:00:00 2001 From: Alastair Holmes Date: Fri, 5 Jan 2024 13:44:56 +0100 Subject: [PATCH 1/3] refactor: parse token at compile time --- engine/src/dot/http_rpc.rs | 18 ++++++++++-------- utilities/src/lib.rs | 9 +++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/engine/src/dot/http_rpc.rs b/engine/src/dot/http_rpc.rs index 7e4a1973d7..dfbe92ff2a 100644 --- a/engine/src/dot/http_rpc.rs +++ b/engine/src/dot/http_rpc.rs @@ -7,7 +7,7 @@ use jsonrpsee::{ core::{client::ClientT, traits::ToRpcParams, Error as JsonRpseeError}, http_client::{HttpClient, HttpClientBuilder}, }; -use reqwest::header::{HeaderMap, AUTHORIZATION}; +use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION}; use serde_json::value::RawValue; use sp_core::H256; use subxt::{ @@ -22,7 +22,7 @@ use subxt::{ use anyhow::Result; use tracing::{error, warn}; -use utilities::{make_periodic_tick, redact_endpoint_secret::SecretUrl}; +use utilities::{const_eval, make_periodic_tick, redact_endpoint_secret::SecretUrl}; use crate::constants::RPC_RETRY_CONNECTION_INTERVAL; @@ -32,12 +32,14 @@ pub struct PolkadotHttpClient(HttpClient); impl PolkadotHttpClient { pub fn new(url: &SecretUrl) -> Result { - let token = format!("Bearer {}", "TOKEN"); - let mut headers = HeaderMap::new(); - headers.insert(AUTHORIZATION, token.parse().unwrap()); - let client = HttpClientBuilder::default().set_headers(headers).build(url)?; - - Ok(Self(client)) + Ok(Self( + HttpClientBuilder::default() + .set_headers(HeaderMap::from_iter([( + AUTHORIZATION, + const_eval!(HeaderValue, HeaderValue::from_static("Bearer TOKEN")), + )])) + .build(url)?, + )) } } diff --git a/utilities/src/lib.rs b/utilities/src/lib.rs index 2149b2855f..d57d2e1ec6 100644 --- a/utilities/src/lib.rs +++ b/utilities/src/lib.rs @@ -30,6 +30,15 @@ macro_rules! assert_err { }; } +/// Forces compile evaluation of expression +#[macro_export] +macro_rules! const_eval { + ($t:ty, $e:expr) => {{ + static X: $t = $e; + X.clone() + }}; +} + /// Note that the resulting `threshold` is the maximum number /// of parties *not* enough to generate a signature, /// i.e. at least `t+1` parties are required. From e74c31a87cbb95d6bad73e5cbfcc37d65be238c9 Mon Sep 17 00:00:00 2001 From: Alastair Holmes Date: Fri, 5 Jan 2024 13:44:28 +0100 Subject: [PATCH 2/3] refactor: encapsulate ConscientiousEthWebsocketBlockHeaderStream --- engine/src/eth/mod.rs | 15 ++++++++++++++- engine/src/eth/rpc.rs | 9 +-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/engine/src/eth/mod.rs b/engine/src/eth/mod.rs index 56d83c9421..4dc346f549 100644 --- a/engine/src/eth/mod.rs +++ b/engine/src/eth/mod.rs @@ -2,7 +2,7 @@ pub mod event; pub mod retry_rpc; pub mod rpc; -use anyhow::Result; +use anyhow::{Context, Result}; use futures::FutureExt; @@ -25,6 +25,19 @@ pub struct ConscientiousEthWebsocketBlockHeaderStream { >, } +impl ConscientiousEthWebsocketBlockHeaderStream { + pub async fn new(web3: web3::Web3) -> Result { + Ok(Self { + stream: Some( + web3.eth_subscribe() + .subscribe_new_heads() + .await + .context("Failed to subscribe to new heads with WS Client")?, + ), + }) + } +} + impl Drop for ConscientiousEthWebsocketBlockHeaderStream { fn drop(&mut self) { tracing::warn!("Dropping the ETH WS connection"); diff --git a/engine/src/eth/rpc.rs b/engine/src/eth/rpc.rs index 110b910d52..c2dc4b4a08 100644 --- a/engine/src/eth/rpc.rs +++ b/engine/src/eth/rpc.rs @@ -318,14 +318,7 @@ impl ReconnectSubscribeApi for ReconnectSubscriptionClient { bail!("Expected chain id {}, eth ws client returned {client_chain_id}.", self.chain_id) } - Ok(ConscientiousEthWebsocketBlockHeaderStream { - stream: Some( - web3.eth_subscribe() - .subscribe_new_heads() - .await - .context("Failed to subscribe to new heads with WS Client")?, - ), - }) + ConscientiousEthWebsocketBlockHeaderStream::new(web3).await } } From db19bab04dc78d2febea50a7a5b6b7471f7132c5 Mon Sep 17 00:00:00 2001 From: Alastair Holmes Date: Fri, 5 Jan 2024 14:19:49 +0100 Subject: [PATCH 3/3] Use Height::ZERO constant --- engine/src/witness/btc/btc_deposits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/src/witness/btc/btc_deposits.rs b/engine/src/witness/btc/btc_deposits.rs index 2fd0ff7e83..36a284d938 100644 --- a/engine/src/witness/btc/btc_deposits.rs +++ b/engine/src/witness/btc/btc_deposits.rs @@ -156,7 +156,7 @@ pub mod tests { VerboseTransaction { txid, version: Version::from_consensus(2), - locktime: LockTime::Blocks(Height::from_consensus(0).unwrap()), + locktime: LockTime::Blocks(Height::ZERO), vin: vec![], vout: tx_outs, fee,