Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Commit

Permalink
Publish v0.1.1 post-updates and cleanup (#111)
Browse files Browse the repository at this point in the history
* Publish v0.1.1 for updates and cleanup

* Pushed changes to tests

Signed-off-by: neoz666 <neoz.blockchain@gmail.com>

* Pushed updates to version

Signed-off-by: neoz666 <neoz.blockchain@gmail.com>

---------

Signed-off-by: neoz666 <neoz.blockchain@gmail.com>
  • Loading branch information
NeoZ666 authored Feb 16, 2025
1 parent 56fe864 commit 057602b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-coinselect"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
description = "A blockchain-agnostic coin selection library built in Rust."
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion examples/bitcoin_crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "coinselect-example-bitcoin"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[dependencies]
Expand Down
10 changes: 10 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,18 @@ pub struct CoinSelectionOpt {
/// Strategy to decide what to do with the excess amount.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ExcessStrategy {
/// Adds the excess amount to the transaction fee. This increases the fee rate
/// and may lead to faster confirmation, but wastes the excess amount.
ToFee,

/// Adds the excess amount to the recipient's output. This avoids creating a change
/// output and reduces transaction size, but may reveal information about the
/// wallet's available UTXOs.
ToRecipient,

/// Creates a change output with the excess amount. This preserves privacy and
/// allows reuse of the excess amount in future transactions, but increases
/// transaction size and creates dust UTXOs if the amount is too small.
ToChange,
}

Expand Down
44 changes: 37 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ pub fn calculate_accumulated_weight(
accumulated_weight
}

// #[inline]
// pub fn calculate_fee(weight: u64, rate: f32) -> u64 {
// (weight as f32 * rate).ceil() as u64
// }

impl fmt::Display for SelectionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down Expand Up @@ -124,6 +119,15 @@ mod tests {
}
}

/// Tests the fee calculation function with various input scenarios.
/// Fee calculation is critical for coin selection as it determines the effective value
/// of each UTXO after accounting for the cost to spend it.
///
/// Test vectors cover:
/// - Normal fee calculation with positive rate
/// - Error case with negative fee rate
/// - Error case with abnormally high fee rate (>1000 sat/vB)
/// - Edge case with zero fee rate
#[test]
fn test_calculate_fee() {
struct TestVector {
Expand Down Expand Up @@ -169,6 +173,19 @@ mod tests {
}
}

/// Tests the effective value calculation which determines the actual spendable amount
/// of a UTXO after subtracting the fee required to spend it.
///
/// Effective value is crucial for coin selection as it helps:
/// - Avoid selecting UTXOs that cost more in fees than their value
/// - Compare UTXOs based on their true spendable amount
/// - Calculate the actual amount available for spending
///
/// Test vectors cover:
/// - Edge case where fees exceed UTXO value
/// - Normal case with positive effective value
/// - Error cases with invalid fee rates
/// - Large value UTXO calculations
#[test]
fn test_effective_value() {
struct TestVector {
Expand Down Expand Up @@ -249,6 +266,19 @@ mod tests {
}
}

/// Tests the waste metric calculation which helps optimize coin selection.
/// Waste represents the cost of creating a change output plus any excess amount
/// that goes to fees or is added to recipient outputs.
///
/// The waste metric considers:
/// - Long-term vs current fee rates
/// - Cost of creating change outputs
/// - Excess amounts based on selected strategy (fee/change/recipient)
///
/// Test vectors cover:
/// - Change output creation (ToChange strategy)
/// - Fee payment (ToFee strategy)
/// - Insufficient funds scenario
#[test]
fn test_calculate_waste() {
struct TestVector {
Expand All @@ -261,15 +291,15 @@ mod tests {

let options = setup_options(100).clone();
let test_vectors = [
// Test for excess srategy to drain(change output)
// Test for excess strategy to drain(change output)
TestVector {
options: options.clone(),
accumulated_value: 1000,
accumulated_weight: 50,
estimated_fee: 20,
result: options.change_cost,
},
// Test for excess srategy to miners
// Test for excess strategy to miners
TestVector {
options: CoinSelectionOpt {
excess_strategy: ExcessStrategy::ToFee,
Expand Down

0 comments on commit 057602b

Please sign in to comment.