Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pay: fix duplicated PSBT inputs #136

Merged
merged 2 commits into from
Mar 27, 2024
Merged
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
19 changes: 11 additions & 8 deletions Cargo.lock

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

9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ strict_types = "2.7.0-beta.2"
bp-core = "0.11.0-beta.5"
bp-seals = "0.11.0-beta.5"
bp-std = "0.11.0-beta.5"
bp-wallet = "0.11.0-beta.4"
bp-util = "0.11.0-beta.4"
bp-wallet = "0.11.0-beta.5"
bp-util = "0.11.0-beta.5"
bp-electrum = "0.11.0-beta.5"
bp-esplora = "0.11.0-beta.5"
descriptors = "0.11.0-beta.5"
Expand Down Expand Up @@ -96,7 +96,4 @@ serde = ["serde_crate", "serde_with", "serde_yaml", "bp-std/serde", "bp-wallet/s
features = ["all"]

[patch.crates-io]
bp-util = { git = "https://github.com/BP-WG/bp-wallet", branch = "master" }
bp-wallet = { git = "https://github.com/BP-WG/bp-wallet", branch = "master" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "master" }
rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "master" }
rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "dups" }
11 changes: 4 additions & 7 deletions psbt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

use bp::dbc::opret::OpretProof;
use bp::dbc::tapret::TapretProof;
pub use psbt::*;

Check failure on line 29 in psbt/src/lib.rs

View workflow job for this annotation

GitHub Actions / testing

multiple candidates for `rlib` dependency `psbt` found
pub use rgb::*;
use rgbstd::containers::{Batch, Fascia};
use rgbstd::{AnchorSet, XAnchor, XChain};
Expand Down Expand Up @@ -55,7 +55,7 @@
Rgb(RgbPsbtError),

#[from]
Dbc(DbcPsbtError),

Check failure on line 58 in psbt/src/lib.rs

View workflow job for this annotation

GitHub Actions / testing

cannot find type `DbcPsbtError` in this scope

Check failure on line 58 in psbt/src/lib.rs

View workflow job for this annotation

GitHub Actions / testing

cannot find type `DbcPsbtError` in this scope
}

#[derive(Clone, Eq, PartialEq, Debug, Display, Error)]
Expand All @@ -71,19 +71,16 @@
fn rgb_extract(&self) -> Result<Fascia, ExtractError>;
}

impl RgbPsbt for Psbt {

Check failure on line 74 in psbt/src/lib.rs

View workflow job for this annotation

GitHub Actions / testing

cannot find type `Psbt` in this scope
fn rgb_embed(&mut self, batch: Batch) -> Result<(), EmbedError> {
for info in batch {
let contract_id = info.transition.contract_id;
let mut inputs = info.inputs.into_inner();
for input in self.inputs_mut() {
let outpoint = input.prevout().outpoint();
if let Some(pos) = inputs.iter().position(|i| **i == XChain::Bitcoin(outpoint)) {
inputs.remove(pos);
input
.set_rgb_consumer(contract_id, info.id)
.map_err(|_| EmbedError::PsbtRepeatedInputs)?;
}
inputs.remove(&XChain::Bitcoin(input.prevout().outpoint()));
input
.set_rgb_consumer(contract_id, info.id)
.map_err(|_| EmbedError::PsbtRepeatedInputs)?;
}
if !inputs.is_empty() {
return Err(EmbedError::AbsentInputs);
Expand Down
33 changes: 21 additions & 12 deletions src/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::{BTreeMap, BTreeSet};
use std::convert::Infallible;

use amplify::confinement::Confined;
Expand Down Expand Up @@ -200,30 +201,37 @@ impl Runtime {
.cloned()
.ok_or(CompositionError::NoAssignment)?;

let outputs = match invoice.owned_state {
let prev_outputs = match invoice.owned_state {
InvoiceState::Amount(amount) => {
let filter = ContractOutpointsFilter {
contract_id,
filter: self,
};
let mut state = contract
let state: BTreeMap<_, Vec<Amount>> = contract
.fungible(assignment_name, &filter)?
.collect::<Vec<_>>();
state.sort_by_key(|a| a.state);
.fold(bmap![], |mut set, a| {
set.entry(a.seal).or_default().push(a.state);
set
});
let mut state: Vec<_> = state
.into_iter()
.map(|(seal, vals)| (vals.iter().copied().sum::<Amount>(), seal, vals))
.collect();
state.sort_by_key(|(sum, _, _)| *sum);
let mut sum = Amount::ZERO;
state
.iter()
.rev()
.take_while(|a| {
.take_while(|(val, _, _)| {
if sum >= amount {
false
} else {
sum += a.state;
sum += *val;
true
}
})
.map(|a| a.seal)
.collect::<Vec<_>>()
.map(|(_, seal, _)| *seal)
.collect::<BTreeSet<_>>()
}
_ => return Err(CompositionError::Unsupported),
};
Expand All @@ -236,14 +244,15 @@ impl Runtime {
)]
}
};
let outpoints = outputs
let prev_outpoints = prev_outputs
.iter()
// TODO: Support liquid
.map(|o| o.as_reduced_unsafe())
.map(|o| Outpoint::new(o.txid, o.vout));
params.tx.change_keychain = RgbKeychain::for_method(method).into();
let (mut psbt, mut meta) =
self.wallet_mut()
.construct_psbt(outpoints, &beneficiaries, params.tx)?;
.construct_psbt(prev_outpoints, &beneficiaries, params.tx)?;

let beneficiary_script =
if let Beneficiary::WitnessVout(addr) = invoice.beneficiary.into_inner() {
Expand Down Expand Up @@ -284,8 +293,8 @@ impl Runtime {
}
Beneficiary::BlindedSeal(_) => None,
};
let batch =
self.compose(invoice, outputs, method, beneficiary_vout, |_, _, _| meta.change_vout)?;
let batch = self
.compose(invoice, prev_outputs, method, beneficiary_vout, |_, _, _| meta.change_vout)?;

let methods = batch.close_method_set();
if methods.has_opret_first() {
Expand Down
Loading