Skip to content

Commit

Permalink
record
Browse files Browse the repository at this point in the history
  • Loading branch information
Longarithm committed Nov 8, 2024
1 parent 35b9cfd commit 1acb164
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
20 changes: 11 additions & 9 deletions core/store/src/trie/mem/mem_trie_update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::cell::RefMut;
use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;

use near_primitives::errors::StorageError;
use near_primitives::hash::{hash, CryptoHash};
Expand Down Expand Up @@ -69,18 +68,22 @@ impl UpdatedMemTrieNodeWithSize {
}
}

/// Allows using in-memory tries to construct the trie node changes entirely
/// (for both in-memory and on-disk updates) because it's much faster.
pub enum TrackingMode<'a> {
/// Don't track any nodes.
None,
/// Track disk refcount changes for trie nodes.
Refcounts,
/// Track disk refcount changes and record all accessed trie nodes.
/// The latter one is needed to record storage proof which is handled by
/// `TrieRecorder`.
/// The main case why recording is needed is a branch with two children,
/// one of which got removed. In this case we need to read another child
/// and squash it together with parent.
RefcountsAndAccesses(RefMut<'a, TrieRecorder>),
}

/// Keeps hashes and encoded trie nodes accessed on updating memtrie.
pub struct TrieAccesses {
/// Hashes and encoded trie nodes.
pub nodes: HashMap<CryptoHash, Arc<[u8]>>,
}

/// Tracks intermediate trie changes, final version of which is to be committed
/// to disk after finishing trie update.
#[derive(Default)]
Expand All @@ -92,8 +95,7 @@ struct TrieChangesTracker<'a> {
/// Separated from `refcount_deleted_hashes` to postpone hash computation
/// as far as possible.
refcount_inserted_values: BTreeMap<Vec<u8>, u32>,
/// All observed internal nodes.
/// Needed to prepare recorded storage.
/// Recorder for observed internal nodes.
/// Note that negative `refcount_deleted_hashes` does not fully cover it,
/// as node or value of the same hash can be removed and inserted for the
/// same update in different parts of trie!
Expand Down
2 changes: 1 addition & 1 deletion core/store/src/trie/mem/resharding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::trie::trie_storage_update::TrieStorageUpdate;
use crate::{Trie, TrieChanges};

use super::arena::ArenaMemory;
use super::mem_trie_update::{MemTrieUpdate, TrieAccesses};
use super::mem_trie_update::MemTrieUpdate;
use near_primitives::errors::StorageError;
use near_primitives::types::{AccountId, StateRoot};
use std::ops::Range;
Expand Down
16 changes: 2 additions & 14 deletions core/store/src/trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,33 +1638,21 @@ impl Trie {

match &self.memtries {
Some(memtries) => {
// If we have in-memory tries, use it to construct the changes entirely (for
// both in-memory and on-disk updates) because it's much faster.
let guard = memtries.read().unwrap();
let tracking_mode = match &self.recorder {
Some(recorder) => TrackingMode::RefcountsAndAccesses(recorder.borrow_mut()),
None => TrackingMode::Refcounts,
};

let mut trie_update = guard.update(self.root, tracking_mode)?;
for (key, value) in changes {
match value {
Some(arr) => trie_update.insert(&key, arr)?,
None => trie_update.generic_delete(0, &key)?,
}
}
let trie_changes = trie_update.to_trie_changes();

// Retroactively record all accessed trie items which are
// required to process trie update but were not recorded at
// processing lookups.
// The main case is a branch with two children, one of which
// got removed, so we need to read another one and squash it
// together with parent.
// if let Some(recorder) = &self.recorder {
// for (node_hash, serialized_node) in trie_accesses.nodes {
// recorder.borrow_mut().record(&node_hash, serialized_node);
// }
// }
let trie_changes = trie_update.to_trie_changes();
Ok(trie_changes)
}
None => {
Expand Down

0 comments on commit 1acb164

Please sign in to comment.