Skip to content

pick the Constructor or Sorter role from a PSBT's flags - #124

Draft
yuval-block wants to merge 1 commit into
sort-uuidfrom
dynamic-constructor
Draft

yuval-block wants to merge 1 commit into
sort-uuidfrom
dynamic-constructor

Conversation

@yuval-block

@yuval-block yuval-block commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Code receiving a PSBT cannot know its modifiability and sort mode as Rust type parameters. roles::dynamic::Role reads those fields and selects the corresponding typed role at runtime.

Role represents the sort mode, including whether a nonempty transaction UUID is present. Its Stage holds a Constructor while inputs or outputs remain modifiable, or a Sorter once both are fixed. ResultRole delegates joins to ResultUnorderedPsbt and selects a role when try_unwrap succeeds.

Selecting a role from a valid UnorderedPsbt cannot fail: its fields already satisfy the selected sort mode. Parsing reports the existing UnorderedPsbt errors.

@github-actions

Copy link
Copy Markdown
range-diff for force push 7bf09f4e6b → a1025bb699 November 16, 2026 03:28 UTC: 1 unchanged; 1 commit; base 0c154de312 → bd4f1dc8f3 (diverged, 20 ahead and 20 behind)

reproduce: git range-diff 0c154de..7bf09f4 bd4f1dc..a1025bb

1:  7bf09f4e6b +326 -1 3f = 1:  a1025bb699 +326 -1 3f pick the Constructor or Sorter role from a PSBT's flags

@github-actions

Copy link
Copy Markdown
range-diff for force push a1025bb699 → 2c7b09f955 November 16, 2026 04:48 UTC: 1 unchanged; 1 commit; base bd4f1dc8f3 → 70a89f49f2 (diverged, 20 ahead and 20 behind)

reproduce: git range-diff bd4f1dc..a1025bb 70a89f4..2c7b09f

1:  a1025bb699 +326 -1 3f = 1:  2c7b09f955 +326 -1 3f pick the Constructor or Sorter role from a PSBT's flags

@github-actions

Copy link
Copy Markdown
range-diff for force push 2c7b09f955 → 6caa821969 November 16, 2026 06:07 UTC: 1 unchanged; 1 commit; base 70a89f49f2 → 0934730995 (diverged, 20 ahead and 20 behind)

reproduce: git range-diff 70a89f4..2c7b09f 0934730..6caa821

1:  2c7b09f955 +326 -1 3f = 1:  6caa821969 +326 -1 3f pick the Constructor or Sorter role from a PSBT's flags

@github-actions

Copy link
Copy Markdown
range-diff for force push 6caa821969 → afea4f88c9 November 16, 2026 18:22 UTC: 1 unchanged; 1 commit; base 0934730995 → ed2bcaa7d8 (diverged, 16 ahead and 16 behind)

reproduce: git range-diff 0934730..6caa821 ed2bcaa..afea4f8

1:  6caa821969 +326 -1 3f = 1:  afea4f88c9 +326 -1 3f pick the Constructor or Sorter role from a PSBT's flags

The typed roles take a PSBT's modifiability and sort mode as type
parameters, so code that parses a PSBT it did not create has to know
both before it can name a role. roles::dynamic::Role reads them from
the PSBT's flags instead: the tx modifiable flags select a Constructor
when inputs or outputs can still be added, or a Sorter when the PSBT is
unmodifiable, and the sort key derivation and PSBT_GLOBAL_TX_UUID
select the sort mode. Stage holds the role for one sort mode.

Dispatch cannot fail. ResultUnorderedPsbt marks every sort rule
violation as a conflict, so a clean UnorderedPsbt always satisfies the
sort mode its own fields select, and the typed roles can be built
without checking it again. Parsing reports the same errors as
UnorderedPsbt.

ResultRole joins like ResultUnorderedPsbt and picks a role only once
the join is clean. Two PSBTs whose flags disagree join to a conflict,
not to some role in between.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
range-diff for force push afea4f88c9 → 068cfa3d52 November 16, 2026 23:11 UTC: 1 modified; 1 commit; same base ed2bcaa7d8

reproduce: git range-diff ed2bcaa..afea4f8 ed2bcaa..068cfa3

1:  afea4f88c9 +326 -1 3f ! 1:  068cfa3d52 +396 -1 3f pick the Constructor or Sorter role from a PSBT's flags
1: afea4f88c9 ! 1: 068cfa3d52 pick the Constructor or Sorter role from a PSBT's flags
@@ crates/concurrent-psbt/src/roles/dynamic.rs (new)
 +        }
 +
 +        #[test]
++        fn identical_roles_join_without_losing_fields() {
++            for flags in 0..4 {
++                for (mode, name) in MODES.into_iter().enumerate() {
++                    let psbt = psbt(global(flags, mode), 1, 7);
++                    let parsed = ResultRole::try_from_psbt(psbt.clone()).expect("valid PSBT");
++                    assert!(parsed.is_ok());
++                    let left = Role::try_from_psbt(psbt.clone()).expect("valid PSBT");
++                    let right = Role::try_from_psbt(psbt.clone()).expect("valid PSBT");
++                    let joined = left.try_join(right);
++                    assert_eq!(joined, parsed);
++                    assert!(joined.is_ok());
++                    let role = joined.try_unwrap().expect("identical values join cleanly");
++                    assert_eq!(describe(&role), (name, flags));
++                    assert_eq!(
++                        role.into_inner(),
++                        UnorderedPsbt::try_from_psbt(psbt).unwrap()
++                    );
++                }
++            }
++        }
++
++        #[test]
++        fn conflicting_roles_retain_the_complete_result() {
++            use crate::conflicts::ConflictPath;
++            use crate::global::GlobalField;
++            use crate::input::InputField;
++
++            let outpoint = bitcoin::OutPoint {
++                txid: bitcoin::Txid::from_byte_array([1; 32]),
++                vout: 0,
++            };
++            for flags in 0..4 {
++                for mode in 0..MODES.len() {
++                    for (other_flags, sequence, path) in [
++                        (
++                            flags ^ 1,
++                            7,
++                            ConflictPath::Global(GlobalField::TxModifiableFlags),
++                        ),
++                        (
++                            flags,
++                            8,
++                            ConflictPath::Input {
++                                outpoint: &outpoint,
++                                field: InputField::Sequence,
++                            },
++                        ),
++                    ] {
++                        let left = Role::try_from_psbt(psbt(global(flags, mode), 1, 7)).unwrap();
++                        let right =
++                            Role::try_from_psbt(psbt(global(other_flags, mode), 1, sequence))
++                                .unwrap();
++                        let joined = left.try_join(right);
++                        assert!(!joined.is_ok());
++                        assert_eq!(
++                            joined
++                                .0
++                                .conflicts()
++                                .map(|conflict| conflict.path())
++                                .collect::<Vec<_>>(),
++                            [path]
++                        );
++                        let unchanged = joined.clone();
++                        assert_eq!(joined.try_unwrap().unwrap_err(), unchanged);
++                    }
++                }
++            }
++        }
++
++        #[test]
 +        fn parsing_reports_the_unordered_psbt_errors() {
 +            let mut ordered = global(0x03, 0);
 +            ordered.set_tx_modifiable_flags(0x07);

@github-code-quality

github-code-quality Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

sort-uuid 5f4ab3b dynamic-constructor 397e1f4 +/-
100% 100% 0%
View coverage of the 10 most impacted files.
File sort-uuid 5f4ab3b dynamic-constructor 397e1f4 +/-
crates/concurre.../src/psbt/tx.rs 0% 100% +100%
crates/concurre...bt/keyed_set.rs 0% 100% +100%
crates/concurre.../psbt/global.rs 0% 100% +100%
crates/concurre...structor/mod.rs 0% 100% +100%
crates/concurre...s/sorter/mod.rs 0% 100% +100%
crates/concurre...sorter/error.rs 0% 100% +100%
crates/concurre...bt/input_set.rs 0% 100% +100%
crates/concurre...t/output_set.rs 0% 100% +100%
crates/concurre...oles/creator.rs 0% 100% +100%
crates/concurre...oles/dynamic.rs 0% 100% +100%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 16, 2026 23:46 UTC

@github-actions

Copy link
Copy Markdown
range-diff for force push 068cfa3d52 → 397e1f4c97 November 16, 2026 23:45 UTC: 1 unchanged; 1 commit; base ed2bcaa7d8 → 000109cd49 (diverged, 17 ahead and 17 behind)

reproduce: git range-diff ed2bcaa..068cfa3 000109c..397e1f4

1:  068cfa3d52 +396 -1 3f = 1:  397e1f4c97 +396 -1 3f pick the Constructor or Sorter role from a PSBT's flags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants