1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 09:54:09 +01:00
electrs/src/tracker.rs

94 lines
2.6 KiB
Rust
Raw Normal View History

2021-03-26 09:05:58 +01:00
use anyhow::{Context, Result};
use bitcoin::{BlockHash, Txid};
2021-03-26 09:05:58 +01:00
use crate::{
cache::Cache,
chain::Chain,
config::Config,
daemon::Daemon,
db::DBStore,
index::Index,
mempool::{Histogram, Mempool},
metrics::Metrics,
signals::ExitFlag,
status::{Balance, HistoryEntry, ScriptHashStatus, UnspentEntry},
2021-03-26 09:05:58 +01:00
};
/// Electrum protocol subscriptions' tracker
pub struct Tracker {
index: Index,
mempool: Mempool,
metrics: Metrics,
2021-05-08 10:14:12 +02:00
ignore_mempool: bool,
2021-03-26 09:05:58 +01:00
}
impl Tracker {
pub fn new(config: &Config) -> Result<Self> {
let metrics = Metrics::new(config.monitoring_addr)?;
let store = DBStore::open(&config.db_path, config.auto_reindex)?;
2021-03-26 09:05:58 +01:00
let chain = Chain::new(config.network);
Ok(Self {
2021-09-21 08:03:24 +02:00
index: Index::load(
store,
chain,
&metrics,
config.index_batch_size,
config.index_lookup_limit,
)
.context("failed to open index")?,
2021-03-26 09:05:58 +01:00
mempool: Mempool::new(),
metrics,
2021-05-08 10:14:12 +02:00
ignore_mempool: config.ignore_mempool,
2021-03-26 09:05:58 +01:00
})
}
pub(crate) fn chain(&self) -> &Chain {
self.index.chain()
}
pub(crate) fn fees_histogram(&self) -> &Histogram {
2021-07-20 17:56:43 +02:00
self.mempool.fees_histogram()
2021-03-26 09:05:58 +01:00
}
pub(crate) fn metrics(&self) -> &Metrics {
&self.metrics
}
2021-08-13 19:34:32 +02:00
pub(crate) fn get_history(&self, status: &ScriptHashStatus) -> Vec<HistoryEntry> {
status.get_history(self.index.chain(), &self.mempool)
2021-03-26 09:05:58 +01:00
}
pub(crate) fn get_unspent(&self, status: &ScriptHashStatus) -> Vec<UnspentEntry> {
status.get_unspent(self.index.chain())
}
pub(crate) fn sync(&mut self, daemon: &Daemon, exit_flag: &ExitFlag) -> Result<()> {
self.index.sync(daemon, exit_flag)?;
2021-05-08 10:14:12 +02:00
if !self.ignore_mempool {
2021-06-04 11:49:37 +02:00
self.mempool.sync(daemon);
2021-05-08 10:14:12 +02:00
}
2021-03-26 09:05:58 +01:00
// TODO: double check tip - and retry on diff
Ok(())
}
2021-09-08 17:46:28 +02:00
pub(crate) fn update_scripthash_status(
2021-03-26 09:05:58 +01:00
&self,
2021-08-13 19:34:32 +02:00
status: &mut ScriptHashStatus,
2021-03-26 09:05:58 +01:00
daemon: &Daemon,
cache: &Cache,
) -> Result<bool> {
let prev_statushash = status.statushash();
status.sync(&self.index, &self.mempool, daemon, cache)?;
Ok(prev_statushash != status.statushash())
}
pub(crate) fn get_balance(&self, status: &ScriptHashStatus) -> Balance {
status.get_balance(self.chain())
2021-03-26 09:05:58 +01:00
}
pub(crate) fn get_blockhash_by_txid(&self, txid: Txid) -> Option<BlockHash> {
2021-03-26 09:05:58 +01:00
// Note: there are two blocks with coinbase transactions having same txid (see BIP-30)
self.index.filter_by_txid(txid).next()
}
}