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

Handle receiver contribution errors as unavailable #534

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 9 additions & 15 deletions payjoin-cli/src/app/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,8 @@ impl App {
.commit_outputs();

let provisional_payjoin = try_contributing_inputs(payjoin.clone(), &bitcoind)
.unwrap_or_else(|e| {
log::warn!("Failed to contribute inputs: {}", e);
payjoin.commit_inputs()
});
.map_err(|e| ReplyableError::Implementation(e.into()))?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use:

Suggested change
.map_err(|e| ReplyableError::Implementation(e.into()))?
.map_err(ReplyableError::Implementation)?

here too?

.commit_inputs();

let payjoin_proposal = provisional_payjoin.finalize_proposal(
|psbt: &Psbt| {
Expand All @@ -394,21 +392,17 @@ impl App {
fn try_contributing_inputs(
payjoin: payjoin::receive::v1::WantsInputs,
bitcoind: &bitcoincore_rpc::Client,
) -> Result<payjoin::receive::v1::ProvisionalProposal> {
) -> Result<payjoin::receive::v1::WantsInputs, ImplementationError> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning for changing the return type to WantsInputs here? We could achieve the same result by changing the return value to:

    Ok(payjoin
        .contribute_inputs(vec![selected_input])
        .map_err(ImplementationError::from)?
        .commit_inputs())

and the caller to

        let provisional_payjoin = try_contributing_inputs(payjoin.clone(), &bitcoind)
            .map_err(ReplyableError::Implementation)?;

let candidate_inputs = bitcoind
.list_unspent(None, None, None, None, None)
.context("Failed to list unspent from bitcoind")?
.map_err(ImplementationError::from)?
.into_iter()
.map(input_pair_from_list_unspent);
let selected_input = payjoin
.try_preserving_privacy(candidate_inputs)
.map_err(|e| anyhow!("Failed to make privacy preserving selection: {}", e))?;
log::debug!("selected input: {:#?}", selected_input);

Ok(payjoin
.contribute_inputs(vec![selected_input])
.expect("This shouldn't happen. Failed to contribute inputs.")
.commit_inputs())

let selected_input =
payjoin.try_preserving_privacy(candidate_inputs).map_err(ImplementationError::from)?;

payjoin.contribute_inputs(vec![selected_input]).map_err(ImplementationError::from)
}

fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, hyper::Error> {
Expand Down
24 changes: 9 additions & 15 deletions payjoin-cli/src/app/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,8 @@ impl App {
.commit_outputs();

let provisional_payjoin = try_contributing_inputs(payjoin.clone(), &bitcoind)
.unwrap_or_else(|e| {
log::warn!("Failed to contribute inputs: {}", e);
payjoin.commit_inputs()
});
.map_err(ReplyableError::Implementation)?
.commit_inputs();

let payjoin_proposal = provisional_payjoin.finalize_proposal(
|psbt: &Psbt| {
Expand Down Expand Up @@ -362,21 +360,17 @@ async fn handle_recoverable_error(
fn try_contributing_inputs(
payjoin: payjoin::receive::v2::WantsInputs,
bitcoind: &bitcoincore_rpc::Client,
) -> Result<payjoin::receive::v2::ProvisionalProposal> {
) -> Result<payjoin::receive::v2::WantsInputs, ImplementationError> {
let candidate_inputs = bitcoind
.list_unspent(None, None, None, None, None)
.context("Failed to list unspent from bitcoind")?
.map_err(ImplementationError::from)?
.into_iter()
.map(input_pair_from_list_unspent);
let selected_input = payjoin
.try_preserving_privacy(candidate_inputs)
.map_err(|e| anyhow!("Failed to make privacy preserving selection: {}", e))?;
log::debug!("selected input: {:#?}", selected_input);

Ok(payjoin
.contribute_inputs(vec![selected_input])
.expect("This shouldn't happen. Failed to contribute inputs.")
.commit_inputs())

let selected_input =
payjoin.try_preserving_privacy(candidate_inputs).map_err(ImplementationError::from)?;

payjoin.contribute_inputs(vec![selected_input]).map_err(ImplementationError::from)
}

async fn unwrap_ohttp_keys_or_else_fetch(config: &AppConfig) -> Result<payjoin::OhttpKeys> {
Expand Down
19 changes: 19 additions & 0 deletions payjoin/src/receive/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ impl fmt::Display for SelectionError {
}
}

impl error::Error for SelectionError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
use InternalSelectionError::*;

match &self.0 {
Empty => None,
TooManyOutputs => None,
NotFound => None,
}
}
}
impl From<InternalSelectionError> for SelectionError {
fn from(value: InternalSelectionError) -> Self { SelectionError(value) }
}
Expand All @@ -363,6 +374,14 @@ impl fmt::Display for InputContributionError {
}
}

impl error::Error for InputContributionError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match &self.0 {
InternalInputContributionError::ValueTooLow => None,
}
}
}

impl From<InternalInputContributionError> for InputContributionError {
fn from(value: InternalInputContributionError) -> Self { InputContributionError(value) }
}
Loading