Skip to content

Commit

Permalink
Allow receiver to set minimum fee_rate
Browse files Browse the repository at this point in the history
    Payjoin receiver can set minimum `fee_rate`
    in order to reject payjoin requests that contain
    a psbt which doesnt meet the minimum `fee_rate`
    threshold.

    We add this functionality to `check_broadcast_suitability`
    function which previously was called `check_can_broadcast`.
  • Loading branch information
jbesraa committed Dec 8, 2023
1 parent 1c0bc01 commit 9b06a53
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions payjoin/src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,14 @@ impl UncheckedProposal {
/// Call this after checking downstream.
pub fn check_broadcast_suitability(
self,
min_feerate: Option<FeeRate>,
min_fee_rate: Option<FeeRate>,
can_broadcast: impl Fn(&bitcoin::Transaction) -> Result<bool, Error>,
) -> Result<MaybeInputsOwned, Error> {
let original_psbt_fee_rate = self.psbt_fee_rate()?;
if let Some(min_feerate) = min_feerate {
if original_psbt_fee_rate < min_feerate {
if let Some(min_fee_rate) = min_fee_rate {
if original_psbt_fee_rate < min_fee_rate {
return Err(Error::BadRequest(
InternalRequestError::PsbtBelowFeeRate(original_psbt_fee_rate, min_feerate)
InternalRequestError::PsbtBelowFeeRate(original_psbt_fee_rate, min_fee_rate)
.into(),
));
}
Expand Down Expand Up @@ -862,9 +862,7 @@ mod test {
}
}

fn proposal_from_test_vector(
min_feerate: Option<u64>,
) -> Result<UncheckedProposal, RequestError> {
fn proposal_from_test_vector() -> Result<UncheckedProposal, RequestError> {
// OriginalPSBT Test Vector from BIP
// | InputScriptType | Orginal PSBT Fee rate | maxadditionalfeecontribution | additionalfeeoutputindex|
// |-----------------|-----------------------|------------------------------|-------------------------|
Expand All @@ -873,17 +871,14 @@ mod test {

let body = original_psbt.as_bytes();
let headers = MockHeaders::new(body.len() as u64);
let min_feerate_param = format!("minfeerate={}", min_feerate.unwrap_or(0));
let query = format!(
"maxadditionalfeecontribution=182&additionalfeeoutputindex=0&{}",
min_feerate_param
);
let query =
format!("maxadditionalfeecontribution=182&additionalfeeoutputindex=0&minfeerate=0");
UncheckedProposal::from_request(body, &query, headers)
}

#[test]
fn can_get_proposal_from_request() {
let proposal = proposal_from_test_vector(None);
let proposal = proposal_from_test_vector();
assert!(proposal.is_ok(), "OriginalPSBT should be a valid request");
}

Expand All @@ -893,7 +888,7 @@ mod test {

use bitcoin::{Address, Network};

let proposal = proposal_from_test_vector(None).unwrap();
let proposal = proposal_from_test_vector().unwrap();
let mut payjoin = proposal
.assume_interactive_receiver()
.check_inputs_not_owned(|_| Ok(false))
Expand All @@ -917,7 +912,7 @@ mod test {

#[test]
fn psbt_fee_rate() {
let proposal = proposal_from_test_vector(None).unwrap();
let proposal = proposal_from_test_vector().unwrap();
assert_eq!(proposal.psbt_fee_rate().unwrap().to_sat_per_vb_floor(), 2);
}
}

0 comments on commit 9b06a53

Please sign in to comment.