Skip to content

Commit 2ba7dbd

Browse files
authored
Merge branch 'main' into blacksmith_runners
2 parents 0706c43 + dbbfd34 commit 2ba7dbd

File tree

29 files changed

+882
-97
lines changed

29 files changed

+882
-97
lines changed

configs/args/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"l2_sync_disabled": false,
3636
"unsafe_starting_block": null,
3737
"disable_tries": false,
38+
"snap_sync": false,
3839
"gateway_key": null,
3940
"gateway_url": null,
4041
"warp_update_port_rpc": 9943,

configs/presets/mainnet.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
config_version: 1
22
chain_name: "Starknet Mainnet"
33
chain_id: "SN_MAIN"
4-
feeder_gateway_url: "https://alpha-mainnet.starknet.io/feeder_gateway/"
4+
feeder_gateway_url: "https://feeder.alpha-mainnet.starknet.io/feeder_gateway/"
55
gateway_url: "https://alpha-mainnet.starknet.io/gateway/"
66
native_fee_token_address: "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"
77
parent_fee_token_address: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"

madara/crates/client/db/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ impl<D: MadaraStorage> MadaraBackendWriter<D> {
733733
/// Write a state diff to the global tries.
734734
/// Returns the new state root.
735735
///
736-
/// **Warning**: The caller must ensure no block parts is saved on top of an existing confirmed block.
736+
/// **Warning**: The caller must ensure no block parts are saved on top of an existing confirmed block.
737737
/// You are only allowed to write block parts past the latest confirmed block.
738738
pub fn apply_to_global_trie<'a>(
739739
&self,
@@ -801,6 +801,9 @@ impl<D: MadaraStorageRead> MadaraBackend<D> {
801801
pub fn get_latest_applied_trie_update(&self) -> Result<Option<u64>> {
802802
self.db.get_latest_applied_trie_update()
803803
}
804+
pub fn get_snap_sync_latest_block(&self) -> Result<Option<u64>> {
805+
self.db.get_snap_sync_latest_block()
806+
}
804807
}
805808
// Delegate these db reads/writes. These are related to specific services, and are not specific to a block view / the chain tip writer handle.
806809
impl<D: MadaraStorageWrite> MadaraBackend<D> {
@@ -828,6 +831,9 @@ impl<D: MadaraStorageWrite> MadaraBackend<D> {
828831
pub fn write_latest_applied_trie_update(&self, block_n: &Option<u64>) -> Result<()> {
829832
self.db.write_latest_applied_trie_update(block_n)
830833
}
834+
pub fn write_snap_sync_latest_block(&self, block_n: &Option<u64>) -> Result<()> {
835+
self.db.write_snap_sync_latest_block(block_n)
836+
}
831837

832838
/// Revert the blockchain to a specific block hash.
833839
pub fn revert_to(&self, new_tip_block_hash: &Felt) -> Result<(u64, Felt)> {

madara/crates/client/db/src/rocksdb/update_global_trie/classes.rs renamed to madara/crates/client/db/src/rocksdb/global_trie/classes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn class_trie_root(
4747
#[cfg(test)]
4848
mod tests {
4949
use super::*;
50-
use crate::{rocksdb::update_global_trie::tests::setup_test_backend, MadaraBackend};
50+
use crate::{rocksdb::global_trie::tests::setup_test_backend, MadaraBackend};
5151
use rstest::*;
5252
use std::sync::Arc;
5353

madara/crates/client/db/src/rocksdb/update_global_trie/contracts.rs renamed to madara/crates/client/db/src/rocksdb/global_trie/contracts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn contract_state_leaf_hash(
136136
#[cfg(test)]
137137
mod contract_trie_root_tests {
138138
use super::*;
139-
use crate::{rocksdb::update_global_trie::tests::setup_test_backend, MadaraBackend};
139+
use crate::{rocksdb::global_trie::tests::setup_test_backend, MadaraBackend};
140140
use mp_chain_config::ChainConfig;
141141
use rstest::*;
142142
use std::sync::Arc;

madara/crates/client/db/src/rocksdb/update_global_trie/mod.rs renamed to madara/crates/client/db/src/rocksdb/global_trie/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use starknet_types_core::{
44
felt::Felt,
55
hash::{Poseidon, StarkHash},
66
};
7+
use crate::rocksdb::trie::WrappedBonsaiError;
78

89
mod classes;
910
mod contracts;
@@ -57,6 +58,18 @@ fn calculate_state_root(contracts_trie_root: Felt, classes_trie_root: Felt) -> F
5758
}
5859
}
5960

61+
pub fn get_state_root(backend: &RocksDBStorage) -> Result<Felt> {
62+
let contract_trie = backend.contract_trie();
63+
let contract_trie_root_hash = contract_trie.root_hash(bonsai_identifier::CONTRACT).map_err(WrappedBonsaiError)?;
64+
65+
let class_trie = backend.class_trie();
66+
let class_trie_root_hash = class_trie.root_hash(bonsai_identifier::CLASS).map_err(WrappedBonsaiError)?;
67+
68+
let state_root = calculate_state_root(contract_trie_root_hash, class_trie_root_hash);
69+
70+
Ok(state_root)
71+
}
72+
6073
#[cfg(test)]
6174
mod tests {
6275
use super::*;

madara/crates/client/db/src/rocksdb/meta.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const META_CONFIRMED_ON_L1_TIP_KEY: &[u8] = b"CONFIRMED_ON_L1_TIP";
1616
const META_CHAIN_TIP_KEY: &[u8] = b"CHAIN_TIP";
1717
const META_CHAIN_INFO_KEY: &[u8] = b"CHAIN_INFO";
1818
const META_LATEST_APPLIED_TRIE_UPDATE: &[u8] = b"LATEST_APPLIED_TRIE_UPDATE";
19+
const META_SNAP_SYNC_LATEST_BLOCK: &[u8] = b"SNAP_SYNC_LATEST_BLOCK";
1920

2021
#[derive(serde::Deserialize, serde::Serialize)]
2122
pub enum StoredChainTipWithoutContent {
@@ -231,4 +232,39 @@ impl RocksDBStorageInner {
231232
}
232233
Ok(())
233234
}
235+
236+
/// Get the latest block number where snap sync computed the trie.
237+
/// Returns None if snap sync was never used.
238+
#[tracing::instrument(skip(self))]
239+
pub(super) fn get_snap_sync_latest_block(&self) -> Result<Option<u64>> {
240+
let Some(res) = self.db.get_pinned_cf(&self.get_column(META_COLUMN), META_SNAP_SYNC_LATEST_BLOCK)? else {
241+
tracing::debug!("📖 Reading snap_sync_latest_block: None (snap sync never used)");
242+
return Ok(None);
243+
};
244+
let block_n = super::deserialize(&res)?;
245+
tracing::debug!("📖 Reading snap_sync_latest_block: Some({})", block_n);
246+
Ok(Some(block_n))
247+
}
248+
249+
/// Set the latest block number where snap sync computed the trie.
250+
#[tracing::instrument(skip(self))]
251+
pub(super) fn write_snap_sync_latest_block(&self, block_n: &Option<u64>) -> Result<()> {
252+
if let Some(block_n) = block_n {
253+
tracing::debug!("✍️ Setting snap_sync_latest_block to: {}", block_n);
254+
self.db.put_cf_opt(
255+
&self.get_column(META_COLUMN),
256+
META_SNAP_SYNC_LATEST_BLOCK,
257+
super::serialize_to_smallvec::<[u8; 128]>(block_n)?,
258+
&self.writeopts,
259+
)?;
260+
} else {
261+
tracing::debug!("🗑️ Clearing snap_sync_latest_block (set to None)");
262+
self.db.delete_cf_opt(
263+
&self.get_column(META_COLUMN),
264+
META_SNAP_SYNC_LATEST_BLOCK,
265+
&self.writeopts,
266+
)?;
267+
}
268+
Ok(())
269+
}
234270
}

madara/crates/client/db/src/rocksdb/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crate::{
88
metrics::DbMetrics,
99
options::rocksdb_global_options,
1010
snapshots::Snapshots,
11-
update_global_trie::apply_to_global_trie,
11+
global_trie::apply_to_global_trie,
12+
global_trie::get_state_root
1213
},
1314
storage::{
1415
ClassInfoWithBlockN, CompiledSierraWithBlockN, DevnetPredeployedKeys, EventFilter, MadaraStorageRead,
@@ -47,7 +48,7 @@ mod state;
4748
// TODO: remove this pub. this is temporary until get_storage_proof is properly abstracted.
4849
pub mod trie;
4950
// TODO: remove this pub. this is temporary until get_storage_proof is properly abstracted.
50-
pub mod update_global_trie;
51+
pub mod global_trie;
5152

5253
type WriteBatchWithTransaction = rocksdb::WriteBatchWithTransaction<false>;
5354
type DB = DBWithThreadMode<MultiThreaded>;
@@ -319,6 +320,9 @@ impl MadaraStorageRead for RocksDBStorage {
319320
fn get_latest_applied_trie_update(&self) -> Result<Option<u64>> {
320321
self.inner.get_latest_applied_trie_update().context("Getting latest applied trie update info from db")
321322
}
323+
fn get_snap_sync_latest_block(&self) -> Result<Option<u64>> {
324+
self.inner.get_snap_sync_latest_block().context("Getting snap sync latest block from db")
325+
}
322326

323327
// L1 to L2 messages
324328

@@ -452,6 +456,10 @@ impl MadaraStorageWrite for RocksDBStorage {
452456
tracing::debug!("Write latest applied trie update block_n={block_n:?}");
453457
self.inner.write_latest_applied_trie_update(block_n).context("Writing latest applied trie update block_n")
454458
}
459+
fn write_snap_sync_latest_block(&self, block_n: &Option<u64>) -> Result<()> {
460+
tracing::debug!("Write snap sync latest block block_n={block_n:?}");
461+
self.inner.write_snap_sync_latest_block(block_n).context("Writing snap sync latest block")
462+
}
455463

456464
fn remove_mempool_transactions(&self, tx_hashes: impl IntoIterator<Item = Felt>) -> Result<()> {
457465
tracing::debug!("Remove mempool transactions");
@@ -465,6 +473,10 @@ impl MadaraStorageWrite for RocksDBStorage {
465473
.with_context(|| format!("Writing mempool transaction from db for tx_hash={tx_hash:#x}"))
466474
}
467475

476+
fn get_state_root_hash(&self) -> Result<Felt> {
477+
get_state_root(self)
478+
}
479+
468480
fn apply_to_global_trie<'a>(
469481
&self,
470482
start_block_n: u64,

madara/crates/client/db/src/storage.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub trait MadaraStorageRead: Send + Sync + 'static {
130130
fn get_l1_messaging_sync_tip(&self) -> Result<Option<u64>>;
131131
fn get_stored_chain_info(&self) -> Result<Option<StoredChainInfo>>;
132132
fn get_latest_applied_trie_update(&self) -> Result<Option<u64>>;
133+
fn get_snap_sync_latest_block(&self) -> Result<Option<u64>>;
133134

134135
// L1 to L2 messages
135136

@@ -165,6 +166,7 @@ pub trait MadaraStorageWrite: Send + Sync + 'static {
165166
fn write_devnet_predeployed_keys(&self, devnet_keys: &DevnetPredeployedKeys) -> Result<()>;
166167
fn write_chain_info(&self, info: &StoredChainInfo) -> Result<()>;
167168
fn write_latest_applied_trie_update(&self, block_n: &Option<u64>) -> Result<()>;
169+
fn write_snap_sync_latest_block(&self, block_n: &Option<u64>) -> Result<()>;
168170

169171
fn remove_mempool_transactions(&self, tx_hashes: impl IntoIterator<Item = Felt>) -> Result<()>;
170172
fn write_mempool_transaction(&self, tx: &ValidatedTransaction) -> Result<()>;
@@ -185,6 +187,9 @@ pub trait MadaraStorageWrite: Send + Sync + 'static {
185187
/// Remove all blocks in the database from this block_n inclusive. This includes partially imported blocks as well.
186188
fn remove_all_blocks_starting_from(&self, starting_from_block_n: u64) -> Result<()>;
187189

190+
/// Fetches the latest global state root.
191+
fn get_state_root_hash(&self) -> Result<Felt>;
192+
188193
/// Revert the blockchain state to a specific block hash.
189194
fn revert_to(&self, new_tip_block_hash: &Felt) -> Result<(u64, Felt)>;
190195
}

madara/crates/client/db/src/view/block_preconfirmed.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use crate::{
33
prelude::*,
44
rocksdb::RocksDBStorage,
55
};
6-
use mp_block::{
7-
header::PreconfirmedHeader, FullBlockWithoutCommitments, MadaraPreconfirmedBlockInfo, TransactionWithReceipt,
8-
};
6+
use mp_block::{header::PreconfirmedHeader, FullBlockWithoutCommitments, MadaraPreconfirmedBlockInfo, TransactionWithReceipt};
97
use mp_class::ConvertedClass;
108
use mp_receipt::EventWithTransactionHash;
119
use mp_state_update::{

0 commit comments

Comments
 (0)