Browse Source

Take RangeInclusive<u64> as argument to Payout constructor

no-contract-setup-message
Lucas Soriano del Pino 3 years ago
parent
commit
1eb237faaa
No known key found for this signature in database GPG Key ID: EE611E973A1530E7
  1. 5
      cfd_protocol/src/interval.rs
  2. 12
      cfd_protocol/src/protocol.rs
  3. 25
      cfd_protocol/tests/cfds.rs

5
cfd_protocol/src/interval.rs

@ -17,7 +17,8 @@ const BASE: usize = 2;
pub struct Digits(BitVec);
impl Digits {
pub fn new(start: u64, end: u64) -> Result<Vec<Self>, Error> {
pub fn new(range: RangeInclusive<u64>) -> Result<Vec<Self>, Error> {
let (start, end) = range.into_inner();
if start > MAX_PRICE_DEC || end > MAX_PRICE_DEC {
return Err(Error::RangeOverMax);
}
@ -127,7 +128,7 @@ mod tests {
}
fn to_digits(&self) -> Result<Vec<Digits>> {
let digits = Digits::new(*self.0.start(), *self.0.end())?;
let digits = Digits::new(self.0.clone())?;
Ok(digits)
}

12
cfd_protocol/src/protocol.rs

@ -22,6 +22,7 @@ use itertools::Itertools;
use secp256k1_zkp::{self, schnorrsig, EcdsaAdaptorSignature, SecretKey, Signature, SECP256K1};
use std::collections::HashMap;
use std::iter::FromIterator;
use std::ops::RangeInclusive;
mod sighash_ext;
mod transaction_ext;
@ -341,12 +342,11 @@ pub struct Payout {
impl Payout {
pub fn new(
start: u64,
end: u64,
range: RangeInclusive<u64>,
maker_amount: Amount,
taker_amount: Amount,
) -> Result<Vec<Self>> {
let digits = interval::Digits::new(start, end).context("invalid interval")?;
let digits = interval::Digits::new(range).context("invalid interval")?;
Ok(digits
.into_iter()
.map(|digits| Self {
@ -473,8 +473,7 @@ mod tests {
let orig_maker_amount = 1000;
let orig_taker_amount = 1000;
let payouts = Payout::new(
0,
10_000,
0..=10_000,
Amount::from_sat(orig_maker_amount),
Amount::from_sat(orig_taker_amount),
)
@ -508,8 +507,7 @@ mod tests {
let orig_maker_amount = dummy_dust_limit.as_sat() - 1;
let orig_taker_amount = 1000;
let payouts = Payout::new(
0,
10_000,
0..=10_000,
Amount::from_sat(orig_maker_amount),
Amount::from_sat(orig_taker_amount),
)

25
cfd_protocol/tests/cfds.rs

@ -32,13 +32,17 @@ fn create_cfd() {
let payouts = vec![
Payout::new(
0,
10_000,
0..=10_000,
Amount::from_btc(1.5).unwrap(),
Amount::from_btc(0.5).unwrap(),
)
.unwrap(),
Payout::new(10_001, 20_000, Amount::ZERO, Amount::from_btc(2.0).unwrap()).unwrap(),
Payout::new(
10_001..=20_000,
Amount::ZERO,
Amount::from_btc(2.0).unwrap(),
)
.unwrap(),
]
.concat();
@ -112,8 +116,13 @@ fn renew_cfd() {
let (_event, announcement) = announce(&mut rng);
let payouts = vec![
Payout::new(0, 10_000, Amount::from_btc(2.0).unwrap(), Amount::ZERO).unwrap(),
Payout::new(10_001, 20_000, Amount::ZERO, Amount::from_btc(2.0).unwrap()).unwrap(),
Payout::new(0..=10_000, Amount::from_btc(2.0).unwrap(), Amount::ZERO).unwrap(),
Payout::new(
10_001..=20_000,
Amount::ZERO,
Amount::from_btc(2.0).unwrap(),
)
.unwrap(),
]
.concat();
@ -140,15 +149,13 @@ fn renew_cfd() {
let payouts = vec![
Payout::new(
0,
10_000,
0..=10_000,
Amount::from_btc(1.5).unwrap(),
Amount::from_btc(0.5).unwrap(),
)
.unwrap(),
Payout::new(
10_001,
20_000,
10_001..=20_000,
Amount::from_btc(0.5).unwrap(),
Amount::from_btc(1.5).unwrap(),
)

Loading…
Cancel
Save