From 886cbfca1bb2a182c1ba0eb58aa6827ba60e2b86 Mon Sep 17 00:00:00 2001 From: pierre-l Date: Mon, 8 Apr 2024 10:00:30 +0200 Subject: [PATCH] chore: Cleanup the new test suite --- Cargo.lock | 12 ++++++ crates/core/src/client.rs | 8 +++- crates/core/src/config.rs | 2 +- crates/rpc/Cargo.toml | 3 +- crates/rpc/src/lib.rs | 7 ++++ crates/rpc/tests/rpc.rs | 80 ++++++++++++++++----------------------- 6 files changed, 60 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7bddd5f69..81c665ec9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -608,6 +608,7 @@ dependencies = [ "starknet", "thiserror", "tokio", + "tokio-retry", "tracing", ] @@ -6645,6 +6646,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-retry" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f57eb36ecbe0fc510036adff84824dd3c24bb781e21bfa67b69d556aa85214f" +dependencies = [ + "pin-project", + "rand 0.8.5", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.23.4" diff --git a/crates/core/src/client.rs b/crates/core/src/client.rs index 8a0dc9793..be907cbca 100644 --- a/crates/core/src/client.rs +++ b/crates/core/src/client.rs @@ -124,6 +124,10 @@ pub struct BeerusClient { impl BeerusClient { pub async fn new(config: &Config) -> Result { + // TODO Remove + tracing::info!("STARTING {:?}", config); + + #[cfg(not(target_arch = "wasm32"))] let mut helios_client: Client = config.to_helios_client().await; #[cfg(target_arch = "wasm32")] @@ -137,8 +141,8 @@ impl BeerusClient { .await .context("failed to fetch syncing status")? { - debug!("{} syncing: head={}", config.network, sync.highest_block); - thread::sleep(time::Duration::from_secs(1)); + info!("{} syncing: head={}", config.network, sync.highest_block); + thread::sleep(time::Duration::from_millis(300)); } get_starknet_state_root( diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index e0fa0cc13..956d66b6c 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -23,7 +23,7 @@ pub const DEFAULT_POLL_SECS: u64 = 5; pub const DEFAULT_FEE_TOKEN_ADDR: &str = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"; const DEFAULT_IP_V4_ADDR: &str = "127.0.0.1"; -const DEFAULT_PORT: u16 = 3030; +pub const DEFAULT_PORT: u16 = 3030; // mainnet constants pub const MAINNET_CC_ADDRESS: &str = "c662c410C0ECf747543f5bA90660f6ABeBD9C8c4"; diff --git a/crates/rpc/Cargo.toml b/crates/rpc/Cargo.toml index 3b6732c69..454352eda 100644 --- a/crates/rpc/Cargo.toml +++ b/crates/rpc/Cargo.toml @@ -22,4 +22,5 @@ tracing.workspace = true env_logger = "0.11" serde_json = "1.0" reqwest = "0.11.13" -tokio = { version = "1", features = ["sync", "macros", "rt", "time"] } \ No newline at end of file +tokio = { version = "1", features = ["sync", "macros", "rt", "time"] } +tokio-retry = "0.3" \ No newline at end of file diff --git a/crates/rpc/src/lib.rs b/crates/rpc/src/lib.rs index ca451e130..bdfa265bb 100644 --- a/crates/rpc/src/lib.rs +++ b/crates/rpc/src/lib.rs @@ -19,8 +19,15 @@ impl BeerusRpc { } pub async fn run(self) -> Result<(SocketAddr, ServerHandle), RunError> { + // TODO REMOVE + tracing::error!("Version check.."); + println!("OK"); + self.check_spec_version(SPEC_VERION).await?; + // TODO REMOVE + tracing::error!("Version check passed"); + let server = ServerBuilder::default().build(self.beerus.config.rpc_addr).await?; diff --git a/crates/rpc/tests/rpc.rs b/crates/rpc/tests/rpc.rs index a75623ba8..9a6c0d8ca 100644 --- a/crates/rpc/tests/rpc.rs +++ b/crates/rpc/tests/rpc.rs @@ -1,21 +1,22 @@ +// These tests need Beerus to run in the background, hence why they're hidden behing the following feature. #![cfg(feature = "integration-tests")] -/* - TODO As expected, these states aren't as reliable as I hoped because to state changes. - I see two possible ways to tackle this: - * retries - * maybe work on "mature" blocks, 100 below the latest to limit state changes? -*/ +use tokio_retry::Retry; +use std::{format, sync::Once}; +use beerus_core::{ + config::Config, + client::BeerusClient, +}; +use beerus_rpc::BeerusRpc; use reqwest::Url; use starknet::{ core::types::{ BlockHashAndNumber, BlockId, BlockTag, BlockWithTxHashes, BlockWithTxs, - BroadcastedInvokeTransactionV3, BroadcastedTransaction, DeclareTransaction, DeployAccountTransaction, Event, EventFilter, - FieldElement, FunctionCall, InvokeTransaction, + FieldElement, InvokeTransaction, MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs, - MaybePendingTransactionReceipt, SimulationFlagForEstimateFee, - Transaction, TransactionExecutionStatus, TransactionReceipt, + MaybePendingTransactionReceipt, + Transaction, TransactionReceipt, }, providers::{ jsonrpc::HttpTransport, JsonRpcClient, Provider, ProviderError, @@ -160,27 +161,12 @@ impl L2ClientExt for JsonRpcClient { } } -// TODO -#[ignore = "draft"] -#[tokio::test] -async fn test_block_hash_and_number() { - let TestContext { client, block, block_id: _, extracted_value: () } = - latest_block_context().await; - - let BlockHashAndNumber { block_hash, block_number } = client - .block_hash_and_number() - .await - .expect("Failed to retrieve the latest hash & block number"); - assert_eq!(block_number, block.block_number); - assert_eq!(block_hash, block.block_hash); -} #[tokio::test] async fn test_block_number() { let TestContext { client, block, block_id: _, extracted_value: () } = latest_block_context().await; - // TODO In the case the block just changed, retry let block_number = client .block_number() .await @@ -193,7 +179,6 @@ async fn test_chain_id() { let TestContext { client, block: _, block_id: _, extracted_value: () } = latest_block_context().await; - // TODO Assertion? client.chain_id().await.expect("Failed to retrieve the chain ID"); } @@ -266,15 +251,16 @@ async fn test_get_class() { Transaction::Declare(DeclareTransaction::V3(declare)) => { Some(declare.class_hash) } - // TODO Make the match exhaustive to make dep upgrades more reliable. _ => None, } }) }) .await; - // TODO Assertions? - client.get_class(block_id, extracted_value).await.expect("getClass failed"); + client + .get_class(block_id, extracted_value) + .await + .expect("getClass failed"); } #[tokio::test] @@ -286,7 +272,6 @@ async fn test_get_class_at() { Transaction::DeployAccount( DeployAccountTransaction::V3(deploy), ) => Some(deploy.transaction_hash), - // TODO Make the match exhaustive to make dep upgrades more reliable. _ => None, } }) @@ -321,7 +306,6 @@ async fn test_get_class_hash_at() { Transaction::DeployAccount( DeployAccountTransaction::V3(deploy), ) => Some(deploy.transaction_hash), - // TODO Make the match exhaustive to make dep upgrades more reliable. _ => None, } }) @@ -360,7 +344,6 @@ async fn test_get_nonce() { Transaction::Invoke(InvokeTransaction::V3(invoke)) => { Some((invoke.nonce, invoke.sender_address)) } - // TODO Make the match exhaustive to make dep upgrades more reliable. _ => None, } }) @@ -483,19 +466,20 @@ async fn test_get_transaction_status() { } /* TODO - Add more test scenarios to cover the following methods: - - starknet_call - starknet_estimateFee - starknet_estimateFeeSingle - starknet_getBalance - starknet_getEvents - starknet_getProof - starknet_getStateRoot - starknet_getStateUpdate - starknet_getStorateAt - starknet_getTransactionReceipt - starknet_getTransactionStatus - starknet_syncing - pathfinder_getProof - */ + Add more test scenarios to cover the following methods: + + starknet_block_hash_and_number + starknet_call + starknet_estimateFee + starknet_estimateFeeSingle + starknet_getBalance + starknet_getEvents + starknet_getProof + starknet_getStateRoot + starknet_getStateUpdate + starknet_getStorateAt + starknet_getTransactionReceipt + starknet_getTransactionStatus + starknet_syncing + pathfinder_getProof +*/