Repository navigation
Unordered psbt - #82
yuval-block wants to merge 8 commits into
Conversation
7fac4d1 to
53d834a
Compare
This comment has been minimized.
This comment has been minimized.
53d834a to
b296c74
Compare
This comment has been minimized.
This comment has been minimized.
b296c74 to
245a723
Compare
This comment has been minimized.
This comment has been minimized.
Code coverage overviewLanguages: Rust Rust / code-coverage/cargo-llvm-cov
View coverage of the 10 most impacted files.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. Updated |
245a723 to
00361be
Compare
This comment has been minimized.
This comment has been minimized.
00361be to
7d465c3
Compare
This comment has been minimized.
This comment has been minimized.
7d465c3 to
f30fa5b
Compare
This comment has been minimized.
This comment has been minimized.
f30fa5b to
c393c64
Compare
This comment has been minimized.
This comment has been minimized.
c393c64 to
396a072
Compare
This comment has been minimized.
This comment has been minimized.
396a072 to
06d29dc
Compare
This comment has been minimized.
This comment has been minimized.
| bip32_derivations: BTreeMap<PublicKey, KeySource>, | ||
| tap_internal_key: Option<XOnlyPublicKey>, | ||
| tap_tree: Option<TapTree>, | ||
| tap_key_origins: BTreeMap<XOnlyPublicKey, (Vec<TapLeafHash>, KeySource)>, |
There was a problem hiding this comment.
tap_key_origins breaks Join laws in ResultOutput
The order-insensitive tap-key-origin join keeps the left leaf order, so a.join(b) != b.join(a) and (a.join(b)).join(c) != a.join(b.join(c)) under the derived PartialEq.
The assert_join_laws! generators never populate tap_key_origins, so the property tests miss it.
oak · automated · 06d29dc
| let other_output_len = other.outputs.len(); | ||
|
|
||
| let inputs = self.inputs.join(other.inputs); | ||
| let outputs = self.outputs.join(other.outputs); |
There was a problem hiding this comment.
Join unions distinct outputs without modifiable flag
Joining two UnorderedPsbts with distinct outputs succeeds even when PSBT_GLOBAL_TX_MODIFIABLE has neither modifiable bit set.
The spec requires combining to fail when distinct identifiers are present without the corresponding flag.
Distinct identifiers are only allowed if the corresponding
PSBT_GLOBAL_TX_MODIFIABLEflag is set, because such combinations represent additions of inputs or outputs.— fungi-protocol/docs@f2806d9 · docs/psbt-concurrent.md#L116
If this is not the case but a distinct input or output is present, then combining must fail.
— fungi-protocol/docs@f2806d9 · docs/psbt-concurrent.md#L116
Test that fails on this commit
use concurrent_psbt::Join;
use concurrent_psbt::input::InputSet;
use concurrent_psbt::output::{OutputSet, OutputUniqueIdExt, UniqueId};
use concurrent_psbt::tx::UnorderedPsbt;
use psbt_v2::v2::{Global, Output};
fn psbt(uid: u8) -> UnorderedPsbt {
let mut output = Output::default();
output.set_unique_id(UniqueId::new(vec![uid]));
let outputs = OutputSet::try_from_outputs([output]).unwrap();
UnorderedPsbt { global: Global::default(), inputs: InputSet::default(), outputs }
}
#[test]
fn join_distinct_outputs_without_modifiable_flag_must_fail() {
let joined = psbt(1).wrap().join(psbt(2).wrap());
assert!(!joined.is_ok());
}cargo test -p concurrent-psbt --test modifiable_flag join_distinct_outputs_without_modifiable_flag_must_fail
oak · automated · 06d29dc
| /// Returns the first input whose outpoint repeats, then the first output | ||
| /// that is missing a `PSBT_OUT_UNIQUE_ID` field or repeats one. | ||
| pub fn try_from_psbt(psbt: Psbt) -> Result<Self, UnorderedPsbtError> { | ||
| let inputs = InputSet::try_from_inputs(psbt.inputs)?.wrap(); |
There was a problem hiding this comment.
SIGHASH_SINGLE input accepted into unordered PSBT
UnorderedPsbt::try_from_psbt accepts inputs whose sighash_type is SIGHASH_SINGLE; into_psbt then shuffles inputs, so a SIGHASH_SINGLE signature can commit to a different output.
The spec requires SIGHASH_SINGLE inputs not be used in unordered transactions.
SIGHASH_SINGLEinputs MUST not be used in unordered transactions.— fungi-protocol/docs@f2806d9 · docs/psbt-concurrent.md#L102
oak · automated · 06d29dc
06d29dc to
61ce7ee
Compare
This comment has been minimized.
This comment has been minimized.
61ce7ee to
a23f4c2
Compare
This comment has been minimized.
This comment has been minimized.
a23f4c2 to
b0919a3
Compare
ac405b3 to
a3a74d8
Compare
range-diff for force push ac405b3333 → a3a74d8ae5
|
oakagent
left a comment
There was a problem hiding this comment.
Reviewed the 6 newest of 8 commits; the rest of the pull request will be reviewed in the next cycles.
| /// list. | ||
| /// Each entry in `fields` mirrors the field of the same name in the `v2` type. | ||
| /// If the two types differ, an `=` after the type names the [`MirroredField`] | ||
| /// that converts between them. The field itself is private: its visibility |
There was a problem hiding this comment.
Doc's visibility claim conflicts with private mirrored fields
The doc says a field's visibility applies to the $Source field and that the struct has the fields "as written", but in the v2 form the recursive call drops $vis for fields entries, so those fields are private and pub reaches only the accessors.
Each field may carry attributes and a visibility, which apply to the
1.
$Sourcestruct with the fields as written
oakagent · automated · a3a74d8
| /// Entries with the same key are joined, and the rest are unioned. A frozen | ||
| /// set's keys are fixed, so each distinct set of frozen keys is also kept. | ||
| /// The join is clean only if there is at most one distinct frozen key set and | ||
| /// it equals the keys of the entries. So a frozen set joins cleanly with |
There was a problem hiding this comment.
Clean-join doc ignores conflicting entries
is_ok also requires every entry to be conflict-free, so a frozen set joined with a same-key or subset-key set whose shared values differ is not clean and try_unwrap returns Err; the doc's unqualified joins cleanly is false for that case.
oakagent · automated · a3a74d8
| /// Extract a clean [`UnorderedPsbt`] if there are no conflicts, otherwise return `self`. | ||
| /// | ||
| /// # Errors | ||
| /// Returns `Err(self)` if any field contains a conflict. |
There was a problem hiding this comment.
is_ok docs omit frozen membership mismatch
try_unwrap and is_ok (line 242) can fail when frozen sets have different members even though every field is conflict-free; ResultKeyedSet::is_ok also requires keys_agree, which the method docs omit.
/// Frozen inputs or outputs join cleanly only with frozen sets of the same /// members, or with modifiable sets whose members they already have.
— fungi-protocol/concurrent-psbt@a3a74d8 · crates/concurrent-psbt/src/psbt/tx.rs#L37-L38
self.keys_agree() && self.entries.0.is_ok()
— fungi-protocol/concurrent-psbt@a3a74d8 · crates/concurrent-psbt/src/psbt/keyed_set.rs#L241
oakagent · automated · a3a74d8
a3a74d8 to
f75c6a9
Compare
range-diff for force push a3a74d8ae5 → f75c6a9162
|
f75c6a9 to
35bf835
Compare
range-diff for force push f75c6a9162 → 35bf83523d
|
35bf835 to
d7528fc
Compare
range-diff for force push 35bf83523d → d7528fc86f
|
d7528fc to
7fe9c87
Compare
range-diff for force push d7528fc86f → 7fe9c87e5c
|
7fe9c87 to
5fa5b95
Compare
range-diff for force push 7fe9c87e5c → 5fa5b95986
|
Choose each field's result type through Lift, so aliases and qualified Option, BTreeMap, and HashMap types behave like their underlying types. Generated PSBT records can themselves be wrapped inside these containers. Lift::wrap pairs with ResultWrapped::try_unwrap. Failed extraction retains the complete wrapped container, including its clean entries. Compose borrowed Conflicts iterators without erasing map keys or value types. Record conflict enums identify the field and any keydata, replacing the Debug-based callback visitor. Suggested-by: Codex Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
joinable_struct! generates both a plain struct and its result-domain counterpart, so Global, Input and Output are this crate's types. wrap is an inherent method, which replaces the GlobalExt, InputExt and OutputExt traits. The macro takes the psbt_v2 type and the field list. Each field mirrors the psbt_v2 field of the same name, so the macro generates the conversions: from_v2_fields moves the mirrored fields out of a psbt_v2 value, and encode_v2 moves them into one, keeping the psbt_v2 fields that this crate does not mirror. From converts each type from psbt_v2 and back. Global has no PSBT version or input and output counts, so Global::into_v2 takes the counts as arguments and sets the version to 2. The fields are private. A public field cannot enforce an invariant that the type system does not encode, and cannot be made private again without breaking callers. The macro generates a getter of the same name and a `set_` setter with the field's declared visibility, using pastey to name the setters. OutputUniqueIdExt still reads and writes the unique ID of an Output. closes #104 Suggested-by: Codex Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UniqueId becomes a required and immutable field of Output. Output::new takes it, and TryFrom<psbt_v2::v2::Output> reads it from PSBT_OUT_UNIQUE_ID. An output without one, or with an empty or overlong one, is rejected: an empty ID would make unrelated outputs join as one. This replaces OutputUniqueIdExt. joinable_struct! takes the field in a new `proprietary` list. Each entry names a ProprietaryField codec, which defines the field's key subtype and encoding, so changing the key later does not change the field's type or join. The macro decodes the proprietary fields in order, removing their keys, and encodes them back. closes #89 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Check compound and proprietary keydata in conflict paths, and exercise optional and map fields in the owned PSBT join and conversion properties.
5fa5b95 to
e4f34a0
Compare
range-diff for force push 5fa5b95986 → e4f34a0d4a
|
oakagent
left a comment
There was a problem hiding this comment.
Reviewed the pull request in full: the 6 newest of 8 commits, then the rest of its changes.
| /// | ||
| /// Each entry in `fields` mirrors the field of the same name in the `v2` type. | ||
| /// Each entry in `proprietary` is written | ||
| /// `name: Type = Codec`, and is stored in a proprietary key of `v2` by the |
There was a problem hiding this comment.
Proprietary entry syntax omits => Variant
The proprietary matcher requires => Variant after = Codec, but the explicit syntax documented for proprietary entries omits it, so an entry written as described does not match and fails to compile. The only non-empty proprietary block uses unique_id: UniqueId = UniqueIdField => UniqueId.
oakagent · automated · e4f34a0
This abstraction stores a set of inputs or outputs, keyed by their identifier. This this behaves like the set of identifiers that can union or conflict when joined depending on whether or not it is frozen. Additionally, with each identifier is also associated a value, so this also works like a hashmap. These values are joined recursively, so a set being frozen only means its identifier set is immutable.
Abstractly this is an unordered set of Output structs. Concretely, it is a hashmap keyed by the unique output ID, so that recursive merging merges disjoint field values associated with the same output ID.
Abstractly this is an unordered set of Input structs. Concretely, it is a hashmap keyed by the spent coin's outpoint, so that recursive merging merges disjoint field values associated with the same input.
This is the unordered counterpart to the upstream Psbt struct. Instead of Vec<Input> and Vec<Output>, these collections are represented using InputSet and OutputSet respectively. The result domain counterpart has additional logic to maintain consistency of counts, which are stored in the Global struct, with the actual counts of the InputSet and OutputSet as these are merged recursively.
e4f34a0 to
c74ab84
Compare
range-diff for force push e4f34a0d4a → c74ab8412a
|
c74ab84 to
2527d0f
Compare
range-diff for force push c74ab8412a → 2527d0f2ef
|
oakagent
left a comment
There was a problem hiding this comment.
Reviewed the 6 newest of 8 commits; the rest of the pull request will be reviewed in the next cycles.
|
|
||
| fn try_from(bytes: Vec<u8>) -> Result<Self, InvalidUniqueId> { | ||
| if bytes.len() > Self::MAX_LEN { | ||
| if bytes.is_empty() || bytes.len() > Self::MAX_LEN { |
There was a problem hiding this comment.
33-byte PSBT_OUT_UNIQUE_ID rejected on conversion
The commit routes Output::try_from through UniqueId::try_from, which rejects IDs longer than MAX_LEN (32), so a 33-byte PSBT_OUT_UNIQUE_ID that the spec allows as <bytes unique id> no longer converts; the parent accepted it.
oakagent · automated · 2527d0f
| /// then the first output whose `PSBT_OUT_UNIQUE_ID` is missing, empty or | ||
| /// longer than [`UniqueId::MAX_LEN`] bytes, then the first unique ID that | ||
| /// repeats. | ||
| pub fn try_from_psbt(psbt: Psbt) -> Result<Self, UnorderedPsbtError> { |
There was a problem hiding this comment.
Unordered PSBT accepts SIGHASH_SINGLE flag
try_from_psbt checks only the input and output modifiable bits of tx_modifiable_flags, so a PSBT with the SIGHASH_SINGLE bit (0x04) is accepted even though the spec forbids it in unordered transactions; into_psbt then reorders inputs and outputs, changing which output a SIGHASH_SINGLE input signs.
If set, the
Has SIGHASH_SINGLEflag ofPSBT_GLOBAL_TX_MODIFIABLEmust not be set.SIGHASH_SINGLEinputs MUST not be used in unordered transactions.— fungi-protocol/docs@f2806d9 · docs/psbt-concurrent.md#L102
oakagent · automated · 2527d0f
opening as draft since a PR can't have more than one target it only makes sense opened against main after its dependencies are merged
depends on #72 #79 #78