From c4d34ae4436063cb3d6e3b7560dae4dd12bb8986 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Mon, 20 Sep 2021 16:20:23 +1000 Subject: [PATCH] Remove Interval from public API --- cfd_protocol/src/lib.rs | 1 - cfd_protocol/src/protocol.rs | 31 +++++++++++++-------- cfd_protocol/tests/cfds.rs | 54 ++++++++++++++++++++++-------------- 3 files changed, 52 insertions(+), 34 deletions(-) diff --git a/cfd_protocol/src/lib.rs b/cfd_protocol/src/lib.rs index c5972b0..02d1eef 100644 --- a/cfd_protocol/src/lib.rs +++ b/cfd_protocol/src/lib.rs @@ -2,7 +2,6 @@ mod interval; mod oracle; mod protocol; -pub use interval::Interval; pub use oracle::{attest, nonce}; pub use protocol::{ commit_descriptor, compute_adaptor_point, create_cfd_transactions, finalize_spend_transaction, diff --git a/cfd_protocol/src/protocol.rs b/cfd_protocol/src/protocol.rs index 058cd3d..ce9fd2e 100644 --- a/cfd_protocol/src/protocol.rs +++ b/cfd_protocol/src/protocol.rs @@ -1,8 +1,9 @@ +use crate::interval::Interval; use crate::protocol::sighash_ext::SigHashExt; use crate::protocol::transactions::{ - lock_transaction, CommitTransaction, ContractExecutionTransaction, RefundTransaction, + lock_transaction, CommitTransaction, ContractExecutionTransaction as ContractExecutionTx, + RefundTransaction, }; -use crate::{oracle, Interval}; use anyhow::{bail, Context, Result}; use bdk::bitcoin::hashes::hex::ToHex; @@ -204,7 +205,7 @@ fn build_cfds( let cets = payouts .into_iter() .map(|payout| { - let cet = ContractExecutionTransaction::new( + let cet = ContractExecutionTx::new( &commit_tx, payout.clone(), &maker_address, @@ -344,12 +345,14 @@ pub struct Payout { impl Payout { pub fn new( - interval: Interval, + start: u64, + end: u64, nonce_pks: Vec, maker_amount: Amount, taker_amount: Amount, - ) -> Vec { - interval + ) -> Result> { + let interval = Interval::new(start, end).context("invalid interval")?; + Ok(interval .as_digits() .into_iter() .map(|digits| { @@ -364,7 +367,7 @@ impl Payout { taker_amount, } }) - .collect() + .collect()) } fn into_txouts(self, maker_address: &Address, taker_address: &Address) -> Vec { @@ -443,7 +446,7 @@ fn compute_signature_point( Ok(secp256k1_zkp::PublicKey::from_slice(&buf)?) } - let hash = oracle::msg_hash(oracle_pk, nonce_pk, msg); + let hash = crate::oracle::msg_hash(oracle_pk, nonce_pk, msg); let mut oracle_pk = schnorr_pubkey_to_pubkey(oracle_pk)?; oracle_pk.mul_assign(SECP256K1, &hash)?; let nonce_pk = schnorr_pubkey_to_pubkey(nonce_pk)?; @@ -486,11 +489,13 @@ mod tests { let orig_maker_amount = 1000; let orig_taker_amount = 1000; let payouts = Payout::new( - Interval::new(0, 10_000).unwrap(), + 0, + 10_000, vec![nonce_pk; 20], Amount::from_sat(orig_maker_amount), Amount::from_sat(orig_taker_amount), - ); + ) + .unwrap(); let fee = 100; for payout in payouts { @@ -523,11 +528,13 @@ mod tests { let orig_maker_amount = dummy_dust_limit.as_sat() - 1; let orig_taker_amount = 1000; let payouts = Payout::new( - Interval::new(0, 10_000).unwrap(), + 0, + 10_000, vec![nonce_pk; 20], Amount::from_sat(orig_maker_amount), Amount::from_sat(orig_taker_amount), - ); + ) + .unwrap(); let fee = 100; for payout in payouts { diff --git a/cfd_protocol/tests/cfds.rs b/cfd_protocol/tests/cfds.rs index 85dab00..ce332c5 100644 --- a/cfd_protocol/tests/cfds.rs +++ b/cfd_protocol/tests/cfds.rs @@ -10,13 +10,11 @@ use bitcoin::util::psbt::PartiallySignedTransaction; use cfd_protocol::{ attest, commit_descriptor, compute_adaptor_point, create_cfd_transactions, finalize_spend_transaction, lock_descriptor, nonce, punish_transaction, renew_cfd_transactions, - spending_tx_sighash, CfdTransactions, Interval, Payout, PunishParams, TransactionExt, - WalletExt, + spending_tx_sighash, CfdTransactions, Payout, PunishParams, TransactionExt, WalletExt, }; use rand::{thread_rng, CryptoRng, Rng, RngCore}; use secp256k1_zkp::{schnorrsig, EcdsaAdaptorSignature, SecretKey, Signature, SECP256K1}; use std::convert::TryInto; -use std::ops::RangeInclusive; #[test] fn create_cfd() { @@ -33,17 +31,21 @@ fn create_cfd() { let payouts = vec![ Payout::new( - Interval::new(0, 10_000).unwrap(), + 0, + 10_000, announcement.nonce_pks(), Amount::from_btc(1.5).unwrap(), Amount::from_btc(0.5).unwrap(), - ), + ) + .unwrap(), Payout::new( - Interval::new(10_001, 20_000).unwrap(), + 10, + 20_000, announcement.nonce_pks(), Amount::ZERO, Amount::from_btc(2.0).unwrap(), - ), + ) + .unwrap(), ] .concat(); @@ -117,17 +119,21 @@ fn renew_cfd() { let payouts = vec![ Payout::new( - Interval::new(0, 10_000).unwrap(), + 0, + 10_000, announcement.nonce_pks(), Amount::from_btc(2.0).unwrap(), Amount::ZERO, - ), + ) + .unwrap(), Payout::new( - Interval::new(10_001, 20_000).unwrap(), + 10, + 20_000, announcement.nonce_pks(), Amount::ZERO, Amount::from_btc(2.0).unwrap(), - ), + ) + .unwrap(), ] .concat(); @@ -154,17 +160,21 @@ fn renew_cfd() { let payouts = vec![ Payout::new( - Interval::new(0, 10_000).unwrap(), + 0, + 10_000, announcement.nonce_pks(), Amount::from_btc(1.5).unwrap(), Amount::from_btc(0.5).unwrap(), - ), + ) + .unwrap(), Payout::new( - Interval::new(10_001, 20_000).unwrap(), + 10, + 20_000, announcement.nonce_pks(), Amount::from_btc(0.5).unwrap(), Amount::from_btc(1.5).unwrap(), - ), + ) + .unwrap(), ] .concat(); @@ -279,13 +289,17 @@ fn cet_unlocked_with_oracle_sig_on_price_in_interval() { let oracle = Oracle::new(&mut rng); let (event, announcement) = announce(&mut rng); - let interval = Interval::new(5_000, 10_000).unwrap(); + let interval_start = 5_000; + let interval_end = 10_000; + let payouts = Payout::new( - interval.clone(), + interval_start, + interval_end, announcement.nonce_pks(), Amount::from_btc(1.5).unwrap(), Amount::from_btc(0.5).unwrap(), - ); + ) + .unwrap(); let refund_timelock = 0; @@ -304,9 +318,7 @@ fn cet_unlocked_with_oracle_sig_on_price_in_interval() { ); let commit_amount = Amount::from_sat(maker_cfd_txs.commit.0.output[0].value); - let interval = RangeInclusive::::from(interval); - let price = rng.gen_range(interval.start(), interval.end()); - + let price = rng.gen_range(interval_start, interval_end); let oracle_sigs = oracle.attest_price(price, &event.nonces.try_into().unwrap()); maker_cfd_txs