From 0f01fdb960653d9e2ac233d289aa36af67d620cc Mon Sep 17 00:00:00 2001 From: jfldde <168934971+jfldde@users.noreply.github.com> Date: Mon, 7 Oct 2024 09:13:39 +0100 Subject: [PATCH 1/2] Add client missing RPC methods --- src/bitcoin.rs | 16 +--------------- src/client.rs | 10 ++++++++++ src/node.rs | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/bitcoin.rs b/src/bitcoin.rs index 29bc450..c149368 100644 --- a/src/bitcoin.rs +++ b/src/bitcoin.rs @@ -8,12 +8,8 @@ use std::{ use anyhow::{bail, Context}; use async_trait::async_trait; use bitcoin::Address; -use bitcoin_da::{ - service::{get_relevant_blobs_from_txs, FINALITY_DEPTH}, - spec::blob::BlobWithSender, -}; +use bitcoin_da::service::FINALITY_DEPTH; use bitcoincore_rpc::{json::AddressType::Bech32m, Auth, Client, RpcApi}; -use citrea_primitives::REVEAL_BATCH_PROOF_PREFIX; use futures::TryStreamExt; use tokio::{process::Command, sync::OnceCell, time::sleep}; @@ -102,16 +98,6 @@ impl BitcoinNode { Ok(self.get_block_count().await? - FINALITY_DEPTH + 1) } - pub async fn get_relevant_blobs_from_block(&self, height: u64) -> Result> { - let hash = self.get_block_hash(height).await?; - let block = self.get_block(&hash).await?; - - Ok(get_relevant_blobs_from_txs( - block.txdata, - REVEAL_BATCH_PROOF_PREFIX, - )) - } - async fn wait_for_shutdown(&self) -> Result<()> { let timeout_duration = Duration::from_secs(30); let start = std::time::Instant::now(); diff --git a/src/client.rs b/src/client.rs index 8709e96..341a1de 100644 --- a/src/client.rs +++ b/src/client.rs @@ -79,6 +79,16 @@ impl Client { Ok(self.client.get_soft_confirmation_by_number(num).await?) } + pub async fn ledger_get_sequencer_commitments_on_slot_by_hash( + &self, + hash: [u8; 32], + ) -> Result>> { + self.client + .get_sequencer_commitments_on_slot_by_hash(hash) + .await + .map_err(|e| e.into()) + } + pub async fn ledger_get_head_soft_confirmation_height(&self) -> Result { Ok(self.client.get_head_soft_confirmation_height().await?) } diff --git a/src/node.rs b/src/node.rs index 05b3d6c..949d13d 100644 --- a/src/node.rs +++ b/src/node.rs @@ -1,7 +1,14 @@ -use std::{fmt, fs::File, path::PathBuf, process::Stdio, time::Duration}; +use std::{ + fmt, + fs::File, + path::PathBuf, + process::Stdio, + time::{Duration, SystemTime}, +}; -use anyhow::Context; +use anyhow::{bail, Context}; use async_trait::async_trait; +use log::debug; use serde::Serialize; use tokio::{ process::Command, @@ -116,6 +123,30 @@ impl Node { .context(format!("Failed to spawn {} process", kind)) .map(SpawnOutput::Child) } + + pub async fn wait_for_l2_height(&self, num: u64, timeout: Option) -> Result<()> { + let start = SystemTime::now(); + let timeout = timeout.unwrap_or(Duration::from_secs(30)); // Default 30 seconds timeout + loop { + debug!("Waiting for soft confirmation {}", num); + let latest_block = self + .client + .ledger_get_head_soft_confirmation_height() + .await?; + + if latest_block >= num { + break; + } + + let now = SystemTime::now(); + if start + timeout <= now { + bail!("Timeout. Latest L2 block is {:?}", latest_block); + } + + sleep(Duration::from_secs(1)).await; + } + Ok(()) + } } #[async_trait] From 44a8ecf98a066356a1b34d24ab883afa440c9d3a Mon Sep 17 00:00:00 2001 From: jfldde <168934971+jfldde@users.noreply.github.com> Date: Mon, 7 Oct 2024 09:20:50 +0100 Subject: [PATCH 2/2] Clippy fixes --- src/bitcoin.rs | 6 +++--- src/client.rs | 2 +- src/docker.rs | 10 +++++----- src/node.rs | 12 ++++++------ src/test_case.rs | 17 ++++++++--------- src/utils.rs | 5 ++--- 6 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/bitcoin.rs b/src/bitcoin.rs index c149368..19c83bd 100644 --- a/src/bitcoin.rs +++ b/src/bitcoin.rs @@ -286,9 +286,9 @@ impl BitcoinNodeCluster { let mut cluster = Self { inner: Vec::with_capacity(n_nodes), }; - for config in ctx.config.bitcoin.iter() { + for config in &ctx.config.bitcoin { let node = BitcoinNode::new(config, Arc::clone(&ctx.docker)).await?; - cluster.inner.push(node) + cluster.inner.push(node); } Ok(cluster) @@ -327,7 +327,7 @@ impl BitcoinNodeCluster { if i != j { let ip = match &to_node.spawn_output { SpawnOutput::Container(container) => container.ip.clone(), - _ => "127.0.0.1".to_string(), + SpawnOutput::Child(_) => "127.0.0.1".to_string(), }; let add_node_arg = format!("{}:{}", ip, to_node.config.p2p_port); diff --git a/src/client.rs b/src/client.rs index 341a1de..fe87110 100644 --- a/src/client.rs +++ b/src/client.rs @@ -19,7 +19,7 @@ pub struct Client { impl Client { pub fn new(host: &str, port: u16) -> Result { - let host = format!("http://{}:{}", host, port); + let host = format!("http://{host}:{port}"); let client = HttpClientBuilder::default() .request_timeout(Duration::from_secs(120)) .build(host)?; diff --git a/src/docker.rs b/src/docker.rs index e248a52..f30eaec 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -74,7 +74,7 @@ impl DockerEnv { } async fn create_network(docker: &Docker, test_case_id: &str) -> Result<(String, String)> { - let network_name = format!("test_network_{}", test_case_id); + let network_name = format!("test_network_{test_case_id}"); let options = CreateNetworkOptions { name: network_name.clone(), check_duplicate: true, @@ -95,7 +95,7 @@ impl DockerEnv { let exposed_ports: HashMap> = config .ports .iter() - .map(|port| (format!("{}/tcp", port), HashMap::new())) + .map(|port| (format!("{port}/tcp"), HashMap::new())) .collect(); let port_bindings: HashMap>> = config @@ -103,7 +103,7 @@ impl DockerEnv { .iter() .map(|port| { ( - format!("{}/tcp", port), + format!("{port}/tcp"), Some(vec![PortBinding { host_ip: Some("0.0.0.0".to_string()), host_port: Some(port.to_string()), @@ -196,7 +196,7 @@ impl DockerEnv { return Ok(()); } - println!("Pulling image: {}", image); + println!("Pulling image: {image}"); let options = Some(CreateImageOptions { from_image: image, ..Default::default() @@ -207,7 +207,7 @@ impl DockerEnv { match result { Ok(info) => { if let (Some(status), Some(progress)) = (info.status, info.progress) { - print!("\r{}: {} ", status, progress); + print!("\r{status}: {progress} "); stdout().flush().unwrap(); } } diff --git a/src/node.rs b/src/node.rs index 949d13d..ccc5cea 100644 --- a/src/node.rs +++ b/src/node.rs @@ -77,12 +77,12 @@ impl Node { let kind = C::node_kind(); config.node_config().map_or(Ok(Vec::new()), |node_config| { - let config_path = dir.join(format!("{}_config.toml", kind)); + let config_path = dir.join(format!("{kind}_config.toml")); config_to_file(node_config, &config_path) - .with_context(|| format!("Error writing {} config to file", kind))?; + .with_context(|| format!("Error writing {kind} config to file"))?; Ok(vec![ - format!("--{}-config-path", kind), + format!("--{kind}-config-path"), config_path.display().to_string(), ]) }) @@ -102,7 +102,7 @@ impl Node { // Handle full node not having any node config let node_config_args = Self::get_node_config_args(config)?; - let rollup_config_path = dir.join(format!("{}_rollup_config.toml", kind)); + let rollup_config_path = dir.join(format!("{kind}_rollup_config.toml")); config_to_file(&config.rollup_config(), &rollup_config_path)?; Command::new(citrea) @@ -120,7 +120,7 @@ impl Node { .stderr(Stdio::from(stderr_file)) .kill_on_drop(true) .spawn() - .context(format!("Failed to spawn {} process", kind)) + .context(format!("Failed to spawn {kind} process")) .map(SpawnOutput::Child) } @@ -229,7 +229,7 @@ where async fn start(&mut self, new_config: Option) -> Result<()> { let config = self.config_mut(); if let Some(new_config) = new_config { - *config = new_config + *config = new_config; } *self.spawn_output() = Self::spawn(config)?; self.wait_for_ready(None).await diff --git a/src/test_case.rs b/src/test_case.rs index 736f6b3..2bd0932 100644 --- a/src/test_case.rs +++ b/src/test_case.rs @@ -1,4 +1,4 @@ -//! This module provides the TestCaseRunner and TestCase trait for running and defining test cases. +//! This module provides the `TestCaseRunner` and `TestCase` trait for running and defining test cases. //! It handles setup, execution, and cleanup of test environments. use std::{ @@ -36,7 +36,7 @@ use crate::{ pub struct TestCaseRunner(T); impl TestCaseRunner { - /// Creates a new TestCaseRunner with the given test case. + /// Creates a new `TestCaseRunner`` with the given test case. pub fn new(test_case: T) -> Self { Self(test_case) } @@ -85,7 +85,7 @@ impl TestCaseRunner { if let Err(_) | Ok(Err(_)) = result { if let Err(e) = f.dump_log() { - eprintln!("Error dumping log: {}", e); + eprintln!("Error dumping log: {e}"); } } @@ -100,8 +100,7 @@ impl TestCaseRunner { Err(panic_error) => { let panic_msg = panic_error .downcast_ref::() - .map(|s| s.to_string()) - .unwrap_or_else(|| "Unknown panic".to_string()); + .map_or_else(|| "Unknown panic".to_string(), ToString::to_string); bail!(panic_msg) } } @@ -138,7 +137,7 @@ impl TestCaseRunner { env: env.bitcoin().clone(), idx: i, ..bitcoin.clone() - }) + }); } // Target first bitcoin node as DA for now @@ -158,7 +157,7 @@ impl TestCaseRunner { ..da_config.clone() }, storage: StorageConfig { - path: dbs_dir.join(format!("{}-db", node_kind)), + path: dbs_dir.join(format!("{node_kind}-db")), db_max_open_files: None, }, rpc: RpcConfig { @@ -193,7 +192,7 @@ impl TestCaseRunner { ..da_config.clone() }, storage: StorageConfig { - path: dbs_dir.join(format!("{}-db", node_kind)), + path: dbs_dir.join(format!("{node_kind}-db")), db_max_open_files: None, }, rpc: RpcConfig { @@ -219,7 +218,7 @@ impl TestCaseRunner { ..da_config.clone() }, storage: StorageConfig { - path: dbs_dir.join(format!("{}-db", node_kind)), + path: dbs_dir.join(format!("{node_kind}-db")), db_max_open_files: None, }, rpc: RpcConfig { diff --git a/src/utils.rs b/src/utils.rs index f5060a5..e05b683 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -42,8 +42,7 @@ pub fn get_stderr_path(dir: &Path) -> PathBuf { /// Get genesis path from resources /// TODO: assess need for customable genesis path in e2e tests pub fn get_default_genesis_path() -> PathBuf { - let workspace_root = get_workspace_root(); - let mut path = workspace_root.to_path_buf(); + let mut path = get_workspace_root(); path.push("resources"); path.push("genesis"); path.push("bitcoin-regtest"); @@ -101,7 +100,7 @@ pub fn tail_file(path: &Path, lines: usize) -> Result<()> { } for line in last_lines { - println!("{}", line); + println!("{line}"); } Ok(())