Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: code cleaniness #4389

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need this header actually? It is a little strange that the token is just TOKEN all the time 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keine ahnung

)]))
.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