Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions ddk-manager/src/contract/accepted_contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! # AcceptedContract

use super::offered_contract::OfferedContract;
use super::utils::cet_ranges;
use super::AdaptorInfo;
use crate::error::Error;
use bitcoin::{Amount, SignedAmount, Transaction};
use ddk_dlc::{DlcTransactions, PartyParams};
use ddk_messages::tlv_stream::TlvStream;
Expand Down Expand Up @@ -36,6 +38,25 @@ pub struct AcceptedContract {
}

impl AcceptedContract {
/// Where the CETs and the adaptor signatures of the contract info at
/// `index` start in `dlc_transactions.cets` and in the contract's adaptor
/// signature list. Both hold every contract info's entries one after
/// another, and a contract info's own indexes count from its start.
pub(crate) fn execution_offsets(&self, index: usize) -> Result<(usize, usize), Error> {
let offered = &self.offered_contract;
let cet_start = cet_ranges(&offered.contract_info, offered.total_collateral)?[index].start;
let adaptor_start = offered
.contract_info
.iter()
.zip(&self.adaptor_infos)
.take(index)
.map(|(contract_info, adaptor_info)| {
contract_info.adaptor_signature_count(adaptor_info)
})
.sum();
Ok((cet_start, adaptor_start))
}

/// Returns the contract id for the contract computed as specified here:
/// <https://github.com/discreetlogcontracts/dlcspecs/blob/master/Protocol.md#requirements-2>
pub fn get_contract_id(&self) -> [u8; 32] {
Expand Down
433 changes: 433 additions & 0 deletions ddk-manager/src/contract/cet_records.rs

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion ddk-manager/src/contract/contract_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use bitcoin::{Script, Transaction};
use ddk_dlc::{OracleInfo, Payout};
use ddk_messages::oracle_msgs;
use ddk_messages::oracle_msgs::{EventDescriptor, OracleAnnouncement, OracleAttestation};
use ddk_trie::combination_iterator::CombinationIterator;
use ddk_trie::{DlcTrie, RangeInfo};
use secp256k1_zkp::schnorr::Signature as SchnorrSignature;
use secp256k1_zkp::{All, EcdsaAdaptorSignature, PublicKey, Secp256k1, SecretKey, Verification};
Expand All @@ -22,7 +23,7 @@ pub(super) type OracleIndexAndPrefixLength = Vec<(usize, usize)>;
pub type RangeInfoAndOracleSignatures = (RangeInfo, Vec<Vec<SchnorrSignature>>);

/// Contains information about the contract conditions and oracles used.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
feature = "use-serde",
derive(serde::Serialize, serde::Deserialize),
Expand All @@ -47,6 +48,23 @@ impl ContractInfo {
}
}

/// How many adaptor signatures this contract info takes in a contract's
/// signature list, which holds every info's signatures one after another.
pub(crate) fn adaptor_signature_count(&self, adaptor_info: &AdaptorInfo) -> usize {
match adaptor_info {
AdaptorInfo::Enum => match &self.contract_descriptor {
ContractDescriptor::Enum(e) => {
e.outcome_payouts.len()
* CombinationIterator::new(self.oracle_announcements.len(), self.threshold)
.count()
}
_ => unreachable!(),
},
AdaptorInfo::Numerical(trie) => trie.iter().count(),
AdaptorInfo::NumericalWithDifference(trie) => trie.iter().count(),
}
}

/// Validate that the descriptor covers all possible outcomes that can be attested
/// by the oracle(s).
pub fn validate(&self) -> Result<(), Error> {
Expand Down
3 changes: 1 addition & 2 deletions ddk-manager/src/contract/contract_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use crate::error::Error;

use super::ContractDescriptor;
use bitcoin::Amount;
use ddk_dlc::DUST_LIMIT;
use secp256k1_zkp::XOnlyPublicKey;
#[cfg(feature = "use-serde")]
use serde::{Deserialize, Serialize};

const DUST_LIMIT: Amount = Amount::from_sat(1000);

/// Oracle information required for the initial creation of a contract.
#[derive(Debug, Clone)]
#[cfg_attr(
Expand Down
2 changes: 1 addition & 1 deletion ddk-manager/src/contract/enum_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use secp256k1_zkp::{
use serde::{Deserialize, Serialize};

/// A descriptor for a contract whose outcomes are represented as an enumeration.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
feature = "use-serde",
derive(Serialize, Deserialize),
Expand Down
4 changes: 3 additions & 1 deletion ddk-manager/src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ use std::fmt::Write;
use self::utils::unordered_equal;

pub mod accepted_contract;
pub mod cet_records;
pub mod contract_info;
pub mod contract_input;
pub mod enum_descriptor;
pub mod numerical_descriptor;
pub mod offered_contract;
pub mod ser;
pub mod signed_contract;
pub mod transactions;
pub(crate) mod utils;

/// The genesis block hash identifying `network`, in the byte order DLC
Expand Down Expand Up @@ -449,7 +451,7 @@ pub enum AdaptorInfo {
}

/// The descriptor of a contract.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
feature = "use-serde",
derive(Serialize, Deserialize),
Expand Down
4 changes: 2 additions & 2 deletions ddk-manager/src/contract/numerical_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use secp256k1_zkp::{All, EcdsaAdaptorSignature, PublicKey, Secp256k1, SecretKey}
use serde::{Deserialize, Serialize};

/// Information about the allowed deviation in outcome value between the oracles.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
feature = "use-serde",
derive(Serialize, Deserialize),
Expand All @@ -31,7 +31,7 @@ pub struct DifferenceParams {
pub maximize_coverage: bool,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
feature = "use-serde",
derive(Serialize, Deserialize),
Expand Down
20 changes: 20 additions & 0 deletions ddk-manager/src/contract/offered_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::dlc_input::get_dlc_inputs_from_funding_inputs;
use crate::error::Error;
use crate::utils::get_new_serial_id;

use super::cet_records::validate_cet_records;
use super::contract_info::ContractInfo;
use super::contract_input::ContractInput;
use super::ContractDescriptor;
Expand Down Expand Up @@ -91,6 +92,7 @@ impl OfferedContract {
));
}
}
validate_cet_records(&self.contract_info, self.total_collateral, &self.tlvs)?;

Ok(())
}
Expand Down Expand Up @@ -285,6 +287,24 @@ mod tests {
assert_eq!(LEGACY_CHAINHASH, chain_hash_from_network(Network::Regtest));
}

/// A record the contract cannot carry is rejected with the offer, before
/// an accept exists to build the CETs with.
#[test]
fn an_enum_outcome_override_on_a_numerical_contract_is_rejected() {
use ddk_messages::{OverrideOutcome, PayoutScriptOverride, PayoutScriptOverrides};
let mut offered = offered_contract(None);
assert!(offered.validate().is_ok());
offered.tlvs.set(&PayoutScriptOverrides {
overrides: vec![PayoutScriptOverride {
outcome: OverrideOutcome::Enum {
outcome: "liquidated".to_string(),
},
script_pubkey: bitcoin::ScriptBuf::from_bytes(vec![0x00, 0x14, 0xaa, 0xbb]),
}],
});
assert!(offered.validate().is_err());
}

#[test]
fn offer_enum_missing_payout() {
validate_offer_test_common(include_str!(
Expand Down
Loading
Loading