use std::collections::HashMap;
use stratum_core::bitcoin::{Block, BlockHash, CompactTarget, Transaction, Wtxid};
#[derive(Default)]
pub struct MempoolMirror {
txdata: HashMap<Wtxid, Transaction>,
current_prev_hash: Option<BlockHash>,
current_nbits: Option<CompactTarget>,
current_min_ntime: Option<u32>,
}
impl MempoolMirror {
pub fn new() -> Self {
Default::default()
}
pub fn update(&mut self, block: &Block) {
let prev_hash = block.header.prev_blockhash;
if self.current_prev_hash != Some(prev_hash) {
self.txdata.clear();
}
self.current_prev_hash = Some(prev_hash);
self.current_nbits = Some(block.header.bits);
self.current_min_ntime = Some(block.header.time);
for tx in block.txdata.iter().skip(1) {
let wtxid = tx.compute_wtxid();
self.txdata.insert(wtxid, tx.clone());
}
}
pub fn add_transactions(&mut self, transactions: Vec<Transaction>) {
for tx in transactions {
let wtxid = tx.compute_wtxid();
self.txdata.insert(wtxid, tx);
}
}
pub fn get_txdata(&self, wtxids: &[Wtxid]) -> Vec<Transaction> {
wtxids
.iter()
.filter_map(|wtxid| self.txdata.get(wtxid).cloned())
.collect()
}
pub fn verify(&self, wtxids: &[Wtxid]) -> Vec<Wtxid> {
wtxids
.iter()
.filter(|&wtxid| !self.txdata.contains_key(wtxid))
.copied()
.collect()
}
pub fn get_current_prev_hash(&self) -> Option<BlockHash> {
self.current_prev_hash
}
pub fn get_current_nbits(&self) -> Option<CompactTarget> {
self.current_nbits
}
pub fn get_current_min_ntime(&self) -> Option<u32> {
self.current_min_ntime
}
}