Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/concurrent-psbt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ prop-tests = []

[dependencies]
bitcoin = "0.32"
pastey = "0.2.3"
psbt-v2 = "0.3.0"
rand = "0.9"

Expand Down
60 changes: 0 additions & 60 deletions crates/concurrent-psbt/src/psbt/conflict_macro.rs

This file was deleted.

154 changes: 96 additions & 58 deletions crates/concurrent-psbt/src/psbt/global.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,71 @@
#![allow(clippy::result_large_err)]

//! PSBT global field merging.
#![cfg_attr(
not(test),
expect(dead_code, reason = "global serialization is used by UnorderedPsbt")
)]
//! PSBT global fields and their merging.
//!
//! [`ResultGlobal`] wraps each global field in the internal result domain,
//! enabling field-by-field merging that accumulates conflicts without short-circuiting.
//! [`GlobalExt::wrap`] lifts a clean [`Global`] into the result domain.
//! [`Global`] holds the global fields of a PSBT, and [`ResultGlobal`] wraps
//! each of them in the internal result domain, enabling field-by-field merging
//! that accumulates conflicts without short-circuiting.

use std::collections::BTreeMap;

use bitcoin::bip32::{KeySource, Xpub};
use bitcoin::locktime::absolute;
use bitcoin::transaction;

use psbt_v2::Version;
use psbt_v2::raw;
pub use psbt_v2::v2::Global;

joinable_struct! {
/// Result-domain wrapper around a BIP 370 [`Global`].
/// The global fields of a PSBT.
///
/// Mirrors the BIP 370 global map except `PSBT_GLOBAL_VERSION`, which is
/// always 2, and the input and output counts, which are filled in when
/// converting to a BIP 370 PSBT.
source: Global,
/// Result-domain counterpart of [`Global`].
///
/// Produced by joining two [`Global`] values via [`GlobalExt::wrap`].
/// Produced by joining [`Global`] values lifted with [`Global::wrap`].
/// Use [`ResultGlobal::is_ok`] to check for conflicts and
/// [`ResultGlobal::try_unwrap`] to extract.
source: Global,
result: ResultGlobal,
conflicts: GlobalConflict, GlobalField {
Version => version: Version,
InputCount => input_count: usize,
OutputCount => output_count: usize,
TxVersion => tx_version: transaction::Version,
FallbackLockTime => fallback_lock_time: absolute::LockTime,
TxModifiableFlags => tx_modifiable_flags: u8,
Xpub => xpubs[Xpub]: KeySource,
Proprietary => proprietaries[raw::ProprietaryKey]: Vec<u8>,
Unknown => unknowns[raw::Key]: Vec<u8>,
},
ext: GlobalExt,
conflicts: GlobalConflict, GlobalField,
v2: psbt_v2::v2::Global,
fields: {
version: Version,
tx_version: transaction::Version,
fallback_lock_time: Option<absolute::LockTime>,
tx_modifiable_flags: u8,
input_count: usize,
output_count: usize,
xpubs: BTreeMap<Xpub, KeySource>,
proprietaries: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
unknowns: BTreeMap<raw::Key, Vec<u8>>,
tx_version: transaction::Version => TxVersion,
fallback_lock_time: Option<absolute::LockTime> => FallbackLockTime(absolute::LockTime),
tx_modifiable_flags: u8 => TxModifiableFlags,
xpubs: BTreeMap<Xpub, KeySource> => Xpub[Xpub](KeySource),
proprietaries: BTreeMap<raw::ProprietaryKey, Vec<u8>> => Proprietary[raw::ProprietaryKey](Vec<u8>),
unknowns: BTreeMap<raw::Key, Vec<u8>> => Unknown[raw::Key](Vec<u8>),
},
proprietary: {},
}

impl Default for Global {
fn default() -> Self {
Self::from_v2_fields(psbt_v2::v2::Global::default())
}
}

/// Drops the version and the input and output counts.
impl From<psbt_v2::v2::Global> for Global {
fn from(global: psbt_v2::v2::Global) -> Self {
let Ok(global) = Self::decode_v2::<std::convert::Infallible>(global);
global
}
}

impl Global {
/// Convert to a BIP 370 global map for a PSBT with `input_count` inputs
/// and `output_count` outputs.
pub(crate) fn into_v2(self, input_count: usize, output_count: usize) -> psbt_v2::v2::Global {
self.encode_v2(psbt_v2::v2::Global {
input_count,
output_count,
..psbt_v2::v2::Global::default()
})
}
}

Expand All @@ -70,6 +90,14 @@ mod tests {
assert!(joined.try_unwrap().is_err());
}

#[test]
fn default_is_an_empty_v2_global() {
assert_eq!(
Global::default().into_v2(0, 0),
psbt_v2::v2::Global::default()
);
}

#[test]
fn conflicts_identify_the_field() {
let mut a = Global::default();
Expand All @@ -87,6 +115,9 @@ mod tests {
#[cfg(feature = "prop-tests")]
mod prop {
use super::*;
use bitcoin::Network;
use bitcoin::bip32::{DerivationPath, Xpriv};
use bitcoin::secp256k1::Secp256k1;
use proptest::prelude::*;

fn arb_proprietary_key() -> impl Strategy<Value = raw::ProprietaryKey> {
Expand All @@ -102,13 +133,19 @@ mod tests {
})
}

/// The master xpub of `seed`, with its own fingerprint as key source.
fn xpub_from_seed(seed: u8) -> (Xpub, KeySource) {
let xpriv = Xpriv::new_master(Network::Bitcoin, &[seed; 16]).expect("valid seed");
let xpub = Xpub::from_priv(&Secp256k1::new(), &xpriv);
(xpub, (xpub.fingerprint(), DerivationPath::master()))
}

fn arb_global() -> impl Strategy<Value = Global> {
(
proptest::bool::ANY,
0u8..4,
proptest::bool::ANY,
0usize..4,
0usize..4,
proptest::option::of(0u8..2),
proptest::collection::btree_map(
arb_proprietary_key(),
proptest::collection::vec(0u8..=255, 0..=8),
Expand All @@ -122,33 +159,21 @@ mod tests {
),
)
.prop_map(
|(
use_v1,
flags,
has_lock_time,
in_count,
out_count,
|(use_v1, flags, has_lock_time, xpub, proprietaries, unknowns)| Global {
tx_version: if use_v1 {
transaction::Version::ONE
} else {
transaction::Version::TWO
},
tx_modifiable_flags: flags,
fallback_lock_time: if has_lock_time {
Some(absolute::LockTime::from_consensus(500_000))
} else {
None
},
xpubs: xpub.map(xpub_from_seed).into_iter().collect(),
proprietaries,
unknowns,
)| {
Global {
tx_version: if use_v1 {
transaction::Version::ONE
} else {
transaction::Version::TWO
},
tx_modifiable_flags: flags,
fallback_lock_time: if has_lock_time {
Some(absolute::LockTime::from_consensus(500_000))
} else {
None
},
input_count: in_count,
output_count: out_count,
proprietaries,
unknowns,
..Global::default()
}
},
)
}
Expand All @@ -170,8 +195,21 @@ mod tests {
prop_assert_eq!(unwrapped.wrap(), wrapped);
}

#[test]
fn v2_roundtrip_derives_version_and_counts(
a in arb_global(),
input_count in 0usize..4,
output_count in 0usize..4,
) {
let v2 = a.clone().into_v2(input_count, output_count);
prop_assert_eq!(v2.version, psbt_v2::Version::TWO);
prop_assert_eq!((v2.input_count, v2.output_count), (input_count, output_count));
prop_assert_eq!(Global::from(v2), a);
}

#[test]
fn is_ok_consistency(a in arb_result_global()) {
prop_assert_eq!(a.conflicts().next().is_none(), a.is_ok());
if a.is_ok() {
prop_assert!(a.try_unwrap().is_ok());
} else {
Expand Down
Loading
Loading