Skip to content

Commit bed21f8

Browse files
committed
Make UnboundedCache a newtype and implement ser. logic
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.
1 parent 5055994 commit bed21f8

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

lightning-block-sync/src/init.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ mod tests {
321321
(fork_chain_3.tip().block_hash, &listener_3 as &dyn chain::Listen),
322322
];
323323
let mut cache = fork_chain_1.header_cache(2..=4);
324-
cache.extend(fork_chain_2.header_cache(3..=4));
325-
cache.extend(fork_chain_3.header_cache(4..=4));
324+
cache.inner.extend(fork_chain_2.header_cache(3..=4).inner);
325+
cache.inner.extend(fork_chain_3.header_cache(4..=4).inner);
326326
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
327327
Ok(header) => assert_eq!(header, main_chain.tip()),
328328
Err(e) => panic!("Unexpected error: {:?}", e),
@@ -364,8 +364,8 @@ mod tests {
364364
(fork_chain_3.tip().block_hash, &listener_3 as &dyn chain::Listen),
365365
];
366366
let mut cache = fork_chain_1.header_cache(2..=4);
367-
cache.extend(fork_chain_2.header_cache(3..=4));
368-
cache.extend(fork_chain_3.header_cache(4..=4));
367+
cache.inner.extend(fork_chain_2.header_cache(3..=4).inner);
368+
cache.inner.extend(fork_chain_3.header_cache(4..=4).inner);
369369
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
370370
Ok(header) => assert_eq!(header, main_chain.tip()),
371371
Err(e) => panic!("Unexpected error: {:?}", e),
@@ -387,8 +387,8 @@ mod tests {
387387
let mut cache = fork_chain.header_cache(2..=2);
388388
match synchronize_listeners(&main_chain, Network::Bitcoin, &mut cache, listeners).await {
389389
Ok(_) => {
390-
assert!(cache.contains_key(&new_tip.block_hash));
391-
assert!(cache.contains_key(&old_tip.block_hash));
390+
assert!(cache.inner.contains_key(&new_tip.block_hash));
391+
assert!(cache.inner.contains_key(&old_tip.block_hash));
392392
},
393393
Err(e) => panic!("Unexpected error: {:?}", e),
394394
}

lightning-block-sync/src/lib.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use bitcoin::hash_types::BlockHash;
4949
use bitcoin::pow::Work;
5050

5151
use lightning::chain::Listen;
52+
use lightning::util::hash_tables::{new_hash_map, HashMap};
5253
use lightning::{chain, impl_writeable_tlv_based};
5354

5455
use std::future::Future;
@@ -218,22 +219,36 @@ pub trait Cache {
218219
}
219220

220221
/// Unbounded cache of block headers keyed by block hash.
221-
pub type UnboundedCache = std::collections::HashMap<BlockHash, ValidatedBlockHeader>;
222+
pub struct UnboundedCache {
223+
pub(crate) inner: HashMap<BlockHash, ValidatedBlockHeader>,
224+
}
225+
226+
impl UnboundedCache {
227+
/// Returns a new `UnboundedCache`
228+
pub fn new() -> Self {
229+
let inner = new_hash_map();
230+
Self { inner }
231+
}
232+
}
222233

223234
impl Cache for UnboundedCache {
224235
fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader> {
225-
self.get(block_hash)
236+
self.inner.get(block_hash)
226237
}
227238

228239
fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
229-
self.insert(block_hash, block_header);
240+
self.inner.insert(block_hash, block_header);
230241
}
231242

232243
fn block_disconnected(&mut self, block_hash: &BlockHash) -> Option<ValidatedBlockHeader> {
233-
self.remove(block_hash)
244+
self.inner.remove(block_hash)
234245
}
235246
}
236247

248+
impl_writeable_tlv_based!(UnboundedCache, {
249+
(0, inner, required),
250+
});
251+
237252
impl<'a, P: Poll, C: Cache, L: Deref> SpvClient<'a, P, C, L>
238253
where
239254
L::Target: chain::Listen,

lightning-block-sync/src/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl Blockchain {
131131
for i in heights {
132132
let value = self.at_height(i);
133133
let key = value.header.block_hash();
134-
assert!(cache.insert(key, value).is_none());
134+
assert!(cache.inner.insert(key, value).is_none());
135135
}
136136
cache
137137
}

0 commit comments

Comments
 (0)