Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(block_production): dynamic block closing #456

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- fix(block_production): dynamic block closing now adds special address with prev block hash
- fix(compilation): crate-level compilation
- chore: Move crates under a madara subdir
- chore(nix): resolve flake and direnv compatibility issues
Expand Down
64 changes: 36 additions & 28 deletions crates/madara/client/block_production/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,20 +358,45 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {
Ok(())
}

fn maybe_add_prev_block_hash(&self, state_diff: &mut StateDiff, block_n: u64) -> Result<(), Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a doc comment to this function explaining why this is needed and what it does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current name maybe_add_prev_block_hash is somewhat ambiguous and doesn't fully capture the function's role in the state management process.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make more sense now? or any suggestion what can be improved?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect with documentation 👍

if block_n >= 10 {
let prev_block_number = block_n - 10;
let prev_block_hash = self
.backend
.get_block_hash(&BlockId::Number(prev_block_number))
.map_err(|err| {
Error::Unexpected(
format!("Error fetching block hash for block {prev_block_number}: {err:#}").into(),
)
})?
.ok_or_else(|| {
Error::Unexpected(format!("No block hash found for block number {prev_block_number}").into())
})?;

state_diff.storage_diffs.push(ContractStorageDiffItem {
address: Felt::ONE,
storage_entries: vec![StorageEntry { key: Felt::from(prev_block_number), value: prev_block_hash }],
});
}
Ok(())
}

#[tracing::instrument(skip(self), fields(module = "BlockProductionTask"))]
pub async fn on_pending_time_tick(&mut self) -> Result<bool, Error> {
let current_pending_tick = self.current_pending_tick;
if current_pending_tick == 0 {
return Ok(false);
}

// Use full bouncer capacity
let bouncer_cap = self.backend.chain_config().bouncer_config.block_max_capacity;

let start_time = Instant::now();

let ContinueBlockResult { state_diff, visited_segments, bouncer_weights, stats, block_now_full } =
self.continue_block(bouncer_cap)?;
let ContinueBlockResult {
state_diff: mut new_state_diff,
visited_segments,
bouncer_weights,
stats,
block_now_full,
} = self.continue_block(self.backend.chain_config().bouncer_config.block_max_capacity)?;

if stats.n_added_to_block > 0 {
tracing::info!(
Expand All @@ -384,16 +409,19 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {

// Check if block is full
if block_now_full {
let block_n = self.block_n();
self.maybe_add_prev_block_hash(&mut new_state_diff, block_n)?;

tracing::info!("Resource limits reached, closing block early");
self.close_and_prepare_next_block(state_diff, visited_segments, start_time).await?;
self.close_and_prepare_next_block(new_state_diff, visited_segments, start_time).await?;
Trantorian1 marked this conversation as resolved.
Show resolved Hide resolved
return Ok(true);
}

// Store pending block
// todo, prefer using the block import pipeline?
self.backend.store_block(
self.block.clone().into(),
state_diff,
new_state_diff,
self.declared_classes.clone(),
Some(visited_segments),
Some(bouncer_weights),
Expand All @@ -420,27 +448,7 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {
block_now_full: _block_now_full,
} = self.continue_block(self.backend.chain_config().bouncer_config.block_max_capacity)?;

// SNOS requirement: For blocks >= 10, the hash of the block 10 blocks prior
// at address 0x1 with the block number as the key
if block_n >= 10 {
let prev_block_number = block_n - 10;
let prev_block_hash = self
.backend
.get_block_hash(&BlockId::Number(prev_block_number))
.map_err(|err| {
Error::Unexpected(
format!("Error fetching block hash for block {prev_block_number}: {err:#}").into(),
)
})?
.ok_or_else(|| {
Error::Unexpected(format!("No block hash found for block number {prev_block_number}").into())
})?;

new_state_diff.storage_diffs.push(ContractStorageDiffItem {
address: Felt::ONE,
storage_entries: vec![StorageEntry { key: Felt::from(prev_block_number), value: prev_block_hash }],
});
}
self.maybe_add_prev_block_hash(&mut new_state_diff, block_n)?;

self.close_and_prepare_next_block(new_state_diff, visited_segments, start_time).await
}
Expand Down
Loading