Browse Source

Construct payouts from intervals

no-contract-setup-message
Lucas Soriano del Pino 3 years ago
parent
commit
b49dc137fe
No known key found for this signature in database GPG Key ID: EE611E973A1530E7
  1. 77
      cfd_protocol/src/lib.rs
  2. 46
      cfd_protocol/tests/cfds.rs
  3. 1
      daemon/src/model/cfd.rs
  4. 3
      daemon/src/setup_contract_actor.rs

77
cfd_protocol/src/lib.rs

@ -1,3 +1,4 @@
pub use interval::Interval;
pub use secp256k1_zkp; pub use secp256k1_zkp;
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
@ -448,15 +449,23 @@ pub struct Payout {
impl Payout { impl Payout {
pub fn new( pub fn new(
msg_nonce_pairs: Vec<(Vec<u8>, schnorrsig::PublicKey)>, interval: Interval,
nonce_pks: Vec<schnorrsig::PublicKey>,
maker_amount: Amount, maker_amount: Amount,
taker_amount: Amount, taker_amount: Amount,
) -> Self { ) -> Vec<Self> {
Self { interval
msg_nonce_pairs, .as_digits()
maker_amount, .into_iter()
taker_amount, .map(|digits| {
} let msg_nonce_pairs = digits.zip(nonce_pks.clone()).collect();
Self {
msg_nonce_pairs,
maker_amount,
taker_amount,
}
})
.collect()
} }
fn into_txouts(self, maker_address: &Address, taker_address: &Address) -> Vec<TxOut> { fn into_txouts(self, maker_address: &Address, taker_address: &Address) -> Vec<TxOut> {
@ -942,24 +951,28 @@ mod tests {
let orig_maker_amount = 1000; let orig_maker_amount = 1000;
let orig_taker_amount = 1000; let orig_taker_amount = 1000;
let payout = Payout::new( let payouts = Payout::new(
vec![(b"win".to_vec(), nonce_pk)], Interval::new(0, 10_000).unwrap(),
vec![nonce_pk; 20],
Amount::from_sat(orig_maker_amount), Amount::from_sat(orig_maker_amount),
Amount::from_sat(orig_taker_amount), Amount::from_sat(orig_taker_amount),
); );
let fee = 100; let fee = 100;
let updated_payout = payout
.with_updated_fee(Amount::from_sat(fee), dummy_dust_limit, dummy_dust_limit)
.unwrap();
assert_eq!( for payout in payouts {
updated_payout.maker_amount, let updated_payout = payout
Amount::from_sat(orig_maker_amount - fee / 2) .with_updated_fee(Amount::from_sat(fee), dummy_dust_limit, dummy_dust_limit)
); .unwrap();
assert_eq!(
updated_payout.taker_amount, assert_eq!(
Amount::from_sat(orig_taker_amount - fee / 2) updated_payout.maker_amount,
); Amount::from_sat(orig_maker_amount - fee / 2)
);
assert_eq!(
updated_payout.taker_amount,
Amount::from_sat(orig_taker_amount - fee / 2)
);
}
} }
#[test] #[test]
@ -975,20 +988,24 @@ mod tests {
let orig_maker_amount = dummy_dust_limit.as_sat() - 1; let orig_maker_amount = dummy_dust_limit.as_sat() - 1;
let orig_taker_amount = 1000; let orig_taker_amount = 1000;
let payout = Payout::new( let payouts = Payout::new(
vec![(b"win".to_vec(), nonce_pk)], Interval::new(0, 10_000).unwrap(),
vec![nonce_pk; 20],
Amount::from_sat(orig_maker_amount), Amount::from_sat(orig_maker_amount),
Amount::from_sat(orig_taker_amount), Amount::from_sat(orig_taker_amount),
); );
let fee = 100; let fee = 100;
let updated_payout = payout
.with_updated_fee(Amount::from_sat(fee), dummy_dust_limit, dummy_dust_limit)
.unwrap();
assert_eq!(updated_payout.maker_amount, Amount::from_sat(0)); for payout in payouts {
assert_eq!( let updated_payout = payout
updated_payout.taker_amount, .with_updated_fee(Amount::from_sat(fee), dummy_dust_limit, dummy_dust_limit)
Amount::from_sat(orig_taker_amount - (fee + orig_maker_amount)) .unwrap();
);
assert_eq!(updated_payout.maker_amount, Amount::from_sat(0));
assert_eq!(
updated_payout.taker_amount,
Amount::from_sat(orig_taker_amount - (fee + orig_maker_amount))
);
}
} }
} }

46
cfd_protocol/tests/cfds.rs

@ -7,6 +7,7 @@ use bdk::wallet::AddressIndex;
use bdk::SignOptions; use bdk::SignOptions;
use bitcoin::util::psbt::PartiallySignedTransaction; use bitcoin::util::psbt::PartiallySignedTransaction;
use bitcoin::Txid; use bitcoin::Txid;
use cfd_protocol::interval::Interval;
use cfd_protocol::{ use cfd_protocol::{
commit_descriptor, compute_adaptor_point, create_cfd_transactions, finalize_spend_transaction, commit_descriptor, compute_adaptor_point, create_cfd_transactions, finalize_spend_transaction,
lock_descriptor, punish_transaction, renew_cfd_transactions, spending_tx_sighash, lock_descriptor, punish_transaction, renew_cfd_transactions, spending_tx_sighash,
@ -32,22 +33,19 @@ fn create_cfd() {
let payouts = vec![ let payouts = vec![
Payout::new( Payout::new(
vec![vec![0u8]; 20] Interval::new(0, 10_000).unwrap(),
.into_iter() announcement.nonce_pks(),
.zip(announcement.nonce_pks())
.collect(),
Amount::from_btc(1.5).unwrap(), Amount::from_btc(1.5).unwrap(),
Amount::from_btc(0.5).unwrap(), Amount::from_btc(0.5).unwrap(),
), ),
Payout::new( Payout::new(
vec![vec![1u8]; 20] Interval::new(10_001, 20_000).unwrap(),
.into_iter() announcement.nonce_pks(),
.zip(announcement.nonce_pks())
.collect(),
Amount::ZERO, Amount::ZERO,
Amount::from_btc(2.0).unwrap(), Amount::from_btc(2.0).unwrap(),
), ),
]; ]
.concat();
let refund_timelock = 0; let refund_timelock = 0;
@ -119,22 +117,19 @@ fn renew_cfd() {
let payouts = vec![ let payouts = vec![
Payout::new( Payout::new(
vec![vec![0u8]; 20] Interval::new(0, 10_000).unwrap(),
.into_iter() announcement.nonce_pks(),
.zip(announcement.nonce_pks())
.collect(),
Amount::from_btc(2.0).unwrap(), Amount::from_btc(2.0).unwrap(),
Amount::ZERO, Amount::ZERO,
), ),
Payout::new( Payout::new(
vec![vec![1u8]; 20] Interval::new(10_001, 20_000).unwrap(),
.into_iter() announcement.nonce_pks(),
.zip(announcement.nonce_pks())
.collect(),
Amount::ZERO, Amount::ZERO,
Amount::from_btc(2.0).unwrap(), Amount::from_btc(2.0).unwrap(),
), ),
]; ]
.concat();
let refund_timelock = 0; let refund_timelock = 0;
@ -159,22 +154,19 @@ fn renew_cfd() {
let payouts = vec![ let payouts = vec![
Payout::new( Payout::new(
vec![vec![0u8]; 20] Interval::new(0, 10_000).unwrap(),
.into_iter() announcement.nonce_pks(),
.zip(announcement.nonce_pks())
.collect(),
Amount::from_btc(1.5).unwrap(), Amount::from_btc(1.5).unwrap(),
Amount::from_btc(0.5).unwrap(), Amount::from_btc(0.5).unwrap(),
), ),
Payout::new( Payout::new(
vec![vec![1u8]; 20] Interval::new(10_001, 20_000).unwrap(),
.into_iter() announcement.nonce_pks(),
.zip(announcement.nonce_pks())
.collect(),
Amount::from_btc(0.5).unwrap(), Amount::from_btc(0.5).unwrap(),
Amount::from_btc(1.5).unwrap(), Amount::from_btc(1.5).unwrap(),
), ),
]; ]
.concat();
let maker_cfd_txs = renew_cfd_transactions( let maker_cfd_txs = renew_cfd_transactions(
maker_cfd_txs.lock, maker_cfd_txs.lock,

1
daemon/src/model/cfd.rs

@ -597,6 +597,7 @@ pub struct FinalizedCfd {
pub lock: PartiallySignedTransaction, pub lock: PartiallySignedTransaction,
pub commit: (Transaction, EcdsaAdaptorSignature), pub commit: (Transaction, EcdsaAdaptorSignature),
#[allow(clippy::type_complexity)]
pub cets: Vec<( pub cets: Vec<(
Transaction, Transaction,
EcdsaAdaptorSignature, EcdsaAdaptorSignature,

3
daemon/src/setup_contract_actor.rs

@ -219,6 +219,7 @@ impl AllParams {
} }
} }
#[allow(clippy::type_complexity)]
fn verify_cets( fn verify_cets(
oracle_pk: &schnorrsig::PublicKey, oracle_pk: &schnorrsig::PublicKey,
other: &PartyParams, other: &PartyParams,
@ -240,7 +241,7 @@ fn verify_cets(
verify_cet_encsig( verify_cet_encsig(
tx, tx,
other_encsig, other_encsig,
&msg_nonce_pairs, msg_nonce_pairs,
&other.identity_pk, &other.identity_pk,
oracle_pk, oracle_pk,
commit_desc, commit_desc,

Loading…
Cancel
Save