-
Notifications
You must be signed in to change notification settings - Fork 96
feat: prune InnerForest
#1635
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
base: main
Are you sure you want to change the base?
feat: prune InnerForest
#1635
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| use std::collections::{BTreeMap, BTreeSet, HashSet}; | ||
| use std::mem::size_of; | ||
| use std::ops::RangeInclusive; | ||
| use std::path::PathBuf; | ||
|
|
||
| use anyhow::Context; | ||
| use diesel::{Connection, QueryableByName, RunQueryDsl, SqliteConnection}; | ||
| use miden_node_proto::domain::account::{AccountInfo, AccountSummary}; | ||
| use miden_node_proto::generated as proto; | ||
| use miden_node_utils::limiter::MAX_RESPONSE_PAYLOAD_BYTES; | ||
| use miden_node_utils::tracing::OpenTelemetrySpanExt; | ||
| use miden_protocol::Word; | ||
| use miden_protocol::account::{AccountHeader, AccountId, AccountStorageHeader}; | ||
|
|
@@ -600,13 +602,109 @@ impl Db { | |
| &self, | ||
| account_id: AccountId, | ||
| block_range: RangeInclusive<BlockNumber>, | ||
| entries_limit: Option<usize>, | ||
| ) -> Result<StorageMapValuesPage> { | ||
| let entries_limit = entries_limit.unwrap_or_else(|| { | ||
| // TODO: These limits should be given by the protocol. | ||
| // See miden-base/issues/1770 for more details | ||
| pub const ROW_OVERHEAD_BYTES: usize = | ||
| 2 * size_of::<Word>() + size_of::<u32>() + size_of::<u8>(); // key + value + block_num + slot_idx | ||
| MAX_RESPONSE_PAYLOAD_BYTES / ROW_OVERHEAD_BYTES | ||
| }); | ||
|
|
||
| self.transact("select storage map sync values", move |conn| { | ||
| models::queries::select_account_storage_map_values(conn, account_id, block_range) | ||
| models::queries::select_account_storage_map_values_paged( | ||
| conn, | ||
| account_id, | ||
| block_range, | ||
| entries_limit, | ||
| ) | ||
| }) | ||
| .await | ||
| } | ||
|
|
||
| /// Reconstructs storage map details from the database for a specific slot at a block. | ||
| /// | ||
| /// Used as fallback when `InnerForest` cache misses (historical or evicted queries). | ||
| /// Rebuilds all entries by querying the DB and filtering to the specific slot. | ||
| /// | ||
| /// Returns: | ||
| /// - `::LimitExceeded` when too many entries are present | ||
| /// - `::AllEntries` if the size is sufficiently small | ||
| pub(crate) async fn reconstruct_storage_map_from_db( | ||
| &self, | ||
| account_id: AccountId, | ||
| slot_name: miden_protocol::account::StorageSlotName, | ||
| block_num: BlockNumber, | ||
| entries_limit: Option<usize>, | ||
| ) -> Result<miden_node_proto::domain::account::AccountStorageMapDetails> { | ||
| use miden_node_proto::domain::account::AccountStorageMapDetails; | ||
| use miden_protocol::EMPTY_WORD; | ||
| use miden_protocol::account::StorageSlotName; | ||
|
|
||
| // TODO this remains expensive with a large history until we implement pruning for DB | ||
| // columns | ||
| let mut values = Vec::new(); | ||
| let mut block_range_start = BlockNumber::GENESIS; | ||
| let entries_limit = entries_limit.unwrap_or_else(|| { | ||
| // TODO: These limits should be given by the protocol. | ||
| // See miden-base/issues/1770 for more details | ||
| pub const ROW_OVERHEAD_BYTES: usize = | ||
| 2 * size_of::<Word>() + size_of::<u32>() + size_of::<u8>(); // key + value + block_num + slot_idx | ||
| MAX_RESPONSE_PAYLOAD_BYTES / ROW_OVERHEAD_BYTES | ||
| }); | ||
|
|
||
| let mut page = self | ||
| .select_storage_map_sync_values( | ||
| account_id, | ||
| block_range_start..=block_num, | ||
| Some(entries_limit), | ||
| ) | ||
| .await?; | ||
|
|
||
| values.extend(page.values); | ||
|
|
||
| loop { | ||
| if page.last_block_included == block_num || page.last_block_included < block_range_start | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| block_range_start = page.last_block_included.child(); | ||
| page = self | ||
| .select_storage_map_sync_values( | ||
| account_id, | ||
| block_range_start..=block_num, | ||
| Some(entries_limit), | ||
| ) | ||
| .await?; | ||
|
|
||
| values.extend(page.values); | ||
| } | ||
|
|
||
| if page.last_block_included != block_num { | ||
| return Ok(AccountStorageMapDetails::limit_exceeded(StorageSlotName::mock(0))); | ||
|
||
| } | ||
|
|
||
| // Filter to the specific slot and collect latest values per key | ||
| let mut latest_values = BTreeMap::<Word, Word>::new(); | ||
| for value in values { | ||
| if value.slot_name == slot_name { | ||
| latest_values.insert(value.key, value.value); | ||
| } | ||
| } | ||
|
|
||
| // Remove EMPTY_WORD entries (deletions) | ||
| latest_values.retain(|_, v| *v != EMPTY_WORD); | ||
|
|
||
| if latest_values.len() > AccountStorageMapDetails::MAX_RETURN_ENTRIES { | ||
| return Ok(AccountStorageMapDetails::limit_exceeded(StorageSlotName::mock(0))); | ||
|
||
| } | ||
|
|
||
| let entries = Vec::from_iter(latest_values.into_iter()); | ||
| Ok(AccountStorageMapDetails::from_forest_entries(slot_name, entries)) | ||
| } | ||
|
|
||
| /// Emits size metrics for each table in the database, and the entire database. | ||
| #[instrument(target = COMPONENT, skip_all, err)] | ||
| pub async fn analyze_table_sizes(&self) -> Result<(), DatabaseError> { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.