Skip to content

Commit

Permalink
sk
Browse files Browse the repository at this point in the history
  • Loading branch information
Looogarithm committed Mar 20, 2024
1 parent 2604e3d commit c50fb22
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
15 changes: 14 additions & 1 deletion core/store/src/trie/mem/updating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,25 @@ pub enum UpdatedMemTrieNode {
},
}

/// Keeps values and internal nodes accessed on updating memtrie.
pub(crate) struct TrieAccesses {
/// Hashes and encoded trie nodes.
pub nodes: HashMap<CryptoHash, Arc<[u8]>>,
/// Hashes of accessed values - because values themselves are not
/// necessarily present in memtrie.
pub values: HashSet<CryptoHash>,
}

/// Tracks intermediate trie changes, final version of which is to be committed
/// to disk after finishing trie update.
struct TrieChangesTracker {
/// Changes of reference count on disk for each impacted node.
refcount_changes: TrieRefcountDeltaMap,
/// All observed values and internal nodes.
/// Needed to prepare recorded storage.
/// Note that negative `refcount_changes` 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!
accesses: TrieAccesses,
}

Expand All @@ -64,7 +76,8 @@ pub struct MemTrieUpdate<'a> {
/// (1) temporarily we take out the node from the slot to process it and put it back
/// later; or (2) the node is deleted afterwards.
pub updated_nodes: Vec<Option<UpdatedMemTrieNode>>,
/// Refcount changes to on-disk trie nodes.
/// Tracks trie changes necessary to make on-disk updates and recorded
/// storage.
tracked_trie_changes: Option<TrieChangesTracker>,
}

Expand Down
5 changes: 5 additions & 0 deletions core/store/src/trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,8 @@ impl Trie {
}
let (trie_changes, trie_accesses) = trie_update.to_trie_changes();

// Sanity check for tests: all modified trie items must be
// present in ever accessed trie items.
#[cfg(test)]
{
for t in trie_changes.deletions.iter() {
Expand All @@ -1513,6 +1515,9 @@ impl Trie {
}
}

// Retroactively record all accessed trie items to account for
// key-value pairs which were only written but never read, thus
// not recorded before.
if let Some(recorder) = &self.recorder {
for (node_hash, serialized_node) in trie_accesses.nodes {
recorder.borrow_mut().record(&node_hash, serialized_node);
Expand Down
1 change: 0 additions & 1 deletion core/store/src/trie/trie_recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ mod trie_recording_tests {
data_in_trie.get(key).map(|value| ValueRef::new(&value))
);
}
trie.accounting_cache.borrow_mut().set_enabled(false);
assert_eq!(trie.get_trie_nodes_count(), baseline_trie_nodes_count);
trie.update(updates.iter().cloned()).unwrap();

Expand Down

0 comments on commit c50fb22

Please sign in to comment.