query.rs 3.93 KB
Newer Older
1
use bincode;
2
use bitcoin::blockdata::block::BlockHeader;
3
use bitcoin::blockdata::transaction::Transaction;
4
use bitcoin::network::serialize::{deserialize, serialize};
5 6 7 8
use bitcoin::util::hash::Sha256dHash;
use itertools::enumerate;

use daemon::Daemon;
9 10
use index::{compute_script_hash, hash_prefix, HashPrefix, Index, TxInKey, TxInRow, TxKey,
            TxOutRow, HASH_PREFIX_LEN};
11
use store::Store;
12
use types::Bytes;
13 14 15 16

pub struct Query<'a> {
    store: &'a Store,
    daemon: &'a Daemon,
17
    index: &'a Index,
18 19 20
}

impl<'a> Query<'a> {
21 22 23 24 25 26
    pub fn new(store: &'a Store, daemon: &'a Daemon, index: &'a Index) -> Query<'a> {
        Query {
            store,
            daemon,
            index,
        }
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    }

    fn load_txns(&self, prefixes: Vec<HashPrefix>) -> Vec<Transaction> {
        let mut txns = Vec::new();
        for txid_prefix in prefixes {
            for row in self.store.scan(&[b"T", &txid_prefix[..]].concat()) {
                let key: TxKey = bincode::deserialize(&row.key).unwrap();
                let txid: Sha256dHash = deserialize(&key.txid).unwrap();
                let txn_bytes = self.daemon.get(&format!("tx/{}.bin", txid.be_hex_string()));
                let txn: Transaction = deserialize(&txn_bytes).unwrap();
                txns.push(txn)
            }
        }
        txns
    }

    fn find_spending_txn(&self, txid: &Sha256dHash, output_index: u32) -> Option<Transaction> {
        let spend_key = bincode::serialize(&TxInKey {
            code: b'I',
            prev_hash_prefix: hash_prefix(&txid[..]),
            prev_index: output_index as u16,
        }).unwrap();
        let mut spending: Vec<Transaction> = self.load_txns(
            self.store
                .scan(&spend_key)
                .iter()
                .map(|row| {
                    bincode::deserialize::<TxInRow>(&row.key)
                        .unwrap()
                        .txid_prefix
                })
                .collect(),
        );
        spending.retain(|item| {
            item.input
                .iter()
                .any(|input| input.prev_hash == *txid && input.prev_index == output_index)
        });
        assert!(spending.len() <= 1);
        if spending.len() == 1 {
            Some(spending.remove(0))
        } else {
            None
        }
    }

    pub fn balance(&self, script_hash: &[u8]) -> f64 {
        let mut funding: Vec<Transaction> = self.load_txns(
            self.store
                .scan(&[b"O", &script_hash[..HASH_PREFIX_LEN]].concat())
                .iter()
                .map(|row| {
                    bincode::deserialize::<TxOutRow>(&row.key)
                        .unwrap()
                        .txid_prefix
                })
                .collect(),
        );
        funding.retain(|item| {
            item.output
                .iter()
                .any(|output| compute_script_hash(&output.script_pubkey[..]) == script_hash)
        });

        let mut balance = 0u64;
        let mut spending = Vec::<Transaction>::new();
        for txn in &funding {
            let txid = txn.txid();
            for (index, output) in enumerate(&txn.output) {
                if let Some(spent) = self.find_spending_txn(&txid, index as u32) {
                    spending.push(spent); // TODO: may contain duplicate TXNs
                } else {
                    balance += output.value;
                }
            }
        }
        balance as f64 / 100_000_000f64
    }
105

106
    pub fn get_tx(&self, tx_hash: &Sha256dHash) -> Bytes {
107 108 109
        self.daemon
            .get(&format!("tx/{}.bin", tx_hash.be_hex_string()))
    }
110 111 112 113 114 115 116 117 118 119 120

    pub fn get_headers(&self, heights: &[usize]) -> Vec<Bytes> {
        let headers_list = self.index.headers_list();
        let headers = headers_list.headers();
        let mut result = Vec::new();
        for h in heights {
            let header: &BlockHeader = headers[*h].header();
            result.push(serialize(header).unwrap());
        }
        result
    }
121
}