forked from bitcoindevkit/bdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain_oracle.rs
More file actions
77 lines (66 loc) · 2.31 KB
/
Copy pathchain_oracle.rs
File metadata and controls
77 lines (66 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use crate::collections::HashSet;
use core::marker::PhantomData;
use alloc::{collections::VecDeque, vec::Vec};
use bitcoin::BlockHash;
use crate::BlockId;
/// Represents a service that tracks the blockchain.
///
/// The main method is [`is_block_in_chain`] which determines whether a given block of [`BlockId`]
/// is an ancestor of another "static block".
///
/// [`is_block_in_chain`]: Self::is_block_in_chain
pub trait ChainOracle {
/// Error type.
type Error: core::fmt::Debug;
/// Determines whether `block` of [`BlockId`] exists as an ancestor of `static_block`.
///
/// If `None` is returned, it means the implementation cannot determine whether `block` exists.
fn is_block_in_chain(
&self,
block: BlockId,
static_block: BlockId,
) -> Result<Option<bool>, Self::Error>;
}
/// A cache structure increases the performance of getting chain data.
///
/// A simple FIFO cache replacement policy is used. Something more efficient and advanced can be
/// implemented later.
#[derive(Debug, Default)]
pub struct CacheBackend<C> {
cache: HashSet<(BlockHash, BlockHash)>,
fifo: VecDeque<(BlockHash, BlockHash)>,
marker: PhantomData<C>,
}
impl<C> CacheBackend<C> {
/// Get the number of elements in the cache.
pub fn cache_size(&self) -> usize {
self.cache.len()
}
/// Prunes the cache to reach the `max_size` target.
///
/// Returns pruned elements.
pub fn prune(&mut self, max_size: usize) -> Vec<(BlockHash, BlockHash)> {
let prune_count = self.cache.len().saturating_sub(max_size);
(0..prune_count)
.filter_map(|_| self.fifo.pop_front())
.filter(|k| self.cache.remove(k))
.collect()
}
pub fn contains(&self, static_block: BlockId, block: BlockId) -> bool {
if static_block.height < block.height
|| static_block.height == block.height && static_block.hash != block.hash
{
return false;
}
self.cache.contains(&(static_block.hash, block.hash))
}
pub fn insert(&mut self, static_block: BlockId, block: BlockId) -> bool {
let cache_key = (static_block.hash, block.hash);
if self.cache.insert(cache_key) {
self.fifo.push_back(cache_key);
true
} else {
false
}
}
}