Skip to content

Commit

Permalink
Use generic configs
Browse files Browse the repository at this point in the history
  • Loading branch information
jfldde committed Dec 19, 2024
1 parent b7d1e08 commit 6e38edb
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 81 deletions.
9 changes: 7 additions & 2 deletions src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::{
traits::{NodeT, Restart, SpawnOutput},
Result,
};
use crate::{log_provider::LogPathProvider, node::NodeKind};
use crate::{config::ConfigBounds, log_provider::LogPathProvider, node::NodeKind};

pub const FINALITY_DEPTH: u64 = 30;

Expand Down Expand Up @@ -293,7 +293,12 @@ pub struct BitcoinNodeCluster {
}

impl BitcoinNodeCluster {
pub async fn new(ctx: &TestContext) -> Result<Self> {
pub async fn new<S, BP, LP>(ctx: &TestContext<S, BP, LP>) -> Result<Self>
where
S: ConfigBounds,
BP: ConfigBounds,
LP: ConfigBounds,
{
let n_nodes = ctx.config.test_case.n_nodes;
let mut cluster = Self {
inner: Vec::with_capacity(n_nodes),
Expand Down
5 changes: 2 additions & 3 deletions src/config/docker.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{fmt::Debug, path::PathBuf};

use serde::Serialize;
use tracing::debug;

use super::{BitcoinConfig, FullL2NodeConfig};
use super::{BitcoinConfig, ConfigBounds, FullL2NodeConfig};
use crate::{
node::{get_citrea_args, NodeKind},
utils::get_genesis_path,
Expand Down Expand Up @@ -61,7 +60,7 @@ impl From<&BitcoinConfig> for DockerConfig {

impl<T> From<FullL2NodeConfig<T>> for DockerConfig
where
T: Clone + Debug + Serialize + Send + Sync,
T: ConfigBounds,
{
fn from(config: FullL2NodeConfig<T>) -> Self {
let kind = config.kind();
Expand Down
15 changes: 7 additions & 8 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub use crate::citrea_config::{
};
use crate::{log_provider::LogPathProvider, node::NodeKind, Result};

pub trait ConfigBounds: Clone + Serialize + Debug + Send + Sync + Default {}
impl<T> ConfigBounds for T where T: Clone + Serialize + Debug + Send + Sync + Default {}

#[derive(Clone, Debug, Default)]
pub enum DaLayer {
#[default]
Expand Down Expand Up @@ -63,7 +66,7 @@ where

impl<T> FullL2NodeConfig<T>
where
T: Clone + Serialize + Debug + Send + Sync,
T: ConfigBounds,
{
pub fn new(
kind: NodeKind,
Expand Down Expand Up @@ -100,18 +103,14 @@ where
}
}

pub type FullSequencerConfig = FullL2NodeConfig<SequencerConfig>;
pub type FullBatchProverConfig = FullL2NodeConfig<BatchProverConfig>;
pub type FullLightClientProverConfig = FullL2NodeConfig<LightClientProverConfig>;

#[derive(Serialize, Clone, Debug)]
#[derive(Serialize, Clone, Debug, Default)]
pub struct EmptyConfig;

pub type FullFullNodeConfig = FullL2NodeConfig<EmptyConfig>;

impl<T> FullL2NodeConfig<T>
where
T: Clone + Serialize + Debug + Send + Sync,
T: ConfigBounds,
{
pub fn dir(&self) -> &PathBuf {
&self.base.dir
Expand Down Expand Up @@ -183,7 +182,7 @@ where

impl<T> LogPathProvider for FullL2NodeConfig<T>
where
T: Clone + Serialize + Debug + Send + Sync,
T: ConfigBounds,
{
fn kind(&self) -> NodeKind {
self.kind()
Expand Down
17 changes: 11 additions & 6 deletions src/config/test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use super::{
bitcoin::BitcoinConfig, test_case::TestCaseConfig, FullBatchProverConfig, FullFullNodeConfig,
FullLightClientProverConfig, FullSequencerConfig,
bitcoin::BitcoinConfig, test_case::TestCaseConfig, ConfigBounds, FullFullNodeConfig,
FullL2NodeConfig,
};

#[derive(Clone)]
pub struct TestConfig {
pub struct TestConfig<S, BP, LP>
where
S: ConfigBounds,
BP: ConfigBounds,
LP: ConfigBounds,
{
pub test_case: TestCaseConfig,
pub bitcoin: Vec<BitcoinConfig>,
pub sequencer: FullSequencerConfig,
pub batch_prover: FullBatchProverConfig,
pub light_client_prover: FullLightClientProverConfig,
pub sequencer: FullL2NodeConfig<S>,
pub batch_prover: FullL2NodeConfig<BP>,
pub light_client_prover: FullL2NodeConfig<LP>,
pub full_node: FullFullNodeConfig,
}
73 changes: 49 additions & 24 deletions src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, Env
use crate::{
bitcoin::BitcoinNodeCluster,
config::{
BitcoinConfig, BitcoinServiceConfig, EmptyConfig, FullBatchProverConfig,
FullFullNodeConfig, FullLightClientProverConfig, FullSequencerConfig, RollupConfig,
RpcConfig, RunnerConfig, StorageConfig, TestCaseConfig, TestConfig,
BitcoinConfig, BitcoinServiceConfig, ConfigBounds, EmptyConfig, FullFullNodeConfig,
FullL2NodeConfig, RollupConfig, RpcConfig, RunnerConfig, StorageConfig, TestCaseConfig,
TestConfig,
},
docker::DockerEnv,
log_provider::{LogPathProvider, LogPathProviderErased},
node::{BatchProver, FullNode, LightClientProver, NodeKind, Sequencer},
node::{FullNode, Node, NodeKind},
test_case::TestCase,
traits::NodeT,
utils::{
Expand All @@ -27,26 +27,41 @@ use crate::{
Result,
};

pub struct TestContext {
pub config: TestConfig,
pub struct TestContext<S, BP, LP>
where
S: ConfigBounds,
BP: ConfigBounds,
LP: ConfigBounds,
{
pub config: TestConfig<S, BP, LP>,
pub docker: Arc<Option<DockerEnv>>,
}

impl TestContext {
fn new(config: TestConfig, docker: Option<DockerEnv>) -> Self {
impl<S, BP, LP> TestContext<S, BP, LP>
where
S: ConfigBounds,
BP: ConfigBounds,
LP: ConfigBounds,
{
fn new(config: TestConfig<S, BP, LP>, docker: Option<DockerEnv>) -> Self {
Self {
config,
docker: Arc::new(docker),
}
}
}

pub struct TestFramework {
ctx: TestContext,
pub struct TestFramework<S, BP, LP>
where
S: ConfigBounds,
BP: ConfigBounds,
LP: ConfigBounds,
{
ctx: TestContext<S, BP, LP>,
pub bitcoin_nodes: BitcoinNodeCluster,
pub sequencer: Option<Sequencer>,
pub batch_prover: Option<BatchProver>,
pub light_client_prover: Option<LightClientProver>,
pub sequencer: Option<Node<S>>,
pub batch_prover: Option<Node<BP>>,
pub light_client_prover: Option<Node<LP>>,
pub full_node: Option<FullNode>,
pub initial_da_height: u64,
}
Expand All @@ -59,8 +74,13 @@ async fn create_optional<T>(pred: bool, f: impl Future<Output = Result<T>>) -> R
}
}

impl TestFramework {
pub async fn new<T: TestCase>() -> Result<Self> {
impl<S, BP, LP> TestFramework<S, BP, LP>
where
S: ConfigBounds,
BP: ConfigBounds,
LP: ConfigBounds,
{
pub async fn new<T: TestCase<S, BP, LP>>() -> Result<Self> {
setup_logging();

let test_case = T::test_config();
Expand All @@ -69,7 +89,7 @@ impl TestFramework {
} else {
None
};
let config = generate_test_config::<T>(test_case, &docker)?;
let config = generate_test_config::<T, S, BP, LP>(test_case, &docker)?;

anyhow::ensure!(
config.test_case.n_nodes > 0,
Expand Down Expand Up @@ -99,7 +119,7 @@ impl TestFramework {
// Has to initialize sequencer first since provers and full node depend on it
self.sequencer = create_optional(
self.ctx.config.test_case.with_sequencer,
Sequencer::new(
Node::<S>::new(
&self.ctx.config.sequencer,
bitcoin_config,
Arc::clone(&self.ctx.docker),
Expand All @@ -110,15 +130,15 @@ impl TestFramework {
(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(
Node::<BP>::new(
&self.ctx.config.batch_prover,
bitcoin_config,
Arc::clone(&self.ctx.docker)
)
),
create_optional(
self.ctx.config.test_case.with_light_client_prover,
LightClientProver::new(
Node::<LP>::new(
&self.ctx.config.light_client_prover,
bitcoin_config,
Arc::clone(&self.ctx.docker)
Expand Down Expand Up @@ -260,10 +280,15 @@ impl TestFramework {
}
}

fn generate_test_config<T: TestCase>(
fn generate_test_config<T: TestCase<S, BP, LP>, S, BP, LP>(
test_case: TestCaseConfig,
docker: &Option<DockerEnv>,
) -> Result<TestConfig> {
) -> Result<TestConfig<S, BP, LP>>
where
S: ConfigBounds,
BP: ConfigBounds,
LP: ConfigBounds,
{
let env = T::test_env();
let bitcoin = T::bitcoin_config();
let batch_prover = T::batch_prover_config();
Expand Down Expand Up @@ -430,23 +455,23 @@ fn generate_test_config<T: TestCase>(
let citrea_docker_image = std::env::var("CITREA_DOCKER_IMAGE").ok();
Ok(TestConfig {
bitcoin: bitcoin_confs,
sequencer: FullSequencerConfig::new(
sequencer: FullL2NodeConfig::<S>::new(
NodeKind::Sequencer,
sequencer,
sequencer_rollup,
citrea_docker_image.clone(),
sequencer_dir,
env.sequencer(),
)?,
batch_prover: FullBatchProverConfig::new(
batch_prover: FullL2NodeConfig::<BP>::new(
NodeKind::BatchProver,
batch_prover,
batch_prover_rollup,
citrea_docker_image.clone(),
batch_prover_dir,
env.batch_prover(),
)?,
light_client_prover: FullLightClientProverConfig::new(
light_client_prover: FullL2NodeConfig::<LP>::new(
NodeKind::LightClientProver,
light_client_prover,
light_client_prover_rollup,
Expand Down
17 changes: 8 additions & 9 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
fmt::{self, Debug},
fmt::{self},
fs::File,
process::Stdio,
sync::{
Expand All @@ -12,7 +12,6 @@ use std::{
use anyhow::{bail, Context};
use async_trait::async_trait;
use bitcoincore_rpc::{Auth, Client as BitcoinClient};
use serde::Serialize;
use tokio::{
process::Command,
time::{sleep, Instant},
Expand All @@ -22,8 +21,8 @@ use tracing::{debug, info, trace};
use crate::{
client::Client,
config::{
BatchProverConfig, BitcoinConfig, DockerConfig, EmptyConfig, FullL2NodeConfig,
LightClientProverConfig, SequencerConfig,
BatchProverConfig, BitcoinConfig, ConfigBounds, DockerConfig, EmptyConfig,
FullL2NodeConfig, LightClientProverConfig, SequencerConfig,
},
docker::DockerEnv,
log_provider::LogPathProvider,
Expand Down Expand Up @@ -72,7 +71,7 @@ pub type BatchProver = Node<BatchProverConfig>;

pub struct Node<C>
where
C: Clone + Debug + Serialize + Send + Sync,
C: ConfigBounds,
{
spawn_output: SpawnOutput,
pub config: FullL2NodeConfig<C>,
Expand All @@ -83,7 +82,7 @@ where

impl<C> Node<C>
where
C: Clone + Debug + Serialize + Send + Sync,
C: ConfigBounds,
{
pub async fn new(
config: &FullL2NodeConfig<C>,
Expand Down Expand Up @@ -192,7 +191,7 @@ where
#[async_trait]
impl<C> NodeT for Node<C>
where
C: Clone + Debug + Serialize + Send + Sync,
C: ConfigBounds,
DockerConfig: From<FullL2NodeConfig<C>>,
{
type Config = FullL2NodeConfig<C>;
Expand Down Expand Up @@ -246,7 +245,7 @@ where
#[async_trait]
impl<C> Restart for Node<C>
where
C: Clone + Serialize + Debug + Send + Sync,
C: ConfigBounds,
DockerConfig: From<FullL2NodeConfig<C>>,
{
async fn wait_until_stopped(&mut self) -> Result<()> {
Expand Down Expand Up @@ -286,7 +285,7 @@ where

pub fn get_citrea_args<C>(config: &FullL2NodeConfig<C>) -> Vec<String>
where
C: Clone + Debug + Serialize + Send + Sync,
C: ConfigBounds,
{
let node_config_args = config.get_node_config_args().unwrap_or_default();
let rollup_config_args = config.get_rollup_config_args();
Expand Down
Loading

0 comments on commit 6e38edb

Please sign in to comment.