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