query.rs 16 KB
Newer Older
1
use bitcoin::blockdata::transaction::Transaction;
2
use bitcoin::consensus::encode::deserialize;
3
use bitcoin::util::hash::Sha256dHash;
4 5
use crypto::digest::Digest;
use crypto::sha2::Sha256;
6
use lru::LruCache;
Roman Zeyde's avatar
Roman Zeyde committed
7
use serde_json::Value;
8
use std::collections::HashMap;
9
use std::sync::{Arc, Mutex, RwLock};
10

Roman Zeyde's avatar
Roman Zeyde committed
11 12 13 14 15 16 17
use crate::app::App;
use crate::errors::*;
use crate::index::{compute_script_hash, TxInRow, TxOutRow, TxRow};
use crate::mempool::Tracker;
use crate::metrics::Metrics;
use crate::store::{ReadStore, Row};
use crate::util::{FullHash, HashPrefix, HeaderEntry};
Roman Zeyde's avatar
Roman Zeyde committed
18

19 20 21 22 23
pub struct FundingOutput {
    pub txn_id: Sha256dHash,
    pub height: u32,
    pub output_index: usize,
    pub value: u64,
Roman Zeyde's avatar
Roman Zeyde committed
24 25
}

26 27
type OutPoint = (Sha256dHash, usize); // (txid, output_index)

28 29
struct SpendingInput {
    txn_id: Sha256dHash,
30
    height: u32,
31
    funding_output: OutPoint,
32
    value: u64,
Roman Zeyde's avatar
Roman Zeyde committed
33 34 35
}

pub struct Status {
36 37 38 39
    confirmed: (Vec<FundingOutput>, Vec<SpendingInput>),
    mempool: (Vec<FundingOutput>, Vec<SpendingInput>),
}

40 41 42 43
fn calc_balance((funding, spending): &(Vec<FundingOutput>, Vec<SpendingInput>)) -> i64 {
    let funded: u64 = funding.iter().map(|output| output.value).sum();
    let spent: u64 = spending.iter().map(|input| input.value).sum();
    funded as i64 - spent as i64
Roman Zeyde's avatar
Roman Zeyde committed
44 45
}

46
impl Status {
47 48 49 50 51 52 53 54
    fn funding(&self) -> impl Iterator<Item = &FundingOutput> {
        self.confirmed.0.iter().chain(self.mempool.0.iter())
    }

    fn spending(&self) -> impl Iterator<Item = &SpendingInput> {
        self.confirmed.1.iter().chain(self.mempool.1.iter())
    }

55
    pub fn confirmed_balance(&self) -> i64 {
56 57 58
        calc_balance(&self.confirmed)
    }

59
    pub fn mempool_balance(&self) -> i64 {
60
        calc_balance(&self.mempool)
61 62
    }

63 64
    pub fn history(&self) -> Vec<(i32, Sha256dHash)> {
        let mut txns_map = HashMap::<Sha256dHash, i32>::new();
65
        for f in self.funding() {
66
            txns_map.insert(f.txn_id, f.height as i32);
67
        }
68
        for s in self.spending() {
69
            txns_map.insert(s.txn_id, s.height as i32);
70 71 72
        }
        let mut txns: Vec<(i32, Sha256dHash)> =
            txns_map.into_iter().map(|item| (item.1, item.0)).collect();
73
        txns.sort_unstable();
74 75 76
        txns
    }

77 78 79 80 81 82 83 84 85 86
    pub fn unspent(&self) -> Vec<&FundingOutput> {
        let mut outputs_map = HashMap::<OutPoint, &FundingOutput>::new();
        for f in self.funding() {
            outputs_map.insert((f.txn_id, f.output_index), f);
        }
        for s in self.spending() {
            if let None = outputs_map.remove(&s.funding_output) {
                warn!("failed to remove {:?}", s.funding_output);
            }
        }
87
        let mut outputs = outputs_map
88 89
            .into_iter()
            .map(|item| item.1) // a reference to unspent output
90 91 92
            .collect::<Vec<&FundingOutput>>();
        outputs.sort_unstable_by_key(|out| out.height);
        outputs
93 94
    }

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    pub fn hash(&self) -> Option<FullHash> {
        let txns = self.history();
        if txns.is_empty() {
            None
        } else {
            let mut hash = FullHash::default();
            let mut sha2 = Sha256::new();
            for (height, txn_id) in txns {
                let part = format!("{}:{}:", txn_id.be_hex_string(), height);
                sha2.input(part.as_bytes());
            }
            sha2.result(&mut hash);
            Some(hash)
        }
    }
}

Roman Zeyde's avatar
Roman Zeyde committed
112 113
struct TxnHeight {
    txn: Transaction,
114
    height: u32,
Roman Zeyde's avatar
Roman Zeyde committed
115 116
}

Roman Zeyde's avatar
Roman Zeyde committed
117 118 119 120 121
fn merklize(left: Sha256dHash, right: Sha256dHash) -> Sha256dHash {
    let data = [&left[..], &right[..]].concat();
    Sha256dHash::from_data(&data)
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
fn create_merkle_branch_and_root(
    mut hashes: Vec<Sha256dHash>,
    mut index: usize,
) -> (Vec<Sha256dHash>, Sha256dHash) {
    let mut merkle = vec![];
    while hashes.len() > 1 {
        if hashes.len() % 2 != 0 {
            let last = hashes.last().unwrap().clone();
            hashes.push(last);
        }
        index = if index % 2 == 0 { index + 1 } else { index - 1 };
        merkle.push(hashes[index]);
        index = index / 2;
        hashes = hashes
            .chunks(2)
            .map(|pair| merklize(pair[0], pair[1]))
            .collect()
    }
    (merkle, hashes[0])
}

143 144 145 146 147 148 149
// TODO: the functions below can be part of ReadStore.
fn txrow_by_txid(store: &ReadStore, txid: &Sha256dHash) -> Option<TxRow> {
    let key = TxRow::filter_full(&txid);
    let value = store.get(&key)?;
    Some(TxRow::from_row(&Row { key, value }))
}

150
fn txrows_by_prefix(store: &ReadStore, txid_prefix: &HashPrefix) -> Vec<TxRow> {
151
    store
152
        .scan(&TxRow::filter_prefix(&txid_prefix))
153 154 155 156
        .iter()
        .map(|row| TxRow::from_row(row))
        .collect()
}
157

158
fn txids_by_script_hash(store: &ReadStore, script_hash: &[u8]) -> Vec<HashPrefix> {
159 160 161 162 163 164
    store
        .scan(&TxOutRow::filter(script_hash))
        .iter()
        .map(|row| TxOutRow::from_row(row).txid_prefix)
        .collect()
}
165

166
fn txids_by_funding_output(
167
    store: &ReadStore,
168 169 170 171 172 173 174 175
    txn_id: &Sha256dHash,
    output_index: usize,
) -> Vec<HashPrefix> {
    store
        .scan(&TxInRow::filter(&txn_id, output_index))
        .iter()
        .map(|row| TxInRow::from_row(row).txid_prefix)
        .collect()
176 177
}

178 179
pub struct TransactionCache {
    map: Mutex<LruCache<Sha256dHash, Transaction>>,
180 181 182
}

impl TransactionCache {
183
    pub fn new(capacity: usize) -> TransactionCache {
184
        TransactionCache {
185
            map: Mutex::new(LruCache::new(capacity)),
186 187 188 189 190 191 192
        }
    }

    fn get_or_else<F>(&self, txid: &Sha256dHash, load_txn_func: F) -> Result<Transaction>
    where
        F: FnOnce() -> Result<Transaction>,
    {
193
        if let Some(txn) = self.map.lock().unwrap().get(txid) {
194 195 196
            return Ok(txn.clone());
        }
        let txn = load_txn_func()?;
197
        self.map.lock().unwrap().put(*txid, txn.clone());
198 199 200 201
        Ok(txn)
    }
}

202 203
pub struct Query {
    app: Arc<App>,
204
    tracker: RwLock<Tracker>,
205
    tx_cache: TransactionCache,
206
    txid_limit: usize,
207 208
}

209
impl Query {
210 211 212 213 214 215
    pub fn new(
        app: Arc<App>,
        metrics: &Metrics,
        tx_cache: TransactionCache,
        txid_limit: usize,
    ) -> Arc<Query> {
216
        Arc::new(Query {
217
            app,
218
            tracker: RwLock::new(Tracker::new(metrics)),
219
            tx_cache,
220
            txid_limit,
221
        })
222 223
    }

224 225 226 227 228
    fn load_txns_by_prefix(
        &self,
        store: &ReadStore,
        prefixes: Vec<HashPrefix>,
    ) -> Result<Vec<TxnHeight>> {
Roman Zeyde's avatar
Roman Zeyde committed
229
        let mut txns = vec![];
230
        for txid_prefix in prefixes {
231
            for tx_row in txrows_by_prefix(store, &txid_prefix) {
232
                let txid: Sha256dHash = deserialize(&tx_row.key.txid).unwrap();
Roman Zeyde's avatar
Roman Zeyde committed
233 234
                let txn = self
                    .tx_cache
235
                    .get_or_else(&txid, || self.load_txn(&txid, tx_row.height))?;
236 237
                txns.push(TxnHeight {
                    txn,
238
                    height: tx_row.height,
239
                })
240 241
            }
        }
242
        Ok(txns)
243 244
    }

245 246 247 248
    fn find_spending_input(
        &self,
        store: &ReadStore,
        funding: &FundingOutput,
249
    ) -> Result<Option<SpendingInput>> {
250
        let spending_txns: Vec<TxnHeight> = self.load_txns_by_prefix(
251
            store,
252
            txids_by_funding_output(store, &funding.txn_id, funding.output_index),
253
        )?;
Roman Zeyde's avatar
Roman Zeyde committed
254
        let mut spending_inputs = vec![];
Roman Zeyde's avatar
Roman Zeyde committed
255
        for t in &spending_txns {
256
            for input in t.txn.input.iter() {
257 258
                if input.previous_output.txid == funding.txn_id
                    && input.previous_output.vout == funding.output_index as u32
Roman Zeyde's avatar
Roman Zeyde committed
259 260 261 262
                {
                    spending_inputs.push(SpendingInput {
                        txn_id: t.txn.txid(),
                        height: t.height,
263
                        funding_output: (funding.txn_id, funding.output_index),
264
                        value: funding.value,
Roman Zeyde's avatar
Roman Zeyde committed
265 266 267 268 269
                    })
                }
            }
        }
        assert!(spending_inputs.len() <= 1);
270
        Ok(if spending_inputs.len() == 1 {
Roman Zeyde's avatar
Roman Zeyde committed
271
            Some(spending_inputs.remove(0))
272 273
        } else {
            None
274
        })
275 276
    }

277
    fn find_funding_outputs(&self, t: &TxnHeight, script_hash: &[u8]) -> Vec<FundingOutput> {
Roman Zeyde's avatar
Roman Zeyde committed
278
        let mut result = vec![];
279
        let txn_id = t.txn.txid();
Roman Zeyde's avatar
Roman Zeyde committed
280
        for (index, output) in t.txn.output.iter().enumerate() {
281 282 283 284 285 286 287 288 289 290 291 292
            if compute_script_hash(&output.script_pubkey[..]) == script_hash {
                result.push(FundingOutput {
                    txn_id: txn_id,
                    height: t.height,
                    output_index: index,
                    value: output.value,
                })
            }
        }
        result
    }

293 294 295 296
    fn confirmed_status(
        &self,
        script_hash: &[u8],
    ) -> Result<(Vec<FundingOutput>, Vec<SpendingInput>)> {
297 298
        let mut funding = vec![];
        let mut spending = vec![];
299 300
        let read_store = self.app.read_store();
        let txid_prefixes = txids_by_script_hash(read_store, script_hash);
301 302 303 304 305 306 307 308 309
        // if the limit is enabled
        if self.txid_limit > 0 {
            if txid_prefixes.len() > self.txid_limit {
                bail!(
                    "{}+ transactions found, query may take a long time",
                    txid_prefixes.len()
                );
            }
        }
310
        for t in self.load_txns_by_prefix(read_store, txid_prefixes)? {
311
            funding.extend(self.find_funding_outputs(&t, script_hash));
312
        }
313
        for funding_output in &funding {
314
            if let Some(spent) = self.find_spending_input(read_store, &funding_output)? {
315
                spending.push(spent);
316 317
            }
        }
318
        Ok((funding, spending))
319 320
    }

321 322 323 324
    fn mempool_status(
        &self,
        script_hash: &[u8],
        confirmed_funding: &[FundingOutput],
325
    ) -> Result<(Vec<FundingOutput>, Vec<SpendingInput>)> {
326 327
        let mut funding = vec![];
        let mut spending = vec![];
328
        let tracker = self.tracker.read().unwrap();
329 330
        let txid_prefixes = txids_by_script_hash(tracker.index(), script_hash);
        for t in self.load_txns_by_prefix(tracker.index(), txid_prefixes)? {
331
            funding.extend(self.find_funding_outputs(&t, script_hash));
332
        }
333
        // // TODO: dedup outputs (somehow) both confirmed and in mempool (e.g. reorg?)
334
        for funding_output in funding.iter().chain(confirmed_funding.iter()) {
335
            if let Some(spent) = self.find_spending_input(tracker.index(), &funding_output)? {
336
                spending.push(spent);
Roman Zeyde's avatar
Roman Zeyde committed
337 338
            }
        }
339
        Ok((funding, spending))
340
    }
341

342
    pub fn status(&self, script_hash: &[u8]) -> Result<Status> {
Roman Zeyde's avatar
Roman Zeyde committed
343 344
        let confirmed = self
            .confirmed_status(script_hash)
345
            .chain_err(|| "failed to get confirmed status")?;
Roman Zeyde's avatar
Roman Zeyde committed
346 347
        let mempool = self
            .mempool_status(script_hash, &confirmed.0)
348
            .chain_err(|| "failed to get mempool status")?;
349
        Ok(Status { confirmed, mempool })
350 351
    }

352
    fn lookup_confirmed_blockhash(
353 354 355
        &self,
        tx_hash: &Sha256dHash,
        block_height: Option<u32>,
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    ) -> Result<Option<Sha256dHash>> {
        let blockhash = if self.tracker.read().unwrap().get_txn(&tx_hash).is_some() {
            None // found in mempool (as unconfirmed transaction)
        } else {
            // Lookup in confirmed transactions' index
            let height = match block_height {
                Some(height) => height,
                None => {
                    txrow_by_txid(self.app.read_store(), &tx_hash)
                        .chain_err(|| format!("not indexed tx {}", tx_hash))?
                        .height
                }
            };
            let header = self
                .app
                .index()
                .get_header(height as usize)
                .chain_err(|| format!("missing header at height {}", height))?;
            Some(*header.hash())
375
        };
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
        Ok(blockhash)
    }

    // Internal API for transaction retrieval
    fn load_txn(&self, tx_hash: &Sha256dHash, block_height: u32) -> Result<Transaction> {
        let blockhash = self.lookup_confirmed_blockhash(tx_hash, Some(block_height))?;
        self.app.daemon().gettransaction(tx_hash, blockhash)
    }

    // Public API for transaction retrieval (for Electrum RPC)
    pub fn get_transaction(&self, tx_hash: &Sha256dHash, verbose: bool) -> Result<Value> {
        let blockhash = self.lookup_confirmed_blockhash(tx_hash, /*block_height*/ None)?;
        self.app
            .daemon()
            .gettransaction_raw(tx_hash, blockhash, verbose)
391
    }
392

393 394 395 396 397 398
    pub fn get_headers(&self, heights: &[usize]) -> Vec<HeaderEntry> {
        let index = self.app.index();
        heights
            .iter()
            .filter_map(|height| index.get_header(*height))
            .collect()
399
    }
Roman Zeyde's avatar
Roman Zeyde committed
400

401
    pub fn get_best_header(&self) -> Result<HeaderEntry> {
402
        let last_header = self.app.index().best_header();
403
        Ok(last_header.chain_err(|| "no headers indexed")?.clone())
Roman Zeyde's avatar
Roman Zeyde committed
404
    }
Roman Zeyde's avatar
Roman Zeyde committed
405 406 407 408 409

    pub fn get_merkle_proof(
        &self,
        tx_hash: &Sha256dHash,
        height: usize,
410
    ) -> Result<(Vec<Sha256dHash>, usize)> {
Roman Zeyde's avatar
Roman Zeyde committed
411 412
        let header_entry = self
            .app
413 414 415
            .index()
            .get_header(height)
            .chain_err(|| format!("missing block #{}", height))?;
416
        let txids = self.app.daemon().getblocktxids(&header_entry.hash())?;
417 418 419 420
        let pos = txids
            .iter()
            .position(|txid| txid == tx_hash)
            .chain_err(|| format!("missing txid {}", tx_hash))?;
421 422
        let (branch, _root) = create_merkle_branch_and_root(txids, pos);
        Ok((branch, pos))
Roman Zeyde's avatar
Roman Zeyde committed
423
    }
424

425 426 427 428 429 430
    pub fn get_header_merkle_proof(
        &self,
        height: usize,
        cp_height: usize,
    ) -> Result<(Vec<Sha256dHash>, Sha256dHash)> {
        if cp_height < height {
431
            bail!("cp_height #{} < height #{}", cp_height, height);
432 433 434 435
        }

        let best_height = self.get_best_header()?.height();
        if best_height < cp_height {
436
            bail!(
437
                "cp_height #{} above best block height #{}",
438 439 440
                cp_height,
                best_height
            );
441 442
        }

443 444 445
        let heights: Vec<usize> = (0..cp_height + 1).collect();
        let header_hashes: Vec<Sha256dHash> = self
            .get_headers(&heights)
446
            .into_iter()
447
            .map(|h| *h.hash())
448
            .collect();
449 450
        assert_eq!(header_hashes.len(), heights.len());
        Ok(create_merkle_branch_and_root(header_hashes, height))
451 452
    }

453 454 455 456 457 458 459 460 461 462 463 464
    pub fn get_id_from_pos(
        &self,
        height: usize,
        tx_pos: usize,
        want_merkle: bool,
    ) -> Result<(Sha256dHash, Vec<Sha256dHash>)> {
        let header_entry = self
            .app
            .index()
            .get_header(height)
            .chain_err(|| format!("missing block #{}", height))?;

465
        let txids = self.app.daemon().getblocktxids(header_entry.hash())?;
466 467 468 469
        let txid = *txids
            .get(tx_pos)
            .chain_err(|| format!("No tx in position #{} in block #{}", tx_pos, height))?;

470 471 472 473 474 475
        let branch = if want_merkle {
            create_merkle_branch_and_root(txids, tx_pos).0
        } else {
            vec![]
        };
        Ok((txid, branch))
476 477
    }

478
    pub fn broadcast(&self, txn: &Transaction) -> Result<Sha256dHash> {
479
        self.app.daemon().broadcast(txn)
480 481
    }

Roman Zeyde's avatar
Roman Zeyde committed
482
    pub fn update_mempool(&self) -> Result<()> {
Roman Zeyde's avatar
Roman Zeyde committed
483
        self.tracker.write().unwrap().update(self.app.daemon())
484
    }
485

486
    /// Returns [vsize, fee_rate] pairs (measured in vbytes and satoshis).
487
    pub fn get_fee_histogram(&self) -> Vec<(f32, u32)> {
488 489 490
        self.tracker.read().unwrap().fee_histogram().clone()
    }

491 492 493 494 495 496 497 498 499 500 501 502 503
    // Fee rate [BTC/kB] to be confirmed in `blocks` from now.
    pub fn estimate_fee(&self, blocks: usize) -> f32 {
        let mut total_vsize = 0u32;
        let mut last_fee_rate = 0.0;
        let blocks_in_vbytes = (blocks * 1_000_000) as u32; // assume ~1MB blocks
        for (fee_rate, vsize) in self.tracker.read().unwrap().fee_histogram() {
            last_fee_rate = *fee_rate;
            total_vsize += vsize;
            if total_vsize >= blocks_in_vbytes {
                break; // under-estimate the fee rate a bit
            }
        }
        last_fee_rate * 1e-5 // [BTC/kB] = 10^5 [sat/B]
504
    }
505 506 507 508

    pub fn get_banner(&self) -> Result<String> {
        self.app.get_banner()
    }
509
}