Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ check:
@echo -e "$(PASS)All code quality checks passed!$(RESET)"

.PHONY: fmt
fmt: setup-cairo
fmt:
@if [ -z "$(NO_CAIRO_SETUP)" ]; then \
$(MAKE) --silent setup-cairo; \
fi
@echo -e "$(DIM)Running code formatters...$(RESET)"
@echo -e "$(INFO)Running taplo formatter...$(RESET)"
@npm install
Expand Down
2 changes: 2 additions & 0 deletions madara/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
Expand Up @@ -226,6 +226,7 @@ impl ExecutorThread {
&exec_ctx,
state.state_adaptor,
|block_n| self.wait_for_hash_of_block_min_10(block_n),
None, // Use backend's chain_config (normal execution)
)?;

Ok(ExecutorStateExecuting {
Expand Down
239 changes: 227 additions & 12 deletions madara/crates/client/block_production/src/lib.rs

Large diffs are not rendered by default.

45 changes: 42 additions & 3 deletions madara/crates/client/block_production/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use blockifier::{
transaction::transaction_execution::Transaction,
};
use mc_db::MadaraBackend;
use mc_exec::{LayeredStateAdapter, MadaraBackendExecutionExt};
use mc_exec::LayeredStateAdapter;
use mp_block::header::{BlockTimestamp, GasPrices, PreconfirmedHeader};
use mp_chain_config::{L1DataAvailabilityMode, StarknetVersion};
use mp_class::ConvertedClass;
Expand Down Expand Up @@ -166,6 +166,23 @@ impl BlockExecutionContext {
use_kzg_da: self.l1_da_mode == L1DataAvailabilityMode::Blob,
})
}

/// Create a BlockContext from this execution context with the given chain config and exec constants.
/// This is a helper function that encapsulates the BlockContext creation logic.
pub fn to_block_context(
&self,
chain_config: &Arc<mp_chain_config::ChainConfig>,
exec_constants: &blockifier::blockifier_versioned_constants::VersionedConstants,
) -> anyhow::Result<blockifier::context::BlockContext> {
use blockifier::context::BlockContext;
let block_info = self.to_blockifier()?;
Ok(BlockContext::new(
block_info,
chain_config.blockifier_chain_info(),
exec_constants.clone(),
chain_config.bouncer_config.clone(),
))
}
}

pub(crate) fn create_execution_context(
Expand Down Expand Up @@ -204,17 +221,39 @@ pub(crate) fn create_execution_context(
/// and sets up the block_n-10 state diff entry if available.
///
/// This is a helper function to avoid code duplication between normal block production
/// and re-execution scenarios.
/// and re-execution scenarios. It reuses `backend.new_executor_for_block_production()`
/// but allows using a custom chain_config (e.g., saved config for re-execution).
pub(crate) fn create_executor_with_block_n_min_10(
backend: &Arc<MadaraBackend>,
exec_ctx: &BlockExecutionContext,
state_adaptor: LayeredStateAdapter,
get_block_n_min_10_hash: impl FnOnce(u64) -> anyhow::Result<Option<(u64, Felt)>>,
custom_chain_config: Option<&Arc<mp_chain_config::ChainConfig>>,
) -> anyhow::Result<TransactionExecutor<LayeredStateAdapter>> {
use mc_exec::MadaraBackendExecutionExt;

// Use backend.new_executor_for_block_production() to create executor (reuses existing logic)
let block_info = exec_ctx.to_blockifier()?;
let mut executor = backend
.new_executor_for_block_production(state_adaptor, exec_ctx.to_blockifier()?)
.clone()
.new_executor_for_block_production(state_adaptor, block_info.clone())
.context("Creating TransactionExecutor")?;

// If custom_chain_config is provided, override BlockContext to use it instead of backend's config
// This is needed for re-execution scenarios where we want to use saved config
if let Some(chain_config) = custom_chain_config {
// Get exec_constants from custom chain_config
let exec_constants = chain_config
.exec_constants_by_protocol_version(exec_ctx.protocol_version)
.context("Failed to resolve execution constants for protocol version")?;

// Use to_block_context helper to create BlockContext (reuses existing logic)
let block_context = exec_ctx.to_block_context(chain_config, &exec_constants)?;
executor.block_context = Arc::new(block_context);
// Override concurrency config as well
executor.config.concurrency_config = chain_config.block_production_concurrency.blockifier_config();
}

// Prepare the block_n-10 state diff entry on the 0x1 contract
if let Some((block_n_min_10, block_hash_n_min_10)) = get_block_n_min_10_hash(exec_ctx.block_number)? {
let contract_address = 1u64.into();
Expand Down
2 changes: 2 additions & 0 deletions madara/crates/client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ opentelemetry-stdout = { workspace = true }
opentelemetry_sdk = { workspace = true, features = ["rt-tokio", "logs"] }
reqwest = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
tracing = { workspace = true }
tracing-core = { workspace = true, default-features = false }
tracing-opentelemetry = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
url = { workspace = true }


[dev-dependencies]
Expand Down
15 changes: 15 additions & 0 deletions madara/crates/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,11 @@ impl<D: MadaraStorageRead> MadaraBackend<D> {
pub fn chain_config(&self) -> &Arc<ChainConfig> {
&self.chain_config
}

/// Get the runtime execution configuration from the database.
pub fn get_runtime_exec_config(&self) -> Result<Option<mp_chain_config::RuntimeExecutionConfig>> {
self.db.get_runtime_exec_config(&self.chain_config)
}
}

/// Structure holding exclusive access to write the blocks and the tip of the chain.
Expand Down Expand Up @@ -623,6 +628,16 @@ impl<D: MadaraStorage> MadaraBackendWriter<D> {
self.replace_chain_tip(ChainTip::on_confirmed_block_n_or_empty(self.inner.latest_confirmed_block_n()))
}

/// Write the runtime execution configuration to the database.
pub fn write_runtime_exec_config(&self, config: &mp_chain_config::RuntimeExecutionConfig) -> Result<()> {
self.inner.db.write_runtime_exec_config(config)
}

/// Clear the runtime execution configuration from the database.
pub fn clear_runtime_exec_config(&self) -> Result<()> {
self.inner.db.clear_runtime_exec_config()
}

/// Start a new preconfirmed block on top of the latest confirmed block. Deletes and replaces the current preconfirmed block if present.
/// Warning: Caller is responsible for ensuring the block_number is the one following the current confirmed block.
pub fn new_preconfirmed(&self, block: PreconfirmedBlock) -> Result<()> {
Expand Down
36 changes: 36 additions & 0 deletions madara/crates/client/db/src/rocksdb/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
storage::{DevnetPredeployedKeys, StorageChainTip, StoredChainInfo},
};
use mp_block::header::PreconfirmedHeader;
use mp_chain_config::{ChainConfig, RuntimeExecutionConfig, RuntimeExecutionConfigSerializable};
use rocksdb::{IteratorMode, ReadOptions};

pub const META_COLUMN: Column = Column::new("meta").set_point_lookup();
Expand All @@ -16,6 +17,7 @@ const META_CONFIRMED_ON_L1_TIP_KEY: &[u8] = b"CONFIRMED_ON_L1_TIP";
const META_CHAIN_TIP_KEY: &[u8] = b"CHAIN_TIP";
const META_CHAIN_INFO_KEY: &[u8] = b"CHAIN_INFO";
const META_LATEST_APPLIED_TRIE_UPDATE: &[u8] = b"LATEST_APPLIED_TRIE_UPDATE";
const META_RUNTIME_EXEC_CONFIG_KEY: &[u8] = b"RUNTIME_EXEC_CONFIG";
const META_SNAP_SYNC_LATEST_BLOCK: &[u8] = b"SNAP_SYNC_LATEST_BLOCK";

#[derive(serde::Deserialize, serde::Serialize)]
Expand Down Expand Up @@ -233,6 +235,40 @@ impl RocksDBStorageInner {
Ok(())
}

/// Write the runtime execution configuration to the database.
#[tracing::instrument(skip(self, config))]
pub(super) fn write_runtime_exec_config(&self, config: &RuntimeExecutionConfig) -> Result<()> {
let serializable = config.to_serializable()?;
self.db.put_cf_opt(
&self.get_column(META_COLUMN),
META_RUNTIME_EXEC_CONFIG_KEY,
super::serialize(&serializable)?,
&self.writeopts,
)?;
Ok(())
}

/// Get the runtime execution configuration from the database.
/// Note: This requires a backend_chain_config to reconstruct the full ChainConfig.
#[tracing::instrument(skip(self))]
pub(super) fn get_runtime_exec_config(
&self,
backend_chain_config: &ChainConfig,
) -> Result<Option<RuntimeExecutionConfig>> {
let Some(res) = self.db.get_pinned_cf(&self.get_column(META_COLUMN), META_RUNTIME_EXEC_CONFIG_KEY)? else {
return Ok(None);
};
let serializable: RuntimeExecutionConfigSerializable = super::deserialize(&res)?;
Ok(Some(RuntimeExecutionConfig::from_serializable(serializable, backend_chain_config)?))
}

/// Clear the runtime execution configuration from the database.
#[tracing::instrument(skip(self))]
pub(super) fn clear_runtime_exec_config(&self) -> Result<()> {
self.db.delete_cf_opt(&self.get_column(META_COLUMN), META_RUNTIME_EXEC_CONFIG_KEY, &self.writeopts)?;
Ok(())
}

/// Get the latest block number where snap sync computed the trie.
/// Returns None if snap sync was never used.
#[tracing::instrument(skip(self))]
Expand Down
16 changes: 15 additions & 1 deletion madara/crates/client/db/src/rocksdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod events_bloom_filter;
mod iter_pinned;
mod l1_to_l2_messages;
mod mempool;
mod meta;
pub mod meta;
mod metrics;
mod options;
mod rocksdb_snapshot;
Expand Down Expand Up @@ -320,6 +320,12 @@ impl MadaraStorageRead for RocksDBStorage {
fn get_latest_applied_trie_update(&self) -> Result<Option<u64>> {
self.inner.get_latest_applied_trie_update().context("Getting latest applied trie update info from db")
}
fn get_runtime_exec_config(
&self,
backend_chain_config: &mp_chain_config::ChainConfig,
) -> Result<Option<mp_chain_config::RuntimeExecutionConfig>> {
self.inner.get_runtime_exec_config(backend_chain_config).context("Getting runtime execution config from db")
}
fn get_snap_sync_latest_block(&self) -> Result<Option<u64>> {
self.inner.get_snap_sync_latest_block().context("Getting snap sync latest block from db")
}
Expand Down Expand Up @@ -456,6 +462,14 @@ impl MadaraStorageWrite for RocksDBStorage {
tracing::debug!("Write latest applied trie update block_n={block_n:?}");
self.inner.write_latest_applied_trie_update(block_n).context("Writing latest applied trie update block_n")
}
fn write_runtime_exec_config(&self, config: &mp_chain_config::RuntimeExecutionConfig) -> Result<()> {
tracing::debug!("Writing runtime execution config");
self.inner.write_runtime_exec_config(config).context("Writing runtime execution config")
}
fn clear_runtime_exec_config(&self) -> Result<()> {
tracing::debug!("Clearing runtime execution config");
self.inner.clear_runtime_exec_config().context("Clearing runtime execution config")
}
fn write_snap_sync_latest_block(&self, block_n: &Option<u64>) -> Result<()> {
tracing::debug!("Write snap sync latest block block_n={block_n:?}");
self.inner.write_snap_sync_latest_block(block_n).context("Writing snap sync latest block")
Expand Down
6 changes: 6 additions & 0 deletions madara/crates/client/db/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ pub trait MadaraStorageRead: Send + Sync + 'static {
fn get_l1_messaging_sync_tip(&self) -> Result<Option<u64>>;
fn get_stored_chain_info(&self) -> Result<Option<StoredChainInfo>>;
fn get_latest_applied_trie_update(&self) -> Result<Option<u64>>;
fn get_runtime_exec_config(
&self,
backend_chain_config: &mp_chain_config::ChainConfig,
) -> Result<Option<mp_chain_config::RuntimeExecutionConfig>>;
fn get_snap_sync_latest_block(&self) -> Result<Option<u64>>;

// L1 to L2 messages
Expand Down Expand Up @@ -166,6 +170,8 @@ pub trait MadaraStorageWrite: Send + Sync + 'static {
fn write_devnet_predeployed_keys(&self, devnet_keys: &DevnetPredeployedKeys) -> Result<()>;
fn write_chain_info(&self, info: &StoredChainInfo) -> Result<()>;
fn write_latest_applied_trie_update(&self, block_n: &Option<u64>) -> Result<()>;
fn write_runtime_exec_config(&self, config: &mp_chain_config::RuntimeExecutionConfig) -> Result<()>;
fn clear_runtime_exec_config(&self) -> Result<()>;
fn write_snap_sync_latest_block(&self, block_n: &Option<u64>) -> Result<()>;

fn remove_mempool_transactions(&self, tx_hashes: impl IntoIterator<Item = Felt>) -> Result<()>;
Expand Down
2 changes: 2 additions & 0 deletions madara/crates/primitives/chain_config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod chain_config;
mod l1_da_mode;
mod rpc_version;
mod runtime_exec_config;
mod starknet_version;

pub use chain_config::*;
pub use l1_da_mode::*;
pub use rpc_version::*;
pub use runtime_exec_config::*;
pub use starknet_version::*;
Loading