Skip to content

Commit

Permalink
Add light client type
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziciahmet committed Oct 7, 2024
1 parent 8e2cc8f commit f8b4918
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bitcoin = { version = "0.32.2", features = ["serde", "rand"] }
bitcoincore-rpc = { version = "0.18.0" }
bollard = { version = "0.17.1" }
futures = "0.3"
jsonrpsee = { version = "0.24.2", features = ["http-client", "ws-client"] }
log = "0.4"
rand = "0.8"
serde = { version = "1.0.192", default-features = false, features = ["alloc", "derive"] }
Expand All @@ -19,7 +20,6 @@ tempfile = "3.8"
tokio = { version = "1.39", features = ["full"] }
toml = "0.8.0"
which = "6.0.1"
jsonrpsee = { version = "0.24.2", features = ["http-client", "ws-client"] }

# Citrea dependencies
bitcoin-da = { git = "https://github.com/chainwayxyz/citrea", rev = "82bf52d", features = ["native"] }
Expand Down
5 changes: 3 additions & 2 deletions src/batch_prover.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::time::SystemTime;

use super::{config::FullBatchProverConfig, Result};
use crate::node::Node;
use anyhow::bail;
use log::debug;
use tokio::time::{sleep, Duration};

use super::{config::FullBatchProverConfig, Result};
use crate::node::Node;

pub type BatchProver = Node<FullBatchProverConfig>;

impl BatchProver {
Expand Down
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::time::{Duration, SystemTime};

use anyhow::{bail, Result};
use jsonrpsee::core::client::ClientT;
use jsonrpsee::{
core::client::ClientT,
http_client::{HttpClient, HttpClientBuilder},
rpc_params,
};
Expand Down
2 changes: 2 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct FullL2NodeConfig<T> {

pub type FullSequencerConfig = FullL2NodeConfig<SequencerConfig>;
pub type FullBatchProverConfig = FullL2NodeConfig<ProverConfig>;
// TODO: use LightClientProverConfig
pub type FullLightClientProverConfig = FullL2NodeConfig<ProverConfig>;
pub type FullFullNodeConfig = FullL2NodeConfig<()>;

pub trait NodeKindMarker {
Expand Down
5 changes: 3 additions & 2 deletions src/config/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
bitcoin::BitcoinConfig, test_case::TestCaseConfig, FullFullNodeConfig, FullBatchProverConfig,
FullSequencerConfig,
bitcoin::BitcoinConfig, test_case::TestCaseConfig, FullBatchProverConfig, FullFullNodeConfig,
FullLightClientProverConfig, FullSequencerConfig,
};

#[derive(Clone)]
Expand All @@ -9,5 +9,6 @@ pub struct TestConfig {
pub bitcoin: Vec<BitcoinConfig>,
pub sequencer: FullSequencerConfig,
pub batch_prover: FullBatchProverConfig,
pub light_client_prover: FullLightClientProverConfig,
pub full_node: FullFullNodeConfig,
}
8 changes: 7 additions & 1 deletion src/config/test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct TestCaseEnv {
pub full_node: Vec<(&'static str, &'static str)>,
pub sequencer: Vec<(&'static str, &'static str)>,
pub batch_prover: Vec<(&'static str, &'static str)>,
pub light_client_prover: Vec<(&'static str, &'static str)>,
pub bitcoin: Vec<(&'static str, &'static str)>,
}

Expand All @@ -29,6 +30,10 @@ impl TestCaseEnv {
[self.test_env(), self.batch_prover.clone()].concat()
}

pub fn light_client_prover(&self) -> Vec<(&'static str, &'static str)> {
[self.test_env(), self.light_client_prover.clone()].concat()
}

pub fn full_node(&self) -> Vec<(&'static str, &'static str)> {
[self.test_env(), self.full_node.clone()].concat()
}
Expand All @@ -44,7 +49,7 @@ pub struct TestCaseConfig {
pub with_sequencer: bool,
pub with_full_node: bool,
pub with_batch_prover: bool,
#[allow(unused)]
pub with_light_client_prover: bool,
pub timeout: Duration,
pub dir: PathBuf,
pub docker: bool,
Expand All @@ -60,6 +65,7 @@ impl Default for TestCaseConfig {
n_nodes: 1,
with_sequencer: true,
with_batch_prover: false,
with_light_client_prover: false,
with_full_node: false,
timeout: Duration::from_secs(60),
dir: TempDir::new()
Expand Down
32 changes: 30 additions & 2 deletions src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{
traits::{LogProvider, LogProviderErased, NodeT},
Result,
};
use crate::{batch_prover::BatchProver, utils::tail_file};
use crate::{batch_prover::BatchProver, light_client_prover::LightClientProver, utils::tail_file};

pub struct TestContext {
pub config: TestConfig,
Expand All @@ -38,6 +38,7 @@ pub struct TestFramework {
pub bitcoin_nodes: BitcoinNodeCluster,
pub sequencer: Option<Sequencer>,
pub batch_prover: Option<BatchProver>,
pub light_client_prover: Option<LightClientProver>,
pub full_node: Option<FullNode>,
show_logs: bool,
pub initial_da_height: u64,
Expand Down Expand Up @@ -67,6 +68,7 @@ impl TestFramework {
bitcoin_nodes,
sequencer: None,
batch_prover: None,
light_client_prover: None,
full_node: None,
ctx,
show_logs: true,
Expand All @@ -82,11 +84,15 @@ impl TestFramework {
)
.await?;

(self.batch_prover, self.full_node) = tokio::try_join!(
(self.batch_prover, self.light_client_prover, self.full_node) = tokio::try_join!(
create_optional(
self.ctx.config.test_case.with_batch_prover,
BatchProver::new(&self.ctx.config.batch_prover)
),
create_optional(
self.ctx.config.test_case.with_light_client_prover,
LightClientProver::new(&self.ctx.config.light_client_prover)
),
create_optional(
self.ctx.config.test_case.with_full_node,
FullNode::new(&self.ctx.config.full_node)
Expand All @@ -102,6 +108,9 @@ impl TestFramework {
self.sequencer.as_ref().map(LogProvider::as_erased),
self.full_node.as_ref().map(LogProvider::as_erased),
self.batch_prover.as_ref().map(LogProvider::as_erased),
self.light_client_prover
.as_ref()
.map(LogProvider::as_erased),
]
.into_iter()
.flatten()
Expand Down Expand Up @@ -154,6 +163,11 @@ impl TestFramework {
println!("Successfully stopped batch_prover");
}

if let Some(light_client_prover) = &mut self.light_client_prover {
let _ = light_client_prover.stop().await;
println!("Successfully stopped light_client_prover");
}

if let Some(full_node) = &mut self.full_node {
let _ = full_node.stop().await;
println!("Successfully stopped full_node");
Expand All @@ -177,6 +191,14 @@ impl TestFramework {
.await?;
da.create_wallet(&NodeKind::BatchProver.to_string(), None, None, None, None)
.await?;
da.create_wallet(
&NodeKind::LightClientProver.to_string(),
None,
None,
None,
None,
)
.await?;
da.create_wallet(&NodeKind::Bitcoin.to_string(), None, None, None, None)
.await?;

Expand All @@ -191,6 +213,12 @@ impl TestFramework {
da.fund_wallet(NodeKind::BatchProver.to_string(), blocks_to_fund)
.await?;
}

if self.ctx.config.test_case.with_light_client_prover {
da.fund_wallet(NodeKind::LightClientProver.to_string(), blocks_to_fund)
.await?;
}

da.fund_wallet(NodeKind::Bitcoin.to_string(), blocks_to_fund)
.await?;

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
pub mod batch_prover;
pub mod bitcoin;
mod client;
pub mod config;
mod docker;
pub mod framework;
pub mod full_node;
pub mod light_client_prover;
pub mod node;
pub mod batch_prover;
pub mod sequencer;
pub mod test_case;
pub mod traits;
Expand Down
37 changes: 37 additions & 0 deletions src/light_client_prover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::time::SystemTime;

use anyhow::bail;
use log::debug;
use tokio::time::{sleep, Duration};

use super::{config::FullLightClientProverConfig, Result};
use crate::node::Node;

pub type LightClientProver = Node<FullLightClientProverConfig>;

impl LightClientProver {
// TODO: remove _l at the end
pub async fn wait_for_l1_height_l(&self, height: u64, timeout: Option<Duration>) -> Result<()> {
let start = SystemTime::now();
let timeout = timeout.unwrap_or(Duration::from_secs(600));
loop {
debug!("Waiting for light client prover height {}", height);
let latest_block = self.client.ledger_get_last_scanned_l1_height().await?;

if latest_block >= height {
break;
}

let now = SystemTime::now();
if start + timeout <= now {
bail!(
"Timeout. Latest light client prover L1 height is {}",
latest_block
);
}

sleep(Duration::from_secs(1)).await;
}
Ok(())
}
}
2 changes: 2 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::{
pub enum NodeKind {
Bitcoin,
BatchProver,
LightClientProver,
Sequencer,
FullNode,
}
Expand All @@ -36,6 +37,7 @@ impl fmt::Display for NodeKind {
match self {
NodeKind::Bitcoin => write!(f, "bitcoin"),
NodeKind::BatchProver => write!(f, "batch-prover"),
NodeKind::LightClientProver => write!(f, "light-client-prover"),
NodeKind::Sequencer => write!(f, "sequencer"),
NodeKind::FullNode => write!(f, "full-node"),
}
Expand Down
3 changes: 1 addition & 2 deletions src/sequencer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::path::PathBuf;

use super::config::FullSequencerConfig;
use crate::node::Node;
use crate::traits::NodeT;
use crate::{node::Node, traits::NodeT};

pub type Sequencer = Node<FullSequencerConfig>;

Expand Down
Loading

0 comments on commit f8b4918

Please sign in to comment.