1 use bitcoin::blockdata::blockchain::Blockchain;
2 use bitcoin::blockdata::transaction::Transaction;
3 use bitcoin::blockdata::block::Block;
4 use bitcoin::blockdata::script::Script;
5 use bitcoin::network::constants::Network;
6 use bitcoin::util::hash::Sha256dHash;
8 use chain::chaininterface::{ChainWatchInterface,ChainWatchInterfaceUtil,ChainListener};
10 use std::sync::{Mutex,Weak};
12 /// Implements a ChainWatchInterface using rust-bitcoin's Blockchain class
13 pub struct ChainWatchImpl {
14 chain: Mutex<Blockchain>,
15 util: ChainWatchInterfaceUtil
18 unsafe impl Send for ChainWatchImpl {} //TODO: GAH WTF
19 unsafe impl Sync for ChainWatchImpl {} //TODO: GAH WTF
21 impl ChainWatchInterface for ChainWatchImpl {
22 fn install_watch_script(&self, spk: Script) {
23 self.util.install_watch_script(spk)
26 fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32)) {
27 self.util.install_watch_outpoint(outpoint)
30 fn watch_all_txn(&self) {
31 self.util.watch_all_txn()
34 fn broadcast_transaction(&self, _tx: &Transaction) {
38 fn register_listener(&self, listener: Weak<ChainListener>) {
39 self.util.register_listener(listener)
44 pub fn new(network: Network) -> ChainWatchImpl {
46 chain: Mutex::new(Blockchain::new(network)),
47 util: ChainWatchInterfaceUtil::new(),
51 pub fn add_block(&mut self, block: Block) {
53 let mut txn_matched: Vec<&Transaction> = Vec::new();
54 let mut indexes_of_txn_matched = Vec::new();
55 for (idx, tx) in block.txdata.iter().enumerate() {
56 if self.util.does_match_tx(&tx) {
58 indexes_of_txn_matched.push(idx as u32);
62 self.util.do_call_block_connected(&block.header, 0, &txn_matched[..], &indexes_of_txn_matched[..]);
64 self.chain.lock().unwrap().add_block(block);