Skip to content

Commit

Permalink
Make UnboundedCache a newtype and implement ser. logic
Browse files Browse the repository at this point in the history
After we implemented serialization logic for all the sub-types in the
last commit, here we switch the `UnboundedCache` type to be a newtype
wrapper around a `HashMap` (rather than a straight typedef) and
implement TLV-based serialization logic on it.
  • Loading branch information
tnull committed Feb 13, 2025
1 parent 5055994 commit bed21f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
12 changes: 6 additions & 6 deletions lightning-block-sync/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ mod tests {
(fork_chain_3.tip().block_hash, &listener_3 as &dyn chain::Listen),
];
let mut cache = fork_chain_1.header_cache(2..=4);
cache.extend(fork_chain_2.header_cache(3..=4));
cache.extend(fork_chain_3.header_cache(4..=4));
cache.inner.extend(fork_chain_2.header_cache(3..=4).inner);
cache.inner.extend(fork_chain_3.header_cache(4..=4).inner);
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
Ok(header) => assert_eq!(header, main_chain.tip()),
Err(e) => panic!("Unexpected error: {:?}", e),
Expand Down Expand Up @@ -364,8 +364,8 @@ mod tests {
(fork_chain_3.tip().block_hash, &listener_3 as &dyn chain::Listen),
];
let mut cache = fork_chain_1.header_cache(2..=4);
cache.extend(fork_chain_2.header_cache(3..=4));
cache.extend(fork_chain_3.header_cache(4..=4));
cache.inner.extend(fork_chain_2.header_cache(3..=4).inner);
cache.inner.extend(fork_chain_3.header_cache(4..=4).inner);
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
Ok(header) => assert_eq!(header, main_chain.tip()),
Err(e) => panic!("Unexpected error: {:?}", e),
Expand All @@ -387,8 +387,8 @@ mod tests {
let mut cache = fork_chain.header_cache(2..=2);
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
Ok(_) => {
assert!(cache.contains_key(&new_tip.block_hash));
assert!(cache.contains_key(&old_tip.block_hash));
assert!(cache.inner.contains_key(&new_tip.block_hash));
assert!(cache.inner.contains_key(&old_tip.block_hash));
},
Err(e) => panic!("Unexpected error: {:?}", e),
}
Expand Down
23 changes: 19 additions & 4 deletions lightning-block-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use bitcoin::hash_types::BlockHash;
use bitcoin::pow::Work;

use lightning::chain::Listen;
use lightning::util::hash_tables::{new_hash_map, HashMap};
use lightning::{chain, impl_writeable_tlv_based};

use std::future::Future;
Expand Down Expand Up @@ -218,22 +219,36 @@ pub trait Cache {
}

/// Unbounded cache of block headers keyed by block hash.
pub type UnboundedCache = std::collections::HashMap<BlockHash, ValidatedBlockHeader>;
pub struct UnboundedCache {
pub(crate) inner: HashMap<BlockHash, ValidatedBlockHeader>,
}

impl UnboundedCache {
/// Returns a new `UnboundedCache`
pub fn new() -> Self {
let inner = new_hash_map();
Self { inner }
}
}

impl Cache for UnboundedCache {
fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader> {
self.get(block_hash)
self.inner.get(block_hash)
}

fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
self.insert(block_hash, block_header);
self.inner.insert(block_hash, block_header);
}

fn block_disconnected(&mut self, block_hash: &BlockHash) -> Option<ValidatedBlockHeader> {
self.remove(block_hash)
self.inner.remove(block_hash)
}
}

impl_writeable_tlv_based!(UnboundedCache, {
(0, inner, required),
});

impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L>
where
L::Target: chain::Listen,
Expand Down
2 changes: 1 addition & 1 deletion lightning-block-sync/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl Blockchain {
for i in heights {
let value = self.at_height(i);
let key = value.header.block_hash();
assert!(cache.insert(key, value).is_none());
assert!(cache.inner.insert(key, value).is_none());
}
cache
}
Expand Down

0 comments on commit bed21f8

Please sign in to comment.