Skip to content

Commit

Permalink
Yaziciahmet/light client type (#13)
Browse files Browse the repository at this point in the history
* Rename Prover namespace to BatchProver

* Add light client type

* Separate batch and light client prover configs

* Add citrea light client proving config

* lint

* Remove unused dep

* Minor cleanup

* Fix comment

* Lint
  • Loading branch information
yaziciahmet authored Oct 8, 2024
1 parent 669d14f commit 629e82f
Show file tree
Hide file tree
Showing 20 changed files with 310 additions and 85 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ bitcoin = { version = "0.32.2", features = ["serde", "rand"] }
bitcoincore-rpc = { version = "0.18.0" }
bollard = { version = "0.17.1" }
futures = "0.3"
hex = { version = "0.4.3", default-features = false, features = ["serde"] }
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,13 +21,10 @@ 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"] }
hex = { version = "0.4.3", default-features = false, features = ["serde"] }

# Citrea dependencies
sov-ledger-rpc = { git = "https://github.com/chainwayxyz/citrea", rev = "82bf52d", default-features = false, features = ["client"] }
sov-rollup-interface = { git = "https://github.com/chainwayxyz/citrea", rev = "82bf52d" }
syn = { version = "1.0", features = ["full", "extra-traits"] }

[patch.crates-io]
bitcoincore-rpc = { version = "0.18.0", git = "https://github.com/chainwayxyz/rust-bitcoincore-rpc.git", rev = "0ae498d" }
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ lint: ## cargo check and clippy. Skip clippy on guest code since it's not suppo
dprint check
cargo +nightly fmt --all --check
cargo check --all-targets --all-features
$(MAKE) check-fuzz
SKIP_GUEST_BUILD=1 cargo clippy --all-targets --all-features
cargo clippy --all-targets --all-features

lint-fix: ## dprint fmt, cargo fmt, fix and clippy. Skip clippy on guest code since it's not supported by risc0
dprint fmt
cargo +nightly fmt --all
cargo fix --allow-dirty
SKIP_GUEST_BUILD=1 cargo clippy --fix --allow-dirty
cargo clippy --fix --allow-dirty

docs: ## Generates documentation locally
cargo doc --open
Expand Down
13 changes: 7 additions & 6 deletions src/prover.rs → src/batch_prover.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use std::time::SystemTime;

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

pub type Prover = Node<FullProverConfig>;
use super::{config::FullBatchProverConfig, Result};
use crate::node::Node;

pub type BatchProver = Node<FullBatchProverConfig>;

impl Prover {
impl BatchProver {
pub async fn wait_for_l1_height(&self, height: u64, timeout: Option<Duration>) -> Result<()> {
let start = SystemTime::now();
let timeout = timeout.unwrap_or(Duration::from_secs(600));
loop {
debug!("Waiting for prover height {}", height);
debug!("Waiting for batch prover height {}", height);
let latest_block = self.client.ledger_get_last_scanned_l1_height().await?;

if latest_block >= height {
Expand All @@ -22,7 +23,7 @@ impl Prover {

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

sleep(Duration::from_secs(1)).await;
Expand Down
5 changes: 4 additions & 1 deletion src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ impl BitcoinNode {
async fn load_wallets(&self) {
let _ = self.load_wallet(&NodeKind::Bitcoin.to_string()).await;
let _ = self.load_wallet(&NodeKind::Sequencer.to_string()).await;
let _ = self.load_wallet(&NodeKind::Prover.to_string()).await;
let _ = self.load_wallet(&NodeKind::BatchProver.to_string()).await;
let _ = self
.load_wallet(&NodeKind::LightClientProver.to_string())
.await;
}

// Switch this over to Node signature once we add support for docker to citrea nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'de> Deserialize<'de> for ProverGuestRunConfig {

/// Prover configuration
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct ProverConfig {
pub struct BatchProverConfig {
/// Prover run mode
pub proving_mode: ProverGuestRunConfig,
/// Average number of commitments to prove
Expand All @@ -41,7 +41,7 @@ pub struct ProverConfig {
pub enable_recovery: bool,
}

impl Default for ProverConfig {
impl Default for BatchProverConfig {
fn default() -> Self {
Self {
proving_mode: ProverGuestRunConfig::Execute,
Expand All @@ -53,12 +53,13 @@ impl Default for ProverConfig {

#[cfg(test)]
mod tests {
use std::io::Write;
use std::{
fs::File,
io::{Read, Write},
path::Path,
};

use serde::de::DeserializeOwned;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use tempfile::NamedTempFile;

use super::*;
Expand Down Expand Up @@ -91,8 +92,8 @@ mod tests {

let config_file = create_config_from(config);

let config: ProverConfig = from_toml_path(config_file.path()).unwrap();
let expected = ProverConfig {
let config: BatchProverConfig = from_toml_path(config_file.path()).unwrap();
let expected = BatchProverConfig {
proving_mode: ProverGuestRunConfig::Skip,
proof_sampling_number: 500,
enable_recovery: true,
Expand Down
75 changes: 75 additions & 0 deletions src/citrea_config/light_client_prover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use serde::{Deserialize, Serialize};

use super::batch_prover::ProverGuestRunConfig;

/// Light client prover configuration
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct LightClientProverConfig {
/// Prover run mode
pub proving_mode: ProverGuestRunConfig,
/// Average number of commitments to prove
pub proof_sampling_number: usize,
/// If true prover will try to recover ongoing proving sessions
pub enable_recovery: bool,
}

impl Default for LightClientProverConfig {
fn default() -> Self {
Self {
proving_mode: ProverGuestRunConfig::Execute,
proof_sampling_number: 0,
enable_recovery: true,
}
}
}

#[cfg(test)]
mod tests {
use std::{
fs::File,
io::{Read, Write},
path::Path,
};

use serde::de::DeserializeOwned;
use tempfile::NamedTempFile;

use super::*;

/// Reads toml file as a specific type.
pub fn from_toml_path<P: AsRef<Path>, R: DeserializeOwned>(path: P) -> anyhow::Result<R> {
let mut contents = String::new();
{
let mut file = File::open(path)?;
file.read_to_string(&mut contents)?;
}
let result: R = toml::from_str(&contents)?;

Ok(result)
}

fn create_config_from(content: &str) -> NamedTempFile {
let mut config_file = NamedTempFile::new().unwrap();
config_file.write_all(content.as_bytes()).unwrap();
config_file
}

#[test]
fn test_correct_prover_config() {
let config = r#"
proving_mode = "skip"
proof_sampling_number = 500
enable_recovery = true
"#;

let config_file = create_config_from(config);

let config: LightClientProverConfig = from_toml_path(config_file.path()).unwrap();
let expected = LightClientProverConfig {
proving_mode: ProverGuestRunConfig::Skip,
proof_sampling_number: 500,
enable_recovery: true,
};
assert_eq!(config, expected);
}
}
3 changes: 2 additions & 1 deletion src/citrea_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// Should ideally be automatically kept in sync somehow but manually copied here for the time being.
// Configs are stable and not expected to change much.

pub(crate) mod batch_prover;
pub(crate) mod bitcoin;
pub(crate) mod prover;
pub(crate) mod light_client_prover;
pub(crate) mod rollup;
pub(crate) mod sequencer;

Expand Down
15 changes: 15 additions & 0 deletions src/citrea_config/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct RunnerConfig {
/// Number of blocks to request during sync
#[serde(default = "default_sync_blocks_count")]
pub sync_blocks_count: u64,
/// Configurations for pruning
pub pruning_config: Option<PruningConfig>,
}

/// RPC configuration.
Expand Down Expand Up @@ -118,3 +120,16 @@ pub struct FullNodeConfig<BitcoinServiceConfig> {
/// Important pubkeys
pub public_keys: RollupPublicKeys,
}

/// A configuration type to define the behaviour of the pruner.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct PruningConfig {
/// Defines the number of blocks from the tip of the chain to remove.
pub distance: u64,
}

impl Default for PruningConfig {
fn default() -> Self {
Self { distance: 256 }
}
}
3 changes: 1 addition & 2 deletions src/citrea_config/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ mod tests {

use tempfile::NamedTempFile;

use crate::citrea_config::from_toml_path;

use super::*;
use crate::citrea_config::from_toml_path;

fn create_config_from(content: &str) -> NamedTempFile {
let mut config_file = NamedTempFile::new().unwrap();
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
24 changes: 15 additions & 9 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ mod utils;

use std::path::PathBuf;

pub use crate::citrea_config::bitcoin::BitcoinServiceConfig;
pub use crate::citrea_config::prover::ProverConfig;
pub use crate::citrea_config::rollup::{
FullNodeConfig, RollupPublicKeys, RpcConfig, RunnerConfig, StorageConfig,
};
pub use crate::citrea_config::sequencer::SequencerConfig;
pub use bitcoin::BitcoinConfig;
pub use docker::DockerConfig;
pub use rollup::{default_rollup_config, RollupConfig};
Expand All @@ -21,6 +15,13 @@ pub use test::TestConfig;
pub use test_case::{TestCaseConfig, TestCaseEnv};
pub use utils::config_to_file;

pub use crate::citrea_config::{
batch_prover::BatchProverConfig,
bitcoin::BitcoinServiceConfig,
light_client_prover::LightClientProverConfig,
rollup::{FullNodeConfig, RollupPublicKeys, RpcConfig, RunnerConfig, StorageConfig},
sequencer::SequencerConfig,
};
use crate::node::{Config, NodeKind};

#[derive(Clone, Debug)]
Expand All @@ -33,7 +34,8 @@ pub struct FullL2NodeConfig<T> {
}

pub type FullSequencerConfig = FullL2NodeConfig<SequencerConfig>;
pub type FullProverConfig = FullL2NodeConfig<ProverConfig>;
pub type FullBatchProverConfig = FullL2NodeConfig<BatchProverConfig>;
pub type FullLightClientProverConfig = FullL2NodeConfig<LightClientProverConfig>;
pub type FullFullNodeConfig = FullL2NodeConfig<()>;

pub trait NodeKindMarker {
Expand All @@ -44,8 +46,12 @@ impl NodeKindMarker for FullSequencerConfig {
const KIND: NodeKind = NodeKind::Sequencer;
}

impl NodeKindMarker for FullProverConfig {
const KIND: NodeKind = NodeKind::Prover;
impl NodeKindMarker for FullBatchProverConfig {
const KIND: NodeKind = NodeKind::BatchProver;
}

impl NodeKindMarker for FullLightClientProverConfig {
const KIND: NodeKind = NodeKind::LightClientProver;
}

impl NodeKindMarker for FullFullNodeConfig {
Expand Down
7 changes: 4 additions & 3 deletions src/config/test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use super::{
bitcoin::BitcoinConfig, test_case::TestCaseConfig, FullFullNodeConfig, FullProverConfig,
FullSequencerConfig,
bitcoin::BitcoinConfig, test_case::TestCaseConfig, FullBatchProverConfig, FullFullNodeConfig,
FullLightClientProverConfig, FullSequencerConfig,
};

#[derive(Clone)]
pub struct TestConfig {
pub test_case: TestCaseConfig,
pub bitcoin: Vec<BitcoinConfig>,
pub sequencer: FullSequencerConfig,
pub prover: FullProverConfig,
pub batch_prover: FullBatchProverConfig,
pub light_client_prover: FullLightClientProverConfig,
pub full_node: FullFullNodeConfig,
}
18 changes: 12 additions & 6 deletions src/config/test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ pub struct TestCaseEnv {
pub test: Vec<(&'static str, &'static str)>,
pub full_node: Vec<(&'static str, &'static str)>,
pub sequencer: Vec<(&'static str, &'static str)>,
pub prover: 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 @@ -25,8 +26,12 @@ impl TestCaseEnv {
[self.test_env(), self.sequencer.clone()].concat()
}

pub fn prover(&self) -> Vec<(&'static str, &'static str)> {
[self.test_env(), self.prover.clone()].concat()
pub fn batch_prover(&self) -> Vec<(&'static str, &'static str)> {
[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)> {
Expand All @@ -43,8 +48,8 @@ pub struct TestCaseConfig {
pub n_nodes: usize,
pub with_sequencer: bool,
pub with_full_node: bool,
pub with_prover: bool,
#[allow(unused)]
pub with_batch_prover: bool,
pub with_light_client_prover: bool,
pub timeout: Duration,
pub dir: PathBuf,
pub docker: bool,
Expand All @@ -59,7 +64,8 @@ impl Default for TestCaseConfig {
TestCaseConfig {
n_nodes: 1,
with_sequencer: true,
with_prover: false,
with_batch_prover: false,
with_light_client_prover: false,
with_full_node: false,
timeout: Duration::from_secs(60),
dir: TempDir::new()
Expand Down
Loading

0 comments on commit 629e82f

Please sign in to comment.