]> Untitled Git - bdk/commitdiff
[blockchain] impl OnlineBlockchain for types wrapped in Arc
authorAlekos Filini <alekos.filini@gmail.com>
Mon, 17 Aug 2020 08:53:38 +0000 (10:53 +0200)
committerAlekos Filini <alekos.filini@gmail.com>
Sun, 30 Aug 2020 18:38:14 +0000 (20:38 +0200)
src/blockchain/mod.rs

index a38f38b5a742d4ef8543f8a28c10d1e605ad7cef..6a602bfdd477120245cd98cc05664fe630b2dc09 100644 (file)
@@ -1,5 +1,7 @@
 use std::collections::HashSet;
+use std::ops::Deref;
 use std::sync::mpsc::{channel, Receiver, Sender};
+use std::sync::Arc;
 
 use bitcoin::{Transaction, Txid};
 
@@ -120,3 +122,52 @@ impl Progress for LogProgress {
         Ok(())
     }
 }
+
+impl<T: Blockchain> Blockchain for Arc<T> {
+    fn is_online(&self) -> bool {
+        self.deref().is_online()
+    }
+
+    fn offline() -> Self {
+        Arc::new(T::offline())
+    }
+}
+
+#[maybe_async]
+impl<T: OnlineBlockchain> OnlineBlockchain for Arc<T> {
+    fn get_capabilities(&self) -> HashSet<Capability> {
+        maybe_await!(self.deref().get_capabilities())
+    }
+
+    fn setup<D: BatchDatabase + DatabaseUtils, P: 'static + Progress>(
+        &self,
+        stop_gap: Option<usize>,
+        database: &mut D,
+        progress_update: P,
+    ) -> Result<(), Error> {
+        maybe_await!(self.deref().setup(stop_gap, database, progress_update))
+    }
+
+    fn sync<D: BatchDatabase + DatabaseUtils, P: 'static + Progress>(
+        &self,
+        stop_gap: Option<usize>,
+        database: &mut D,
+        progress_update: P,
+    ) -> Result<(), Error> {
+        maybe_await!(self.deref().sync(stop_gap, database, progress_update))
+    }
+
+    fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
+        maybe_await!(self.deref().get_tx(txid))
+    }
+    fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
+        maybe_await!(self.deref().broadcast(tx))
+    }
+
+    fn get_height(&self) -> Result<u32, Error> {
+        maybe_await!(self.deref().get_height())
+    }
+    fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
+        maybe_await!(self.deref().estimate_fee(target))
+    }
+}