use std::collections::HashSet; use std::sync::mpsc::{channel, Receiver, Sender}; use bitcoin::{Transaction, Txid}; use crate::database::{BatchDatabase, DatabaseUtils}; use crate::error::Error; pub mod utils; #[cfg(feature = "electrum")] pub mod electrum; #[cfg(feature = "electrum")] pub use self::electrum::ElectrumBlockchain; #[cfg(feature = "esplora")] pub mod esplora; #[cfg(feature = "esplora")] pub use self::esplora::EsploraBlockchain; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Capability { FullHistory, GetAnyTx, } pub trait Blockchain { fn is_online(&self) -> bool; fn offline() -> Self; } pub struct OfflineBlockchain; impl Blockchain for OfflineBlockchain { fn offline() -> Self { OfflineBlockchain } fn is_online(&self) -> bool { false } } pub trait OnlineBlockchain: Blockchain { fn get_capabilities(&self) -> HashSet; fn setup( &mut self, stop_gap: Option, database: &mut D, progress_update: P, ) -> Result<(), Error>; fn sync( &mut self, stop_gap: Option, database: &mut D, progress_update: P, ) -> Result<(), Error> { self.setup(stop_gap, database, progress_update) } fn get_tx(&mut self, txid: &Txid) -> Result, Error>; fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error>; fn get_height(&mut self) -> Result; } pub type ProgressData = (f32, Option); pub trait Progress { fn update(&self, progress: f32, message: Option) -> Result<(), Error>; } pub fn progress() -> (Sender, Receiver) { channel() } impl Progress for Sender { fn update(&self, progress: f32, message: Option) -> Result<(), Error> { if progress < 0.0 || progress > 100.0 { return Err(Error::InvalidProgressValue(progress)); } self.send((progress, message)) .map_err(|_| Error::ProgressUpdateError) } } pub struct NoopProgress; pub fn noop_progress() -> NoopProgress { NoopProgress } impl Progress for NoopProgress { fn update(&self, _progress: f32, _message: Option) -> Result<(), Error> { Ok(()) } }