-
Notifications
You must be signed in to change notification settings - Fork 99
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
Open
drahnr
wants to merge
22
commits into
main
Choose a base branch
from
bernhard-cleanup-inner-forest-for-main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,063
−553
Open
feat: prune InnerForest
#1635
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4d0c39b
y
drahnr 0c96836
yay
drahnr edeb505
y
drahnr 74efb5a
yes
drahnr b201602
mepty root
drahnr e2c5289
fix test
drahnr a938411
hashing keys fun, deliberate naming
drahnr 68b4b04
add test to ensure raw and hashed keys are disambiguated
drahnr 0743c37
Merge remote-tracking branch 'origin/main' into bernhard-cleanup-inne…
drahnr 1c9bb4f
migrate to LargeSmtForest
drahnr 774c2fd
fixes
drahnr 63d4deb
remove lru dep
drahnr 65bc39d
fixup
drahnr 843dcbe
fix names
drahnr cb18729
fmt
drahnr b9f2a45
clippy
drahnr cb6cf7e
Merge remote-tracking branch 'origin/main' into bernhard-cleanup-inne…
drahnr 899fccb
docs update
drahnr 3ade4d6
self review, again
drahnr afcf072
cleanup
drahnr cd05f31
fun
drahnr ebe2955
clipp + comm
drahnr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
drahnr marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}; | ||
|
|
@@ -34,6 +36,13 @@ use crate::db::models::{Page, queries}; | |
| use crate::errors::{DatabaseError, DatabaseSetupError, NoteSyncError, StateSyncError}; | ||
| use crate::genesis::GenesisBlock; | ||
|
|
||
| const STORAGE_MAP_VALUE_PER_ROW_BYTES: usize = | ||
| 2 * size_of::<Word>() + size_of::<u32>() + size_of::<u8>(); | ||
|
|
||
| fn default_storage_map_entries_limit() -> usize { | ||
| MAX_RESPONSE_PAYLOAD_BYTES / STORAGE_MAP_VALUE_PER_ROW_BYTES | ||
| } | ||
|
|
||
| pub(crate) mod manager; | ||
|
|
||
| mod migrations; | ||
|
|
@@ -600,13 +609,106 @@ impl Db { | |
| &self, | ||
| account_id: AccountId, | ||
| block_range: RangeInclusive<BlockNumber>, | ||
| entries_limit: Option<usize>, | ||
| ) -> Result<StorageMapValuesPage> { | ||
| let entries_limit = entries_limit.unwrap_or_else(default_storage_map_entries_limit); | ||
|
|
||
| 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 less than or equal given `entries_limit`, if any | ||
| 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, StorageMapEntries}; | ||
| use miden_protocol::EMPTY_WORD; | ||
|
|
||
| // TODO this remains expensive with a large history until we implement pruning for DB | ||
| // columns | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's already done in |
||
| let mut values = Vec::new(); | ||
| let mut block_range_start = BlockNumber::GENESIS; | ||
| let entries_limit = entries_limit.unwrap_or_else(default_storage_map_entries_limit); | ||
|
|
||
| let mut page = self | ||
| .select_storage_map_sync_values( | ||
| account_id, | ||
| block_range_start..=block_num, | ||
| Some(entries_limit), | ||
| ) | ||
| .await?; | ||
|
|
||
| values.extend(page.values); | ||
| let mut last_block_included = page.last_block_included; | ||
|
|
||
| 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?; | ||
|
|
||
| if page.last_block_included <= last_block_included { | ||
| return Ok(AccountStorageMapDetails::limit_exceeded(slot_name)); | ||
| } | ||
|
|
||
| last_block_included = page.last_block_included; | ||
| values.extend(page.values); | ||
| } | ||
drahnr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if page.last_block_included != block_num { | ||
| return Ok(AccountStorageMapDetails::limit_exceeded(slot_name)); | ||
| } | ||
|
|
||
| // 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 { | ||
| let raw_key = value.key; | ||
| latest_values.insert(raw_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(slot_name)); | ||
| } | ||
|
|
||
| let entries = Vec::from_iter(latest_values.into_iter()); | ||
| Ok(AccountStorageMapDetails { | ||
| slot_name, | ||
| entries: StorageMapEntries::AllEntries(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> { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.