From 12bc70923e110f66be7e562b7581f7b39ac586d7 Mon Sep 17 00:00:00 2001 From: Manoj Sharma <87001470+ManojJiSharma@users.noreply.github.com> Date: Mon, 23 Sep 2024 22:09:55 +0530 Subject: [PATCH] remove repeated code (#258) --- Cargo.lock | 10 ++++++ Cargo.toml | 1 + chains/avalanche/Cargo.toml | 1 + chains/avalanche/src/lib.rs | 35 ++----------------- chains/binance/Cargo.toml | 1 + chains/binance/src/lib.rs | 35 ++----------------- chains/ethereum/backend/src/jsonrpsee.rs | 10 ++++-- chains/ethereum/backend/src/lib.rs | 30 ++++++++++++---- chains/ethereum/server/src/lib.rs | 5 ++- .../rosetta-testing-polygon/Cargo.toml | 1 + .../rosetta-testing-polygon/src/lib.rs | 35 ++----------------- chains/rosetta-chain-testing/Cargo.toml | 10 ++++++ chains/rosetta-chain-testing/src/lib.rs | 33 +++++++++++++++++ deny.toml | 6 ++++ rosetta-server/src/ws/reconnect_impl.rs | 15 ++++++-- 15 files changed, 117 insertions(+), 111 deletions(-) create mode 100644 chains/rosetta-chain-testing/Cargo.toml create mode 100644 chains/rosetta-chain-testing/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 87868a51..035b00ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5579,6 +5579,13 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "rosetta-chain-testing" +version = "0.1.0" +dependencies = [ + "tokio", +] + [[package]] name = "rosetta-client" version = "0.6.0" @@ -5897,6 +5904,7 @@ dependencies = [ "ethers-solc", "hex-literal", "rand_core 0.6.4", + "rosetta-chain-testing", "rosetta-client", "rosetta-config-ethereum", "rosetta-core", @@ -5918,6 +5926,7 @@ dependencies = [ "ethers", "ethers-solc", "hex-literal", + "rosetta-chain-testing", "rosetta-client", "rosetta-config-ethereum", "rosetta-core", @@ -5935,6 +5944,7 @@ dependencies = [ "ethers", "ethers-solc", "hex-literal", + "rosetta-chain-testing", "rosetta-client", "rosetta-config-ethereum", "rosetta-core", diff --git a/Cargo.toml b/Cargo.toml index df91943a..fba29034 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ members = [ "chains/polygon/rosetta-testing-polygon", "chains/binance", "chains/avalanche", + "chains/rosetta-chain-testing", ] resolver = "2" diff --git a/chains/avalanche/Cargo.toml b/chains/avalanche/Cargo.toml index 74a282a7..efebd629 100644 --- a/chains/avalanche/Cargo.toml +++ b/chains/avalanche/Cargo.toml @@ -13,6 +13,7 @@ ethers = { version = "2.0", default-features = true, features = ["abigen", "rust ethers-solc = "2.0" hex-literal = "0.4" rand_core = { version = "0.6", features = ["getrandom"] } +rosetta-chain-testing = { path = "../rosetta-chain-testing" } rosetta-client.workspace = true rosetta-config-ethereum.workspace = true rosetta-core.workspace = true diff --git a/chains/avalanche/src/lib.rs b/chains/avalanche/src/lib.rs index e50c9790..c316c3c9 100644 --- a/chains/avalanche/src/lib.rs +++ b/chains/avalanche/src/lib.rs @@ -44,13 +44,14 @@ mod tests { use ethers::types::H256; use ethers_solc::{artifacts::Source, CompilerInput, EvmVersion, Solc}; use hex_literal::hex; + use rosetta_chain_testing::run_test; use rosetta_client::Wallet; use rosetta_config_ethereum::{AtBlock, CallResult}; use rosetta_core::BlockchainClient; use rosetta_server_ethereum::MaybeWsEthereumClient; use serial_test::serial; use sha3::Digest; - use std::{collections::BTreeMap, future::Future, path::Path}; + use std::{collections::BTreeMap, path::Path}; /// Account used to fund other testing accounts. const FUNDING_ACCOUNT_PRIVATE_KEY: [u8; 32] = @@ -68,38 +69,6 @@ mod tests { } } - /// Run the test in another thread while sending txs to force binance to mine new blocks - /// # Panic - /// Panics if the future panics - async fn run_test + Send + 'static>(future: Fut) { - // Guarantee that only one test is incrementing blocks at a time - static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); - - // Run the test in another thread - let test_handler = tokio::spawn(future); - - // Acquire Lock - let guard = LOCK.lock().await; - - // Check if the test is finished after acquiring the lock - if test_handler.is_finished() { - // Release lock - drop(guard); - - // Now is safe to panic - if let Err(err) = test_handler.await { - std::panic::resume_unwind(err.into_panic()); - } - return; - } - - // Now is safe to panic - if let Err(err) = test_handler.await { - // Resume the panic on the main task - std::panic::resume_unwind(err.into_panic()); - } - } - #[tokio::test] #[serial] async fn network_status() { diff --git a/chains/binance/Cargo.toml b/chains/binance/Cargo.toml index f8a34ded..e252ef8d 100644 --- a/chains/binance/Cargo.toml +++ b/chains/binance/Cargo.toml @@ -12,6 +12,7 @@ anyhow = "1.0" ethers = { version = "2.0", default-features = true, features = ["abigen", "rustls", "ws"] } ethers-solc = "2.0" hex-literal = "0.4" +rosetta-chain-testing = { path = "../rosetta-chain-testing" } rosetta-client.workspace = true rosetta-config-ethereum.workspace = true rosetta-core.workspace = true diff --git a/chains/binance/src/lib.rs b/chains/binance/src/lib.rs index 19a630a7..149a7d59 100644 --- a/chains/binance/src/lib.rs +++ b/chains/binance/src/lib.rs @@ -43,12 +43,13 @@ mod tests { use ethers_solc::{artifacts::Source, CompilerInput, EvmVersion, Solc}; use hex_literal::hex; + use rosetta_chain_testing::run_test; use rosetta_client::Wallet; use rosetta_config_ethereum::{AtBlock, CallResult}; use rosetta_core::BlockchainClient; use rosetta_server_ethereum::MaybeWsEthereumClient; use sha3::Digest; - use std::{collections::BTreeMap, future::Future, path::Path}; + use std::{collections::BTreeMap, path::Path}; /// Binance rpc url const BINANCE_RPC_WS_URL: &str = "ws://127.0.0.1:8546"; @@ -62,38 +63,6 @@ mod tests { } } - /// Run the test in another thread while sending txs to force binance to mine new blocks - /// # Panic - /// Panics if the future panics - async fn run_test + Send + 'static>(future: Fut) { - // Guarantee that only one test is incrementing blocks at a time - static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); - - // Run the test in another thread - let test_handler: tokio::task::JoinHandle<()> = tokio::spawn(future); - - // Acquire Lock - let guard = LOCK.lock().await; - - // Check if the test is finished after acquiring the lock - if test_handler.is_finished() { - // Release lock - drop(guard); - - // Now is safe to panic - if let Err(err) = test_handler.await { - std::panic::resume_unwind(err.into_panic()); - } - return; - } - - // Now is safe to panic - if let Err(err) = test_handler.await { - // Resume the panic on the main task - std::panic::resume_unwind(err.into_panic()); - } - } - #[tokio::test] async fn network_status() { run_test(async move { diff --git a/chains/ethereum/backend/src/jsonrpsee.rs b/chains/ethereum/backend/src/jsonrpsee.rs index 453b34fb..756e6459 100644 --- a/chains/ethereum/backend/src/jsonrpsee.rs +++ b/chains/ethereum/backend/src/jsonrpsee.rs @@ -398,8 +398,14 @@ where T: SubscriptionClientT + Send + Sync, { type SubscriptionError = serde_json::Error; - type NewHeadsStream<'a> = Subscription> where Self: 'a; - type LogsStream<'a> = Subscription where Self: 'a; + type NewHeadsStream<'a> + = Subscription> + where + Self: 'a; + type LogsStream<'a> + = Subscription + where + Self: 'a; /// Fires a notification each time a new header is appended to the chain, including chain /// reorganizations. diff --git a/chains/ethereum/backend/src/lib.rs b/chains/ethereum/backend/src/lib.rs index ff9dffe2..603b5c30 100644 --- a/chains/ethereum/backend/src/lib.rs +++ b/chains/ethereum/backend/src/lib.rs @@ -313,8 +313,14 @@ pub trait EthereumPubSub: EthereumRpc { impl<'b, T: 'b + EthereumPubSub + ?Sized> EthereumPubSub for &'b T { type SubscriptionError = T::SubscriptionError; - type NewHeadsStream<'a> = T::NewHeadsStream<'a> where Self: 'a; - type LogsStream<'a> = T::LogsStream<'a> where Self: 'a; + type NewHeadsStream<'a> + = T::NewHeadsStream<'a> + where + Self: 'a; + type LogsStream<'a> + = T::LogsStream<'a> + where + Self: 'a; fn new_heads<'a, 'async_trait>( &'a self, ) -> BoxFuture<'async_trait, Result, Self::Error>> @@ -342,8 +348,14 @@ impl<'b, T: 'b + EthereumPubSub + ?Sized> EthereumPubSub for &'b T { // https://github.com/auto-impl-rs/auto_impl/issues/93 impl EthereumPubSub for Arc { type SubscriptionError = T::SubscriptionError; - type NewHeadsStream<'a> = T::NewHeadsStream<'a> where Self: 'a; - type LogsStream<'a> = T::LogsStream<'a> where Self: 'a; + type NewHeadsStream<'a> + = T::NewHeadsStream<'a> + where + Self: 'a; + type LogsStream<'a> + = T::LogsStream<'a> + where + Self: 'a; fn new_heads<'a, 'async_trait>( &'a self, @@ -370,8 +382,14 @@ impl EthereumPubSub for Arc { impl EthereumPubSub for Box { type SubscriptionError = T::SubscriptionError; - type NewHeadsStream<'a> = T::NewHeadsStream<'a> where Self: 'a; - type LogsStream<'a> = T::LogsStream<'a> where Self: 'a; + type NewHeadsStream<'a> + = T::NewHeadsStream<'a> + where + Self: 'a; + type LogsStream<'a> + = T::LogsStream<'a> + where + Self: 'a; fn new_heads<'a, 'async_trait>( &'a self, diff --git a/chains/ethereum/server/src/lib.rs b/chains/ethereum/server/src/lib.rs index 13691714..71832655 100644 --- a/chains/ethereum/server/src/lib.rs +++ b/chains/ethereum/server/src/lib.rs @@ -113,7 +113,10 @@ impl MaybeWsEthereumClient { impl BlockchainClient for MaybeWsEthereumClient { type MetadataParams = EthereumMetadataParams; type Metadata = EthereumMetadata; - type EventStream<'a> = BlockStreamType where Self: 'a; + type EventStream<'a> + = BlockStreamType + where + Self: 'a; type Call = EthQuery; type CallResult = EthQueryResult; diff --git a/chains/polygon/rosetta-testing-polygon/Cargo.toml b/chains/polygon/rosetta-testing-polygon/Cargo.toml index 69bd46d2..d66824b6 100644 --- a/chains/polygon/rosetta-testing-polygon/Cargo.toml +++ b/chains/polygon/rosetta-testing-polygon/Cargo.toml @@ -12,6 +12,7 @@ anyhow = "1.0" ethers = { version = "2.0", default-features = true, features = ["abigen", "rustls", "ws"] } ethers-solc = "2.0" hex-literal = "0.4" +rosetta-chain-testing = { path = "../../rosetta-chain-testing" } rosetta-client.workspace = true rosetta-config-ethereum.workspace = true rosetta-core.workspace = true diff --git a/chains/polygon/rosetta-testing-polygon/src/lib.rs b/chains/polygon/rosetta-testing-polygon/src/lib.rs index 513692c1..0c8b5097 100644 --- a/chains/polygon/rosetta-testing-polygon/src/lib.rs +++ b/chains/polygon/rosetta-testing-polygon/src/lib.rs @@ -43,12 +43,13 @@ mod tests { use ethers_solc::{artifacts::Source, CompilerInput, EvmVersion, Solc}; use hex_literal::hex; + use rosetta_chain_testing::run_test; use rosetta_client::Wallet; use rosetta_config_ethereum::{AtBlock, CallResult}; use rosetta_core::BlockchainClient; use rosetta_server_ethereum::MaybeWsEthereumClient; use sha3::Digest; - use std::{collections::BTreeMap, future::Future, path::Path}; + use std::{collections::BTreeMap, path::Path}; /// Polygon rpc url const POLYGON_RPC_WS_URL: &str = "ws://127.0.0.1:8546"; @@ -62,38 +63,6 @@ mod tests { } } - /// Run the test in another thread while sending txs to force polygon to mine new blocks - /// # Panic - /// Panics if the future panics - async fn run_test + Send + 'static>(future: Fut) { - // Guarantee that only one test is incrementing blocks at a time - static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); - - // Run the test in another thread - let test_handler: tokio::task::JoinHandle<()> = tokio::spawn(future); - - // Acquire Lock - let guard = LOCK.lock().await; - - // Check if the test is finished after acquiring the lock - if test_handler.is_finished() { - // Release lock - drop(guard); - - // Now is safe to panic - if let Err(err) = test_handler.await { - std::panic::resume_unwind(err.into_panic()); - } - return; - } - - // Now is safe to panic - if let Err(err) = test_handler.await { - // Resume the panic on the main task - std::panic::resume_unwind(err.into_panic()); - } - } - #[ignore = "No Polygon CI"] #[tokio::test] async fn network_status() { diff --git a/chains/rosetta-chain-testing/Cargo.toml b/chains/rosetta-chain-testing/Cargo.toml new file mode 100644 index 00000000..7d9963aa --- /dev/null +++ b/chains/rosetta-chain-testing/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rosetta-chain-testing" +version = "0.1.0" +edition = "2021" +license = "MIT" +repository = "https://github.com/analog-labs/chain-connectors" +description = "utils for rosetta test." + +[dependencies] +tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } diff --git a/chains/rosetta-chain-testing/src/lib.rs b/chains/rosetta-chain-testing/src/lib.rs new file mode 100644 index 00000000..de5be757 --- /dev/null +++ b/chains/rosetta-chain-testing/src/lib.rs @@ -0,0 +1,33 @@ +use std::future::Future; + +/// Run the test in another thread while sending txs +/// # Panic +/// Panics if the future panics +pub async fn run_test + Send + 'static>(future: Fut) { + // Guarantee that only one test is incrementing blocks at a time + static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); + + // Run the test in another thread + let test_handler = tokio::spawn(future); + + // Acquire Lock + let guard = LOCK.lock().await; + + // Check if the test is finished after acquiring the lock + if test_handler.is_finished() { + // Release lock + drop(guard); + + // Now is safe to panic + if let Err(err) = test_handler.await { + std::panic::resume_unwind(err.into_panic()); + } + return; + } + + // Now is safe to panic + if let Err(err) = test_handler.await { + // Resume the panic on the main task + std::panic::resume_unwind(err.into_panic()); + } +} diff --git a/deny.toml b/deny.toml index a023e845..0da55ea3 100644 --- a/deny.toml +++ b/deny.toml @@ -101,6 +101,12 @@ ignore = [ # Subxt 0.37 depends deprecated libraries: https://github.com/paritytech/subxt/issues/1608 { id = 'RUSTSEC-2024-0370', reason = "Waiting for subxt to update their deprecated dependencies" }, + + # RUSTSEC-2024-0377 contains multiple soundness issues: + { id = 'RUSTSEC-2024-0377', reason = 'Version 1.0 fixes these issues, removes the vast majority of unsafe code, and also fixes some correctness issues and on our connector using older' }, + + # RUSTSEC-2023-0086 contains multiple soundness issues: + { id = 'RUSTSEC-2023-0086', reason = 'Version 1.0 fixes these issues, removes the vast majority of unsafe code, and also fixes some correctness issues and on our connector using older' }, ] # This section is considered when running `cargo deny check sources`. diff --git a/rosetta-server/src/ws/reconnect_impl.rs b/rosetta-server/src/ws/reconnect_impl.rs index e6e2ff5e..1712f9d2 100644 --- a/rosetta-server/src/ws/reconnect_impl.rs +++ b/rosetta-server/src/ws/reconnect_impl.rs @@ -137,9 +137,18 @@ impl DefaultStrategy { impl Reconnect for DefaultStrategy { type Client = T::Client; type ClientRef = Arc; - type ReadyFuture<'a> = ReadyOrWaitFuture where Self: 'a; - type RestartNeededFuture<'a> = ReadyOrWaitFuture where Self: 'a; - type ReconnectFuture<'a> = ReadyOrWaitFuture where Self: 'a; + type ReadyFuture<'a> + = ReadyOrWaitFuture + where + Self: 'a; + type RestartNeededFuture<'a> + = ReadyOrWaitFuture + where + Self: 'a; + type ReconnectFuture<'a> + = ReadyOrWaitFuture + where + Self: 'a; fn ready(&self) -> Self::ReadyFuture<'_> { self.acquire_client()