Skip to content

Commit

Permalink
port to async for container execution
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Oct 13, 2023
1 parent 2cc1e43 commit a4f0080
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 91 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/composer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ use bollard::{
},
exec::{CreateExecOptions, StartExecResults},
image::CreateImageOptions,
service::{ContainerConfig, ContainerCreateResponse, ContainerSummary},
service::{ContainerCreateResponse, ContainerSummary},
Docker,
};
use eyre::{bail, Result};
use futures_util::{StreamExt, TryStreamExt};
use serde::Serialize;

pub use bollard::service::ContainerConfig;

/// The Composer is responsible for managing the OP-UP docker containers.
#[derive(Debug)]
pub struct Composer {
Expand Down
10 changes: 10 additions & 0 deletions crates/primitives/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ impl Artifacts {
self.path().join("addresses.json")
}

/// Returns the l1 genesis file path.
pub fn l1_genesis(&self) -> PathBuf {
self.path().join("genesis-l1.json")
}

/// Returns the jwt secret file path.
pub fn jwt_secret(&self) -> PathBuf {
self.path().join("jwt-secret.txt")
}

/// Create the artifacts directory if it does not exist.
pub fn create(&self) -> Result<()> {
if !self.pwd.exists() {
Expand Down
1 change: 1 addition & 0 deletions crates/stages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ tracing.workspace = true
serde.workspace = true
serde_json.workspace = true

async-trait = "0.1.73"
project-root = "0.2.2"
5 changes: 4 additions & 1 deletion crates/stages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use async_trait::async_trait;

pub(crate) mod json;
pub(crate) mod net;

/// Stage
///
/// A stage is a synchronous step in the [Stages] executor that handles a component of the op stack.
#[async_trait]
pub trait Stage: std::fmt::Debug {
/// Execute the stage.
fn execute(&self) -> eyre::Result<()>;
async fn execute(&self) -> eyre::Result<()>;
}

/// Core Stages.
Expand Down
51 changes: 31 additions & 20 deletions crates/stages/src/stages.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use eyre::Result;
use std::rc::Rc;
use std::sync::Arc;

use op_config::Config;
use op_primitives::genesis;
Expand Down Expand Up @@ -46,14 +46,17 @@ pub struct Stages<'a> {
pub config: Config<'a>,
/// The inner stages.
pub inner: Option<Vec<Box<dyn crate::Stage>>>,
/// A docker composer.
pub composer: Option<Arc<op_composer::Composer>>,
}

impl Stages<'_> {
/// Build the default docker-based stages.
pub fn docker(
&self,
artifacts: Rc<Artifacts>,
monorepo: Rc<Monorepo>,
artifacts: Arc<Artifacts>,
monorepo: Arc<Monorepo>,
composer: Arc<op_composer::Composer>,
) -> Vec<Box<dyn crate::Stage>> {
let genesis_timestamp = genesis::current_timestamp();
let l1_client = self.config.l1_client.to_string();
Expand All @@ -62,29 +65,31 @@ impl Stages<'_> {
let challenge_agent = self.config.challenger.to_string();
vec![
Box::new(directories::Directories::new(
Rc::clone(&artifacts),
Rc::clone(&monorepo),
Arc::clone(&artifacts),
Arc::clone(&monorepo),
)),
Box::new(cannon::Prestate::new(Rc::clone(&monorepo))),
Box::new(cannon::Prestate::new(Arc::clone(&monorepo))),
Box::new(allocs::Allocs::new(
Rc::clone(&artifacts),
Rc::clone(&monorepo),
Arc::clone(&artifacts),
Arc::clone(&monorepo),
)),
Box::new(deploy_config::DeployConfig::new(
Rc::clone(&monorepo),
Arc::clone(&monorepo),
genesis_timestamp,
)),
Box::new(l1_genesis::L1Genesis::new(
Rc::clone(&monorepo),
Arc::clone(&monorepo),
genesis_timestamp,
)),
Box::new(l1_exec::Executor::new(
self.config.l1_client_port,
l1_client,
composer,
Arc::clone(&artifacts),
)),
Box::new(l2_genesis::L2Genesis::new(
self.config.l1_client_url.clone(),
Rc::clone(&monorepo),
Arc::clone(&monorepo),
)),
Box::new(contracts::Contracts::new()),
Box::new(l2_exec::Executor::new(
Expand All @@ -95,37 +100,42 @@ impl Stages<'_> {
self.config.rollup_client_port,
rollup_client,
)),
Box::new(proposer::Proposer::new(Rc::clone(&artifacts))),
Box::new(proposer::Proposer::new(Arc::clone(&artifacts))),
Box::new(batcher::Batcher::new(
Rc::clone(&artifacts),
Rc::clone(&monorepo),
Arc::clone(&artifacts),
Arc::clone(&monorepo),
)),
Box::new(challenger::Challenger::new(
Rc::clone(&artifacts),
Arc::clone(&artifacts),
challenge_agent,
)),
Box::new(stateviz::Stateviz::new(Rc::clone(&artifacts))),
Box::new(stateviz::Stateviz::new(Arc::clone(&artifacts))),
]
}

/// Execute the stages of the stack.
pub async fn execute(&self) -> eyre::Result<()> {
tracing::debug!(target: "stages", "executing stages");

let monorepo = Rc::new(Monorepo::new()?);
let monorepo = Arc::new(Monorepo::new()?);

let composer = self
.composer
.clone()
.unwrap_or(Arc::new(op_composer::Composer::new()?));

// todo: fix this to use the stack config once the artifacts directory is configurable in
// docker containers.
let artifacts = Rc::new(Artifacts::from(
let artifacts = Arc::new(Artifacts::from(
std::env::current_dir()?.join(".devnet").as_path(),
));
// let artifacts = Rc::new(Artifacts::from(self.config.artifacts.as_path()));

let docker_stages = self.docker(artifacts, monorepo);
let docker_stages = self.docker(artifacts, monorepo, composer);
let inner = self.inner.as_ref().unwrap_or(&docker_stages);

for stage in inner {
stage.execute()?;
stage.execute().await?;
}

tracing::info!(target: "stages", "finished executing stages");
Expand Down Expand Up @@ -155,6 +165,7 @@ impl<'a> From<Config<'a>> for Stages<'a> {
Self {
config,
inner: None,
composer: None,
}
}
}
12 changes: 7 additions & 5 deletions crates/stages/src/stages/allocs.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use async_trait::async_trait;
use eyre::Result;
use op_primitives::{Artifacts, Monorepo};
use std::process::Command;
use std::rc::Rc;
use std::sync::Arc;

/// Devnet Allocs Stage
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Allocs {
artifacts: Rc<Artifacts>,
monorepo: Rc<Monorepo>,
artifacts: Arc<Artifacts>,
monorepo: Arc<Monorepo>,
}

#[async_trait]
impl crate::Stage for Allocs {
/// Executes the allocs stage.
fn execute(&self) -> Result<()> {
async fn execute(&self) -> Result<()> {
tracing::info!(target: "stages", "Executing allocs stage");

let l2_genesis_file = self.monorepo.l2_genesis();
Expand Down Expand Up @@ -43,7 +45,7 @@ impl crate::Stage for Allocs {

impl Allocs {
/// Creates a new stage.
pub fn new(artifacts: Rc<Artifacts>, monorepo: Rc<Monorepo>) -> Self {
pub fn new(artifacts: Arc<Artifacts>, monorepo: Arc<Monorepo>) -> Self {
Self {
artifacts,
monorepo,
Expand Down
12 changes: 7 additions & 5 deletions crates/stages/src/stages/batcher.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use async_trait::async_trait;
use eyre::Result;
use op_primitives::{Artifacts, Monorepo};
use std::process::Command;
use std::rc::Rc;
use std::sync::Arc;

/// Batcher Stage
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Batcher {
artifacts: Rc<Artifacts>,
monorepo: Rc<Monorepo>,
artifacts: Arc<Artifacts>,
monorepo: Arc<Monorepo>,
}

#[async_trait]
impl crate::Stage for Batcher {
/// Executes the [Batcher] stage.
fn execute(&self) -> Result<()> {
async fn execute(&self) -> Result<()> {
tracing::info!(target: "stages", "Executing batcher stage");

// todo: this should be replaced with running the docker container inline through
Expand Down Expand Up @@ -49,7 +51,7 @@ impl crate::Stage for Batcher {

impl Batcher {
/// Creates a new stage.
pub fn new(artifacts: Rc<Artifacts>, monorepo: Rc<Monorepo>) -> Self {
pub fn new(artifacts: Arc<Artifacts>, monorepo: Arc<Monorepo>) -> Self {
Self {
artifacts,
monorepo,
Expand Down
10 changes: 6 additions & 4 deletions crates/stages/src/stages/cannon.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use async_trait::async_trait;
use eyre::Result;
use op_primitives::Monorepo;
use std::process::Command;
use std::rc::Rc;
use std::sync::Arc;

/// Cannon Prestate Stage
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Prestate {
monorepo: Rc<Monorepo>,
monorepo: Arc<Monorepo>,
}

#[async_trait]
impl crate::Stage for Prestate {
/// Executes the cannon prestate stage.
fn execute(&self) -> Result<()> {
async fn execute(&self) -> Result<()> {
tracing::info!(target: "stages", "Executing cannon prestate stage");

let monorepo = self.monorepo.path();
Expand Down Expand Up @@ -46,7 +48,7 @@ impl crate::Stage for Prestate {

impl Prestate {
/// Creates a new stage.
pub fn new(monorepo: Rc<Monorepo>) -> Self {
pub fn new(monorepo: Arc<Monorepo>) -> Self {
Self { monorepo }
}
}
10 changes: 6 additions & 4 deletions crates/stages/src/stages/challenger.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use async_trait::async_trait;
use eyre::Result;
use op_primitives::Artifacts;
use std::process::Command;
use std::rc::Rc;
use std::sync::Arc;

/// Challenger Stage
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Challenger {
artifacts: Rc<Artifacts>,
artifacts: Arc<Artifacts>,
challenger: String,
}

#[async_trait]
impl crate::Stage for Challenger {
/// Executes the [Challenger] stage.
fn execute(&self) -> Result<()> {
async fn execute(&self) -> Result<()> {
tracing::info!(target: "stages", "Executing challenger stage");

// todo: this should be replaced with running the docker container inline through
Expand Down Expand Up @@ -46,7 +48,7 @@ impl crate::Stage for Challenger {

impl Challenger {
/// Creates a new challenger stage.
pub fn new(artifacts: Rc<Artifacts>, challenger: String) -> Self {
pub fn new(artifacts: Arc<Artifacts>, challenger: String) -> Self {
Self {
artifacts,
challenger,
Expand Down
4 changes: 3 additions & 1 deletion crates/stages/src/stages/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use async_trait::async_trait;
use eyre::Result;

/// Contract Deployment Stage
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Contracts;

#[async_trait]
impl crate::Stage for Contracts {
/// Executes the [Contracts] stage.
fn execute(&self) -> Result<()> {
async fn execute(&self) -> Result<()> {
tracing::info!(target: "stages", "Executing contract deployment stage");

// contract deployment is already done in allocs stage
Expand Down
10 changes: 6 additions & 4 deletions crates/stages/src/stages/deploy_config.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use async_trait::async_trait;
use eyre::Result;
use op_primitives::Monorepo;
use std::rc::Rc;
use std::sync::Arc;

/// Deploy Config Stage
#[derive(Debug, Default, Clone, PartialEq)]
pub struct DeployConfig {
monorepo: Rc<Monorepo>,
monorepo: Arc<Monorepo>,
genesis_timestamp: u64,
}

#[async_trait]
impl crate::Stage for DeployConfig {
/// Executes the [DeployConfig] stage.
fn execute(&self) -> Result<()> {
async fn execute(&self) -> Result<()> {
tracing::info!(target: "stages", "Executing deploy config stage");
let deploy_config_file = self.monorepo.deploy_config();
let mut deploy_config = crate::json::read_json(&deploy_config_file)?;
Expand All @@ -29,7 +31,7 @@ impl crate::Stage for DeployConfig {

impl DeployConfig {
/// Creates a new stage.
pub fn new(monorepo: Rc<Monorepo>, genesis_timestamp: u64) -> Self {
pub fn new(monorepo: Arc<Monorepo>, genesis_timestamp: u64) -> Self {
Self {
monorepo,
genesis_timestamp,
Expand Down
Loading

0 comments on commit a4f0080

Please sign in to comment.