Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Model PriceOutOfRange error explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
luckysori committed Jul 26, 2022
1 parent 59adb8c commit 20ce513
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions model/src/cfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ impl Cfd {
None => return Ok(None),
};

let cet = dlc.signed_cet(attestation)?;
let cet = dlc.signed_cet(attestation)??;

let cet = match cet {
Ok(cet) => cet,
Expand Down Expand Up @@ -2292,21 +2292,26 @@ impl Dlc {
pub fn signed_cet(
&self,
attestation: &olivia::Attestation,
) -> Result<Result<Transaction, IrrelevantAttestation>> {
let cets = match self.cets.get(&attestation.id) {
) -> Result<Result<Result<Transaction, IrrelevantAttestation>, PriceOutOfRange>> {
let event_id = attestation.id;
let price = attestation.price;
let cets = match self.cets.get(&event_id) {
Some(cets) => cets,
None => {
return Ok(Err(IrrelevantAttestation {
id: attestation.id,
return Ok(Ok(Err(IrrelevantAttestation {
id: event_id,
txid: self.lock.0.txid(),
}))
})))
}
};

let cet = cets
.iter()
.find(|Cet { range, .. }| range.contains(&attestation.price))
.context("Price out of range of cets")?;
.find(|Cet { range, .. }| range.contains(&price))
.ok_or(PriceOutOfRange {
id: event_id,
price,
})?;
let encsig = cet.adaptor_sig;

let mut decryption_sk = attestation.scalars[0];
Expand Down Expand Up @@ -2343,7 +2348,7 @@ impl Dlc {
(counterparty_pubkey, counterparty_sig),
)?;

Ok(Ok(signed_cet))
Ok(Ok(Ok(signed_cet)))
}
}

Expand All @@ -2354,6 +2359,13 @@ pub struct IrrelevantAttestation {
txid: Txid,
}

#[derive(Debug, thiserror::Error, Clone, Copy)]
#[error("Attested price {price} is not in range of any CETs for event {id}")]
pub struct PriceOutOfRange {
id: BitMexPriceEventId,
price: u64,
}

/// Information which we need to remember in order to construct a
/// punishment transaction in case the counterparty publishes a
/// revoked commit transaction.
Expand Down

0 comments on commit 20ce513

Please sign in to comment.