Skip to content

Commit

Permalink
Merge branch 'main' into ceyhun/bridge_backend_support
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Oct 7, 2024
2 parents 31dc639 + 44a8ecf commit 5dde565
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 44 deletions.
22 changes: 4 additions & 18 deletions src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<Vec<BlobWithSender>> {
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();
Expand Down Expand Up @@ -300,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)
Expand Down Expand Up @@ -341,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);
Expand Down
12 changes: 11 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Client {

impl Client {
pub fn new(host: &str, port: u16) -> Result<Self> {
let host = format!("http://{}:{}", host, port);
let host = format!("http://{host}:{port}");
let client = HttpClientBuilder::default()
.request_timeout(Duration::from_secs(120))
.build(host)?;
Expand Down Expand Up @@ -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<Option<Vec<SequencerCommitmentResponse>>> {
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<u64> {
Ok(self.client.get_head_soft_confirmation_height().await?)
}
Expand Down
10 changes: 5 additions & 5 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -95,15 +95,15 @@ impl DockerEnv {
let exposed_ports: HashMap<String, HashMap<(), ()>> = config
.ports
.iter()
.map(|port| (format!("{}/tcp", port), HashMap::new()))
.map(|port| (format!("{port}/tcp"), HashMap::new()))
.collect();

let port_bindings: HashMap<String, Option<Vec<PortBinding>>> = config
.ports
.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()),
Expand Down Expand Up @@ -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()
Expand All @@ -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();
}
}
Expand Down
47 changes: 39 additions & 8 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -72,12 +79,12 @@ impl<C: Config> Node<C> {
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(),
])
})
Expand All @@ -97,7 +104,7 @@ impl<C: Config> Node<C> {
// 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)
Expand All @@ -115,9 +122,33 @@ impl<C: Config> Node<C> {
.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)
}

pub async fn wait_for_l2_height(&self, num: u64, timeout: Option<Duration>) -> 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]
Expand Down Expand Up @@ -200,7 +231,7 @@ where
async fn start(&mut self, new_config: Option<Self::Config>) -> 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
Expand Down
17 changes: 8 additions & 9 deletions src/test_case.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -37,7 +37,7 @@ use crate::{
pub struct TestCaseRunner<T: TestCase>(T);

impl<T: TestCase> TestCaseRunner<T> {
/// 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)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<T: TestCase> TestCaseRunner<T> {

if let Err(_) | Ok(Err(_)) = result {
if let Err(e) = f.dump_log() {
eprintln!("Error dumping log: {}", e);
eprintln!("Error dumping log: {e}");
}
}

Expand All @@ -101,8 +101,7 @@ impl<T: TestCase> TestCaseRunner<T> {
Err(panic_error) => {
let panic_msg = panic_error
.downcast_ref::<String>()
.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)
}
}
Expand Down Expand Up @@ -139,7 +138,7 @@ impl<T: TestCase> TestCaseRunner<T> {
env: env.bitcoin().clone(),
idx: i,
..bitcoin.clone()
})
});
}

let mut bridge_backend_confs = vec![];
Expand Down Expand Up @@ -168,7 +167,7 @@ impl<T: TestCase> TestCaseRunner<T> {
..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 {
Expand Down Expand Up @@ -203,7 +202,7 @@ impl<T: TestCase> TestCaseRunner<T> {
..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 {
Expand All @@ -229,7 +228,7 @@ impl<T: TestCase> TestCaseRunner<T> {
..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 {
Expand Down
5 changes: 2 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -101,7 +100,7 @@ pub fn tail_file(path: &Path, lines: usize) -> Result<()> {
}

for line in last_lines {
println!("{}", line);
println!("{line}");
}

Ok(())
Expand Down

0 comments on commit 5dde565

Please sign in to comment.