2021-03-26 09:05:58 +01:00
|
|
|
use anyhow::{Context, Result};
|
2022-01-08 09:36:55 +01:00
|
|
|
use bitcoin::{BlockHash, Transaction, Txid};
|
2023-08-22 19:46:53 +02:00
|
|
|
use bitcoin_slices::{
|
|
|
|
bsl::{self, FindTransaction},
|
|
|
|
Error::VisitBreak,
|
|
|
|
Visit,
|
|
|
|
};
|
2021-03-26 09:05:58 +01:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
cache::Cache,
|
|
|
|
chain::Chain,
|
|
|
|
config::Config,
|
|
|
|
daemon::Daemon,
|
|
|
|
db::DBStore,
|
|
|
|
index::Index,
|
2021-10-15 09:27:18 +02:00
|
|
|
mempool::{FeeHistogram, Mempool},
|
2021-03-26 09:05:58 +01:00
|
|
|
metrics::Metrics,
|
2021-09-21 11:47:42 +02:00
|
|
|
signals::ExitFlag,
|
2021-10-04 10:02:48 +02:00
|
|
|
status::{Balance, 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
|
|
|
}
|
|
|
|
|
2021-10-10 14:44:05 +02:00
|
|
|
pub(crate) enum Error {
|
|
|
|
NotReady,
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:05:58 +01:00
|
|
|
impl Tracker {
|
2021-10-26 12:09:03 +02:00
|
|
|
pub fn new(config: &Config, metrics: Metrics) -> Result<Self> {
|
2021-09-13 11:50:21 +02:00
|
|
|
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,
|
2021-10-11 09:26:27 +02:00
|
|
|
config.reindex_last_blocks,
|
2021-09-21 08:03:24 +02:00
|
|
|
)
|
|
|
|
.context("failed to open index")?,
|
2021-10-15 09:39:13 +02:00
|
|
|
mempool: Mempool::new(&metrics),
|
2021-03-26 09:05:58 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-10-15 09:27:18 +02:00
|
|
|
pub(crate) fn fees_histogram(&self) -> &FeeHistogram {
|
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-09-15 20:00:17 +02:00
|
|
|
pub(crate) fn get_unspent(&self, status: &ScriptHashStatus) -> Vec<UnspentEntry> {
|
|
|
|
status.get_unspent(self.index.chain())
|
|
|
|
}
|
|
|
|
|
2021-10-10 14:44:05 +02:00
|
|
|
pub(crate) fn sync(&mut self, daemon: &Daemon, exit_flag: &ExitFlag) -> Result<bool> {
|
|
|
|
let done = self.index.sync(daemon, exit_flag)?;
|
|
|
|
if done && !self.ignore_mempool {
|
2023-08-15 19:46:10 +02:00
|
|
|
self.mempool.sync(daemon, exit_flag);
|
2021-10-10 14:44:05 +02:00
|
|
|
// TODO: double check tip - and retry on diff
|
|
|
|
}
|
|
|
|
Ok(done)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn status(&self) -> Result<(), Error> {
|
|
|
|
if self.index.is_ready() {
|
|
|
|
return Ok(());
|
2021-05-08 10:14:12 +02:00
|
|
|
}
|
2021-10-10 14:44:05 +02:00
|
|
|
Err(Error::NotReady)
|
2021-03-26 09:05:58 +01:00
|
|
|
}
|
|
|
|
|
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();
|
2021-11-08 11:45:27 +01:00
|
|
|
status.sync(&self.index, &self.mempool, daemon, cache)?;
|
2021-03-26 09:05:58 +01:00
|
|
|
Ok(prev_statushash != status.statushash())
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:11:16 +02:00
|
|
|
pub(crate) fn get_balance(&self, status: &ScriptHashStatus) -> Balance {
|
|
|
|
status.get_balance(self.chain())
|
2021-03-26 09:05:58 +01:00
|
|
|
}
|
|
|
|
|
2022-01-08 09:36:55 +01:00
|
|
|
pub(crate) fn lookup_transaction(
|
|
|
|
&self,
|
|
|
|
daemon: &Daemon,
|
|
|
|
txid: Txid,
|
|
|
|
) -> Result<Option<(BlockHash, Transaction)>> {
|
2021-03-26 09:05:58 +01:00
|
|
|
// Note: there are two blocks with coinbase transactions having same txid (see BIP-30)
|
2022-01-08 09:36:55 +01:00
|
|
|
let blockhashes = self.index.filter_by_txid(txid);
|
|
|
|
let mut result = None;
|
|
|
|
daemon.for_blocks(blockhashes, |blockhash, block| {
|
2023-08-22 19:46:53 +02:00
|
|
|
let mut visitor = FindTransaction::new(txid);
|
|
|
|
match bsl::Block::visit(&block, &mut visitor) {
|
|
|
|
Ok(_) | Err(VisitBreak) => (),
|
|
|
|
Err(e) => panic!("core returned invalid block: {:?}", e),
|
|
|
|
}
|
|
|
|
if let Some(tx) = visitor.tx_found() {
|
|
|
|
result = Some((blockhash, tx));
|
2022-01-08 09:36:55 +01:00
|
|
|
}
|
|
|
|
})?;
|
|
|
|
Ok(result)
|
2021-03-26 09:05:58 +01:00
|
|
|
}
|
|
|
|
}
|