|
| 1 | +// Bitcoin Dev Kit |
| 2 | +// Written in 2020 by Alekos Filini <[email protected]> |
| 3 | +// |
| 4 | +// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers |
| 5 | +// |
| 6 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 7 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 8 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 9 | +// You may not use this file except in accordance with one or both of these |
| 10 | +// licenses. |
| 11 | + |
| 12 | +//! Coin control |
| 13 | +//! |
| 14 | +//! This module defines how coins are to be sorted and/or grouped before coin selection. |
| 15 | +
|
| 16 | +use std::collections::HashMap; |
| 17 | + |
| 18 | +use bitcoin::OutPoint; |
| 19 | + |
| 20 | +use crate::TransactionDetails; |
| 21 | + |
| 22 | +use super::COINBASE_MATURITY; |
| 23 | + |
| 24 | +/// Parameters to determine which coins/outputs to filter. |
| 25 | +pub struct CoinFilterParams { |
| 26 | + /// Outpoints to manually keep (`true`) or skip (`false`). This overrides all other parameters. |
| 27 | + pub manual: HashMap<OutPoint, bool>, |
| 28 | + |
| 29 | + /// Whether we should filter out unconfirmed transactions. |
| 30 | + /// TODO: Use minimum confirmations instead. |
| 31 | + pub filter_unconfirmed: bool, |
| 32 | + |
| 33 | + /// Whether we should filter out immature coinbase outputs. |
| 34 | + /// Coinbase transaction outputs need to be at least 100 blocks deep before being spendable. |
| 35 | + /// If this is set, but `current_height == None`, all coinbase outputs will be filtered out. |
| 36 | + pub filter_immature_coinbase: bool, |
| 37 | + |
| 38 | + /// Current block height. |
| 39 | + pub current_height: Option<u32>, |
| 40 | +} |
| 41 | + |
| 42 | +impl CoinFilterParams { |
| 43 | + /// Returns true if coin is to be kept, false if coin is to be filtered out. |
| 44 | + pub(crate) fn keep(&self, tx: &TransactionDetails, outpoint: &OutPoint) -> bool { |
| 45 | + let raw_tx = tx.transaction.as_ref().expect("failed to obtain raw tx"); |
| 46 | + |
| 47 | + if let Some(&keep) = self.manual.get(outpoint) { |
| 48 | + if keep { |
| 49 | + return true; |
| 50 | + } else { |
| 51 | + return false; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if self.filter_unconfirmed && tx.confirmation_time.is_none() { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + // https://github.com/bitcoin/bitcoin/blob/c5e67be03bb06a5d7885c55db1f016fbf2333fe3/src/validation.cpp#L373-L375 |
| 60 | + if self.filter_immature_coinbase |
| 61 | + && raw_tx.is_coin_base() |
| 62 | + && !matches!((self.current_height, &tx.confirmation_time), (Some(tip), Some(conf)) if tip.saturating_sub(conf.height) >= COINBASE_MATURITY) |
| 63 | + { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + return true; |
| 68 | + } |
| 69 | +} |
0 commit comments