Skip to content

Commit

Permalink
Fork Manager (#922)
Browse files Browse the repository at this point in the history
* Fork Manager

* Pass around fork manager

* Implement ForkMigration for ledgerDB

* Commit missing files

* Move SpecId to sov interface

* Pass SpecID around

* Cleanups

* Register block with fork manager

* Pass active fork down to verifier

* Add type alias

* Register block with fullnode and prover

* Invoke spec_activated callback

* Fix comments

* Fix tests

* Fix send / sync issue

* Fix too many arguments

* Set default fork specs

* Log fork activation

* Use forks instead of config

* Remove activated config

* Remove fork_specs from config

* Mark FORKS as pub crate

* Clippy

* Deref instead of clone

* Clippy again

* Update current_spec inside guest code

* Fix lints

* Fix prover tests

* Remove dummy forks

* Add fork manager tests

* Pass current_spec to HookSoftConfirmationInfo

* Add current_spec to RuntimeTxHook

* Fix test compilation

* Fix nostd checks

* Allow module inception

* Fix clippy warning

* Pass specId to dispatch_call

* Fix module dispatch tests

* Store SpecId directly

* Fix rpc tests

* Remove need for lazy_static

* Fix macros RPC tests
  • Loading branch information
rakanalh authored Aug 11, 2024
1 parent 70e19be commit 0ab85c2
Show file tree
Hide file tree
Showing 67 changed files with 1,021 additions and 400 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ futures = "0.3"
pin-project = { version = "1.1.3" }
hashbrown = { version = "0.14", default-features = false, features = ["ahash"] }
hex = { version = "0.4.3", default-features = false, features = ["alloc", "serde"] }
lazy_static = { version = "1.5.0" }
log-panics = { version = "2", features = ["with-backtrace"] }
once_cell = { version = "1.19.0", default-features = false, features = ["alloc"] }
prometheus = { version = "0.13.3", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions bin/citrea/provers/risc0/guest-bitcoin/Cargo.lock

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

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_main]
use bitcoin_da::spec::RollupParams;
use bitcoin_da::verifier::BitcoinVerifier;

use citrea_primitives::{DA_TX_ID_LEADING_ZEROS, ROLLUP_NAME};
use citrea_stf::runtime::Runtime;
use citrea_stf::StfVerifier;
Expand Down
11 changes: 11 additions & 0 deletions bin/citrea/provers/risc0/guest-mock/Cargo.lock

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

47 changes: 45 additions & 2 deletions bin/citrea/src/rollup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
use anyhow::anyhow;
use async_trait::async_trait;
pub use bitcoin::*;
use citrea_fullnode::{CitreaFullnode, FullNode};
use citrea_primitives::fork::ForkManager;
use citrea_primitives::forks::FORKS;
use citrea_prover::{CitreaProver, Prover};
use citrea_sequencer::{CitreaSequencer, Sequencer, SequencerConfig};
pub use mock::*;
use sov_db::ledger_db::SharedLedgerOps;
use sov_db::schema::types::BatchNumber;
use sov_modules_api::storage::HierarchicalStorageManager;
use sov_modules_api::Spec;
use sov_modules_rollup_blueprint::RollupBlueprint;
use sov_modules_stf_blueprint::{Runtime as RuntimeTrait, StfBlueprint};
use sov_rollup_interface::spec::SpecId;
use sov_state::storage::NativeStorage;
use sov_stf_runner::{FullNodeConfig, InitVariant, ProverConfig};
use tokio::sync::broadcast;
use tracing::instrument;

mod bitcoin;
mod mock;
pub use bitcoin::*;
pub use mock::*;

/// Overrides RollupBlueprint methods
#[async_trait]
Expand Down Expand Up @@ -79,6 +85,18 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
},
};

let current_l2_height = ledger_db
.get_head_soft_batch()
.map_err(|e| anyhow!("Failed to get head soft batch: {}", e))?
.map(|(l2_height, _)| l2_height)
.unwrap_or(BatchNumber(0));

let active_spec: SpecId = ledger_db.get_active_fork()?;

let mut fork_manager =
ForkManager::new(current_l2_height.into(), active_spec, FORKS.to_vec());
fork_manager.register_handler(Box::new(ledger_db.clone()));

let seq = CitreaSequencer::new(
da_service,
prover_storage,
Expand All @@ -89,6 +107,7 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
rollup_config.public_keys,
ledger_db,
rollup_config.rpc,
fork_manager,
soft_confirmation_tx,
)
.unwrap();
Expand Down Expand Up @@ -161,6 +180,17 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {

let code_commitment = self.get_code_commitment();

let current_l2_height = ledger_db
.get_head_soft_batch()
.map_err(|e| anyhow!("Failed to get head soft batch: {}", e))?
.map(|(l2_height, _)| l2_height)
.unwrap_or(BatchNumber(0));

let active_spec: SpecId = ledger_db.get_active_fork()?;
let mut fork_manager =
ForkManager::new(current_l2_height.into(), active_spec, FORKS.to_vec());
fork_manager.register_handler(Box::new(ledger_db.clone()));

let runner = CitreaFullnode::new(
runner_config,
rollup_config.public_keys,
Expand All @@ -172,6 +202,7 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
init_variant,
code_commitment,
rollup_config.sync_blocks_count,
fork_manager,
soft_confirmation_tx,
)?;

Expand Down Expand Up @@ -248,6 +279,17 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {

let code_commitment = self.get_code_commitment();

let current_l2_height = ledger_db
.get_head_soft_batch()
.map_err(|e| anyhow!("Failed to get head soft batch: {}", e))?
.map(|(l2_height, _)| l2_height)
.unwrap_or(BatchNumber(0));

let active_spec: SpecId = ledger_db.get_active_fork()?;
let mut fork_manager =
ForkManager::new(current_l2_height.into(), active_spec, FORKS.to_vec());
fork_manager.register_handler(Box::new(ledger_db.clone()));

let runner = CitreaProver::new(
runner_config,
rollup_config.public_keys,
Expand All @@ -261,6 +303,7 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
Some(prover_config),
code_commitment,
rollup_config.sync_blocks_count,
fork_manager,
soft_confirmation_tx,
)?;

Expand Down
1 change: 1 addition & 0 deletions crates/citrea-stf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sov-state = { path = "../sovereign-sdk/module-system/sov-state" }
sov-stf-runner = { path = "../sovereign-sdk/full-node/sov-stf-runner" }

citrea-evm = { path = "../evm" }
citrea-primitives = { path = "../primitives" }
soft-confirmation-rule-enforcer = { path = "../soft-confirmation-rule-enforcer" }

[dev-dependencies]
Expand Down
6 changes: 5 additions & 1 deletion crates/citrea-stf/src/hooks_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ impl<C: Context, Da: DaSpec> TxHooks for Runtime<C, Da> {
working_set: &mut WorkingSet<C>,
arg: &RuntimeTxHook<C>,
) -> anyhow::Result<C> {
let RuntimeTxHook { height, sequencer } = arg;
let RuntimeTxHook {
height,
sequencer,
current_spec: _current_spec,
} = arg;
let AccountsTxHook { sender, sequencer } =
self.accounts
.pre_dispatch_tx_hook(tx, working_set, sequencer)?;
Expand Down
2 changes: 2 additions & 0 deletions crates/citrea-stf/src/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::marker::PhantomData;

use citrea_primitives::forks::FORKS;
use sov_rollup_interface::da::{BlockHeaderTrait, DaVerifier};
use sov_rollup_interface::stf::StateTransitionFunction;
use sov_rollup_interface::zk::{StateTransition, StateTransitionData, Zkvm, ZkvmGuest};
Expand Down Expand Up @@ -61,6 +62,7 @@ where
data.da_block_headers_of_soft_confirmations,
&validity_condition,
data.soft_confirmations,
FORKS.to_vec(),
);

println!("out of apply_soft_confirmations_from_sequencer_commitments");
Expand Down
1 change: 1 addition & 0 deletions crates/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ reth-provider = { workspace = true }
revm = { workspace = true, features = ["optional_block_gas_limit", "optional_eip3607", "optional_no_base_fee"] }
sov-modules-api = { path = "../sovereign-sdk/module-system/sov-modules-api", features = ["macros"] }
sov-prover-storage-manager = { path = "../sovereign-sdk/full-node/sov-prover-storage-manager", features = ["test-utils"] }
sov-rollup-interface = { path = "../sovereign-sdk/rollup-interface", features = ["testing"] }
tempfile = { workspace = true }
tracing-subscriber = { workspace = true }
walkdir = "2.3.3"
Expand Down
12 changes: 12 additions & 0 deletions crates/evm/src/tests/call_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::hooks::HookSoftConfirmationInfo;
use sov_modules_api::utils::generate_address;
use sov_modules_api::{Context, Module, StateMapAccessor, StateVecAccessor};
use sov_rollup_interface::spec::SpecId as SovSpecId;

use crate::call::CallMessage;
use crate::evm::primitive_types::Receipt;
Expand Down Expand Up @@ -55,6 +56,7 @@ fn call_multiple_test() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [10u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down Expand Up @@ -163,6 +165,7 @@ fn call_test() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [10u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down Expand Up @@ -242,6 +245,7 @@ fn failed_transaction_test() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [10u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down Expand Up @@ -299,6 +303,7 @@ fn self_destruct_test() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [10u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down Expand Up @@ -359,6 +364,7 @@ fn self_destruct_test() {
da_slot_height: 2,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [99u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down Expand Up @@ -440,6 +446,7 @@ fn test_block_hash_in_evm() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [10u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down Expand Up @@ -475,6 +482,7 @@ fn test_block_hash_in_evm() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [99u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down Expand Up @@ -551,6 +559,7 @@ fn test_block_gas_limit() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [10u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down Expand Up @@ -830,6 +839,7 @@ fn test_l1_fee_success() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [10u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down Expand Up @@ -933,6 +943,7 @@ fn test_l1_fee_not_enough_funds() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [10u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down Expand Up @@ -993,6 +1004,7 @@ fn test_l1_fee_halt() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [10u8; 32].to_vec(),
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand Down
Loading

0 comments on commit 0ab85c2

Please sign in to comment.