Skip to content

Commit

Permalink
refactor: code cleaniness (#4389)
Browse files Browse the repository at this point in the history
* refactor: parse token at compile time

* refactor: encapsulate ConscientiousEthWebsocketBlockHeaderStream

* Use Height::ZERO constant
  • Loading branch information
AlastairHolmes authored Jan 8, 2024
1 parent b845e92 commit da30422
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
18 changes: 10 additions & 8 deletions engine/src/dot/http_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;

Expand All @@ -32,12 +32,14 @@ pub struct PolkadotHttpClient(HttpClient);

impl PolkadotHttpClient {
pub fn new(url: &SecretUrl) -> Result<Self> {
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)?,
))
}
}

Expand Down
15 changes: 14 additions & 1 deletion engine/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod event;
pub mod retry_rpc;
pub mod rpc;

use anyhow::Result;
use anyhow::{Context, Result};

use futures::FutureExt;

Expand All @@ -25,6 +25,19 @@ pub struct ConscientiousEthWebsocketBlockHeaderStream {
>,
}

impl ConscientiousEthWebsocketBlockHeaderStream {
pub async fn new(web3: web3::Web3<web3::transports::WebSocket>) -> Result<Self> {
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");
Expand Down
9 changes: 1 addition & 8 deletions engine/src/eth/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion engine/src/witness/btc/btc_deposits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions utilities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit da30422

Please sign in to comment.