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

feat(katana): special block hash registry contract #2894

Merged
merged 6 commits into from
Jan 11, 2025
Merged
Changes from 4 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
42 changes: 39 additions & 3 deletions crates/katana/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use std::sync::Arc;
use gas_oracle::L1GasOracle;
use katana_executor::{ExecutionOutput, ExecutionResult, ExecutorFactory};
use katana_primitives::block::{
FinalityStatus, Header, PartialHeader, SealedBlock, SealedBlockWithStatus,
BlockHash, BlockNumber, FinalityStatus, Header, PartialHeader, SealedBlock,
SealedBlockWithStatus,
};
use katana_primitives::chain_spec::ChainSpec;
use katana_primitives::da::L1DataAvailabilityMode;
use katana_primitives::env::BlockEnv;
use katana_primitives::receipt::{Event, ReceiptWithTxHash};
use katana_primitives::state::{compute_state_diff_hash, StateUpdates};
use katana_primitives::transaction::{TxHash, TxWithHash};
use katana_primitives::Felt;
use katana_primitives::{address, ContractAddress, Felt};
use katana_provider::traits::block::{BlockHashProvider, BlockWriter};
use katana_provider::traits::trie::TrieWriter;
use katana_trie::compute_merkle_root;
Expand Down Expand Up @@ -49,7 +50,7 @@ impl<EF: ExecutorFactory> Backend<EF> {
pub fn do_mine_block(
&self,
block_env: &BlockEnv,
execution_output: ExecutionOutput,
mut execution_output: ExecutionOutput,
) -> Result<MinedBlockOutcome, BlockProductionError> {
// we optimistically allocate the maximum amount possible
let mut txs = Vec::with_capacity(execution_output.transactions.len());
Expand All @@ -68,6 +69,12 @@ impl<EF: ExecutorFactory> Backend<EF> {
let tx_count = txs.len() as u32;
let tx_hashes = txs.iter().map(|tx| tx.hash).collect::<Vec<TxHash>>();

// Update special contract address 0x1
self.update_block_hash_registry_contract(
&mut execution_output.states.state_updates,
block_env.number,
)?;

// create a new block and compute its commitment
let block = self.commit_block(
block_env.clone(),
Expand All @@ -93,6 +100,35 @@ impl<EF: ExecutorFactory> Backend<EF> {
Ok(MinedBlockOutcome { block_number, txs: tx_hashes, stats: execution_output.stats })
}

// TODO: create a dedicated class for this contract.
// https://docs.starknet.io/architecture-and-concepts/network-architecture/starknet-state/#address_0x1
fn update_block_hash_registry_contract(
&self,
state_updates: &mut StateUpdates,
block_number: BlockNumber,
) -> Result<(), BlockProductionError> {
const STORED_BLOCK_HASH_BUFFER: u64 = 10;

if block_number >= STORED_BLOCK_HASH_BUFFER {
let block_number = block_number - STORED_BLOCK_HASH_BUFFER;
let block_hash = self.blockchain.provider().block_hash_by_num(block_number)?;

// When in forked mode, we might not have the older block hash in the database. This
// could be the case where the `block_number - STORED_BLOCK_HASH_BUFFER` is
// earlier than the forked block, which right now, Katana doesn't
// yet have the ability to fetch older blocks on the database level. So, we default to
// `BlockHash::ZERO` in this case.
//
// TODO: Fix quick!
let block_hash = block_hash.unwrap_or(BlockHash::ZERO);

let storages = state_updates.storage_updates.entry(address!("0x1")).or_default();
storages.insert(block_number.into(), block_hash);
}

Ok(())
}

pub fn update_block_env(&self, block_env: &mut BlockEnv) {
let mut context_gen = self.block_context_generator.write();
let current_timestamp_secs = get_current_timestamp().as_secs() as i64;
Expand Down
Loading