Skip to content

TxOrdering::Untouched no longer ensures the order of tx input #244

Description

@eauxxs

Describe the bug
Since add_utxo of build_tx in 1.2.0 uses hashmap to contain input, TxOrdrering cannot ensure that the order is not changed.

To Reproduce

    let utxos = wallet1
        .list_unspent()
        .map(|o| o.outpoint)
        .collect::<Vec<_>>();
    let builder = wallet1.build_tx();
    builder
        .ordering(bdk_wallet::TxOrdering::Untouched)
        .fee_rate(FeeRate::from_sat_per_vb(5).unwrap())
        .add_utxo(utxos[0])
        .unwrap()
        .add_utxo(utxos[1])
        .unwrap()
        .add_recipient(
            wallet1.peek_address(External, 0).address.script_pubkey(),
            bitcoin::Amount::from_sat(1000),
        );

    let inputs = builder.finish().unwrap().unsigned_tx.input;
    // can't assert
    assert!(inputs[0].previous_output == utxos[0]);
    assert!(inputs[1].previous_output == utxos[1]);

Expected behavior

    assert!(inputs[0].previous_output == utxos[0]);
    assert!(inputs[1].previous_output == utxos[1]);

Build environment

  • BDK tag/commit: 1.2.0
  • OS+version:
  • Rust/Cargo version: *
  • Rust/Cargo target: *

Additional context

pub(crate) utxos: HashMap<OutPoint, WeightedUtxo>,

Activity

  1. eauxxs commented on May 28, 2025

    @eauxxs
    Author
  2. nymius commented on Jun 9, 2025

    @nymius
    Contributor

    Sorry for the late reply. You're right, I over sighted the importance of the insertion order. However, I would like to explore a solution that does not include an extra dependency just for this case. I will try to think in something else.

  3. added 2 commits that reference this issue on Jun 9, 2025
    3555ccf
    a7a5888
  4. added this to the Wallet 2.1.0 milestone on Jun 12, 2025
  5. moved this to In Progress in BDK Walleton Jun 12, 2025
  6. stevenroose commented on Jun 23, 2025

    @stevenroose
    Contributor

    Also, coin selection has no guarantee of maintaining the ordering. There are two solutions to this:

    • the coin selection should only be allowed to return "additional" UTXOs so that the downstream code can guarantee the order is maintained
    • the coin selection trait should require implementations to maintain the order of required UTXOs to be at the start.

    Currently this line in the default bnb coin selection algorithm is violating that (but it's also not part of the CoinSelection contract):

    selected_utxos.append(&mut required_utxos);

    I think opting for the first option would be best.

  7. added a commit that references this issue on Jun 23, 2025
    1e21630
  8. nymius commented on Jun 23, 2025

    @nymius
    Contributor

    Also, coin selection has no guarantee of maintaining the ordering. There are two solutions to this:

    * the coin selection should only be allowed to return "additional" utxos so that the downstream code can guarantee the order is maintained
    
    * the coin selection trait should require implementations to maintain the order of required vtxos to be at the start.
    

    I think opting for the first option would be best.

    I agree coin selection should only return additional UTxOs, and is something which has been already proposed before (look at the third point of the changelog of this old PR).

    TxBuilder is expected to be replaced by bdk-tx, so its going to only receive fixes from now on, mainly.

    As bdk-tx is leaning towards using bdk-coin-select. As long as I can tell, is also providing the required UTxOs to the selection algorithms.

    If bdk-coin-select is not already doing what you propose, I think the change should be there.

  9. added a commit that references this issue on Jun 23, 2025
    2424ddc
  10. ValuedMammal commented on Jun 24, 2025

    @ValuedMammal
    Contributor

    Currently this line in the default bnb coin selection algorithm is violating that (but it's also not part of the CoinSelection contract):

    Still it clearly looks like a bug and should probably be fixed. It would make sense to have a test for every coin-select algo that manually selected inputs come first as long as we specify a tx ordering of Untouched.

  11. stevenroose commented on Jun 24, 2025

    @stevenroose
    Contributor
  12. added 2 commits that reference this issue on Jun 25, 2025
    9ef9d78
    8520c41
  13. moved this from In Progress to Needs Review in BDK Walleton Jun 26, 2025
  14. added a commit that references this issue on Jun 29, 2025
    3316236
  15. added a commit that references this issue on Jul 1, 2025
    b925402
  16. moved this from Needs Review to Done in BDK Walleton Jul 1, 2025
  17. added a commit that references this issue on Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions