Skip to content

Commit

Permalink
Add process_and_select_coins function
Browse files Browse the repository at this point in the history
There were some common tasks identified in the construction of a coin
selection result that the different algorithms performed separately.
Those tasks were blended inside the algorithms or separated into
functions, but the underlying actions were the same:
- Do some preprocessing to the inputs.
- Select utxos based on the algorithm criterium.
- Decide change.
- Build the coin selection result.

This commit intends to create a single function that consumes different
algorithms and perform all the mentioned tasks as a pipeline taking
utxos as input and producing coin selection results at the end.

The new function reduces the work needed to implement other coin
selection algorithms, modularizes the code not related directly to the
selection process and reduces the number of lines to review and
understand its logic.
  • Loading branch information
csralvall committed Sep 14, 2022
1 parent 562cb81 commit 144807b
Show file tree
Hide file tree
Showing 4 changed files with 635 additions and 656 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Add capacity to create FeeRate from sats/kvbytes and sats/kwu.
- Rename `as_sat_vb` to `as_sat_per_vb`. Move all `FeeRate` test to `types.rs`.
- Add custom Harware Wallet Signer `HwiSigner` in `src/wallet/harwaresigner/` module.
- Added `process_and_select_coins()` function.
- Added `OutputGroup`s as a public structure that holds the `WeightedUtxo`, its
effective value, and its fees.
- Changed `CoinSelectionAlgorithm::coin_select()` signature. The scope of the
selection was reduced to select utxos only from `optional_utxos`. It only
returns the selected `OutputGroups` from now on.
- Added `WeightedScriptPubkey` struct.

## [v0.21.0] - [v0.20.0]

- Add `descriptor::checksum::get_checksum_bytes` method.
Expand Down
14 changes: 13 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::convert::AsRef;
use std::ops::Sub;

use bitcoin::blockdata::transaction::{OutPoint, Transaction, TxOut};
use bitcoin::{hash_types::Txid, util::psbt};
use bitcoin::{hash_types::Txid, util::psbt, Script};

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -177,6 +177,18 @@ pub struct WeightedUtxo {
pub utxo: Utxo,
}

/// A wallet owned script_pubkey with the weight of the `witness` + `scriptSig` in [weight units]
/// to spend from it.
/// Useful to optimize reducing the waste metric in coin selection algorithms.
/// [weight units]: <https://en.bitcoin.it/wiki/Weight_units>
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WeightedScriptPubkey {
/// The weight of the witness data and `scriptSig` expressed in [weight units].
pub satisfaction_weight: usize,
/// The script_pubkey
pub script_pubkey: Script,
}

#[derive(Debug, Clone, PartialEq)]
/// An unspent transaction output (UTXO).
pub enum Utxo {
Expand Down
Loading

0 comments on commit 144807b

Please sign in to comment.