mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add CachedReads::extend (#12277)
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
@ -57,6 +57,15 @@ impl CachedReads {
|
||||
) {
|
||||
self.accounts.insert(address, CachedAccount { info: Some(info), storage });
|
||||
}
|
||||
|
||||
/// Extends current cache with entries from another [`CachedReads`] instance.
|
||||
///
|
||||
/// Note: It is expected that both instances are based on the exact same state.
|
||||
pub fn extend(&mut self, other: Self) {
|
||||
self.accounts.extend(other.accounts);
|
||||
self.contracts.extend(other.contracts);
|
||||
self.block_hashes.extend(other.block_hashes);
|
||||
}
|
||||
}
|
||||
|
||||
/// A [Database] that caches reads inside [`CachedReads`].
|
||||
@ -184,3 +193,57 @@ impl CachedAccount {
|
||||
Self { info, storage: HashMap::default() }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_extend_with_two_cached_reads() {
|
||||
// Setup test data
|
||||
let hash1 = B256::from_slice(&[1u8; 32]);
|
||||
let hash2 = B256::from_slice(&[2u8; 32]);
|
||||
let address1 = Address::from_slice(&[1u8; 20]);
|
||||
let address2 = Address::from_slice(&[2u8; 20]);
|
||||
|
||||
// Create primary cache
|
||||
let mut primary = {
|
||||
let mut cache = CachedReads::default();
|
||||
cache.accounts.insert(address1, CachedAccount::new(Some(AccountInfo::default())));
|
||||
cache.contracts.insert(hash1, Bytecode::default());
|
||||
cache.block_hashes.insert(1, hash1);
|
||||
cache
|
||||
};
|
||||
|
||||
// Create additional cache
|
||||
let additional = {
|
||||
let mut cache = CachedReads::default();
|
||||
cache.accounts.insert(address2, CachedAccount::new(Some(AccountInfo::default())));
|
||||
cache.contracts.insert(hash2, Bytecode::default());
|
||||
cache.block_hashes.insert(2, hash2);
|
||||
cache
|
||||
};
|
||||
|
||||
// Extending primary with additional cache
|
||||
primary.extend(additional);
|
||||
|
||||
// Verify the combined state
|
||||
assert!(
|
||||
primary.accounts.len() == 2 &&
|
||||
primary.contracts.len() == 2 &&
|
||||
primary.block_hashes.len() == 2,
|
||||
"All maps should contain 2 entries"
|
||||
);
|
||||
|
||||
// Verify specific entries
|
||||
assert!(
|
||||
primary.accounts.contains_key(&address1) &&
|
||||
primary.accounts.contains_key(&address2) &&
|
||||
primary.contracts.contains_key(&hash1) &&
|
||||
primary.contracts.contains_key(&hash2) &&
|
||||
primary.block_hashes.get(&1) == Some(&hash1) &&
|
||||
primary.block_hashes.get(&2) == Some(&hash2),
|
||||
"All expected entries should be present"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user