Skip to content

Commit

Permalink
Embedded elf guests for forks (#1495)
Browse files Browse the repository at this point in the history
Co-authored-by: eyusufatik <[email protected]>
Co-authored-by: jfldde <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2024
1 parent 4ac3098 commit e645f33
Show file tree
Hide file tree
Showing 39 changed files with 545 additions and 236 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Check TOML
uses: dprint/[email protected]
- name: Build guests
run: make build-risc0
- name: Run lint
run: |
if ! make lint ; then
Expand Down Expand Up @@ -163,6 +165,8 @@ jobs:
uses: ./.github/actions/install-risc0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Build guests
run: make build-risc0
- name: Run cargo-udeps
env:
RUSTFLAGS: -A warnings
Expand Down
6 changes: 5 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ tokio-util = { version = "0.7.12", features = ["rt"] }
num_cpus = "1.0"

# Risc0 dependencies
risc0-binfmt = { version = "1.1.3" }
risc0-zkvm = { version = "1.1.3", default-features = false }
risc0-zkvm-platform = { version = "1.1.3" }
risc0-zkp = { version = "1.1.3" }
Expand Down Expand Up @@ -155,7 +156,7 @@ tower-http = { version = "0.5.0", features = ["full"] }
tower = { version = "0.4.13", features = ["full"] }
hyper = { version = "1.4.0" }

citrea-e2e = { git = "https://github.com/chainwayxyz/citrea-e2e", rev = "c3f27fc" }
citrea-e2e = { git = "https://github.com/chainwayxyz/citrea-e2e", rev = "423db61" }

[patch.crates-io]
bitcoincore-rpc = { version = "0.18.0", git = "https://github.com/chainwayxyz/rust-bitcoincore-rpc.git", rev = "ca3cfa2" }
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ PARALLEL_PROOF_LIMIT := 1
help: ## Display this help message
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

.PHONY: build-risc0
build-risc0:
$(MAKE) -j 2 -C guests/risc0 all

.PHONY: build-sp1
build-sp1:
$(MAKE) -C guests/sp1 all

.PHONY: build
build: ## Build the project
@cargo build

build-release: ## Build the project in release mode
build-release: build-risc0 build-sp1 ## Build the project in release mode
@cargo build --release

clean: ## Cleans compiled
Expand All @@ -36,7 +44,7 @@ clean-all: clean clean-node clean-txs
test-legacy: ## Runs test suite with output from tests printed
@cargo test -- --nocapture -Zunstable-options --report-time

test: build $(EF_TESTS_DIR) ## Runs test suite using next test
test: build-risc0 build-sp1 build $(EF_TESTS_DIR) ## Runs test suite using next test
RISC0_DEV_MODE=1 cargo nextest run --workspace --all-features --no-fail-fast $(filter-out $@,$(MAKECMDGOALS))

install-dev-tools: ## Installs all necessary cargo helpers
Expand Down
5 changes: 3 additions & 2 deletions bin/citrea/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ borsh = { workspace = true, features = ["bytes"] }
clap = { workspace = true }
hex = { workspace = true, optional = true }
jsonrpsee = { workspace = true, features = ["http-client", "server"] }
lazy_static = { workspace = true }
log-panics = { workspace = true }
reth-primitives = { workspace = true }
reth-rpc-types = { workspace = true }
reth-transaction-pool = { workspace = true }
risc0-binfmt = { workspace = true }
secp256k1 = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down Expand Up @@ -98,8 +100,7 @@ sp1-helper = { version = "3.0.0", default-features = false }

[features]
default = [] # Deviate from convention by making the "native" feature active by default. This aligns with how this package is meant to be used (as a binary first, library second).

bench = ["hex"] # "sov-risc0-adapter/bench", "risc0/bench"]
bench = ["hex"]

[[bin]]
name = "citrea"
Expand Down
69 changes: 69 additions & 0 deletions bin/citrea/src/guests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::collections::HashMap;

use citrea_risc0_adapter::Digest;
use lazy_static::lazy_static;
use risc0_binfmt::compute_image_id;
use sov_rollup_interface::spec::SpecId;

macro_rules! guest {
($a:expr) => {{
let code = include_bytes!($a).to_vec();
let id = compute_image_id(&code).unwrap();

(id, code)
}};
}

lazy_static! {
/// The following 2 are used as latest guest builds for tests that use mock DA.
pub(crate) static ref BATCH_PROOF_LATEST_MOCK_GUESTS: HashMap<SpecId, (Digest, Vec<u8>)> = {
let mut m = HashMap::new();

m.insert(SpecId::Genesis, (Digest::new(citrea_risc0::BATCH_PROOF_MOCK_ID), citrea_risc0::BATCH_PROOF_MOCK_ELF.to_vec()));
m
};
pub(crate) static ref LIGHT_CLIENT_LATEST_MOCK_GUESTS: HashMap<SpecId, (Digest, Vec<u8>)> = {
let mut m = HashMap::new();

m.insert(SpecId::Genesis, (Digest::new(citrea_risc0::LIGHT_CLIENT_PROOF_MOCK_ID), citrea_risc0::LIGHT_CLIENT_PROOF_MOCK_ELF.to_vec()));
m
};
/// The following 2 are used as latest guest builds for tests that use Bitcoin DA.
pub(crate) static ref BATCH_PROOF_LATEST_BITCOIN_GUESTS: HashMap<SpecId, (Digest, Vec<u8>)> = {
let mut m = HashMap::new();

m.insert(SpecId::Genesis, (Digest::new(citrea_risc0::BATCH_PROOF_BITCOIN_ID), citrea_risc0::BATCH_PROOF_BITCOIN_ELF.to_vec()));
m
};
pub(crate) static ref LIGHT_CLIENT_LATEST_BITCOIN_GUESTS: HashMap<SpecId, (Digest, Vec<u8>)> = {
let mut m = HashMap::new();

m.insert(SpecId::Genesis, (Digest::new(citrea_risc0::LIGHT_CLIENT_PROOF_BITCOIN_ID), citrea_risc0::LIGHT_CLIENT_PROOF_BITCOIN_ELF.to_vec()));
m
};
/// Production guests
pub(crate) static ref BATCH_PROOF_MAINNET_GUESTS: HashMap<SpecId, (Digest, Vec<u8>)> = {
let mut m = HashMap::new();

m.insert(SpecId::Genesis, guest!("../../../resources/guests/risc0/mainnet/batch-0.elf"));
m
};
pub(crate) static ref BATCH_PROOF_TESTNET_GUESTS: HashMap<SpecId, (Digest, Vec<u8>)> = {
let mut m = HashMap::new();

m.insert(SpecId::Genesis, guest!("../../../resources/guests/risc0/testnet/batch-0.elf"));
m
};
pub(crate) static ref LIGHT_CLIENT_MAINNET_GUESTS: HashMap<SpecId, (Digest, Vec<u8>)> = {
let mut m = HashMap::new();

m.insert(SpecId::Genesis, guest!("../../../resources/guests/risc0/mainnet/light-0.elf"));
m
};
pub(crate) static ref LIGHT_CLIENT_TESTNET_GUESTS: HashMap<SpecId, (Digest, Vec<u8>)> = {
let mut m = HashMap::new();

m.insert(SpecId::Genesis, guest!("../../../resources/guests/risc0/testnet/light-0.elf"));
m
};
}
23 changes: 23 additions & 0 deletions bin/citrea/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,38 @@
use std::env;
use std::str::FromStr;

use serde::Serialize;
use sov_modules_rollup_blueprint::Network;
use tracing::Level;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{fmt, EnvFilter};

mod eth;
mod guests;
mod rollup;
pub use rollup::*;

/// The network currently running.
#[derive(clap::ValueEnum, Copy, Clone, Default, Debug, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum NetworkArg {
/// Mainnet
#[default]
Mainnet,
/// Testnet
Testnet,
}

impl From<NetworkArg> for Network {
fn from(value: NetworkArg) -> Self {
match value {
NetworkArg::Mainnet => Network::Mainnet,
NetworkArg::Testnet => Network::Testnet,
}
}
}

/// Default initialization of logging
pub fn initialize_logging(level: Level) {
let env_filter = EnvFilter::from_str(&env::var("RUST_LOG").unwrap_or_else(|_| {
Expand Down
36 changes: 32 additions & 4 deletions bin/citrea/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use core::fmt::Debug as DebugTrait;

use anyhow::Context as _;
use bitcoin_da::service::BitcoinServiceConfig;
use citrea::{initialize_logging, BitcoinRollup, CitreaRollupBlueprint, MockDemoRollup};
use citrea::{
initialize_logging, BitcoinRollup, CitreaRollupBlueprint, MockDemoRollup, NetworkArg,
};
use citrea_common::{
from_toml_path, BatchProverConfig, FromEnv, FullNodeConfig, LightClientProverConfig,
SequencerConfig,
Expand All @@ -11,9 +13,9 @@ use citrea_stf::genesis_config::GenesisPaths;
use clap::Parser;
use sov_mock_da::MockDaConfig;
use sov_modules_api::Spec;
use sov_modules_rollup_blueprint::RollupBlueprint;
use sov_modules_rollup_blueprint::{Network, RollupBlueprint};
use sov_state::storage::NativeStorage;
use tracing::{error, instrument};
use tracing::{error, info, instrument};

#[cfg(test)]
mod test_rpc;
Expand All @@ -23,6 +25,20 @@ mod test_rpc;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// The mode in which the node runs.
/// This determines which guest code to use.
/// Default is Mainnet.
#[clap(short, long, default_value_t, value_enum)]
network: NetworkArg,

/// Override network to run testnet directly.
#[arg(long)]
testnet: bool,

/// Run the development chain
#[arg(long, conflicts_with_all = ["testnet"])]
dev: bool,

/// Path to the genesis configuration.
/// Defines the genesis of module states like evm.
#[arg(long)]
Expand Down Expand Up @@ -131,9 +147,19 @@ async fn main() -> Result<(), anyhow::Error> {
));
}

let mut network = args.network.into();
if args.testnet {
network = Network::Testnet;
} else if args.dev {
network = Network::Nightly;
}

info!("Starting node on {network}");

match args.da_layer {
SupportedDaLayer::Mock => {
start_rollup::<MockDemoRollup, MockDaConfig>(
network,
&GenesisPaths::from_dir(&args.genesis_paths),
args.rollup_config_path,
batch_prover_config,
Expand All @@ -144,6 +170,7 @@ async fn main() -> Result<(), anyhow::Error> {
}
SupportedDaLayer::Bitcoin => {
start_rollup::<BitcoinRollup, BitcoinServiceConfig>(
network,
&GenesisPaths::from_dir(&args.genesis_paths),
args.rollup_config_path,
batch_prover_config,
Expand All @@ -159,6 +186,7 @@ async fn main() -> Result<(), anyhow::Error> {

#[instrument(level = "trace", skip_all, err)]
async fn start_rollup<S, DaC>(
network: Network,
rt_genesis_paths: &<<S as RollupBlueprint>::NativeRuntime as sov_modules_stf_blueprint::Runtime<
<S as RollupBlueprint>::NativeContext,
<S as RollupBlueprint>::DaSpec,
Expand All @@ -179,7 +207,7 @@ where
None => FullNodeConfig::from_env()
.context("Failed to read rollup configuration from the environment")?,
};
let rollup_blueprint = S::new();
let rollup_blueprint = S::new(network);

if let Some(sequencer_config) = sequencer_config {
let sequencer_rollup = rollup_blueprint
Expand Down
Loading

0 comments on commit e645f33

Please sign in to comment.