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

Roman Zeyde's avatar
Roman Zeyde committed
13 14 15 16 17 18 19
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
20

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

28 29
type OutPoint = (Sha256dHash, usize); // (txid, output_index)

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

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

42 43 44 45
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
46 47
}

48
impl Status {
49 50 51 52 53 54 55 56
    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())
    }

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

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

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

79 80 81 82 83 84
    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() {
Roman Zeyde's avatar
Roman Zeyde committed
85
            if outputs_map.remove(&s.funding_output).is_none() {
86 87 88
                warn!("failed to remove {:?}", s.funding_output);
            }
        }
89
        let mut outputs = outputs_map
90 91
            .into_iter()
            .map(|item| item.1) // a reference to unspent output
92 93 94
            .collect::<Vec<&FundingOutput>>();
        outputs.sort_unstable_by_key(|out| out.height);
        outputs
95 96
    }

97 98 99 100 101 102 103 104
    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 {
Roman Zeyde's avatar
Roman Zeyde committed
105
                let part = format!("{}:{}:", txn_id.to_hex(), height);
106 107 108 109 110 111 112 113
                sha2.input(part.as_bytes());
            }
            sha2.result(&mut hash);
            Some(hash)
        }
    }
}

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

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

124 125 126 127 128 129 130
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 {
Roman Zeyde's avatar
Roman Zeyde committed
131
            let last = *hashes.last().unwrap();
132 133 134 135
            hashes.push(last);
        }
        index = if index % 2 == 0 { index + 1 } else { index - 1 };
        merkle.push(hashes[index]);
Roman Zeyde's avatar
Roman Zeyde committed
136
        index /= 2;
137 138 139 140 141 142 143 144
        hashes = hashes
            .chunks(2)
            .map(|pair| merklize(pair[0], pair[1]))
            .collect()
    }
    (merkle, hashes[0])
}

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

152
fn txrows_by_prefix(store: &dyn ReadStore, txid_prefix: HashPrefix) -> Vec<TxRow> {
153
    store
Roman Zeyde's avatar
Roman Zeyde committed
154
        .scan(&TxRow::filter_prefix(txid_prefix))
155 156 157 158
        .iter()
        .map(|row| TxRow::from_row(row))
        .collect()
}
159

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

168
fn txids_by_funding_output(
169
    store: &dyn ReadStore,
170 171 172 173 174 175 176 177
    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()
178 179
}

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

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

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

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

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

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

245 246
    fn find_spending_input(
        &self,
247
        store: &dyn ReadStore,
248
        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
            if compute_script_hash(&output.script_pubkey[..]) == script_hash {
                result.push(FundingOutput {
Roman Zeyde's avatar
Roman Zeyde committed
283
                    txn_id,
284 285 286 287 288 289 290 291 292
                    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
        // if the limit is enabled
Roman Zeyde's avatar
Roman Zeyde committed
302 303 304 305 306
        if self.txid_limit > 0 && txid_prefixes.len() > self.txid_limit {
            bail!(
                "{}+ transactions found, query may take a long time",
                txid_prefixes.len()
            );
307
        }
308
        for t in self.load_txns_by_prefix(read_store, txid_prefixes)? {
309
            funding.extend(self.find_funding_outputs(&t, script_hash));
310
        }
311
        for funding_output in &funding {
312
            if let Some(spent) = self.find_spending_input(read_store, &funding_output)? {
313
                spending.push(spent);
314 315
            }
        }
316
        Ok((funding, spending))
317 318
    }

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

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

350
    fn lookup_confirmed_blockhash(
351 352 353
        &self,
        tx_hash: &Sha256dHash,
        block_height: Option<u32>,
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    ) -> 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())
373
        };
374 375 376 377
        Ok(blockhash)
    }

    // Internal API for transaction retrieval
378 379 380 381 382
    fn load_txn(&self, txid: &Sha256dHash, block_height: Option<u32>) -> Result<Transaction> {
        self.tx_cache.get_or_else(&txid, || {
            let blockhash = self.lookup_confirmed_blockhash(txid, block_height)?;
            self.app.daemon().gettransaction(txid, blockhash)
        })
383 384 385 386 387 388 389 390
    }

    // 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
        }

Roman Zeyde's avatar
Roman Zeyde committed
443
        let heights: Vec<usize> = (0..=cp_height).collect();
444 445
        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
}