Browse Source

Remove Interval from public API

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

1
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,

31
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<schnorrsig::PublicKey>,
maker_amount: Amount,
taker_amount: Amount,
) -> Vec<Self> {
interval
) -> Result<Vec<Self>> {
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<TxOut> {
@ -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 {

54
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::<u64>::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

Loading…
Cancel
Save