Skip to content

Commit

Permalink
chore: remove primitives/commitments (keep-starknet-strange#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Jan 11, 2024
1 parent 2d3f829 commit 6ab29b7
Show file tree
Hide file tree
Showing 20 changed files with 43 additions and 2,194 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Next release

- chore(primitives/commitment): remove crate
- chore(primitives/block/header): remove starknet-trie dependent fields
- refacto(primitives/db): add a temporary way to get a fake global state root
- chore: feature flags for avail and celestia DA
- feat(rpc): added support for v0.5.1 JSON-RPC specs
- feat(rpc): added ordered messages/events in trace fields
Expand Down
20 changes: 1 addition & 19 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ members = [
"crates/primitives/block",
"crates/primitives/sequencer-address",
"crates/primitives/storage",
"crates/primitives/commitments",
"crates/primitives/chain-id",
"crates/primitives/messages",
"crates/primitives/program-hash",
Expand Down Expand Up @@ -50,7 +49,6 @@ default-members = [
"crates/primitives/block",
"crates/primitives/sequencer-address",
"crates/primitives/storage",
"crates/primitives/commitments",
"crates/primitives/chain-id",
"crates/primitives/messages",
"crates/primitives/program-hash",
Expand Down Expand Up @@ -176,7 +174,6 @@ mp-snos-output = { path = "crates/primitives/snos-output", default-features = fa
mp-state = { path = "crates/primitives/state", default-features = false }
mp-storage = { path = "crates/primitives/storage", default-features = false }
mp-transactions = { path = "crates/primitives/transactions", default-features = false }
mp-commitments = { path = "crates/primitives/commitments", default-features = false }
mp-chain-id = { path = "crates/primitives/chain-id", default-features = false }
mp-simulations = { path = "crates/primitives/simulations", default-features = false }
mp-program-hash = { path = "crates/primitives/program-hash", default-features = false }
Expand Down
8 changes: 8 additions & 0 deletions crates/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use error::DbError;

mod mapping_db;
pub use mapping_db::MappingCommitment;
use starknet_api::hash::StarkHash;
mod da_db;
mod db_opening_utils;
mod messaging_db;
Expand Down Expand Up @@ -143,4 +144,11 @@ impl<B: BlockT> Backend<B> {
pub fn messaging(&self) -> &Arc<MessagingDb<B>> {
&self.messaging
}

/// In the future, we will compute the block global state root asynchronously in the client,
/// using the Starknet-Bonzai-trie.
/// That what replaces it for now :)
pub fn temporary_global_state_root_getter(&self) -> StarkHash {
Default::default()
}
}
8 changes: 4 additions & 4 deletions crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ where
block_hash: block_hash.into(),
parent_hash: Felt252Wrapper::from(parent_blockhash).into(),
block_number: starknet_block.header().block_number,
new_root: Felt252Wrapper::from(starknet_block.header().global_state_root).into(),
new_root: Felt252Wrapper::from(self.backend.temporary_global_state_root_getter()).into(),
timestamp: starknet_block.header().block_timestamp,
sequencer_address: Felt252Wrapper::from(starknet_block.header().sequencer_address).into(),
l1_gas_price: starknet_block.header().l1_gas_price.into(),
Expand Down Expand Up @@ -1097,7 +1097,7 @@ where
block_hash: block_hash.into(),
parent_hash: Felt252Wrapper::from(starknet_block.header().parent_block_hash).into(),
block_number: starknet_block.header().block_number,
new_root: Felt252Wrapper::from(starknet_block.header().global_state_root).into(),
new_root: Felt252Wrapper::from(self.backend.temporary_global_state_root_getter()).into(),
timestamp: starknet_block.header().block_timestamp,
sequencer_address: Felt252Wrapper::from(starknet_block.header().sequencer_address).into(),
transactions,
Expand Down Expand Up @@ -1144,14 +1144,14 @@ where

let parent_block =
get_block_by_block_hash(self.client.as_ref(), substrate_parent_block_hash).unwrap_or_default();
Felt252Wrapper::from(parent_block.header().global_state_root).into()
Felt252Wrapper::from(self.backend.temporary_global_state_root_getter()).into()
} else {
FieldElement::default()
};

Ok(StateUpdate {
block_hash: starknet_block.header().hash::<H>().into(),
new_root: Felt252Wrapper::from(starknet_block.header().global_state_root).into(),
new_root: Felt252Wrapper::from(self.backend.temporary_global_state_root_getter()).into(),
old_root,
state_diff: StateDiff {
storage_diffs: Vec::new(),
Expand Down
1 change: 1 addition & 0 deletions crates/client/settlement/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ sp-runtime = { workspace = true, default-features = true }

# Starknet
mc-data-availability = { workspace = true, default-features = true }
mc-db = { workspace = true, default-features = true }
mp-block = { workspace = true, default-features = true }
mp-digest-log = { workspace = true, features = ["std"] }
mp-felt = { workspace = true, default-features = true }
Expand Down
27 changes: 19 additions & 8 deletions crates/client/settlement/src/sync_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ where
pub async fn sync_state(
substrate_client: Arc<SC>,
settlement_provider: Box<dyn SettlementProvider<B>>,
madara_backend: Arc<mc_db::Backend<B>>,
retry_strategy: Box<dyn RetryStrategy<B>>,
) {
loop {
match Self::sync_state_loop(&substrate_client, settlement_provider.as_ref()).await {
match Self::sync_state_loop(&substrate_client, settlement_provider.as_ref(), &madara_backend).await {
Ok(()) => {
return;
}
Expand Down Expand Up @@ -80,7 +81,11 @@ where
/// In case chain is stuck it won't update the state, even if there are pending blocks.
/// It is ok, since it's not a normal condition, and generally we expect that the chain will
/// advance indefinitely.
async fn sync_state_loop<SP>(substrate_client: &SC, settlement_provider: &SP) -> Result<(), B>
async fn sync_state_loop<SP>(
substrate_client: &SC,
settlement_provider: &SP,
madara_backend: &mc_db::Backend<B>,
) -> Result<(), B>
where
SP: ?Sized + SettlementProvider<B>,
{
Expand All @@ -99,7 +104,7 @@ where

// If we haven't reached the settled level yet (e.g. syncing from scratch) this check will pass.
// But we need to run it again once we are up to speed.
Self::verify_starknet_state(substrate_client, &last_settled_state)?;
Self::verify_starknet_state(substrate_client, &last_settled_state, madara_backend)?;

let mut finality_notifications = substrate_client.finality_notification_stream();
let mut sync_from: u64 = last_settled_state.block_number.try_into()?;
Expand All @@ -115,7 +120,7 @@ where

if sync_from == sync_to {
log::info!("[settlement] Verifying state root for block {}", sync_to);
Self::verify_starknet_state(substrate_client, &last_settled_state)?;
Self::verify_starknet_state(substrate_client, &last_settled_state, madara_backend)?;
continue;
}

Expand All @@ -135,6 +140,7 @@ where
&next_block,
substrate_block_hash,
starknet_spec.config_hash,
madara_backend,
)
.await?;

Expand Down Expand Up @@ -204,12 +210,16 @@ where
///
/// If Madara chain haven't reached the given block level yet, it returns OK, assuming that as
/// soon as it catches up - this check will be done again.
fn verify_starknet_state(substrate_client: &SC, state: &StarknetState) -> Result<(), B> {
fn verify_starknet_state(
substrate_client: &SC,
state: &StarknetState,
madara_backend: &mc_db::Backend<B>,
) -> Result<(), B> {
let height: u64 = state.block_number.try_into()?;

match Self::get_starknet_block(substrate_client, height) {
Ok((block, _)) => {
let state_root = block.header().global_state_root;
Ok((_block, _)) => {
let state_root = madara_backend.temporary_global_state_root_getter();
// Verify that current onchain state is consistent with corresponding Madara block
if state.state_root != state_root {
return Err(Error::StateRootMismatch { height, expected: state_root, actual: state.state_root });
Expand Down Expand Up @@ -241,13 +251,14 @@ where
next_block: &StarknetBlock,
substrate_block_hash: B::Hash,
config_hash: StarkHash,
madara_backend: &mc_db::Backend<B>,
) -> Result<StarknetState, B>
where
SP: ?Sized + SettlementProvider<B>,
{
let next_state = StarknetState {
block_number: next_block.header().block_number.into(),
state_root: next_block.header().global_state_root,
state_root: madara_backend.temporary_global_state_root_getter(),
};

let mut messages_to_l1: Vec<MessageL2ToL1> = Vec::new();
Expand Down
7 changes: 6 additions & 1 deletion crates/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,12 @@ pub fn new_full(
task_manager.spawn_essential_handle().spawn(
"settlement-worker-sync-state",
Some("madara"),
SettlementWorker::<_, StarknetHasher, _>::sync_state(client.clone(), settlement_provider, retry_strategy),
SettlementWorker::<_, StarknetHasher, _>::sync_state(
client.clone(),
settlement_provider,
madara_backend.clone(),
retry_strategy,
),
);
}

Expand Down
1 change: 0 additions & 1 deletion crates/pallets/starknet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ targets = ["x86_64-unknown-linux-gnu"]
# Madara primitives
mp-block = { workspace = true }
mp-chain-id = { workspace = true }
mp-commitments = { workspace = true }
mp-digest-log = { workspace = true }
mp-fee = { workspace = true }
mp-felt = { workspace = true, features = ["parity-scale-codec", "serde"] }
Expand Down
8 changes: 0 additions & 8 deletions crates/pallets/starknet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,14 +946,9 @@ impl<T: Config> Pallet<T> {
let parent_block_hash = Self::parent_block_hash(&block_number);
let events: Vec<StarknetEvent> = transaction_hashes.iter().flat_map(TxEvents::<T>::take).collect();

let global_state_root = Felt252Wrapper::default();

let sequencer_address = Self::sequencer_address();
let block_timestamp = Self::block_timestamp();

let chain_id = Self::chain_id();
let (transaction_commitment, event_commitment) =
mp_commitments::calculate_commitments::<T::SystemHash>(&transactions, &events, chain_id);
let protocol_version = T::ProtocolVersion::get();
let extra_data = None;

Expand All @@ -964,13 +959,10 @@ impl<T: Config> Pallet<T> {
StarknetHeader::new(
parent_block_hash.into(),
block_number,
global_state_root.into(),
sequencer_address,
block_timestamp,
transaction_count as u128,
transaction_commitment.into(),
events.len() as u128,
event_commitment.into(),
protocol_version,
l1_gas_price,
extra_data,
Expand Down
16 changes: 0 additions & 16 deletions crates/primitives/block/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@ pub struct Header {
pub parent_block_hash: StarkHash,
/// The number (height) of this block.
pub block_number: u64,
/// The state commitment after this block.
pub global_state_root: StarkHash,
/// The Starknet address of the sequencer who created this block.
pub sequencer_address: ContractAddress,
/// The time the sequencer created this block before executing transactions
pub block_timestamp: u64,
/// The number of transactions in a block
pub transaction_count: u128,
/// A commitment to the transactions included in the block
pub transaction_commitment: StarkHash,
/// The number of events
pub event_count: u128,
/// A commitment to the events produced in this block
pub event_commitment: StarkHash,
/// The version of the Starknet protocol used when creating this block
pub protocol_version: u8,
/// l1 gas price for this block
Expand All @@ -49,27 +43,21 @@ impl Header {
pub fn new(
parent_block_hash: StarkHash,
block_number: u64,
global_state_root: StarkHash,
sequencer_address: ContractAddress,
block_timestamp: u64,
transaction_count: u128,
transaction_commitment: StarkHash,
event_count: u128,
event_commitment: StarkHash,
protocol_version: u8,
l1_gas_price: ResourcePrice,
extra_data: Option<U256>,
) -> Self {
Self {
parent_block_hash,
block_number,
global_state_root,
sequencer_address,
block_timestamp,
transaction_count,
transaction_commitment,
event_count,
event_commitment,
protocol_version,
l1_gas_price,
extra_data,
Expand All @@ -94,17 +82,13 @@ impl Header {
}

/// Compute the hash of the header.
#[must_use]
pub fn hash<H: HasherT>(&self) -> Felt252Wrapper {
let data: &[Felt252Wrapper] = &[
self.block_number.into(),
self.global_state_root.into(),
self.sequencer_address.0.0.into(),
self.block_timestamp.into(),
self.transaction_count.into(),
self.transaction_commitment.into(),
self.event_count.into(),
self.event_commitment.into(),
self.protocol_version.into(),
Felt252Wrapper::ZERO,
self.parent_block_hash.into(),
Expand Down
46 changes: 0 additions & 46 deletions crates/primitives/commitments/Cargo.toml

This file was deleted.

Loading

0 comments on commit 6ab29b7

Please sign in to comment.