Skip to content

Commit

Permalink
Re-implement citrea configs and remove related dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jfldde committed Oct 7, 2024
1 parent c9c0eee commit 669d14f
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 10 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ 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
bitcoin-da = { git = "https://github.com/chainwayxyz/citrea", rev = "82bf52d", features = ["native"] }
citrea-sequencer = { git = "https://github.com/chainwayxyz/citrea", rev = "82bf52d" }
sov-ledger-rpc = { git = "https://github.com/chainwayxyz/citrea", rev = "82bf52d", features = ["client"] }
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" }
sov-stf-runner = { git = "https://github.com/chainwayxyz/citrea", rev = "82bf52d", features = ["native"] }
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" }
3 changes: 2 additions & 1 deletion src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{
use anyhow::{bail, Context};
use async_trait::async_trait;
use bitcoin::Address;
use bitcoin_da::service::FINALITY_DEPTH;
use bitcoincore_rpc::{json::AddressType::Bech32m, Auth, Client, RpcApi};
use futures::TryStreamExt;
use tokio::{process::Command, sync::OnceCell, time::sleep};
Expand All @@ -22,6 +21,8 @@ use super::{
};
use crate::node::NodeKind;

pub const FINALITY_DEPTH: u64 = 8;

pub struct BitcoinNode {
spawn_output: SpawnOutput,
pub config: BitcoinConfig,
Expand Down
19 changes: 19 additions & 0 deletions src/citrea_config/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use serde::{Deserialize, Serialize};

/// Runtime configuration for the DA service
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct BitcoinServiceConfig {
/// The URL of the Bitcoin node to connect to
pub node_url: String,
pub node_username: String,
pub node_password: String,

// network of the bitcoin node
pub network: bitcoin::Network,

// da private key of the sequencer
pub da_private_key: Option<String>,

// absolute path to the directory where the txs will be written to
pub tx_backup_dir: String,
}
29 changes: 29 additions & 0 deletions src/citrea_config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Config imported as is from `citrea` repository `node-config` directory.
// This is done in order not to have cyclical dependencies with `citrea`.
// 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 bitcoin;
pub(crate) mod prover;
pub(crate) mod rollup;
pub(crate) mod sequencer;

#[cfg(test)]
/// Reads toml file as a specific type.
pub fn from_toml_path<P: AsRef<std::path::Path>, R: serde::de::DeserializeOwned>(
path: P,
) -> anyhow::Result<R> {
use std::{fs::File, io::Read};

let mut contents = String::new();
{
let mut file = File::open(path)?;
file.read_to_string(&mut contents)?;
}
log::debug!("Config file size: {} bytes", contents.len());
log::trace!("Config file contents: {}", &contents);

let result: R = toml::from_str(&contents)?;

Ok(result)
}
102 changes: 102 additions & 0 deletions src/citrea_config/prover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use serde::{Deserialize, Serialize};

/// The possible configurations of the prover.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ProverGuestRunConfig {
/// Skip proving.
Skip,
/// Run the rollup verification logic inside the current process.
Simulate,
/// Run the rollup verifier in a zkVM executor.
Execute,
/// Run the rollup verifier and create a SNARK of execution.
Prove,
}

impl<'de> Deserialize<'de> for ProverGuestRunConfig {
fn deserialize<D>(deserializer: D) -> Result<ProverGuestRunConfig, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match s.as_str() {
"skip" => Ok(ProverGuestRunConfig::Skip),
"simulate" => Ok(ProverGuestRunConfig::Simulate),
"execute" => Ok(ProverGuestRunConfig::Execute),
"prove" => Ok(ProverGuestRunConfig::Prove),
_ => Err(serde::de::Error::custom("invalid prover guest run config")),
}
}
}

/// Prover configuration
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct ProverConfig {
/// 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 ProverConfig {
fn default() -> Self {
Self {
proving_mode: ProverGuestRunConfig::Execute,
proof_sampling_number: 0,
enable_recovery: true,
}
}
}

#[cfg(test)]
mod tests {
use std::io::Write;

use serde::de::DeserializeOwned;
use std::fs::File;
use std::io::Read;
use std::path::Path;
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: ProverConfig = from_toml_path(config_file.path()).unwrap();
let expected = ProverConfig {
proving_mode: ProverGuestRunConfig::Skip,
proof_sampling_number: 500,
enable_recovery: true,
};
assert_eq!(config, expected);
}
}
120 changes: 120 additions & 0 deletions src/citrea_config/rollup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

/// Runner configuration.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct RunnerConfig {
/// Sequencer client configuration.
pub sequencer_client_url: String,
/// Saves sequencer soft confirmations if set to true
pub include_tx_body: bool,
/// Only true for tests
pub accept_public_input_as_proven: Option<bool>,
/// Number of blocks to request during sync
#[serde(default = "default_sync_blocks_count")]
pub sync_blocks_count: u64,
}

/// RPC configuration.
#[derive(Debug, Clone, PartialEq, Deserialize, Default, Serialize)]
pub struct RpcConfig {
/// RPC host.
pub bind_host: String,
/// RPC port.
pub bind_port: u16,
/// Maximum number of concurrent requests.
/// if not set defaults to 100.
#[serde(default = "default_max_connections")]
pub max_connections: u32,
/// Max request body request
#[serde(default = "default_max_request_body_size")]
pub max_request_body_size: u32,
/// Max response body request
#[serde(default = "default_max_response_body_size")]
pub max_response_body_size: u32,
/// Maximum number of batch requests
#[serde(default = "default_batch_requests_limit")]
pub batch_requests_limit: u32,
/// Disable subscription RPCs
#[serde(default = "default_enable_subscriptions")]
pub enable_subscriptions: bool,
/// Maximum number of subscription connections
#[serde(default = "default_max_subscriptions_per_connection")]
pub max_subscriptions_per_connection: u32,
}

#[inline]
const fn default_max_connections() -> u32 {
100
}

#[inline]
const fn default_max_request_body_size() -> u32 {
10 * 1024 * 1024
}

#[inline]
const fn default_max_response_body_size() -> u32 {
10 * 1024 * 1024
}

#[inline]
const fn default_batch_requests_limit() -> u32 {
50
}

#[inline]
const fn default_sync_blocks_count() -> u64 {
10
}

#[inline]
const fn default_enable_subscriptions() -> bool {
true
}

#[inline]
const fn default_max_subscriptions_per_connection() -> u32 {
100
}

/// Simple storage configuration
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StorageConfig {
/// Path that can be utilized by concrete rollup implementation
pub path: PathBuf,
/// File descriptor limit for RocksDB
pub db_max_open_files: Option<i32>,
}

/// Important public keys for the rollup
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct RollupPublicKeys {
/// Soft confirmation signing public key of the Sequencer
#[serde(with = "hex::serde")]
pub sequencer_public_key: Vec<u8>,
/// DA Signing Public Key of the Sequencer
/// serialized as hex
#[serde(with = "hex::serde")]
pub sequencer_da_pub_key: Vec<u8>,
/// DA Signing Public Key of the Prover
/// serialized as hex
#[serde(with = "hex::serde")]
pub prover_da_pub_key: Vec<u8>,
}

/// Rollup Configuration
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct FullNodeConfig<BitcoinServiceConfig> {
/// RPC configuration
pub rpc: RpcConfig,
/// Currently rollup config runner only supports storage path parameter
pub storage: StorageConfig,
/// Runner own configuration.
pub runner: Option<RunnerConfig>, // optional bc sequencer doesn't need it
/// Data Availability service configuration.
pub da: BitcoinServiceConfig,
/// Important pubkeys
pub public_keys: RollupPublicKeys,
}
Loading

0 comments on commit 669d14f

Please sign in to comment.