clementine_core/builder/transaction/
operator_assert.rs

1//! This module contains the creation of BitVM operator assertion transactions and timeout transactions related to assertions.
2
3use self::output::UnspentTxOut;
4use super::input::UtxoVout;
5use crate::builder;
6pub use crate::builder::transaction::txhandler::TxHandler;
7pub use crate::builder::transaction::*;
8use crate::config::protocol::ProtocolParamset;
9use crate::rpc::clementine::NormalSignatureKind;
10use bitcoin::Sequence;
11use clementine_errors::BridgeError;
12use clementine_primitives::TransactionType;
13
14/// Creates a [`TxHandler`] for the `disprove_timeout_tx`.
15///
16/// This transaction is sent by the operator to enable sending a `reimburse_tx` later, if operator's asserted proof did not get disproved.
17///
18/// # Inputs
19/// 1. KickoffTx: Disprove utxo
20/// 2. KickoffTx: KickoffFinalizer utxo
21///
22/// # Outputs
23/// 1. Anchor output for CPFP
24///
25/// # Arguments
26/// * `kickoff_txhandler` - The kickoff transaction handler providing the input.
27/// * `paramset` - Protocol parameter set.
28///
29/// # Returns
30/// A [`TxHandler`] for the disprove timeout transaction, or a [`BridgeError`] if construction fails.
31pub fn create_disprove_timeout_txhandler(
32    kickoff_txhandler: &TxHandler,
33    paramset: &'static ProtocolParamset,
34) -> Result<TxHandler<Unsigned>, BridgeError> {
35    Ok(TxHandlerBuilder::new(TransactionType::DisproveTimeout)
36        .with_version(NON_STANDARD_V3)
37        .add_input(
38            NormalSignatureKind::OperatorSighashDefault,
39            kickoff_txhandler.get_spendable_output(UtxoVout::Disprove)?,
40            SpendPath::ScriptSpend(0),
41            Sequence::from_height(paramset.disprove_timeout_timelock),
42        )
43        .add_input(
44            NormalSignatureKind::DisproveTimeout2,
45            kickoff_txhandler.get_spendable_output(UtxoVout::KickoffFinalizer)?,
46            SpendPath::ScriptSpend(0),
47            DEFAULT_SEQUENCE,
48        )
49        .add_output(UnspentTxOut::from_partial(anchor_output(
50            paramset.anchor_amount(),
51        )))
52        .finalize())
53}
54
55/// Creates a [`TxHandler`] for the `latest_blockhash_timeout_tx`.
56///
57/// This transaction is sent by the verifiers if the latest blockhash is not provided in time by operator.
58///
59/// # Inputs
60/// 1. KickoffTx: LatestBlockhash utxo
61/// 2. KickoffTx: KickoffFinalizer utxo
62/// 3. RoundTx: BurnConnector utxo
63///
64/// # Outputs
65/// 1. Anchor output for CPFP
66///
67/// # Arguments
68/// * `kickoff_txhandler` - The kickoff transaction handler providing the input.
69/// * `round_txhandler` - The round transaction handler providing an additional input.
70/// * `paramset` - Protocol parameter set.
71///
72/// # Returns
73/// A [`TxHandler`] for the latest blockhash timeout transaction, or a [`BridgeError`] if construction fails.
74pub fn create_latest_blockhash_timeout_txhandler(
75    kickoff_txhandler: &TxHandler,
76    round_txhandler: &TxHandler,
77    paramset: &'static ProtocolParamset,
78) -> Result<TxHandler<Unsigned>, BridgeError> {
79    Ok(
80        TxHandlerBuilder::new(TransactionType::LatestBlockhashTimeout)
81            .with_version(NON_STANDARD_V3)
82            .add_input(
83                NormalSignatureKind::LatestBlockhashTimeout1,
84                kickoff_txhandler.get_spendable_output(UtxoVout::LatestBlockhash)?,
85                SpendPath::ScriptSpend(0),
86                Sequence::from_height(paramset.latest_blockhash_timeout_timelock),
87            )
88            .add_input(
89                NormalSignatureKind::LatestBlockhashTimeout2,
90                kickoff_txhandler.get_spendable_output(UtxoVout::KickoffFinalizer)?,
91                SpendPath::ScriptSpend(0),
92                DEFAULT_SEQUENCE,
93            )
94            .add_input(
95                NormalSignatureKind::LatestBlockhashTimeout3,
96                round_txhandler.get_spendable_output(UtxoVout::CollateralInRound)?,
97                SpendPath::KeySpend,
98                DEFAULT_SEQUENCE,
99            )
100            .add_output(UnspentTxOut::from_partial(anchor_output(
101                paramset.anchor_amount(),
102            )))
103            .finalize(),
104    )
105}
106
107/// Creates a vector of [`TxHandler`] for `mini_assert` transactions.
108///
109/// These transactions are used to commit BitVM assertions of operator's proof that it paid the payout corresponding to the deposit.
110///
111/// # Inputs
112/// 1. KickoffTx: Assert utxo (per mini assert)
113///
114/// # Outputs
115/// 1. Anchor output for CPFP
116/// 2. Dummy OP_RETURN output (to pad the size of the transaction, as it is too small otherwise)
117///
118/// # Arguments
119/// * `kickoff_txhandler` - The kickoff transaction handler providing the input.
120/// * `num_asserts` - Number of mini assert transactions to create.
121///
122/// # Returns
123/// A vector of [`TxHandler`] for mini assert transactions, or a [`BridgeError`] if construction fails.
124pub fn create_mini_asserts(
125    kickoff_txhandler: &TxHandler,
126    num_asserts: usize,
127    paramset: &'static ProtocolParamset,
128) -> Result<Vec<TxHandler>, BridgeError> {
129    let mut txhandlers = Vec::new();
130    for idx in 0..num_asserts {
131        txhandlers.push(
132            TxHandlerBuilder::new(TransactionType::MiniAssert(idx))
133                .with_version(NON_STANDARD_V3)
134                .add_input(
135                    NormalSignatureKind::MiniAssert1,
136                    kickoff_txhandler.get_spendable_output(UtxoVout::Assert(idx))?,
137                    SpendPath::ScriptSpend(1),
138                    DEFAULT_SEQUENCE,
139                )
140                .add_output(UnspentTxOut::from_partial(
141                    builder::transaction::anchor_output(paramset.anchor_amount()),
142                ))
143                .add_output(UnspentTxOut::from_partial(op_return_txout(b"")))
144                .finalize(),
145        );
146    }
147    Ok(txhandlers)
148}
149
150/// Creates a [`TxHandler`] for the `latest_blockhash_tx`.
151///
152/// This transaction is used by operator to commit the latest blockhash of the bitcoin chain. This latest blockhash will be used later
153/// in the operator's bridge proof. Mainly used to reduce the time operator can spend building a private fork.
154///
155/// # Inputs
156/// 1. KickoffTx: LatestBlockhash utxo
157///
158/// # Outputs
159/// 1. Anchor output for CPFP
160/// 2. Dummy OP_RETURN output (to pad the size of the transaction, as it is too small otherwise)
161///
162/// # Arguments
163/// * `kickoff_txhandler` - The kickoff transaction handler providing the input.
164///
165/// # Returns
166/// A [`TxHandler`] for the latest blockhash transaction, or a [`BridgeError`] if construction fails.
167pub fn create_latest_blockhash_txhandler(
168    kickoff_txhandler: &TxHandler,
169    paramset: &'static ProtocolParamset,
170) -> Result<TxHandler<Unsigned>, BridgeError> {
171    Ok(TxHandlerBuilder::new(TransactionType::LatestBlockhash)
172        .with_version(NON_STANDARD_V3)
173        .add_input(
174            NormalSignatureKind::LatestBlockhash,
175            kickoff_txhandler.get_spendable_output(UtxoVout::LatestBlockhash)?,
176            SpendPath::ScriptSpend(1),
177            DEFAULT_SEQUENCE,
178        )
179        .add_output(UnspentTxOut::from_partial(anchor_output(
180            paramset.anchor_amount(),
181        )))
182        .add_output(UnspentTxOut::from_partial(op_return_txout(b"")))
183        .finalize())
184}