• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stacks-network / stacks-core / 24140301216

08 Apr 2026 02:18PM UTC coverage: 46.817% (-38.9%) from 85.712%
24140301216

Pull #6959

github

279acf
web-flow
Merge efbee1783 into 882e27245
Pull Request #6959: Perf/cache epoch version in ClarityDatabase

66 of 149 new or added lines in 8 files covered. (44.3%)

85999 existing lines in 334 files now uncovered.

102056 of 217989 relevant lines covered (46.82%)

12315981.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.57
/stacks-node/src/burnchains/bitcoin_regtest_controller.rs
1
// Copyright (C) 2013-2020 Blockstack PBC, a public benefit corporation
2
// Copyright (C) 2020-2024 Stacks Open Internet Foundation
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

17
use std::cmp;
18
use std::collections::HashSet;
19
use std::sync::atomic::{AtomicBool, Ordering};
20
use std::sync::Arc;
21
use std::time::Instant;
22

23
use stacks::burnchains::bitcoin::address::{
24
    BitcoinAddress, LegacyBitcoinAddress, LegacyBitcoinAddressType, SegwitBitcoinAddress,
25
};
26
use stacks::burnchains::bitcoin::indexer::{
27
    BitcoinIndexer, BitcoinIndexerConfig, BitcoinIndexerRuntime,
28
};
29
use stacks::burnchains::bitcoin::spv::SpvClient;
30
use stacks::burnchains::bitcoin::{BitcoinNetworkType, Error as btc_error};
31
use stacks::burnchains::db::BurnchainDB;
32
use stacks::burnchains::indexer::BurnchainIndexer;
33
use stacks::burnchains::{
34
    Burnchain, BurnchainParameters, BurnchainStateTransitionOps, Error as burnchain_error,
35
    PoxConstants, PublicKey, Txid,
36
};
37
use stacks::chainstate::burn::db::sortdb::SortitionDB;
38
use stacks::chainstate::burn::operations::{
39
    BlockstackOperationType, DelegateStxOp, LeaderBlockCommitOp, LeaderKeyRegisterOp, PreStxOp,
40
    StackStxOp, TransferStxOp, VoteForAggregateKeyOp,
41
};
42
#[cfg(test)]
43
use stacks::chainstate::burn::Opcodes;
44
use stacks::chainstate::coordinator::comm::CoordinatorChannels;
45
#[cfg(test)]
46
use stacks::chainstate::stacks::address::PoxAddress;
47
use stacks::config::BurnchainConfig;
48
#[cfg(test)]
49
use stacks::config::{
50
    OP_TX_ANY_ESTIM_SIZE, OP_TX_DELEGATE_STACKS_ESTIM_SIZE, OP_TX_PRE_STACKS_ESTIM_SIZE,
51
    OP_TX_STACK_STX_ESTIM_SIZE, OP_TX_TRANSFER_STACKS_ESTIM_SIZE, OP_TX_VOTE_AGG_ESTIM_SIZE,
52
};
53
use stacks::core::{EpochList, StacksEpochId};
54
use stacks::monitoring::{increment_btc_blocks_received_counter, increment_btc_ops_sent_counter};
55
use stacks_common::codec::StacksMessageCodec;
56
use stacks_common::deps_common::bitcoin::blockdata::opcodes;
57
use stacks_common::deps_common::bitcoin::blockdata::script::{Builder, Script};
58
use stacks_common::deps_common::bitcoin::blockdata::transaction::{
59
    OutPoint, Transaction, TxIn, TxOut,
60
};
61
use stacks_common::deps_common::bitcoin::network::serialize::{serialize, serialize_hex};
62
use stacks_common::deps_common::bitcoin::util::hash::Sha256dHash;
63
use stacks_common::types::chainstate::BurnchainHeaderHash;
64
use stacks_common::util::hash::{hex_bytes, Hash160};
65
use stacks_common::util::secp256k1::Secp256k1PublicKey;
66
use stacks_common::util::sleep_ms;
67

68
use super::super::operations::BurnchainOpSigner;
69
use super::super::Config;
70
use super::{BurnchainController, BurnchainTip, Error as BurnchainControllerError};
71
use crate::burnchains::rpc::bitcoin_rpc_client::{
72
    BitcoinRpcClient, BitcoinRpcClientError, BitcoinRpcClientResult, ImportDescriptorsRequest,
73
    Timestamp,
74
};
75

76
/// The number of bitcoin blocks that can have
77
///  passed since the UTXO cache was last refreshed before
78
///  the cache is force-reset.
79
const UTXO_CACHE_STALENESS_LIMIT: u64 = 6;
80
const DUST_UTXO_LIMIT: u64 = 5500;
81

82
#[cfg(test)]
83
// Used to inject invalid block commits during testing.
84
pub static TEST_MAGIC_BYTES: std::sync::Mutex<Option<[u8; 2]>> = std::sync::Mutex::new(None);
85

86
pub struct BitcoinRegtestController {
87
    config: Config,
88
    indexer: BitcoinIndexer,
89
    db: Option<SortitionDB>,
90
    burnchain_db: Option<BurnchainDB>,
91
    chain_tip: Option<BurnchainTip>,
92
    use_coordinator: Option<CoordinatorChannels>,
93
    burnchain_config: Option<Burnchain>,
94
    ongoing_block_commit: Option<OngoingBlockCommit>,
95
    should_keep_running: Option<Arc<AtomicBool>>,
96
    /// Optional Bitcoin RPC client used to interact with a `bitcoind` node.
97
    /// - For **miner** node this field must be always `Some`.
98
    /// - For **other** node (e.g. follower node), this field is `None`.
99
    rpc_client: Option<BitcoinRpcClient>,
100
}
101

102
#[derive(Clone)]
103
pub struct OngoingBlockCommit {
104
    pub payload: LeaderBlockCommitOp,
105
    utxos: UTXOSet,
106
    fees: LeaderBlockCommitFees,
107
    txids: Vec<Txid>,
108
}
109

110
#[derive(Clone)]
111
struct LeaderBlockCommitFees {
112
    sunset_fee: u64,
113
    fee_rate: u64,
114
    sortition_fee: u64,
115
    outputs_len: u64,
116
    default_tx_size: u64,
117
    spent_in_attempts: u64,
118
    is_rbf_enabled: bool,
119
    final_size: u64,
120
}
121

122
// TODO: add tests from mutation testing results #4862
123
#[cfg_attr(test, mutants::skip)]
124
pub fn burnchain_params_from_config(config: &BurnchainConfig) -> BurnchainParameters {
167,790✔
125
    let (network, _) = config.get_bitcoin_network();
167,790✔
126
    let mut params = BurnchainParameters::from_params(&config.chain, &network)
167,790✔
127
        .expect("Bitcoin network unsupported");
167,790✔
128
    if let Some(first_burn_block_height) = config.first_burn_block_height {
167,790✔
129
        params.first_block_height = first_burn_block_height;
×
130
    }
167,790✔
131
    params
167,790✔
132
}
167,790✔
133

134
// TODO: add tests from mutation testing results #4863
135
#[cfg_attr(test, mutants::skip)]
136
/// Helper method to create a BitcoinIndexer
137
pub fn make_bitcoin_indexer(
1,116✔
138
    config: &Config,
1,116✔
139
    should_keep_running: Option<Arc<AtomicBool>>,
1,116✔
140
) -> BitcoinIndexer {
1,116✔
141
    let burnchain_params = burnchain_params_from_config(&config.burnchain);
1,116✔
142
    let indexer_config = {
1,116✔
143
        let burnchain_config = config.burnchain.clone();
1,116✔
144
        BitcoinIndexerConfig {
1,116✔
145
            peer_host: burnchain_config.peer_host,
1,116✔
146
            peer_port: burnchain_config.peer_port,
1,116✔
147
            rpc_port: burnchain_config.rpc_port,
1,116✔
148
            rpc_ssl: burnchain_config.rpc_ssl,
1,116✔
149
            username: burnchain_config.username,
1,116✔
150
            password: burnchain_config.password,
1,116✔
151
            timeout: burnchain_config.timeout,
1,116✔
152
            socket_timeout: burnchain_config.socket_timeout,
1,116✔
153
            spv_headers_path: config.get_spv_headers_file_path(),
1,116✔
154
            first_block: burnchain_params.first_block_height,
1,116✔
155
            magic_bytes: burnchain_config.magic_bytes,
1,116✔
156
            epochs: burnchain_config.epochs,
1,116✔
157
        }
1,116✔
158
    };
159

160
    let (_, network_type) = config.burnchain.get_bitcoin_network();
1,116✔
161
    let indexer_runtime = BitcoinIndexerRuntime::new(network_type, indexer_config.timeout);
1,116✔
162
    BitcoinIndexer {
1,116✔
163
        config: indexer_config,
1,116✔
164
        runtime: indexer_runtime,
1,116✔
165
        should_keep_running,
1,116✔
166
    }
1,116✔
167
}
1,116✔
168

169
pub fn get_satoshis_per_byte(config: &Config) -> u64 {
24,735✔
170
    config.get_burnchain_config().satoshis_per_byte
24,735✔
171
}
24,735✔
172

173
pub fn get_rbf_fee_increment(config: &Config) -> u64 {
322✔
174
    config.get_burnchain_config().rbf_fee_increment
322✔
175
}
322✔
176

177
pub fn get_max_rbf(config: &Config) -> u64 {
14,923✔
178
    config.get_burnchain_config().max_rbf
14,923✔
179
}
14,923✔
180

181
impl LeaderBlockCommitFees {
182
    pub fn fees_from_previous_tx(
322✔
183
        &self,
322✔
184
        payload: &LeaderBlockCommitOp,
322✔
185
        config: &Config,
322✔
186
    ) -> LeaderBlockCommitFees {
322✔
187
        let mut fees = LeaderBlockCommitFees::estimated_fees_from_payload(payload, config);
322✔
188
        fees.spent_in_attempts = cmp::max(1, self.spent_in_attempts);
322✔
189
        fees.final_size = self.final_size;
322✔
190
        fees.fee_rate = self.fee_rate + get_rbf_fee_increment(config);
322✔
191
        fees.is_rbf_enabled = true;
322✔
192
        fees
322✔
193
    }
322✔
194

195
    pub fn estimated_fees_from_payload(
9,225✔
196
        payload: &LeaderBlockCommitOp,
9,225✔
197
        config: &Config,
9,225✔
198
    ) -> LeaderBlockCommitFees {
9,225✔
199
        let sunset_fee = if payload.sunset_burn > 0 {
9,225✔
200
            cmp::max(payload.sunset_burn, DUST_UTXO_LIMIT)
21✔
201
        } else {
202
            0
9,204✔
203
        };
204

205
        let number_of_transfers = payload.commit_outs.len() as u64;
9,225✔
206
        let value_per_transfer = payload.burn_fee / number_of_transfers;
9,225✔
207
        let sortition_fee = value_per_transfer * number_of_transfers;
9,225✔
208
        let spent_in_attempts = 0;
9,225✔
209
        let fee_rate = get_satoshis_per_byte(config);
9,225✔
210
        let default_tx_size = config.burnchain.block_commit_tx_estimated_size;
9,225✔
211

212
        LeaderBlockCommitFees {
9,225✔
213
            sunset_fee,
9,225✔
214
            fee_rate,
9,225✔
215
            sortition_fee,
9,225✔
216
            outputs_len: number_of_transfers,
9,225✔
217
            default_tx_size,
9,225✔
218
            spent_in_attempts,
9,225✔
219
            is_rbf_enabled: false,
9,225✔
220
            final_size: 0,
9,225✔
221
        }
9,225✔
222
    }
9,225✔
223

224
    pub fn estimated_miner_fee(&self) -> u64 {
9,225✔
225
        self.fee_rate * self.default_tx_size
9,225✔
226
    }
9,225✔
227

228
    pub fn rbf_fee(&self) -> u64 {
9,225✔
229
        if self.is_rbf_enabled {
9,225✔
230
            self.spent_in_attempts + self.default_tx_size
322✔
231
        } else {
232
            0
8,903✔
233
        }
234
    }
9,225✔
235

236
    pub fn estimated_amount_required(&self) -> u64 {
9,225✔
237
        self.estimated_miner_fee() + self.rbf_fee() + self.sunset_fee + self.sortition_fee
9,225✔
238
    }
9,225✔
239

240
    pub fn total_spent(&self) -> u64 {
8,273✔
241
        self.fee_rate * self.final_size
8,273✔
242
            + self.spent_in_attempts
8,273✔
243
            + self.sunset_fee
8,273✔
244
            + self.sortition_fee
8,273✔
245
    }
8,273✔
246

247
    pub fn amount_per_output(&self) -> u64 {
14,614✔
248
        self.sortition_fee / self.outputs_len
14,614✔
249
    }
14,614✔
250

251
    pub fn total_spent_in_outputs(&self) -> u64 {
8,273✔
252
        self.sunset_fee + self.sortition_fee
8,273✔
253
    }
8,273✔
254

255
    pub fn min_tx_size(&self) -> u64 {
8,273✔
256
        cmp::max(self.final_size, self.default_tx_size)
8,273✔
257
    }
8,273✔
258

259
    pub fn register_replacement(&mut self, tx_size: u64) {
8,273✔
260
        let new_size = cmp::max(tx_size, self.final_size);
8,273✔
261
        if self.is_rbf_enabled {
8,273✔
262
            self.spent_in_attempts += new_size;
322✔
263
        }
7,951✔
264
        self.final_size = new_size;
8,273✔
265
    }
8,273✔
266
}
267

268
/// Extension methods for working with [`BitcoinRpcClient`] result
269
/// that log failures and panic.
270
#[cfg(test)]
271
trait BitcoinRpcClientResultExt<T> {
272
    /// Unwraps the result, returning the value if `Ok`.
273
    ///
274
    /// If the result is an `Err`, it logs the error with the given context
275
    /// using the [`error!`] macro and then panics.
276
    fn unwrap_or_log_panic(self, context: &str) -> T;
277
    /// Ensure the result is `Ok`, ignoring its value.
278
    ///
279
    /// If the result is an `Err`, it logs the error with the given context
280
    /// using the [`error!`] macro and then panics.
281
    fn ok_or_log_panic(self, context: &str);
282
}
283

284
#[cfg(test)]
285
impl<T> BitcoinRpcClientResultExt<T> for Result<T, BitcoinRpcClientError> {
286
    fn unwrap_or_log_panic(self, context: &str) -> T {
7,805✔
287
        match self {
7,805✔
288
            Ok(val) => val,
7,805✔
289
            Err(e) => {
×
290
                error!("Bitcoin RPC failure: {context} {e:?}");
×
291
                panic!();
×
292
            }
293
        }
294
    }
7,805✔
295

296
    fn ok_or_log_panic(self, context: &str) {
7,726✔
297
        _ = self.unwrap_or_log_panic(context);
7,726✔
298
    }
7,726✔
299
}
300

301
/// Represents errors that can occur when using [`BitcoinRegtestController`].
302
#[derive(Debug, thiserror::Error)]
303
pub enum BitcoinRegtestControllerError {
304
    /// Error related to Bitcoin RPC failures.
305
    #[error("Bitcoin RPC error: {0}")]
306
    Rpc(#[from] BitcoinRpcClientError),
307
    /// Error related to invalid or malformed [`Secp256k1PublicKey`].
308
    #[error("Invalid public key: {0}")]
309
    InvalidPublicKey(btc_error),
310
}
311

312
/// Alias for results returned from [`BitcoinRegtestController`] operations.
313
pub type BitcoinRegtestControllerResult<T> = Result<T, BitcoinRegtestControllerError>;
314

315
impl BitcoinRegtestController {
316
    pub fn new(config: Config, coordinator_channel: Option<CoordinatorChannels>) -> Self {
260✔
317
        BitcoinRegtestController::with_burnchain(config, coordinator_channel, None, None)
260✔
318
    }
260✔
319

320
    // TODO: add tests from mutation testing results #4864
321
    #[cfg_attr(test, mutants::skip)]
322
    pub fn with_burnchain(
788✔
323
        config: Config,
788✔
324
        coordinator_channel: Option<CoordinatorChannels>,
788✔
325
        burnchain: Option<Burnchain>,
788✔
326
        should_keep_running: Option<Arc<AtomicBool>>,
788✔
327
    ) -> Self {
788✔
328
        std::fs::create_dir_all(config.get_burnchain_path_str()).expect("Unable to create workdir");
788✔
329
        let (_, network_id) = config.burnchain.get_bitcoin_network();
788✔
330

331
        let res = SpvClient::new(
788✔
332
            &config.get_spv_headers_file_path(),
788✔
333
            0,
334
            None,
788✔
335
            network_id,
788✔
336
            true,
337
            false,
338
        );
339
        if let Err(err) = res {
788✔
340
            error!("Unable to init block headers: {err}");
×
341
            panic!()
×
342
        }
788✔
343

344
        let burnchain_params = burnchain_params_from_config(&config.burnchain);
788✔
345

346
        if network_id == BitcoinNetworkType::Mainnet && config.burnchain.epochs.is_some() {
788✔
347
            panic!("It is an error to set custom epochs while running on Mainnet: network_id {network_id:?} config.burnchain {:#?}",
×
348
                   &config.burnchain);
×
349
        }
788✔
350

351
        let indexer_config = {
788✔
352
            let burnchain_config = config.burnchain.clone();
788✔
353
            BitcoinIndexerConfig {
788✔
354
                peer_host: burnchain_config.peer_host,
788✔
355
                peer_port: burnchain_config.peer_port,
788✔
356
                rpc_port: burnchain_config.rpc_port,
788✔
357
                rpc_ssl: burnchain_config.rpc_ssl,
788✔
358
                username: burnchain_config.username,
788✔
359
                password: burnchain_config.password,
788✔
360
                timeout: burnchain_config.timeout,
788✔
361
                socket_timeout: burnchain_config.socket_timeout,
788✔
362
                spv_headers_path: config.get_spv_headers_file_path(),
788✔
363
                first_block: burnchain_params.first_block_height,
788✔
364
                magic_bytes: burnchain_config.magic_bytes,
788✔
365
                epochs: burnchain_config.epochs,
788✔
366
            }
788✔
367
        };
368

369
        let (_, network_type) = config.burnchain.get_bitcoin_network();
788✔
370
        let indexer_runtime = BitcoinIndexerRuntime::new(network_type, config.burnchain.timeout);
788✔
371
        let burnchain_indexer = BitcoinIndexer {
788✔
372
            config: indexer_config,
788✔
373
            runtime: indexer_runtime,
788✔
374
            should_keep_running: should_keep_running.clone(),
788✔
375
        };
788✔
376

377
        let rpc_client = Self::create_rpc_client_unchecked(&config);
788✔
378

379
        Self {
788✔
380
            use_coordinator: coordinator_channel,
788✔
381
            config,
788✔
382
            indexer: burnchain_indexer,
788✔
383
            db: None,
788✔
384
            burnchain_db: None,
788✔
385
            chain_tip: None,
788✔
386
            burnchain_config: burnchain,
788✔
387
            ongoing_block_commit: None,
788✔
388
            should_keep_running,
788✔
389
            rpc_client,
788✔
390
        }
788✔
391
    }
788✔
392

393
    // TODO: add tests from mutation testing results #4864
394
    #[cfg_attr(test, mutants::skip)]
395
    /// create a dummy bitcoin regtest controller.
396
    ///   used just for submitting bitcoin ops.
397
    pub fn new_dummy(config: Config) -> Self {
165,470✔
398
        let burnchain_params = burnchain_params_from_config(&config.burnchain);
165,470✔
399

400
        let indexer_config = {
165,470✔
401
            let burnchain_config = config.burnchain.clone();
165,470✔
402
            BitcoinIndexerConfig {
165,470✔
403
                peer_host: burnchain_config.peer_host,
165,470✔
404
                peer_port: burnchain_config.peer_port,
165,470✔
405
                rpc_port: burnchain_config.rpc_port,
165,470✔
406
                rpc_ssl: burnchain_config.rpc_ssl,
165,470✔
407
                username: burnchain_config.username,
165,470✔
408
                password: burnchain_config.password,
165,470✔
409
                timeout: burnchain_config.timeout,
165,470✔
410
                socket_timeout: burnchain_config.socket_timeout,
165,470✔
411
                spv_headers_path: config.get_spv_headers_file_path(),
165,470✔
412
                first_block: burnchain_params.first_block_height,
165,470✔
413
                magic_bytes: burnchain_config.magic_bytes,
165,470✔
414
                epochs: burnchain_config.epochs,
165,470✔
415
            }
165,470✔
416
        };
417

418
        let (_, network_type) = config.burnchain.get_bitcoin_network();
165,470✔
419
        let indexer_runtime = BitcoinIndexerRuntime::new(network_type, config.burnchain.timeout);
165,470✔
420
        let burnchain_indexer = BitcoinIndexer {
165,470✔
421
            config: indexer_config,
165,470✔
422
            runtime: indexer_runtime,
165,470✔
423
            should_keep_running: None,
165,470✔
424
        };
165,470✔
425

426
        let rpc_client = Self::create_rpc_client_unchecked(&config);
165,470✔
427

428
        Self {
165,470✔
429
            use_coordinator: None,
165,470✔
430
            config,
165,470✔
431
            indexer: burnchain_indexer,
165,470✔
432
            db: None,
165,470✔
433
            burnchain_db: None,
165,470✔
434
            chain_tip: None,
165,470✔
435
            burnchain_config: None,
165,470✔
436
            ongoing_block_commit: None,
165,470✔
437
            should_keep_running: None,
165,470✔
438
            rpc_client,
165,470✔
439
        }
165,470✔
440
    }
165,470✔
441

442
    /// Creates a dummy bitcoin regtest controller, with the given ongoing block-commits
443
    pub fn new_ongoing_dummy(config: Config, ongoing: Option<OngoingBlockCommit>) -> Self {
164,954✔
444
        let mut ret = Self::new_dummy(config);
164,954✔
445
        ret.ongoing_block_commit = ongoing;
164,954✔
446
        ret
164,954✔
447
    }
164,954✔
448

449
    /// Get an owned copy of the ongoing block commit state
450
    pub fn get_ongoing_commit(&self) -> Option<OngoingBlockCommit> {
171,672✔
451
        self.ongoing_block_commit.clone()
171,672✔
452
    }
171,672✔
453

454
    /// Set the ongoing block commit state
455
    pub fn set_ongoing_commit(&mut self, ongoing: Option<OngoingBlockCommit>) {
6,712✔
456
        self.ongoing_block_commit = ongoing;
6,712✔
457
    }
6,712✔
458

459
    /// Get the default Burnchain instance from our config
460
    fn default_burnchain(&self) -> Burnchain {
800,116✔
461
        match &self.burnchain_config {
800,116✔
462
            Some(burnchain) => burnchain.clone(),
×
463
            None => self.config.get_burnchain(),
800,116✔
464
        }
465
    }
800,116✔
466

467
    /// Get the PoX constants in use
468
    pub fn get_pox_constants(&self) -> PoxConstants {
×
469
        let burnchain = self.get_burnchain();
×
470
        burnchain.pox_constants
×
471
    }
×
472

473
    /// Get the Burnchain in use
474
    pub fn get_burnchain(&self) -> Burnchain {
853,884✔
475
        match self.burnchain_config {
853,884✔
476
            Some(ref burnchain) => burnchain.clone(),
53,768✔
477
            None => self.default_burnchain(),
800,116✔
478
        }
479
    }
853,884✔
480

481
    /// Attempt to create a new [`BitcoinRpcClient`] from the given [`Config`].
482
    ///
483
    /// If the provided config indicates that the node is a **miner**,
484
    /// tries to instantiate it or **panics** otherwise.
485
    /// If the node is **not** a miner, returns None (e.g. follower node).
486
    fn create_rpc_client_unchecked(config: &Config) -> Option<BitcoinRpcClient> {
166,258✔
487
        config.node.miner.then(|| {
166,258✔
488
            BitcoinRpcClient::from_stx_config(&config)
166,239✔
489
                .expect("unable to instantiate the RPC client for miner node!")
166,239✔
490
        })
166,239✔
491
    }
166,258✔
492

493
    /// Attempt to get a reference to the underlying [`BitcoinRpcClient`].
494
    ///
495
    /// This function will panic if the RPC client has not been configured
496
    /// (i.e. [`Self::create_rpc_client_unchecked`] returned `None` during initialization),
497
    /// but an attempt is made to use it anyway.
498
    ///
499
    /// In practice, this means the node is expected to act as a miner,
500
    /// yet no [`BitcoinRpcClient`] was created or properly configured.
501
    fn get_rpc_client(&self) -> &BitcoinRpcClient {
60,454✔
502
        self.rpc_client
60,454✔
503
            .as_ref()
60,454✔
504
            .expect("BUG: BitcoinRpcClient is required, but it has not been configured properly!")
60,454✔
505
    }
60,454✔
506

507
    /// Helium (devnet) blocks receiver.  Returns the new burnchain tip.
508
    fn receive_blocks_helium(&mut self) -> BurnchainTip {
×
509
        let mut burnchain = self.get_burnchain();
×
510
        let (block_snapshot, state_transition) = loop {
×
511
            match burnchain.sync_with_indexer_deprecated(&mut self.indexer) {
×
512
                Ok(x) => {
×
513
                    break x;
×
514
                }
515
                Err(e) => {
×
516
                    // keep trying
517
                    error!("Unable to sync with burnchain: {e}");
×
518
                    match e {
×
519
                        burnchain_error::TrySyncAgain => {
520
                            // try again immediately
521
                            continue;
×
522
                        }
523
                        burnchain_error::BurnchainPeerBroken => {
524
                            // remote burnchain peer broke, and produced a shorter blockchain fork.
525
                            // just keep trying
526
                            sleep_ms(5000);
×
527
                            continue;
×
528
                        }
529
                        _ => {
530
                            // delay and try again
531
                            sleep_ms(5000);
×
532
                            continue;
×
533
                        }
534
                    }
535
                }
536
            }
537
        };
538

539
        let rest = match (state_transition, &self.chain_tip) {
×
540
            (None, Some(chain_tip)) => chain_tip.clone(),
×
541
            (Some(state_transition), _) => {
×
542
                let burnchain_tip = BurnchainTip {
×
543
                    block_snapshot,
×
544
                    state_transition: BurnchainStateTransitionOps::from(state_transition),
×
545
                    received_at: Instant::now(),
×
546
                };
×
547
                self.chain_tip = Some(burnchain_tip.clone());
×
548
                burnchain_tip
×
549
            }
550
            (None, None) => {
551
                // can happen at genesis
552
                let burnchain_tip = BurnchainTip {
×
553
                    block_snapshot,
×
554
                    state_transition: BurnchainStateTransitionOps::noop(),
×
555
                    received_at: Instant::now(),
×
556
                };
×
557
                self.chain_tip = Some(burnchain_tip.clone());
×
558
                burnchain_tip
×
559
            }
560
        };
561

562
        debug!("Done receiving blocks");
×
563
        rest
×
564
    }
×
565

566
    fn receive_blocks(
375,256✔
567
        &mut self,
375,256✔
568
        block_for_sortitions: bool,
375,256✔
569
        target_block_height_opt: Option<u64>,
375,256✔
570
    ) -> Result<(BurnchainTip, u64), BurnchainControllerError> {
375,256✔
571
        let coordinator_comms = match self.use_coordinator.as_ref() {
375,256✔
572
            Some(x) => x.clone(),
375,256✔
573
            None => {
574
                // pre-PoX helium node
575
                let tip = self.receive_blocks_helium();
×
576
                let height = tip.block_snapshot.block_height;
×
577
                return Ok((tip, height));
×
578
            }
579
        };
580

581
        let mut burnchain = self.get_burnchain();
375,256✔
582
        let (block_snapshot, burnchain_height, state_transition) = loop {
375,203✔
583
            if !self.should_keep_running() {
375,318✔
584
                return Err(BurnchainControllerError::CoordinatorClosed);
48✔
585
            }
375,270✔
586

587
            match burnchain.sync_with_indexer(
375,270✔
588
                &mut self.indexer,
375,270✔
589
                coordinator_comms.clone(),
375,270✔
590
                target_block_height_opt,
375,270✔
591
                Some(burnchain.pox_constants.reward_cycle_length as u64),
375,270✔
592
                self.should_keep_running.clone(),
375,270✔
593
            ) {
375,270✔
594
                Ok(x) => {
375,204✔
595
                    increment_btc_blocks_received_counter();
375,204✔
596

597
                    // initialize the dbs...
598
                    self.sortdb_mut();
375,204✔
599

600
                    // wait for the chains coordinator to catch up with us.
601
                    // don't wait for heights beyond the burnchain tip.
602
                    if block_for_sortitions {
375,204✔
603
                        self.wait_for_sortitions(
374,654✔
604
                            coordinator_comms,
374,654✔
605
                            target_block_height_opt.unwrap_or(x.block_height),
374,654✔
606
                        )?;
1✔
607
                    }
550✔
608

609
                    // NOTE: This is the latest _sortition_ on the canonical sortition history, not the latest burnchain block!
610
                    let sort_tip =
375,203✔
611
                        SortitionDB::get_canonical_burn_chain_tip(self.sortdb_ref().conn())
375,203✔
612
                            .expect("Sortition DB error.");
375,203✔
613

614
                    let (snapshot, state_transition) = self
375,203✔
615
                        .sortdb_ref()
375,203✔
616
                        .get_sortition_result(&sort_tip.sortition_id)
375,203✔
617
                        .expect("Sortition DB error.")
375,203✔
618
                        .expect("BUG: no data for the canonical chain tip");
375,203✔
619

620
                    let burnchain_height = self
375,203✔
621
                        .indexer
375,203✔
622
                        .get_highest_header_height()
375,203✔
623
                        .map_err(BurnchainControllerError::IndexerError)?;
375,203✔
624
                    break (snapshot, burnchain_height, state_transition);
375,203✔
625
                }
626
                Err(e) => {
66✔
627
                    // keep trying
628
                    error!("Unable to sync with burnchain: {e}");
66✔
629
                    match e {
66✔
630
                        burnchain_error::CoordinatorClosed => {
631
                            return Err(BurnchainControllerError::CoordinatorClosed)
4✔
632
                        }
633
                        burnchain_error::TrySyncAgain => {
634
                            // try again immediately
635
                            continue;
49✔
636
                        }
637
                        burnchain_error::BurnchainPeerBroken => {
638
                            // remote burnchain peer broke, and produced a shorter blockchain fork.
639
                            // just keep trying
640
                            sleep_ms(5000);
3✔
641
                            continue;
3✔
642
                        }
643
                        _ => {
644
                            // delay and try again
645
                            sleep_ms(5000);
17✔
646
                            continue;
17✔
647
                        }
648
                    }
649
                }
650
            }
651
        };
652

653
        let burnchain_tip = BurnchainTip {
375,203✔
654
            block_snapshot,
375,203✔
655
            state_transition,
375,203✔
656
            received_at: Instant::now(),
375,203✔
657
        };
375,203✔
658

659
        let received = self
375,203✔
660
            .chain_tip
375,203✔
661
            .as_ref()
375,203✔
662
            .map(|tip| tip.block_snapshot.block_height)
375,203✔
663
            .unwrap_or(0)
375,203✔
664
            == burnchain_tip.block_snapshot.block_height;
375,203✔
665
        self.chain_tip = Some(burnchain_tip.clone());
375,203✔
666
        debug!("Done receiving blocks");
375,203✔
667

668
        if self.config.burnchain.fault_injection_burnchain_block_delay > 0 && received {
375,203✔
669
            info!(
×
670
                "Fault injection: delaying burnchain blocks by {} milliseconds",
671
                self.config.burnchain.fault_injection_burnchain_block_delay
672
            );
673
            sleep_ms(self.config.burnchain.fault_injection_burnchain_block_delay);
×
674
        }
375,203✔
675

676
        Ok((burnchain_tip, burnchain_height))
375,203✔
677
    }
375,256✔
678

679
    fn should_keep_running(&self) -> bool {
382,345✔
680
        match self.should_keep_running {
382,345✔
681
            Some(ref should_keep_running) => should_keep_running.load(Ordering::SeqCst),
382,345✔
682
            _ => true,
×
683
        }
684
    }
382,345✔
685

686
    /// Retrieves all UTXOs associated with the given public key.
687
    ///
688
    /// The address to query is computed from the public key,
689
    /// disregard the epoch we're in and currently set to [`StacksEpochId::Epoch21`].
690
    ///
691
    /// Automatically imports descriptors into the wallet for the public_key
692
    #[cfg(test)]
693
    pub fn get_all_utxos(&self, public_key: &Secp256k1PublicKey) -> Vec<UTXO> {
32✔
694
        const EPOCH: StacksEpochId = StacksEpochId::Epoch21;
695
        let address = self.get_miner_address(EPOCH, public_key);
32✔
696
        let pub_key_rev = self.to_epoch_aware_pubkey(EPOCH, public_key);
32✔
697

698
        test_debug!("Import public key '{}'", &pub_key_rev.to_hex());
32✔
699
        self.import_public_key(&pub_key_rev)
32✔
700
            .unwrap_or_else(|error| {
32✔
701
                panic!(
×
702
                    "Import public key '{}' failed: {error:?}",
703
                    pub_key_rev.to_hex()
×
704
                )
705
            });
706

707
        sleep_ms(1000);
32✔
708

709
        self.retrieve_utxo_set(&address, true, 1, &None, 0)
32✔
710
            .unwrap_or_log_panic("retrieve all utxos")
32✔
711
            .utxos
32✔
712
    }
32✔
713

714
    /// Retrieve all loaded wallets.
715
    pub fn list_wallets(&self) -> BitcoinRegtestControllerResult<Vec<String>> {
766✔
716
        Ok(self.get_rpc_client().list_wallets()?)
766✔
717
    }
766✔
718

719
    /// Checks if the config-supplied wallet exists.
720
    /// If it does not exist, this function creates it.
721
    pub fn create_wallet_if_dne(&self) -> BitcoinRegtestControllerResult<()> {
763✔
722
        let wallets = self.list_wallets()?;
763✔
723
        let wallet = self.get_wallet_name();
763✔
724
        if !wallets.contains(wallet) {
763✔
725
            self.get_rpc_client().create_wallet(wallet, Some(true))?
261✔
726
        }
502✔
727
        Ok(())
763✔
728
    }
763✔
729

730
    pub fn get_utxos(
8,759✔
731
        &self,
8,759✔
732
        epoch_id: StacksEpochId,
8,759✔
733
        public_key: &Secp256k1PublicKey,
8,759✔
734
        total_required: u64,
8,759✔
735
        utxos_to_exclude: Option<UTXOSet>,
8,759✔
736
        block_height: u64,
8,759✔
737
    ) -> Option<UTXOSet> {
8,759✔
738
        let pub_key_rev = self.to_epoch_aware_pubkey(epoch_id, public_key);
8,759✔
739

740
        // Configure UTXO filter
741
        let address = self.get_miner_address(epoch_id, &pub_key_rev);
8,759✔
742
        test_debug!("Get UTXOs for {} ({address})", pub_key_rev.to_hex());
8,759✔
743

744
        let mut utxos = loop {
8,759✔
745
            let result = self.retrieve_utxo_set(
8,759✔
746
                &address,
8,759✔
747
                false,
748
                total_required,
8,759✔
749
                &utxos_to_exclude,
8,759✔
750
                block_height,
8,759✔
751
            );
752

753
            // Perform request
754
            match result {
8,759✔
755
                Ok(utxos) => {
8,759✔
756
                    break utxos;
8,759✔
757
                }
758
                Err(e) => {
×
759
                    error!("Bitcoin RPC failure: error listing utxos {e:?}");
×
760
                    sleep_ms(5000);
×
761
                    continue;
×
762
                }
763
            };
764
        };
765

766
        let utxos = if utxos.is_empty() {
8,759✔
767
            let (_, network) = self.config.burnchain.get_bitcoin_network();
25✔
768
            loop {
769
                if let BitcoinNetworkType::Regtest = network {
25✔
770
                    // Performing this operation on Mainnet / Testnet is very expensive, and can be longer than bitcoin block time.
771
                    // Assuming that miners are in charge of correctly operating their bitcoind nodes sounds
772
                    // reasonable to me.
773
                    // $ bitcoin-cli importaddress mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk
774
                    let result = self.import_public_key(&pub_key_rev);
25✔
775
                    if let Err(error) = result {
25✔
776
                        warn!(
×
777
                            "Import public key '{}' failed: {error:?}",
778
                            &pub_key_rev.to_hex()
×
779
                        );
780
                    }
25✔
781
                    sleep_ms(1000);
25✔
782
                }
×
783

784
                let result = self.retrieve_utxo_set(
25✔
785
                    &address,
25✔
786
                    false,
787
                    total_required,
25✔
788
                    &utxos_to_exclude,
25✔
789
                    block_height,
25✔
790
                );
791

792
                utxos = match result {
25✔
793
                    Ok(utxos) => utxos,
25✔
794
                    Err(e) => {
×
795
                        error!("Bitcoin RPC failure: error listing utxos {e:?}");
×
796
                        sleep_ms(5000);
×
797
                        continue;
×
798
                    }
799
                };
800

801
                test_debug!("Unspent for {address:?}: {utxos:?}");
25✔
802

803
                if utxos.is_empty() {
25✔
804
                    return None;
14✔
805
                } else {
806
                    break utxos;
11✔
807
                }
808
            }
809
        } else {
810
            debug!("Got {} UTXOs for {address:?}", utxos.utxos.len(),);
8,734✔
811
            utxos
8,734✔
812
        };
813

814
        let total_unspent = utxos.total_available();
8,745✔
815
        if total_unspent < total_required {
8,745✔
816
            warn!(
×
817
                "Total unspent {total_unspent} < {total_required} for {:?}",
818
                &pub_key_rev.to_hex()
×
819
            );
820
            return None;
×
821
        }
8,745✔
822

823
        Some(utxos)
8,745✔
824
    }
8,759✔
825

826
    fn build_leader_key_register_tx(
267✔
827
        &mut self,
267✔
828
        epoch_id: StacksEpochId,
267✔
829
        payload: LeaderKeyRegisterOp,
267✔
830
        signer: &mut BurnchainOpSigner,
267✔
831
    ) -> Result<Transaction, BurnchainControllerError> {
267✔
832
        let public_key = signer.get_public_key();
267✔
833

834
        // reload the config to find satoshis_per_byte changes
835
        let btc_miner_fee = self.config.burnchain.leader_key_tx_estimated_size
267✔
836
            * get_satoshis_per_byte(&self.config);
267✔
837
        let budget_for_outputs = DUST_UTXO_LIMIT;
267✔
838
        let total_required = btc_miner_fee + budget_for_outputs;
267✔
839

840
        let (mut tx, mut utxos) =
266✔
841
            self.prepare_tx(epoch_id, &public_key, total_required, None, None, 0)?;
267✔
842

843
        // Serialize the payload
844
        let op_bytes = {
266✔
845
            let mut buffer = vec![];
266✔
846
            let mut magic_bytes = self.config.burnchain.magic_bytes.as_bytes().to_vec();
266✔
847
            buffer.append(&mut magic_bytes);
266✔
848
            payload
266✔
849
                .consensus_serialize(&mut buffer)
266✔
850
                .expect("FATAL: invalid operation");
266✔
851
            buffer
266✔
852
        };
853

854
        let consensus_output = TxOut {
266✔
855
            value: 0,
266✔
856
            script_pubkey: Builder::new()
266✔
857
                .push_opcode(opcodes::All::OP_RETURN)
266✔
858
                .push_slice(&op_bytes)
266✔
859
                .into_script(),
266✔
860
        };
266✔
861

862
        tx.output = vec![consensus_output];
266✔
863

864
        let fee_rate = get_satoshis_per_byte(&self.config);
266✔
865

866
        self.finalize_tx(
266✔
867
            epoch_id,
266✔
868
            &mut tx,
266✔
869
            budget_for_outputs,
266✔
870
            0,
871
            self.config.burnchain.leader_key_tx_estimated_size,
266✔
872
            fee_rate,
266✔
873
            &mut utxos,
266✔
874
            signer,
266✔
875
            true, // key register op requires change output to exist
876
        );
877

878
        increment_btc_ops_sent_counter();
266✔
879

880
        info!(
266✔
881
            "Miner node: submitting leader_key_register op - {}, waiting for its inclusion in the next Bitcoin block",
882
            public_key.to_hex()
266✔
883
        );
884

885
        Ok(tx)
266✔
886
    }
267✔
887

888
    #[cfg(not(test))]
889
    fn build_transfer_stacks_tx(
890
        &mut self,
891
        _epoch_id: StacksEpochId,
892
        _payload: TransferStxOp,
893
        _signer: &mut BurnchainOpSigner,
894
        _utxo: Option<UTXO>,
895
    ) -> Result<Transaction, BurnchainControllerError> {
896
        unimplemented!()
897
    }
898

899
    #[cfg(not(test))]
900
    fn build_delegate_stacks_tx(
901
        &mut self,
902
        _epoch_id: StacksEpochId,
903
        _payload: DelegateStxOp,
904
        _signer: &mut BurnchainOpSigner,
905
        _utxo: Option<UTXO>,
906
    ) -> Result<Transaction, BurnchainControllerError> {
907
        unimplemented!()
908
    }
909

910
    #[cfg(test)]
911
    pub fn submit_manual(
2✔
912
        &mut self,
2✔
913
        epoch_id: StacksEpochId,
2✔
914
        operation: BlockstackOperationType,
2✔
915
        op_signer: &mut BurnchainOpSigner,
2✔
916
        utxo: Option<UTXO>,
2✔
917
    ) -> Result<Transaction, BurnchainControllerError> {
2✔
918
        let transaction = match operation {
2✔
919
            BlockstackOperationType::LeaderBlockCommit(_)
920
            | BlockstackOperationType::LeaderKeyRegister(_)
921
            | BlockstackOperationType::StackStx(_)
922
            | BlockstackOperationType::DelegateStx(_)
923
            | BlockstackOperationType::VoteForAggregateKey(_) => {
924
                unimplemented!();
×
925
            }
926
            BlockstackOperationType::PreStx(payload) => {
1✔
927
                self.build_pre_stacks_tx(epoch_id, payload, op_signer)
1✔
928
            }
929
            BlockstackOperationType::TransferStx(payload) => {
1✔
930
                self.build_transfer_stacks_tx(epoch_id, payload, op_signer, utxo)
1✔
931
            }
932
        }?;
×
933
        self.send_transaction(&transaction).map(|_| transaction)
2✔
934
    }
2✔
935

936
    #[cfg(test)]
937
    /// Build a transfer stacks tx.
938
    ///   this *only* works if the only existant UTXO is from a PreStx Op
939
    ///   this is okay for testing, but obviously not okay for actual use.
940
    ///   The reason for this constraint is that the bitcoin_regtest_controller's UTXO
941
    ///     and signing logic are fairly intertwined, and untangling the two seems excessive
942
    ///     for a functionality that won't be implemented for production via this controller.
943
    fn build_transfer_stacks_tx(
4✔
944
        &mut self,
4✔
945
        epoch_id: StacksEpochId,
4✔
946
        payload: TransferStxOp,
4✔
947
        signer: &mut BurnchainOpSigner,
4✔
948
        utxo_to_use: Option<UTXO>,
4✔
949
    ) -> Result<Transaction, BurnchainControllerError> {
4✔
950
        let public_key = signer.get_public_key();
4✔
951
        let max_tx_size = OP_TX_TRANSFER_STACKS_ESTIM_SIZE;
4✔
952
        let (mut tx, mut utxos) = if let Some(utxo) = utxo_to_use {
4✔
953
            (
1✔
954
                Transaction {
1✔
955
                    input: vec![],
1✔
956
                    output: vec![],
1✔
957
                    version: 1,
1✔
958
                    lock_time: 0,
1✔
959
                },
1✔
960
                UTXOSet {
1✔
961
                    bhh: BurnchainHeaderHash::zero(),
1✔
962
                    utxos: vec![utxo],
1✔
963
                },
1✔
964
            )
1✔
965
        } else {
966
            self.prepare_tx(
3✔
967
                epoch_id,
3✔
968
                &public_key,
3✔
969
                DUST_UTXO_LIMIT + max_tx_size * get_satoshis_per_byte(&self.config),
3✔
970
                None,
3✔
971
                None,
3✔
972
                0,
973
            )?
×
974
        };
975

976
        // Serialize the payload
977
        let op_bytes = {
4✔
978
            let mut bytes = self.config.burnchain.magic_bytes.as_bytes().to_vec();
4✔
979
            payload
4✔
980
                .consensus_serialize(&mut bytes)
4✔
981
                .map_err(BurnchainControllerError::SerializerError)?;
4✔
982
            bytes
4✔
983
        };
984

985
        let consensus_output = TxOut {
4✔
986
            value: 0,
4✔
987
            script_pubkey: Builder::new()
4✔
988
                .push_opcode(opcodes::All::OP_RETURN)
4✔
989
                .push_slice(&op_bytes)
4✔
990
                .into_script(),
4✔
991
        };
4✔
992

993
        tx.output = vec![consensus_output];
4✔
994
        tx.output
4✔
995
            .push(PoxAddress::Standard(payload.recipient, None).to_bitcoin_tx_out(DUST_UTXO_LIMIT));
4✔
996

997
        self.finalize_tx(
4✔
998
            epoch_id,
4✔
999
            &mut tx,
4✔
1000
            DUST_UTXO_LIMIT,
1001
            0,
1002
            max_tx_size,
4✔
1003
            get_satoshis_per_byte(&self.config),
4✔
1004
            &mut utxos,
4✔
1005
            signer,
4✔
1006
            false,
1007
        );
1008

1009
        increment_btc_ops_sent_counter();
4✔
1010

1011
        info!(
4✔
1012
            "Miner node: submitting stacks transfer op - {}",
1013
            public_key.to_hex()
4✔
1014
        );
1015

1016
        Ok(tx)
4✔
1017
    }
4✔
1018

1019
    #[cfg(test)]
1020
    /// Build a delegate stacks tx.
1021
    ///   this *only* works if the only existant UTXO is from a PreStx Op
1022
    ///   this is okay for testing, but obviously not okay for actual use.
1023
    ///   The reason for this constraint is that the bitcoin_regtest_controller's UTXO
1024
    ///     and signing logic are fairly intertwined, and untangling the two seems excessive
1025
    ///     for a functionality that won't be implemented for production via this controller.
1026
    fn build_delegate_stacks_tx(
2✔
1027
        &mut self,
2✔
1028
        epoch_id: StacksEpochId,
2✔
1029
        payload: DelegateStxOp,
2✔
1030
        signer: &mut BurnchainOpSigner,
2✔
1031
        utxo_to_use: Option<UTXO>,
2✔
1032
    ) -> Result<Transaction, BurnchainControllerError> {
2✔
1033
        let public_key = signer.get_public_key();
2✔
1034
        let max_tx_size = OP_TX_DELEGATE_STACKS_ESTIM_SIZE;
2✔
1035

1036
        let (mut tx, mut utxos) = if let Some(utxo) = utxo_to_use {
2✔
1037
            (
×
1038
                Transaction {
×
1039
                    input: vec![],
×
1040
                    output: vec![],
×
1041
                    version: 1,
×
1042
                    lock_time: 0,
×
1043
                },
×
1044
                UTXOSet {
×
1045
                    bhh: BurnchainHeaderHash::zero(),
×
1046
                    utxos: vec![utxo],
×
1047
                },
×
1048
            )
×
1049
        } else {
1050
            self.prepare_tx(
2✔
1051
                epoch_id,
2✔
1052
                &public_key,
2✔
1053
                DUST_UTXO_LIMIT + max_tx_size * get_satoshis_per_byte(&self.config),
2✔
1054
                None,
2✔
1055
                None,
2✔
1056
                0,
1057
            )?
×
1058
        };
1059

1060
        // Serialize the payload
1061
        let op_bytes = {
2✔
1062
            let mut bytes = self.config.burnchain.magic_bytes.as_bytes().to_vec();
2✔
1063
            payload
2✔
1064
                .consensus_serialize(&mut bytes)
2✔
1065
                .map_err(BurnchainControllerError::SerializerError)?;
2✔
1066
            bytes
2✔
1067
        };
1068

1069
        let consensus_output = TxOut {
2✔
1070
            value: 0,
2✔
1071
            script_pubkey: Builder::new()
2✔
1072
                .push_opcode(opcodes::All::OP_RETURN)
2✔
1073
                .push_slice(&op_bytes)
2✔
1074
                .into_script(),
2✔
1075
        };
2✔
1076

1077
        tx.output = vec![consensus_output];
2✔
1078
        tx.output.push(
2✔
1079
            PoxAddress::Standard(payload.delegate_to, None).to_bitcoin_tx_out(DUST_UTXO_LIMIT),
2✔
1080
        );
1081

1082
        self.finalize_tx(
2✔
1083
            epoch_id,
2✔
1084
            &mut tx,
2✔
1085
            DUST_UTXO_LIMIT,
1086
            0,
1087
            max_tx_size,
2✔
1088
            get_satoshis_per_byte(&self.config),
2✔
1089
            &mut utxos,
2✔
1090
            signer,
2✔
1091
            false,
1092
        );
1093

1094
        increment_btc_ops_sent_counter();
2✔
1095

1096
        info!(
2✔
1097
            "Miner node: submitting stacks delegate op - {}",
1098
            public_key.to_hex()
2✔
1099
        );
1100

1101
        Ok(tx)
2✔
1102
    }
2✔
1103

1104
    #[cfg(test)]
1105
    /// Build a vote-for-aggregate-key burn op tx
1106
    fn build_vote_for_aggregate_key_tx(
2✔
1107
        &mut self,
2✔
1108
        epoch_id: StacksEpochId,
2✔
1109
        payload: VoteForAggregateKeyOp,
2✔
1110
        signer: &mut BurnchainOpSigner,
2✔
1111
        utxo_to_use: Option<UTXO>,
2✔
1112
    ) -> Result<Transaction, BurnchainControllerError> {
2✔
1113
        let public_key = signer.get_public_key();
2✔
1114
        let max_tx_size = OP_TX_VOTE_AGG_ESTIM_SIZE;
2✔
1115

1116
        let (mut tx, mut utxos) = if let Some(utxo) = utxo_to_use {
2✔
1117
            (
×
1118
                Transaction {
×
1119
                    input: vec![],
×
1120
                    output: vec![],
×
1121
                    version: 1,
×
1122
                    lock_time: 0,
×
1123
                },
×
1124
                UTXOSet {
×
1125
                    bhh: BurnchainHeaderHash::zero(),
×
1126
                    utxos: vec![utxo],
×
1127
                },
×
1128
            )
×
1129
        } else {
1130
            self.prepare_tx(
2✔
1131
                epoch_id,
2✔
1132
                &public_key,
2✔
1133
                DUST_UTXO_LIMIT + max_tx_size * get_satoshis_per_byte(&self.config),
2✔
1134
                None,
2✔
1135
                None,
2✔
1136
                0,
1137
            )?
×
1138
        };
1139

1140
        // Serialize the payload
1141
        let op_bytes = {
2✔
1142
            let mut bytes = self.config.burnchain.magic_bytes.as_bytes().to_vec();
2✔
1143
            payload
2✔
1144
                .consensus_serialize(&mut bytes)
2✔
1145
                .map_err(BurnchainControllerError::SerializerError)?;
2✔
1146
            bytes
2✔
1147
        };
1148

1149
        let consensus_output = TxOut {
2✔
1150
            value: 0,
2✔
1151
            script_pubkey: Builder::new()
2✔
1152
                .push_opcode(opcodes::All::OP_RETURN)
2✔
1153
                .push_slice(&op_bytes)
2✔
1154
                .into_script(),
2✔
1155
        };
2✔
1156

1157
        tx.output = vec![consensus_output];
2✔
1158

1159
        self.finalize_tx(
2✔
1160
            epoch_id,
2✔
1161
            &mut tx,
2✔
1162
            DUST_UTXO_LIMIT,
1163
            0,
1164
            max_tx_size,
2✔
1165
            get_satoshis_per_byte(&self.config),
2✔
1166
            &mut utxos,
2✔
1167
            signer,
2✔
1168
            false,
1169
        );
1170

1171
        increment_btc_ops_sent_counter();
2✔
1172

1173
        info!(
2✔
1174
            "Miner node: submitting vote for aggregate key op - {}",
1175
            public_key.to_hex()
2✔
1176
        );
1177

1178
        Ok(tx)
2✔
1179
    }
2✔
1180

1181
    #[cfg(not(test))]
1182
    /// Build a vote-for-aggregate-key burn op tx
1183
    fn build_vote_for_aggregate_key_tx(
1184
        &mut self,
1185
        _epoch_id: StacksEpochId,
1186
        _payload: VoteForAggregateKeyOp,
1187
        _signer: &mut BurnchainOpSigner,
1188
        _utxo_to_use: Option<UTXO>,
1189
    ) -> Result<Transaction, BurnchainControllerError> {
1190
        unimplemented!()
1191
    }
1192

1193
    #[cfg(not(test))]
1194
    fn build_pre_stacks_tx(
1195
        &mut self,
1196
        _epoch_id: StacksEpochId,
1197
        _payload: PreStxOp,
1198
        _signer: &mut BurnchainOpSigner,
1199
    ) -> Result<Transaction, BurnchainControllerError> {
1200
        unimplemented!()
1201
    }
1202

1203
    #[cfg(test)]
1204
    fn build_pre_stacks_tx(
16✔
1205
        &mut self,
16✔
1206
        epoch_id: StacksEpochId,
16✔
1207
        payload: PreStxOp,
16✔
1208
        signer: &mut BurnchainOpSigner,
16✔
1209
    ) -> Result<Transaction, BurnchainControllerError> {
16✔
1210
        let public_key = signer.get_public_key();
16✔
1211
        let max_tx_size = OP_TX_PRE_STACKS_ESTIM_SIZE;
16✔
1212

1213
        let max_tx_size_any_op = OP_TX_ANY_ESTIM_SIZE;
16✔
1214
        let output_amt = DUST_UTXO_LIMIT + max_tx_size_any_op * get_satoshis_per_byte(&self.config);
16✔
1215

1216
        let (mut tx, mut utxos) =
15✔
1217
            self.prepare_tx(epoch_id, &public_key, output_amt, None, None, 0)?;
16✔
1218

1219
        // Serialize the payload
1220
        let op_bytes = {
15✔
1221
            let mut bytes = self.config.burnchain.magic_bytes.as_bytes().to_vec();
15✔
1222
            bytes.push(Opcodes::PreStx as u8);
15✔
1223
            bytes
15✔
1224
        };
1225

1226
        let consensus_output = TxOut {
15✔
1227
            value: 0,
15✔
1228
            script_pubkey: Builder::new()
15✔
1229
                .push_opcode(opcodes::All::OP_RETURN)
15✔
1230
                .push_slice(&op_bytes)
15✔
1231
                .into_script(),
15✔
1232
        };
15✔
1233

1234
        tx.output = vec![consensus_output];
15✔
1235
        tx.output
15✔
1236
            .push(PoxAddress::Standard(payload.output, None).to_bitcoin_tx_out(output_amt));
15✔
1237

1238
        self.finalize_tx(
15✔
1239
            epoch_id,
15✔
1240
            &mut tx,
15✔
1241
            output_amt,
15✔
1242
            0,
1243
            max_tx_size,
15✔
1244
            get_satoshis_per_byte(&self.config),
15✔
1245
            &mut utxos,
15✔
1246
            signer,
15✔
1247
            false,
1248
        );
1249

1250
        increment_btc_ops_sent_counter();
15✔
1251

1252
        info!(
15✔
1253
            "Miner node: submitting pre_stacks op - {}",
1254
            public_key.to_hex()
15✔
1255
        );
1256

1257
        Ok(tx)
15✔
1258
    }
16✔
1259

1260
    #[cfg_attr(test, mutants::skip)]
1261
    #[cfg(not(test))]
1262
    fn build_stack_stx_tx(
1263
        &mut self,
1264
        _epoch_id: StacksEpochId,
1265
        _payload: StackStxOp,
1266
        _signer: &mut BurnchainOpSigner,
1267
        _utxo_to_use: Option<UTXO>,
1268
    ) -> Result<Transaction, BurnchainControllerError> {
1269
        unimplemented!()
1270
    }
1271

1272
    #[cfg(test)]
1273
    fn build_stack_stx_tx(
4✔
1274
        &mut self,
4✔
1275
        epoch_id: StacksEpochId,
4✔
1276
        payload: StackStxOp,
4✔
1277
        signer: &mut BurnchainOpSigner,
4✔
1278
        utxo_to_use: Option<UTXO>,
4✔
1279
    ) -> Result<Transaction, BurnchainControllerError> {
4✔
1280
        let public_key = signer.get_public_key();
4✔
1281
        let max_tx_size = OP_TX_STACK_STX_ESTIM_SIZE;
4✔
1282

1283
        let (mut tx, mut utxos) = if let Some(utxo) = utxo_to_use {
4✔
1284
            (
×
1285
                Transaction {
×
1286
                    input: vec![],
×
1287
                    output: vec![],
×
1288
                    version: 1,
×
1289
                    lock_time: 0,
×
1290
                },
×
1291
                UTXOSet {
×
1292
                    bhh: BurnchainHeaderHash::zero(),
×
1293
                    utxos: vec![utxo],
×
1294
                },
×
1295
            )
×
1296
        } else {
1297
            self.prepare_tx(
4✔
1298
                epoch_id,
4✔
1299
                &public_key,
4✔
1300
                DUST_UTXO_LIMIT + max_tx_size * get_satoshis_per_byte(&self.config),
4✔
1301
                None,
4✔
1302
                None,
4✔
1303
                0,
1304
            )?
×
1305
        };
1306

1307
        // Serialize the payload
1308
        let op_bytes = {
4✔
1309
            let mut bytes = self.config.burnchain.magic_bytes.as_bytes().to_vec();
4✔
1310
            payload
4✔
1311
                .consensus_serialize(&mut bytes)
4✔
1312
                .map_err(BurnchainControllerError::SerializerError)?;
4✔
1313
            bytes
4✔
1314
        };
1315

1316
        let consensus_output = TxOut {
4✔
1317
            value: 0,
4✔
1318
            script_pubkey: Builder::new()
4✔
1319
                .push_opcode(opcodes::All::OP_RETURN)
4✔
1320
                .push_slice(&op_bytes)
4✔
1321
                .into_script(),
4✔
1322
        };
4✔
1323

1324
        tx.output = vec![consensus_output];
4✔
1325
        tx.output
4✔
1326
            .push(payload.reward_addr.to_bitcoin_tx_out(DUST_UTXO_LIMIT));
4✔
1327

1328
        self.finalize_tx(
4✔
1329
            epoch_id,
4✔
1330
            &mut tx,
4✔
1331
            DUST_UTXO_LIMIT,
1332
            0,
1333
            max_tx_size,
4✔
1334
            get_satoshis_per_byte(&self.config),
4✔
1335
            &mut utxos,
4✔
1336
            signer,
4✔
1337
            false,
1338
        );
1339

1340
        increment_btc_ops_sent_counter();
4✔
1341

1342
        info!(
4✔
1343
            "Miner node: submitting stack-stx op - {}",
1344
            public_key.to_hex()
4✔
1345
        );
1346

1347
        Ok(tx)
4✔
1348
    }
4✔
1349

1350
    fn magic_bytes(&self) -> Vec<u8> {
8,273✔
1351
        #[cfg(test)]
1352
        {
1353
            if let Some(set_bytes) = *TEST_MAGIC_BYTES
8,273✔
1354
                .lock()
8,273✔
1355
                .expect("FATAL: test magic bytes mutex poisoned")
8,273✔
1356
            {
1357
                return set_bytes.to_vec();
1✔
1358
            }
8,272✔
1359
        }
1360
        self.config.burnchain.magic_bytes.as_bytes().to_vec()
8,272✔
1361
    }
8,273✔
1362

1363
    #[allow(clippy::too_many_arguments)]
1364
    fn send_block_commit_operation(
9,225✔
1365
        &mut self,
9,225✔
1366
        epoch_id: StacksEpochId,
9,225✔
1367
        payload: LeaderBlockCommitOp,
9,225✔
1368
        signer: &mut BurnchainOpSigner,
9,225✔
1369
        utxos_to_include: Option<UTXOSet>,
9,225✔
1370
        utxos_to_exclude: Option<UTXOSet>,
9,225✔
1371
        previous_fees: Option<LeaderBlockCommitFees>,
9,225✔
1372
        previous_txids: &[Txid],
9,225✔
1373
    ) -> Result<Transaction, BurnchainControllerError> {
9,225✔
1374
        let _ = self.sortdb_mut();
9,225✔
1375
        let burn_chain_tip = self
9,225✔
1376
            .burnchain_db
9,225✔
1377
            .as_ref()
9,225✔
1378
            .ok_or(BurnchainControllerError::BurnchainError)?
9,225✔
1379
            .get_canonical_chain_tip()
9,225✔
1380
            .map_err(|_| BurnchainControllerError::BurnchainError)?;
9,225✔
1381
        let estimated_fees = match previous_fees {
9,225✔
1382
            Some(fees) => fees.fees_from_previous_tx(&payload, &self.config),
322✔
1383
            None => LeaderBlockCommitFees::estimated_fees_from_payload(&payload, &self.config),
8,903✔
1384
        };
1385

1386
        self.send_block_commit_operation_at_burnchain_height(
9,225✔
1387
            epoch_id,
9,225✔
1388
            payload,
9,225✔
1389
            signer,
9,225✔
1390
            utxos_to_include,
9,225✔
1391
            utxos_to_exclude,
9,225✔
1392
            estimated_fees,
9,225✔
1393
            previous_txids,
9,225✔
1394
            burn_chain_tip.block_height,
9,225✔
1395
        )
1396
    }
9,225✔
1397

1398
    #[allow(clippy::too_many_arguments)]
1399
    fn send_block_commit_operation_at_burnchain_height(
9,225✔
1400
        &mut self,
9,225✔
1401
        epoch_id: StacksEpochId,
9,225✔
1402
        payload: LeaderBlockCommitOp,
9,225✔
1403
        signer: &mut BurnchainOpSigner,
9,225✔
1404
        utxos_to_include: Option<UTXOSet>,
9,225✔
1405
        utxos_to_exclude: Option<UTXOSet>,
9,225✔
1406
        mut estimated_fees: LeaderBlockCommitFees,
9,225✔
1407
        previous_txids: &[Txid],
9,225✔
1408
        burnchain_block_height: u64,
9,225✔
1409
    ) -> Result<Transaction, BurnchainControllerError> {
9,225✔
1410
        let public_key = signer.get_public_key();
9,225✔
1411
        let (mut tx, mut utxos) = self.prepare_tx(
9,225✔
1412
            epoch_id,
9,225✔
1413
            &public_key,
9,225✔
1414
            estimated_fees.estimated_amount_required(),
9,225✔
1415
            utxos_to_include,
9,225✔
1416
            utxos_to_exclude,
9,225✔
1417
            burnchain_block_height,
9,225✔
1418
        )?;
952✔
1419

1420
        // Serialize the payload
1421
        let op_bytes = {
8,273✔
1422
            let mut buffer = vec![];
8,273✔
1423
            let mut magic_bytes = self.magic_bytes();
8,273✔
1424
            buffer.append(&mut magic_bytes);
8,273✔
1425
            payload
8,273✔
1426
                .consensus_serialize(&mut buffer)
8,273✔
1427
                .expect("FATAL: invalid operation");
8,273✔
1428
            buffer
8,273✔
1429
        };
1430

1431
        let consensus_output = TxOut {
8,273✔
1432
            value: estimated_fees.sunset_fee,
8,273✔
1433
            script_pubkey: Builder::new()
8,273✔
1434
                .push_opcode(opcodes::All::OP_RETURN)
8,273✔
1435
                .push_slice(&op_bytes)
8,273✔
1436
                .into_script(),
8,273✔
1437
        };
8,273✔
1438

1439
        tx.output = vec![consensus_output];
8,273✔
1440

1441
        for commit_to in payload.commit_outs.iter() {
14,614✔
1442
            tx.output
14,614✔
1443
                .push(commit_to.to_bitcoin_tx_out(estimated_fees.amount_per_output()));
14,614✔
1444
        }
14,614✔
1445

1446
        let fee_rate = estimated_fees.fee_rate;
8,273✔
1447
        self.finalize_tx(
8,273✔
1448
            epoch_id,
8,273✔
1449
            &mut tx,
8,273✔
1450
            estimated_fees.total_spent_in_outputs(),
8,273✔
1451
            estimated_fees.spent_in_attempts,
8,273✔
1452
            estimated_fees.min_tx_size(),
8,273✔
1453
            fee_rate,
8,273✔
1454
            &mut utxos,
8,273✔
1455
            signer,
8,273✔
1456
            true, // block commit op requires change output to exist
1457
        );
1458
        debug!("Transaction relying on UTXOs: {utxos:?}");
8,273✔
1459

1460
        let serialized_tx = serialize(&tx).expect("BUG: failed to serialize to a vec");
8,273✔
1461
        let tx_size = serialized_tx.len() as u64;
8,273✔
1462
        estimated_fees.register_replacement(tx_size);
8,273✔
1463

1464
        let txid = Txid::from_bitcoin_tx_hash(&tx.txid());
8,273✔
1465
        let mut txids = previous_txids.to_vec();
8,273✔
1466
        txids.push(txid.clone());
8,273✔
1467
        let ongoing_block_commit = OngoingBlockCommit {
8,273✔
1468
            payload,
8,273✔
1469
            utxos,
8,273✔
1470
            fees: estimated_fees,
8,273✔
1471
            txids,
8,273✔
1472
        };
8,273✔
1473

1474
        info!(
8,273✔
1475
            "Miner node: submitting leader_block_commit (txid: {}, rbf: {}, total spent: {}, size: {}, fee_rate: {fee_rate})",
1476
            txid.to_hex(),
8,273✔
1477
            ongoing_block_commit.fees.is_rbf_enabled,
1478
            ongoing_block_commit.fees.total_spent(),
8,273✔
1479
            ongoing_block_commit.fees.final_size
1480
        );
1481

1482
        self.ongoing_block_commit = Some(ongoing_block_commit);
8,273✔
1483

1484
        increment_btc_ops_sent_counter();
8,273✔
1485

1486
        Ok(tx)
8,273✔
1487
    }
9,225✔
1488

1489
    fn build_leader_block_commit_tx(
25,051✔
1490
        &mut self,
25,051✔
1491
        epoch_id: StacksEpochId,
25,051✔
1492
        payload: LeaderBlockCommitOp,
25,051✔
1493
        signer: &mut BurnchainOpSigner,
25,051✔
1494
    ) -> Result<Transaction, BurnchainControllerError> {
25,051✔
1495
        // Are we currently tracking an operation?
1496
        if self.ongoing_block_commit.is_none() {
25,051✔
1497
            // Good to go, let's build the transaction and send it.
1498
            let res =
1,411✔
1499
                self.send_block_commit_operation(epoch_id, payload, signer, None, None, None, &[]);
1,411✔
1500
            return res;
1,411✔
1501
        }
23,640✔
1502

1503
        let ongoing_op = self.ongoing_block_commit.take().unwrap();
23,640✔
1504

1505
        let _ = self.sortdb_mut();
23,640✔
1506
        let burnchain_db = self.burnchain_db.as_ref().expect("BurnchainDB not opened");
23,640✔
1507

1508
        for txid in ongoing_op.txids.iter() {
23,919✔
1509
            // check if ongoing_op is in the burnchain_db *or* has been confirmed via the bitcoin RPC
1510
            let mined_op = burnchain_db.find_burnchain_op(&self.indexer, txid);
23,919✔
1511
            let ongoing_tx_confirmed = mined_op.is_some() || self.is_transaction_confirmed(txid);
23,919✔
1512

1513
            test_debug!("Ongoing Tx confirmed: {ongoing_tx_confirmed} - TXID: {txid}");
23,919✔
1514
            if ongoing_tx_confirmed {
23,919✔
1515
                if ongoing_op.payload == payload {
8,713✔
1516
                    info!("Abort attempt to re-submit confirmed LeaderBlockCommit");
1,225✔
1517
                    self.ongoing_block_commit = Some(ongoing_op);
1,225✔
1518
                    return Err(BurnchainControllerError::IdenticalOperation);
1,225✔
1519
                }
7,488✔
1520

1521
                debug!("Was able to retrieve confirmation of ongoing burnchain TXID - {txid}");
7,488✔
1522
                let res = self.send_block_commit_operation(
7,488✔
1523
                    epoch_id,
7,488✔
1524
                    payload,
7,488✔
1525
                    signer,
7,488✔
1526
                    None,
7,488✔
1527
                    None,
7,488✔
1528
                    None,
7,488✔
1529
                    &[],
7,488✔
1530
                );
1531
                return res;
7,488✔
1532
            } else {
1533
                debug!("Was unable to retrieve ongoing TXID - {txid}");
15,206✔
1534
            };
1535
        }
1536

1537
        // Did a re-org occur since we fetched our UTXOs, or are the UTXOs so stale that they should be abandoned?
1538
        let mut traversal_depth = 0;
14,927✔
1539
        let mut burn_chain_tip = burnchain_db
14,927✔
1540
            .get_canonical_chain_tip()
14,927✔
1541
            .map_err(|_| BurnchainControllerError::BurnchainError)?;
14,927✔
1542
        let mut found_last_mined_at = false;
14,927✔
1543
        while traversal_depth < UTXO_CACHE_STALENESS_LIMIT {
14,997✔
1544
            if burn_chain_tip.block_hash == ongoing_op.utxos.bhh {
14,993✔
1545
                found_last_mined_at = true;
14,923✔
1546
                break;
14,923✔
1547
            }
70✔
1548

1549
            let parent = BurnchainDB::get_burnchain_block(
70✔
1550
                burnchain_db.conn(),
70✔
1551
                &burn_chain_tip.parent_block_hash,
70✔
1552
            )
1553
            .map_err(|_| BurnchainControllerError::BurnchainError)?;
70✔
1554

1555
            burn_chain_tip = parent.header;
70✔
1556
            traversal_depth += 1;
70✔
1557
        }
1558

1559
        if !found_last_mined_at {
14,927✔
1560
            info!(
4✔
1561
                "Possible presence of fork or stale UTXO cache, invalidating cached set of UTXOs.";
1562
                "cached_burn_block_hash" => %ongoing_op.utxos.bhh,
1563
            );
1564
            let res =
4✔
1565
                self.send_block_commit_operation(epoch_id, payload, signer, None, None, None, &[]);
4✔
1566
            return res;
4✔
1567
        }
14,923✔
1568

1569
        // Stop as soon as the fee_rate is ${self.config.burnchain.max_rbf} percent higher, stop RBF
1570
        if ongoing_op.fees.fee_rate
14,923✔
1571
            > (get_satoshis_per_byte(&self.config) * get_max_rbf(&self.config) / 100)
14,923✔
1572
        {
1573
            warn!(
×
1574
                "RBF'd block commits reached {}% satoshi per byte fee rate, not resubmitting",
1575
                get_max_rbf(&self.config)
×
1576
            );
1577
            self.ongoing_block_commit = Some(ongoing_op);
×
1578
            return Err(BurnchainControllerError::MaxFeeRateExceeded);
×
1579
        }
14,923✔
1580

1581
        // An ongoing operation is in the mempool and we received a new block. The desired behaviour is the following:
1582
        // (1) If the ongoing and the incoming operation are **strictly** identical, we will be idempotent and discard the incoming.
1583
        // (2) If the 2 operations are different, attempt to RBF the outgoing transaction:
1584

1585
        // Let's start by early returning (1)
1586
        if payload == ongoing_op.payload {
14,923✔
1587
            info!("Abort attempt to re-submit identical LeaderBlockCommit");
14,601✔
1588
            self.ongoing_block_commit = Some(ongoing_op);
14,601✔
1589
            return Err(BurnchainControllerError::IdenticalOperation);
14,601✔
1590
        }
322✔
1591

1592
        // If we reach this point, we are attempting to RBF the ongoing operation (2)
1593
        info!(
322✔
1594
            "Attempt to replace by fee an outdated leader block commit";
1595
            "ongoing_txids" => ?ongoing_op.txids
1596
        );
1597
        let res = self.send_block_commit_operation(
322✔
1598
            epoch_id,
322✔
1599
            payload,
322✔
1600
            signer,
322✔
1601
            Some(ongoing_op.utxos.clone()),
322✔
1602
            None,
322✔
1603
            Some(ongoing_op.fees.clone()),
322✔
1604
            &ongoing_op.txids,
322✔
1605
        );
1606

1607
        if res.is_err() {
322✔
1608
            self.ongoing_block_commit = Some(ongoing_op);
×
1609
        }
322✔
1610

1611
        res
322✔
1612
    }
25,051✔
1613

1614
    pub(crate) fn get_miner_address(
33,504✔
1615
        &self,
33,504✔
1616
        epoch_id: StacksEpochId,
33,504✔
1617
        public_key: &Secp256k1PublicKey,
33,504✔
1618
    ) -> BitcoinAddress {
33,504✔
1619
        let (_, network_id) = self.config.burnchain.get_bitcoin_network();
33,504✔
1620

1621
        if self.config.miner.segwit && epoch_id >= StacksEpochId::Epoch21 {
33,504✔
UNCOV
1622
            let hash160 = Hash160::from_data(&public_key.to_bytes_compressed());
×
UNCOV
1623
            BitcoinAddress::from_bytes_segwit_p2wpkh(network_id, &hash160.0)
×
UNCOV
1624
                .expect("Public key incorrect")
×
1625
        } else {
1626
            let hash160 = Hash160::from_data(&public_key.to_bytes());
33,504✔
1627
            BitcoinAddress::from_bytes_legacy(
33,504✔
1628
                network_id,
33,504✔
1629
                LegacyBitcoinAddressType::PublicKeyHash,
33,504✔
1630
                &hash160.0,
33,504✔
1631
            )
1632
            .expect("Public key incorrect")
33,504✔
1633
        }
1634
    }
33,504✔
1635

1636
    // TODO: add tests from mutation testing results #4865
1637
    #[cfg_attr(test, mutants::skip)]
1638
    fn prepare_tx(
9,519✔
1639
        &mut self,
9,519✔
1640
        epoch_id: StacksEpochId,
9,519✔
1641
        public_key: &Secp256k1PublicKey,
9,519✔
1642
        total_required: u64,
9,519✔
1643
        utxos_to_include: Option<UTXOSet>,
9,519✔
1644
        utxos_to_exclude: Option<UTXOSet>,
9,519✔
1645
        block_height: u64,
9,519✔
1646
    ) -> Result<(Transaction, UTXOSet), BurnchainControllerError> {
9,519✔
1647
        let utxos = if let Some(utxos) = utxos_to_include {
9,519✔
1648
            // in RBF, you have to consume the same UTXOs
1649
            utxos
322✔
1650
        } else {
1651
            // if mock mining, do not even bother requesting UTXOs
1652
            if self.config.node.mock_mining {
9,197✔
1653
                return Err(BurnchainControllerError::NoUTXOs);
952✔
1654
            }
8,245✔
1655

1656
            // Fetch some UTXOs
1657
            let addr = self.get_miner_address(epoch_id, public_key);
8,245✔
1658
            match self.get_utxos(
8,245✔
1659
                epoch_id,
8,245✔
1660
                public_key,
8,245✔
1661
                total_required,
8,245✔
1662
                utxos_to_exclude,
8,245✔
1663
                block_height,
8,245✔
1664
            ) {
8,245✔
1665
                Some(utxos) => utxos,
8,243✔
1666
                None => {
1667
                    warn!(
2✔
1668
                        "No UTXOs for {} ({addr}) in epoch {epoch_id}",
1669
                        &public_key.to_hex(),
2✔
1670
                    );
1671
                    return Err(BurnchainControllerError::NoUTXOs);
2✔
1672
                }
1673
            }
1674
        };
1675

1676
        // Prepare a backbone for the tx
1677
        let transaction = Transaction {
8,565✔
1678
            input: vec![],
8,565✔
1679
            output: vec![],
8,565✔
1680
            version: 1,
8,565✔
1681
            lock_time: 0,
8,565✔
1682
        };
8,565✔
1683

1684
        Ok((transaction, utxos))
8,565✔
1685
    }
9,519✔
1686

1687
    #[allow(clippy::too_many_arguments)]
1688
    fn finalize_tx(
8,566✔
1689
        &mut self,
8,566✔
1690
        epoch_id: StacksEpochId,
8,566✔
1691
        tx: &mut Transaction,
8,566✔
1692
        spent_in_outputs: u64,
8,566✔
1693
        spent_in_rbf: u64,
8,566✔
1694
        min_tx_size: u64,
8,566✔
1695
        fee_rate: u64,
8,566✔
1696
        utxos_set: &mut UTXOSet,
8,566✔
1697
        signer: &mut BurnchainOpSigner,
8,566✔
1698
        force_change_output: bool,
8,566✔
1699
    ) {
8,566✔
1700
        // spend UTXOs in order by confirmations.  Spend the least-confirmed UTXO first, and in the
1701
        // event of a tie, spend the smallest-value UTXO first.
1702
        utxos_set.utxos.sort_by(|u1, u2| {
6,368,482✔
1703
            if u1.confirmations != u2.confirmations {
6,368,466✔
1704
                u1.confirmations.cmp(&u2.confirmations)
6,368,466✔
1705
            } else {
1706
                // for block-commits, the smaller value is likely the UTXO-chained value, so
1707
                // continue to prioritize it as the first spend in order to avoid breaking the
1708
                // miner commit chain.
UNCOV
1709
                u1.amount.cmp(&u2.amount)
×
1710
            }
1711
        });
6,368,466✔
1712

1713
        let tx_size = {
8,566✔
1714
            // We will be calling 2 times serialize_tx, the first time with an estimated size,
1715
            // Second time with the actual size, computed thanks to the 1st attempt.
1716
            let estimated_rbf = if spent_in_rbf == 0 {
8,566✔
1717
                0
8,244✔
1718
            } else {
1719
                spent_in_rbf + min_tx_size // we're spending 1 sat / byte in RBF
322✔
1720
            };
1721
            let mut tx_cloned = tx.clone();
8,566✔
1722
            let mut utxos_cloned = utxos_set.clone();
8,566✔
1723
            self.serialize_tx(
8,566✔
1724
                epoch_id,
8,566✔
1725
                &mut tx_cloned,
8,566✔
1726
                spent_in_outputs + min_tx_size * fee_rate + estimated_rbf,
8,566✔
1727
                &mut utxos_cloned,
8,566✔
1728
                signer,
8,566✔
1729
                force_change_output,
8,566✔
1730
            );
1731
            let serialized_tx = serialize(&tx_cloned).expect("BUG: failed to serialize to a vec");
8,566✔
1732
            cmp::max(min_tx_size, serialized_tx.len() as u64)
8,566✔
1733
        };
1734

1735
        let rbf_fee = if spent_in_rbf == 0 {
8,566✔
1736
            0
8,244✔
1737
        } else {
1738
            spent_in_rbf + tx_size // we're spending 1 sat / byte in RBF
322✔
1739
        };
1740
        self.serialize_tx(
8,566✔
1741
            epoch_id,
8,566✔
1742
            tx,
8,566✔
1743
            spent_in_outputs + tx_size * fee_rate + rbf_fee,
8,566✔
1744
            utxos_set,
8,566✔
1745
            signer,
8,566✔
1746
            force_change_output,
8,566✔
1747
        );
1748
        signer.dispose();
8,566✔
1749
    }
8,566✔
1750

1751
    /// Sign and serialize a tx, consuming the UTXOs in utxo_set and spending total_to_spend
1752
    /// satoshis.  Uses the key in signer.
1753
    /// If self.config.miner.segwit is true, the transaction's change address will be a p2wpkh
1754
    /// output. Otherwise, it will be a p2pkh output.
1755
    fn serialize_tx(
17,132✔
1756
        &mut self,
17,132✔
1757
        epoch_id: StacksEpochId,
17,132✔
1758
        tx: &mut Transaction,
17,132✔
1759
        tx_cost: u64,
17,132✔
1760
        utxos_set: &mut UTXOSet,
17,132✔
1761
        signer: &mut BurnchainOpSigner,
17,132✔
1762
        force_change_output: bool,
17,132✔
1763
    ) -> bool {
17,132✔
1764
        let mut public_key = signer.get_public_key();
17,132✔
1765

1766
        let total_target = if force_change_output {
17,132✔
1767
            tx_cost + DUST_UTXO_LIMIT
17,078✔
1768
        } else {
1769
            tx_cost
54✔
1770
        };
1771

1772
        // select UTXOs until we have enough to cover the cost
1773
        let mut total_consumed = 0;
17,132✔
1774
        let mut available_utxos = vec![];
17,132✔
1775
        available_utxos.append(&mut utxos_set.utxos);
17,132✔
1776
        for utxo in available_utxos.into_iter() {
17,132✔
1777
            total_consumed += utxo.amount;
17,132✔
1778
            utxos_set.utxos.push(utxo);
17,132✔
1779

1780
            if total_consumed >= total_target {
17,132✔
1781
                break;
17,132✔
UNCOV
1782
            }
×
1783
        }
1784

1785
        if total_consumed < total_target {
17,132✔
1786
            warn!("Consumed total {total_consumed} is less than intended spend: {total_target}");
×
1787
            return false;
×
1788
        }
17,132✔
1789

1790
        // Append the change output
1791
        let value = total_consumed - tx_cost;
17,132✔
1792
        debug!(
17,132✔
1793
            "Payments value: {value:?}, total_consumed: {total_consumed:?}, total_spent: {total_target:?}"
1794
        );
1795
        if value >= DUST_UTXO_LIMIT {
17,132✔
1796
            let change_output = if self.config.miner.segwit && epoch_id >= StacksEpochId::Epoch21 {
17,123✔
1797
                // p2wpkh
1798
                public_key.set_compressed(true);
×
1799
                let change_address_hash = Hash160::from_data(&public_key.to_bytes());
×
1800
                SegwitBitcoinAddress::to_p2wpkh_tx_out(&change_address_hash.0, value)
×
1801
            } else {
1802
                // p2pkh
1803
                let change_address_hash = Hash160::from_data(&public_key.to_bytes());
17,123✔
1804
                LegacyBitcoinAddress::to_p2pkh_tx_out(&change_address_hash, value)
17,123✔
1805
            };
1806
            tx.output.push(change_output);
17,123✔
1807
        } else {
1808
            // Instead of leaving that change to the BTC miner, we could / should bump the sortition fee
1809
            debug!("Not enough change to clear dust limit. Not adding change address.");
9✔
1810
        }
1811

1812
        for utxo in utxos_set.utxos.iter() {
17,132✔
1813
            let input = TxIn {
17,132✔
1814
                previous_output: OutPoint {
17,132✔
1815
                    txid: utxo.txid.clone(),
17,132✔
1816
                    vout: utxo.vout,
17,132✔
1817
                },
17,132✔
1818
                script_sig: Script::new(),
17,132✔
1819
                sequence: 0xFFFFFFFD, // allow RBF
17,132✔
1820
                witness: vec![],
17,132✔
1821
            };
17,132✔
1822
            tx.input.push(input);
17,132✔
1823
        }
17,132✔
1824
        for (i, utxo) in utxos_set.utxos.iter().enumerate() {
17,132✔
1825
            let script_pub_key = utxo.script_pub_key.clone();
17,132✔
1826
            let sig_hash_all = 0x01;
17,132✔
1827

1828
            let (sig_hash, is_segwit) = if script_pub_key.as_bytes().len() == 22
17,132✔
1829
                && script_pub_key.as_bytes()[0..2] == [0x00, 0x14]
×
1830
            {
1831
                // p2wpkh
1832
                (
×
1833
                    tx.segwit_signature_hash(i, &script_pub_key, utxo.amount, sig_hash_all),
×
1834
                    true,
×
1835
                )
×
1836
            } else {
1837
                // p2pkh
1838
                (tx.signature_hash(i, &script_pub_key, sig_hash_all), false)
17,132✔
1839
            };
1840

1841
            let sig1_der = {
17,132✔
1842
                let message = signer
17,132✔
1843
                    .sign_message(sig_hash.as_bytes())
17,132✔
1844
                    .expect("Unable to sign message");
17,132✔
1845
                message
17,132✔
1846
                    .to_secp256k1_recoverable()
17,132✔
1847
                    .expect("Unable to get recoverable signature")
17,132✔
1848
                    .to_standard()
17,132✔
1849
                    .serialize_der()
17,132✔
1850
            };
1851

1852
            if is_segwit {
17,132✔
1853
                // segwit
×
1854
                public_key.set_compressed(true);
×
1855
                tx.input[i].script_sig = Script::from(vec![]);
×
1856
                tx.input[i].witness = vec![
×
1857
                    [&*sig1_der, &[sig_hash_all as u8][..]].concat().to_vec(),
×
1858
                    public_key.to_bytes(),
×
1859
                ];
×
1860
            } else {
17,132✔
1861
                // legacy scriptSig
17,132✔
1862
                tx.input[i].script_sig = Builder::new()
17,132✔
1863
                    .push_slice(&[&*sig1_der, &[sig_hash_all as u8][..]].concat())
17,132✔
1864
                    .push_slice(&public_key.to_bytes())
17,132✔
1865
                    .into_script();
17,132✔
1866
                tx.input[i].witness.clear();
17,132✔
1867
            }
17,132✔
1868
        }
1869
        true
17,132✔
1870
    }
17,132✔
1871

1872
    /// Broadcast a signed raw [`Transaction`] to the underlying Bitcoin node.
1873
    ///
1874
    /// The transaction is submitted with following parameters:
1875
    /// - `max_fee_rate = 0.0` (uncapped, accept any fee rate),
1876
    /// - `max_burn_amount = 1_000_000` (in sats).
1877
    ///
1878
    /// # Arguments
1879
    /// * `transaction` - A fully signed raw [`Transaction`] to broadcast.
1880
    ///
1881
    /// # Returns
1882
    /// On success, returns the [`Txid`] of the broadcasted transaction.
1883
    pub fn send_transaction(&self, tx: &Transaction) -> Result<Txid, BurnchainControllerError> {
8,556✔
1884
        debug!(
8,556✔
1885
            "Sending raw transaction: {}",
1886
            serialize_hex(tx).unwrap_or("SERIALIZATION FAILED".to_string())
×
1887
        );
1888

1889
        const UNCAPPED_FEE: f64 = 0.0;
1890
        const MAX_BURN_AMOUNT: u64 = 1_000_000;
1891
        self.get_rpc_client()
8,556✔
1892
            .send_raw_transaction(tx, Some(UNCAPPED_FEE), Some(MAX_BURN_AMOUNT))
8,556✔
1893
            .map(|txid| {
8,556✔
1894
                debug!("Transaction {txid} sent successfully");
8,554✔
1895
                txid
8,554✔
1896
            })
8,554✔
1897
            .map_err(|e| {
8,556✔
1898
                error!("Bitcoin RPC error: transaction submission failed - {e:?}");
2✔
1899
                BurnchainControllerError::TransactionSubmissionFailed(format!("{e:?}"))
2✔
1900
            })
2✔
1901
    }
8,556✔
1902

1903
    /// wait until the ChainsCoordinator has processed sortitions up to
1904
    /// height_to_wait
1905
    pub fn wait_for_sortitions(
375,447✔
1906
        &self,
375,447✔
1907
        coord_comms: CoordinatorChannels,
375,447✔
1908
        height_to_wait: u64,
375,447✔
1909
    ) -> Result<BurnchainTip, BurnchainControllerError> {
375,447✔
1910
        let mut debug_ctr = 0;
375,447✔
1911
        loop {
1912
            let canonical_sortition_tip =
382,473✔
1913
                SortitionDB::get_canonical_burn_chain_tip(self.sortdb_ref().conn()).unwrap();
382,473✔
1914

1915
            if debug_ctr % 10 == 0 {
382,473✔
1916
                debug!(
375,476✔
1917
                    "Waiting until canonical sortition height reaches {height_to_wait} (currently {})",
1918
                    canonical_sortition_tip.block_height
1919
                );
1920
            }
6,997✔
1921
            debug_ctr += 1;
382,473✔
1922

1923
            if canonical_sortition_tip.block_height >= height_to_wait {
382,473✔
1924
                let (_, state_transition) = self
375,446✔
1925
                    .sortdb_ref()
375,446✔
1926
                    .get_sortition_result(&canonical_sortition_tip.sortition_id)
375,446✔
1927
                    .expect("Sortition DB error.")
375,446✔
1928
                    .expect("BUG: no data for the canonical chain tip");
375,446✔
1929

1930
                return Ok(BurnchainTip {
375,446✔
1931
                    block_snapshot: canonical_sortition_tip,
375,446✔
1932
                    received_at: Instant::now(),
375,446✔
1933
                    state_transition,
375,446✔
1934
                });
375,446✔
1935
            }
7,027✔
1936

1937
            if !self.should_keep_running() {
7,027✔
1938
                return Err(BurnchainControllerError::CoordinatorClosed);
1✔
1939
            }
7,026✔
1940

1941
            // help the chains coordinator along
1942
            coord_comms.announce_new_burn_block();
7,026✔
1943
            coord_comms.announce_new_stacks_block();
7,026✔
1944

1945
            // yield some time
1946
            sleep_ms(1000);
7,026✔
1947
        }
1948
    }
375,447✔
1949

1950
    /// Instruct a regtest Bitcoin node to build the next block.
1951
    pub fn build_next_block(&self, num_blocks: u64) {
8,256✔
1952
        debug!("Generate {num_blocks} block(s)");
8,256✔
1953
        let public_key_bytes = match &self.config.burnchain.local_mining_public_key {
8,256✔
1954
            Some(public_key) => hex_bytes(public_key).expect("Invalid byte sequence"),
8,256✔
1955
            None => panic!("Unable to make new block, mining public key"),
×
1956
        };
1957

1958
        // NOTE: miner address is whatever the configured segwit setting is
1959
        let public_key = Secp256k1PublicKey::from_slice(&public_key_bytes)
8,256✔
1960
            .expect("FATAL: invalid public key bytes");
8,256✔
1961
        let address = self.get_miner_address(StacksEpochId::Epoch21, &public_key);
8,256✔
1962

1963
        let result = self
8,256✔
1964
            .get_rpc_client()
8,256✔
1965
            .generate_to_address(num_blocks, &address);
8,256✔
1966
        /*
1967
            Temporary: not using `BitcoinRpcClientResultExt::ok_or_log_panic` (test code related),
1968
            because we need this logic available outside `#[cfg(test)]` due to Helium network.
1969

1970
            After the Helium cleanup (https://github.com/stacks-network/stacks-core/issues/6408),
1971
            we can:
1972
              - move `build_next_block` behind `#[cfg(test)]`
1973
              - simplify this match by using `ok_or_log_panic`.
1974
        */
1975
        match result {
8,256✔
1976
            Ok(_) => {}
8,256✔
1977
            Err(e) => {
×
1978
                error!("Bitcoin RPC failure: error generating block {e:?}");
×
1979
                panic!();
×
1980
            }
1981
        }
1982
    }
8,256✔
1983

1984
    /// Instruct a regtest Bitcoin node to build an empty block.
1985
    #[cfg(test)]
1986
    pub fn build_empty_block(&self) {
4✔
1987
        info!("Generate empty block");
4✔
1988
        let public_key_bytes = match &self.config.burnchain.local_mining_public_key {
4✔
1989
            Some(public_key) => hex_bytes(public_key).expect("Invalid byte sequence"),
4✔
1990
            None => panic!("Unable to make new block, mining public key"),
×
1991
        };
1992

1993
        // NOTE: miner address is whatever the configured segwit setting is
1994
        let public_key = Secp256k1PublicKey::from_slice(&public_key_bytes)
4✔
1995
            .expect("FATAL: invalid public key bytes");
4✔
1996
        let address = self.get_miner_address(StacksEpochId::Epoch21, &public_key);
4✔
1997

1998
        self.get_rpc_client()
4✔
1999
            .generate_block(&address, &[])
4✔
2000
            .ok_or_log_panic("generating block")
4✔
2001
    }
4✔
2002

2003
    /// Invalidate a block given its hash as a [`BurnchainHeaderHash`].
2004
    #[cfg(test)]
2005
    pub fn invalidate_block(&self, block: &BurnchainHeaderHash) {
30✔
2006
        info!("Invalidating block {block}");
30✔
2007
        self.get_rpc_client()
30✔
2008
            .invalidate_block(block)
30✔
2009
            .ok_or_log_panic("invalidate block")
30✔
2010
    }
30✔
2011

2012
    /// Retrieve the hash (as a [`BurnchainHeaderHash`]) of the block at the given height.
2013
    #[cfg(test)]
2014
    pub fn get_block_hash(&self, height: u64) -> BurnchainHeaderHash {
33✔
2015
        self.get_rpc_client()
33✔
2016
            .get_block_hash(height)
33✔
2017
            .unwrap_or_log_panic("retrieve block")
33✔
2018
    }
33✔
2019

2020
    #[cfg(test)]
2021
    pub fn get_mining_pubkey(&self) -> Option<String> {
4✔
2022
        self.config.burnchain.local_mining_public_key.clone()
4✔
2023
    }
4✔
2024

2025
    #[cfg(test)]
2026
    pub fn set_mining_pubkey(&mut self, pubkey: String) -> Option<String> {
×
2027
        let old_key = self.config.burnchain.local_mining_public_key.take();
×
2028
        self.config.burnchain.local_mining_public_key = Some(pubkey);
×
2029
        old_key
×
2030
    }
×
2031

2032
    #[cfg(test)]
2033
    pub fn set_use_segwit(&mut self, segwit: bool) {
×
2034
        self.config.miner.segwit = segwit;
×
2035
    }
×
2036

2037
    // TODO: add tests from mutation testing results #4866
2038
    #[cfg_attr(test, mutants::skip)]
2039
    fn make_operation_tx(
25,331✔
2040
        &mut self,
25,331✔
2041
        epoch_id: StacksEpochId,
25,331✔
2042
        operation: BlockstackOperationType,
25,331✔
2043
        op_signer: &mut BurnchainOpSigner,
25,331✔
2044
    ) -> Result<Transaction, BurnchainControllerError> {
25,331✔
2045
        match operation {
25,331✔
2046
            BlockstackOperationType::LeaderBlockCommit(payload) => {
25,042✔
2047
                self.build_leader_block_commit_tx(epoch_id, payload, op_signer)
25,042✔
2048
            }
2049
            BlockstackOperationType::LeaderKeyRegister(payload) => {
265✔
2050
                self.build_leader_key_register_tx(epoch_id, payload, op_signer)
265✔
2051
            }
2052
            BlockstackOperationType::PreStx(payload) => {
13✔
2053
                self.build_pre_stacks_tx(epoch_id, payload, op_signer)
13✔
2054
            }
2055
            BlockstackOperationType::TransferStx(payload) => {
3✔
2056
                self.build_transfer_stacks_tx(epoch_id, payload, op_signer, None)
3✔
2057
            }
2058
            BlockstackOperationType::StackStx(_payload) => {
4✔
2059
                self.build_stack_stx_tx(epoch_id, _payload, op_signer, None)
4✔
2060
            }
2061
            BlockstackOperationType::DelegateStx(payload) => {
2✔
2062
                self.build_delegate_stacks_tx(epoch_id, payload, op_signer, None)
2✔
2063
            }
2064
            BlockstackOperationType::VoteForAggregateKey(payload) => {
2✔
2065
                self.build_vote_for_aggregate_key_tx(epoch_id, payload, op_signer, None)
2✔
2066
            }
2067
        }
2068
    }
25,331✔
2069

2070
    /// Retrieves a raw [`Transaction`] by its [`Txid`]
2071
    #[cfg(test)]
2072
    pub fn get_raw_transaction(&self, txid: &Txid) -> Transaction {
14✔
2073
        self.get_rpc_client()
14✔
2074
            .get_raw_transaction(txid)
14✔
2075
            .unwrap_or_log_panic("retrieve raw tx")
14✔
2076
    }
14✔
2077

2078
    /// Produce `num_blocks` regtest bitcoin blocks, sending the bitcoin coinbase rewards
2079
    ///  to the bitcoin single sig addresses corresponding to `pks` in a round robin fashion.
2080
    #[cfg(test)]
2081
    pub fn bootstrap_chain_to_pks(&self, num_blocks: u64, pks: &[Secp256k1PublicKey]) {
260✔
2082
        info!("Creating wallet if it does not exist");
260✔
2083
        if let Err(e) = self.create_wallet_if_dne() {
260✔
2084
            error!("Error when creating wallet: {e:?}");
×
2085
        }
260✔
2086

2087
        for pk in pks {
299✔
2088
            debug!("Import public key '{}'", &pk.to_hex());
299✔
2089
            if let Err(e) = self.import_public_key(pk) {
299✔
2090
                warn!("Error when importing pubkey: {e:?}");
×
2091
            }
299✔
2092
        }
2093

2094
        if pks.len() == 1 {
260✔
2095
            // if we only have one pubkey, just generate all the blocks at once
2096
            let address = self.get_miner_address(StacksEpochId::Epoch21, &pks[0]);
221✔
2097
            debug!(
221✔
2098
                "Generate to address '{address}' for public key '{}'",
2099
                &pks[0].to_hex()
×
2100
            );
2101
            self.get_rpc_client()
221✔
2102
                .generate_to_address(num_blocks, &address)
221✔
2103
                .ok_or_log_panic("generating block");
221✔
2104
            return;
221✔
2105
        }
39✔
2106

2107
        // otherwise, round robin generate blocks
2108
        let num_blocks = num_blocks as usize;
39✔
2109
        for i in 0..num_blocks {
7,471✔
2110
            let pk = &pks[i % pks.len()];
7,471✔
2111
            let address = self.get_miner_address(StacksEpochId::Epoch21, pk);
7,471✔
2112
            if i < pks.len() {
7,471✔
2113
                debug!(
78✔
2114
                    "Generate to address '{}' for public key '{}'",
2115
                    address.to_string(),
×
2116
                    &pk.to_hex(),
×
2117
                );
2118
            }
7,393✔
2119
            self.get_rpc_client()
7,471✔
2120
                .generate_to_address(1, &address)
7,471✔
2121
                .ok_or_log_panic("generating block");
7,471✔
2122
        }
2123
    }
260✔
2124

2125
    /// Checks whether a transaction has been confirmed by the burnchain
2126
    ///
2127
    /// # Arguments
2128
    ///
2129
    /// * `txid` - The transaction ID to check (in big-endian order)
2130
    ///
2131
    /// # Returns
2132
    ///
2133
    /// * `true` if the transaction is confirmed (has at least one confirmation).
2134
    /// * `false` if the transaction is unconfirmed or could not be found.
2135
    pub fn is_transaction_confirmed(&self, txid: &Txid) -> bool {
16,478✔
2136
        match self
16,478✔
2137
            .get_rpc_client()
16,478✔
2138
            .get_transaction(self.get_wallet_name(), txid)
16,478✔
2139
        {
2140
            Ok(info) => info.confirmations > 0,
15,888✔
2141
            Err(e) => {
590✔
2142
                error!("Bitcoin RPC failure: checking tx confirmation {e:?}");
590✔
2143
                false
590✔
2144
            }
2145
        }
2146
    }
16,478✔
2147

2148
    /// Returns the configured wallet name from [`Config`].
2149
    fn get_wallet_name(&self) -> &String {
26,423✔
2150
        &self.config.burnchain.wallet_name
26,423✔
2151
    }
26,423✔
2152

2153
    /// Imports a public key into configured wallet by registering its
2154
    /// corresponding addresses as descriptors.
2155
    ///
2156
    /// This computes both **legacy (P2PKH)** and, if the miner is configured
2157
    /// with `segwit` enabled, also **SegWit (P2WPKH)** addresses, then imports
2158
    /// the related descriptors into the wallet.
2159
    pub fn import_public_key(
360✔
2160
        &self,
360✔
2161
        public_key: &Secp256k1PublicKey,
360✔
2162
    ) -> BitcoinRegtestControllerResult<()> {
360✔
2163
        let pkh = Hash160::from_data(&public_key.to_bytes())
360✔
2164
            .to_bytes()
360✔
2165
            .to_vec();
360✔
2166
        let (_, network_id) = self.config.burnchain.get_bitcoin_network();
360✔
2167

2168
        // import both the legacy and segwit variants of this public key
2169
        let mut addresses = vec![BitcoinAddress::from_bytes_legacy(
360✔
2170
            network_id,
360✔
2171
            LegacyBitcoinAddressType::PublicKeyHash,
360✔
2172
            &pkh,
360✔
2173
        )
2174
        .map_err(BitcoinRegtestControllerError::InvalidPublicKey)?];
360✔
2175

2176
        if self.config.miner.segwit {
360✔
2177
            addresses.push(
1✔
2178
                BitcoinAddress::from_bytes_segwit_p2wpkh(network_id, &pkh)
1✔
2179
                    .map_err(BitcoinRegtestControllerError::InvalidPublicKey)?,
1✔
2180
            );
2181
        }
359✔
2182

2183
        for address in addresses.into_iter() {
361✔
2184
            debug!(
361✔
2185
                "Import address {address} for public key {}",
2186
                public_key.to_hex()
×
2187
            );
2188

2189
            let descriptor = format!("addr({address})");
361✔
2190
            let info = self.get_rpc_client().get_descriptor_info(&descriptor)?;
361✔
2191

2192
            let descr_req = ImportDescriptorsRequest {
361✔
2193
                descriptor: format!("addr({address})#{}", info.checksum),
361✔
2194
                timestamp: Timestamp::Time(0),
361✔
2195
                internal: Some(true),
361✔
2196
            };
361✔
2197

2198
            self.get_rpc_client()
361✔
2199
                .import_descriptors(self.get_wallet_name(), &[&descr_req])?;
361✔
2200
        }
2201
        Ok(())
360✔
2202
    }
360✔
2203

2204
    /// Returns a copy of the given public key adjusted to the current epoch rules.
2205
    ///
2206
    /// In particular:
2207
    /// - For epochs **before** [`StacksEpochId::Epoch21`], the public key is returned
2208
    ///   unchanged.
2209
    /// - Starting with [`StacksEpochId::Epoch21`], if **SegWit** is enabled in the miner
2210
    ///   configuration, the key is forced into compressed form.
2211
    ///
2212
    /// # Arguments
2213
    /// * `epoch_id` — The epoch identifier to check against protocol upgrade rules.
2214
    /// * `public_key` — The original public key to adjust.
2215
    ///
2216
    /// # Returns
2217
    /// A [`Secp256k1PublicKey`] that is either the same as the input or compressed,
2218
    /// depending on the epoch and miner configuration.
2219
    fn to_epoch_aware_pubkey(
8,791✔
2220
        &self,
8,791✔
2221
        epoch_id: StacksEpochId,
8,791✔
2222
        public_key: &Secp256k1PublicKey,
8,791✔
2223
    ) -> Secp256k1PublicKey {
8,791✔
2224
        let mut reviewed = public_key.clone();
8,791✔
2225
        if self.config.miner.segwit && epoch_id >= StacksEpochId::Epoch21 {
8,791✔
UNCOV
2226
            reviewed.set_compressed(true);
×
2227
        }
8,791✔
2228
        return reviewed;
8,791✔
2229
    }
8,791✔
2230

2231
    /// Retrieves the set of UTXOs for a given address at a specific block height.
2232
    ///
2233
    /// This method queries all unspent outputs belonging to the provided address:
2234
    /// 1. Using a confirmation window of `0..=9_999_999` for the RPC call.
2235
    /// 2. Filtering out UTXOs that:
2236
    ///    - Are present in the optional exclusion set (matched by transaction ID).
2237
    ///    - Have an amount below the specified `minimum_sum_amount`.
2238
    ///
2239
    /// Note: The `block_height` is only used to retrieve the corresponding block hash
2240
    /// and does not affect which UTXOs are included in the result.
2241
    ///
2242
    /// # Arguments
2243
    /// - `address`: The Bitcoin address whose UTXOs should be retrieved.
2244
    /// - `include_unsafe`: Whether to include unsafe UTXOs.
2245
    /// - `minimum_sum_amount`: Minimum amount (in satoshis) that a UTXO must have to be included in the final set.
2246
    /// - `utxos_to_exclude`: Optional set of UTXOs to exclude from the final result.
2247
    /// - `block_height`: The block height at which to resolve the block hash used in the result.
2248
    ///
2249
    /// # Returns
2250
    /// A [`UTXOSet`] containing the filtered UTXOs and the block hash corresponding to `block_height`.
2251
    fn retrieve_utxo_set(
8,821✔
2252
        &self,
8,821✔
2253
        address: &BitcoinAddress,
8,821✔
2254
        include_unsafe: bool,
8,821✔
2255
        minimum_sum_amount: u64,
8,821✔
2256
        utxos_to_exclude: &Option<UTXOSet>,
8,821✔
2257
        block_height: u64,
8,821✔
2258
    ) -> BitcoinRpcClientResult<UTXOSet> {
8,821✔
2259
        let bhh = self.get_rpc_client().get_block_hash(block_height)?;
8,821✔
2260

2261
        const MIN_CONFIRMATIONS: u64 = 0;
2262
        const MAX_CONFIRMATIONS: u64 = 9_999_999;
2263
        let unspents = self.get_rpc_client().list_unspent(
8,821✔
2264
            &self.get_wallet_name(),
8,821✔
2265
            Some(MIN_CONFIRMATIONS),
8,821✔
2266
            Some(MAX_CONFIRMATIONS),
8,821✔
2267
            Some(&[address]),
8,821✔
2268
            Some(include_unsafe),
8,821✔
2269
            Some(minimum_sum_amount),
8,821✔
2270
            self.config.burnchain.max_unspent_utxos.clone(),
8,821✔
2271
        )?;
×
2272

2273
        let txids_to_exclude = utxos_to_exclude.as_ref().map_or_else(HashSet::new, |set| {
8,821✔
2274
            set.utxos
4✔
2275
                .iter()
4✔
2276
                .map(|utxo| Txid::from_bitcoin_tx_hash(&utxo.txid))
92✔
2277
                .collect()
4✔
2278
        });
4✔
2279

2280
        let utxos = unspents
8,821✔
2281
            .into_iter()
8,821✔
2282
            .filter(|each| !txids_to_exclude.contains(&each.txid))
921,884✔
2283
            .filter(|each| each.amount >= minimum_sum_amount)
921,794✔
2284
            .map(|each| UTXO {
8,821✔
2285
                txid: Txid::to_bitcoin_tx_hash(&each.txid),
921,767✔
2286
                vout: each.vout,
921,767✔
2287
                script_pub_key: each.script_pub_key,
921,767✔
2288
                amount: each.amount,
921,767✔
2289
                confirmations: each.confirmations,
921,767✔
2290
            })
921,767✔
2291
            .collect::<Vec<_>>();
8,821✔
2292
        Ok(UTXOSet { bhh, utxos })
8,821✔
2293
    }
8,821✔
2294
}
2295

2296
impl BurnchainController for BitcoinRegtestController {
2297
    fn sortdb_ref(&self) -> &SortitionDB {
1,936,720✔
2298
        self.db
1,936,720✔
2299
            .as_ref()
1,936,720✔
2300
            .expect("BUG: did not instantiate the burn DB")
1,936,720✔
2301
    }
1,936,720✔
2302

2303
    fn sortdb_mut(&mut self) -> &mut SortitionDB {
475,432✔
2304
        let burnchain = self.get_burnchain();
475,432✔
2305

2306
        let (db, burnchain_db) = burnchain.open_db(true).unwrap();
475,432✔
2307
        self.db = Some(db);
475,432✔
2308
        self.burnchain_db = Some(burnchain_db);
475,432✔
2309

2310
        match self.db {
475,432✔
2311
            Some(ref mut sortdb) => sortdb,
475,432✔
2312
            None => unreachable!(),
×
2313
        }
2314
    }
475,432✔
2315

2316
    fn get_chain_tip(&self) -> BurnchainTip {
×
2317
        match &self.chain_tip {
×
2318
            Some(chain_tip) => chain_tip.clone(),
×
2319
            None => {
2320
                unreachable!();
×
2321
            }
2322
        }
2323
    }
×
2324

2325
    fn get_headers_height(&self) -> u64 {
375,734✔
2326
        let (_, network_id) = self.config.burnchain.get_bitcoin_network();
375,734✔
2327
        let spv_client = SpvClient::new(
375,734✔
2328
            &self.config.get_spv_headers_file_path(),
375,734✔
2329
            0,
2330
            None,
375,734✔
2331
            network_id,
375,734✔
2332
            false,
2333
            false,
2334
        )
2335
        .expect("Unable to open burnchain headers DB");
375,734✔
2336
        spv_client
375,734✔
2337
            .get_headers_height()
375,734✔
2338
            .expect("Unable to query number of burnchain headers")
375,734✔
2339
    }
375,734✔
2340

2341
    fn connect_dbs(&mut self) -> Result<(), BurnchainControllerError> {
525✔
2342
        let burnchain = self.get_burnchain();
525✔
2343
        burnchain.connect_db(
525✔
2344
            true,
2345
            &self.indexer.get_first_block_header_hash()?,
525✔
2346
            self.indexer.get_first_block_header_timestamp()?,
525✔
2347
            self.indexer.get_stacks_epochs(),
525✔
2348
        )?;
×
2349
        Ok(())
525✔
2350
    }
525✔
2351

2352
    fn get_stacks_epochs(&self) -> EpochList {
518✔
2353
        self.indexer.get_stacks_epochs()
518✔
2354
    }
518✔
2355

2356
    fn start(
518✔
2357
        &mut self,
518✔
2358
        target_block_height_opt: Option<u64>,
518✔
2359
    ) -> Result<(BurnchainTip, u64), BurnchainControllerError> {
518✔
2360
        // if no target block height is given, just fetch the first burnchain block.
2361
        self.receive_blocks(false, target_block_height_opt.map_or_else(|| Some(1), Some))
518✔
2362
    }
518✔
2363

2364
    fn sync(
374,738✔
2365
        &mut self,
374,738✔
2366
        target_block_height_opt: Option<u64>,
374,738✔
2367
    ) -> Result<(BurnchainTip, u64), BurnchainControllerError> {
374,738✔
2368
        let (burnchain_tip, burnchain_height) = if self.config.burnchain.mode == "helium" {
374,738✔
2369
            // Helium: this node is responsible for mining new burnchain blocks
2370
            self.build_next_block(1);
×
2371
            self.receive_blocks(true, None)?
×
2372
        } else {
2373
            // Neon: this node is waiting on a block to be produced
2374
            self.receive_blocks(true, target_block_height_opt)?
374,738✔
2375
        };
2376

2377
        // Evaluate process_exit_at_block_height setting
2378
        if let Some(cap) = self.config.burnchain.process_exit_at_block_height {
374,690✔
2379
            if burnchain_tip.block_snapshot.block_height >= cap {
37✔
2380
                info!("Node succesfully reached the end of the ongoing {cap} blocks epoch!");
×
2381
                info!("This process will automatically terminate in 30s, restart your node for participating in the next epoch.");
×
2382
                sleep_ms(30000);
×
2383
                std::process::exit(0);
×
2384
            }
37✔
2385
        }
374,653✔
2386
        Ok((burnchain_tip, burnchain_height))
374,690✔
2387
    }
374,738✔
2388

2389
    /// Build and send a burnchain operation transaction.
2390
    /// Returns the [`Txid`] on success, [`BurnchainControllerError`] otherwise.
2391
    /// On [`BitcoinRegtestController::send_transaction`] failure for block commits,
2392
    /// clears `ongoing_block_commit` so the commit can be resubmitted.
2393
    fn submit_operation(
25,328✔
2394
        &mut self,
25,328✔
2395
        epoch_id: StacksEpochId,
25,328✔
2396
        operation: BlockstackOperationType,
25,328✔
2397
        op_signer: &mut BurnchainOpSigner,
25,328✔
2398
    ) -> Result<Txid, BurnchainControllerError> {
25,328✔
2399
        let is_block_commit = matches!(operation, BlockstackOperationType::LeaderBlockCommit(_));
25,328✔
2400
        let transaction = self.make_operation_tx(epoch_id, operation, op_signer)?;
25,328✔
2401
        self.send_transaction(&transaction).inspect_err(|_| {
8,552✔
2402
            if is_block_commit {
2✔
2403
                self.ongoing_block_commit = None;
2✔
2404
            }
2✔
2405
        })
2✔
2406
    }
25,328✔
2407

2408
    #[cfg(test)]
2409
    fn bootstrap_chain(&self, num_blocks: u64) {
122✔
2410
        let Some(ref local_mining_pubkey) = &self.config.burnchain.local_mining_public_key else {
122✔
2411
            warn!("No local mining pubkey while bootstrapping bitcoin regtest, will not generate bitcoin blocks");
1✔
2412
            return;
1✔
2413
        };
2414

2415
        // NOTE: miner address is whatever the miner's segwit setting says it is here
2416
        let mut local_mining_pubkey = Secp256k1PublicKey::from_hex(local_mining_pubkey).unwrap();
121✔
2417

2418
        if self.config.miner.segwit {
121✔
UNCOV
2419
            local_mining_pubkey.set_compressed(true);
×
2420
        }
121✔
2421

2422
        self.bootstrap_chain_to_pks(num_blocks, &[local_mining_pubkey])
121✔
2423
    }
122✔
2424
}
2425

2426
#[derive(Debug, Clone)]
2427
pub struct UTXOSet {
2428
    bhh: BurnchainHeaderHash,
2429
    utxos: Vec<UTXO>,
2430
}
2431

2432
impl UTXOSet {
2433
    pub fn is_empty(&self) -> bool {
8,784✔
2434
        self.utxos.len() == 0
8,784✔
2435
    }
8,784✔
2436

2437
    pub fn total_available(&self) -> u64 {
8,749✔
2438
        self.utxos.iter().map(|o| o.amount).sum()
8,749✔
2439
    }
8,749✔
2440

2441
    pub fn num_utxos(&self) -> usize {
6✔
2442
        self.utxos.len()
6✔
2443
    }
6✔
2444
}
2445

2446
#[derive(Clone, Debug, PartialEq)]
2447
pub struct UTXO {
2448
    pub txid: Sha256dHash,
2449
    pub vout: u32,
2450
    pub script_pub_key: Script,
2451
    pub amount: u64,
2452
    pub confirmations: u32,
2453
}
2454

2455
#[cfg(test)]
2456
mod tests {
2457
    use std::env::{self, temp_dir};
2458
    use std::fs::File;
2459
    use std::io::Write;
2460
    use std::panic::{self, AssertUnwindSafe};
2461

2462
    use stacks::burnchains::BurnchainSigner;
2463
    use stacks::config::DEFAULT_SATS_PER_VB;
2464
    use stacks_common::deps_common::bitcoin::blockdata::script::Builder;
2465
    use stacks_common::types::chainstate::{BlockHeaderHash, StacksAddress, VRFSeed};
2466
    use stacks_common::util::hash::to_hex;
2467
    use stacks_common::util::secp256k1::Secp256k1PrivateKey;
2468

2469
    use super::*;
2470
    use crate::burnchains::bitcoin::core_controller::BitcoinCoreController;
2471
    use crate::burnchains::bitcoin_regtest_controller::tests::utils::{
2472
        create_follower_config, create_miner_config, to_address_legacy,
2473
    };
2474
    use crate::Keychain;
2475

2476
    mod utils {
2477
        use std::net::TcpListener;
2478

2479
        use stacks::burnchains::MagicBytes;
2480
        use stacks::chainstate::burn::ConsensusHash;
2481
        use stacks::util::vrf::{VRFPrivateKey, VRFPublicKey};
2482

2483
        use super::*;
2484
        use crate::burnchains::bitcoin::core_controller::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
2485
        use crate::util::get_epoch_time_nanos;
2486

2487
        pub fn create_miner_config() -> Config {
31✔
2488
            let mut config = Config::default();
31✔
2489
            config.node.miner = true;
31✔
2490
            config.burnchain.magic_bytes = "T3".as_bytes().into();
31✔
2491
            config.burnchain.username = Some(String::from("user"));
31✔
2492
            config.burnchain.password = Some(String::from("12345"));
31✔
2493
            // overriding default "0.0.0.0" because doesn't play nicely on Windows.
2494
            config.burnchain.peer_host = String::from("127.0.0.1");
31✔
2495
            // avoiding peer port biding to reduce the number of ports to bind to.
2496
            config.burnchain.peer_port = BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
31✔
2497

2498
            //Ask the OS for a free port. Not guaranteed to stay free,
2499
            //after TcpListner is dropped, but good enough for testing
2500
            //and starting bitcoind right after config is created
2501
            let tmp_listener =
31✔
2502
                TcpListener::bind("127.0.0.1:0").expect("Failed to bind to get a free port");
31✔
2503
            let port = tmp_listener.local_addr().unwrap().port();
31✔
2504

2505
            config.burnchain.rpc_port = port;
31✔
2506

2507
            let now = get_epoch_time_nanos();
31✔
2508
            let dir = format!("/tmp/regtest-ctrl-{port}-{now}");
31✔
2509
            config.node.working_dir = dir;
31✔
2510

2511
            config
31✔
2512
        }
31✔
2513

2514
        pub fn create_keychain() -> Keychain {
16✔
2515
            create_keychain_with_seed(1)
16✔
2516
        }
16✔
2517

2518
        pub fn create_keychain_with_seed(value: u8) -> Keychain {
31✔
2519
            let seed = vec![value; 4];
31✔
2520
            let keychain = Keychain::default(seed);
31✔
2521
            keychain
31✔
2522
        }
31✔
2523

2524
        pub fn create_miner1_pubkey() -> Secp256k1PublicKey {
13✔
2525
            create_keychain_with_seed(1).get_pub_key()
13✔
2526
        }
13✔
2527

2528
        pub fn create_miner2_pubkey() -> Secp256k1PublicKey {
2✔
2529
            create_keychain_with_seed(2).get_pub_key()
2✔
2530
        }
2✔
2531

2532
        pub fn to_address_legacy(pub_key: &Secp256k1PublicKey) -> BitcoinAddress {
3✔
2533
            let hash160 = Hash160::from_data(&pub_key.to_bytes());
3✔
2534
            BitcoinAddress::from_bytes_legacy(
3✔
2535
                BitcoinNetworkType::Regtest,
3✔
2536
                LegacyBitcoinAddressType::PublicKeyHash,
3✔
2537
                &hash160.0,
3✔
2538
            )
2539
            .expect("Public key incorrect")
3✔
2540
        }
3✔
2541

UNCOV
2542
        pub fn to_address_segwit_p2wpkh(pub_key: &Secp256k1PublicKey) -> BitcoinAddress {
×
2543
            // pub_key.to_byte_compressed() equivalent to pub_key.set_compressed(true) + pub_key.to_bytes()
UNCOV
2544
            let hash160 = Hash160::from_data(&pub_key.to_bytes_compressed());
×
UNCOV
2545
            BitcoinAddress::from_bytes_segwit_p2wpkh(BitcoinNetworkType::Regtest, &hash160.0)
×
UNCOV
2546
                .expect("Public key incorrect")
×
UNCOV
2547
        }
×
2548

2549
        pub fn mine_tx(btc_controller: &BitcoinRegtestController, tx: &Transaction) {
2✔
2550
            btc_controller
2✔
2551
                .send_transaction(tx)
2✔
2552
                .expect("Tx should be sent to the burnchain!");
2✔
2553
            btc_controller.build_next_block(1); // Now tx is confirmed
2✔
2554
        }
2✔
2555

2556
        pub fn create_templated_commit_op() -> LeaderBlockCommitOp {
8✔
2557
            LeaderBlockCommitOp {
8✔
2558
                block_header_hash: BlockHeaderHash::from_hex(
8✔
2559
                    "e88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32af",
8✔
2560
                )
8✔
2561
                .unwrap(),
8✔
2562
                new_seed: VRFSeed::from_hex(
8✔
2563
                    "d5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375",
8✔
2564
                )
8✔
2565
                .unwrap(),
8✔
2566
                parent_block_ptr: 2211, // 0x000008a3
8✔
2567
                parent_vtxindex: 1,     // 0x0001
8✔
2568
                key_block_ptr: 1432,    // 0x00000598
8✔
2569
                key_vtxindex: 1,        // 0x0001
8✔
2570
                memo: vec![11],         // 0x5a >> 3
8✔
2571

8✔
2572
                burn_fee: 110_000, //relevant for fee calculation when sending the tx
8✔
2573
                input: (Txid([0x00; 32]), 0),
8✔
2574
                burn_parent_modulus: 2, // 0x5a & 0b111
8✔
2575

8✔
2576
                apparent_sender: BurnchainSigner("mgbpit8FvkVJ9kuXY8QSM5P7eibnhcEMBk".to_string()),
8✔
2577
                commit_outs: vec![
8✔
2578
                    PoxAddress::Standard(StacksAddress::burn_address(false), None),
8✔
2579
                    PoxAddress::Standard(StacksAddress::burn_address(false), None),
8✔
2580
                ],
8✔
2581

8✔
2582
                treatment: vec![],
8✔
2583
                sunset_burn: 5_500, //relevant for fee calculation when sending the tx
8✔
2584

8✔
2585
                txid: Txid([0x00; 32]),
8✔
2586
                vtxindex: 0,
8✔
2587
                block_height: 2212,
8✔
2588
                burn_header_hash: BurnchainHeaderHash([0x01; 32]),
8✔
2589
            }
8✔
2590
        }
8✔
2591

2592
        pub fn txout_opreturn<T: StacksMessageCodec>(
5✔
2593
            op: &T,
5✔
2594
            magic: &MagicBytes,
5✔
2595
            value: u64,
5✔
2596
        ) -> TxOut {
5✔
2597
            let op_bytes = {
5✔
2598
                let mut buffer = vec![];
5✔
2599
                let mut magic_bytes = magic.as_bytes().to_vec();
5✔
2600
                buffer.append(&mut magic_bytes);
5✔
2601
                op.consensus_serialize(&mut buffer)
5✔
2602
                    .expect("FATAL: invalid operation");
5✔
2603
                buffer
5✔
2604
            };
2605

2606
            TxOut {
5✔
2607
                value,
5✔
2608
                script_pubkey: Builder::new()
5✔
2609
                    .push_opcode(opcodes::All::OP_RETURN)
5✔
2610
                    .push_slice(&op_bytes)
5✔
2611
                    .into_script(),
5✔
2612
            }
5✔
2613
        }
5✔
2614

2615
        pub fn txout_opdup_commit_to(addr: &PoxAddress, amount: u64) -> TxOut {
6✔
2616
            addr.to_bitcoin_tx_out(amount)
6✔
2617
        }
6✔
2618

2619
        pub fn txout_opdup_change_legacy(signer: &mut BurnchainOpSigner, amount: u64) -> TxOut {
5✔
2620
            let public_key = signer.get_public_key();
5✔
2621
            let change_address_hash = Hash160::from_data(&public_key.to_bytes());
5✔
2622
            LegacyBitcoinAddress::to_p2pkh_tx_out(&change_address_hash, amount)
5✔
2623
        }
5✔
2624

2625
        pub fn txin_at_index(
5✔
2626
            complete_tx: &Transaction,
5✔
2627
            signer: &BurnchainOpSigner,
5✔
2628
            utxos: &[UTXO],
5✔
2629
            index: usize,
5✔
2630
        ) -> TxIn {
5✔
2631
            //Refresh op signer
2632
            let mut signer = signer.undisposed();
5✔
2633
            let mut public_key = signer.get_public_key();
5✔
2634

2635
            let mut tx = Transaction {
5✔
2636
                version: complete_tx.version,
5✔
2637
                lock_time: complete_tx.lock_time,
5✔
2638
                input: vec![],
5✔
2639
                output: complete_tx.output.clone(),
5✔
2640
            };
5✔
2641

2642
            for utxo in utxos.iter() {
5✔
2643
                let input = TxIn {
5✔
2644
                    previous_output: OutPoint {
5✔
2645
                        txid: utxo.txid.clone(),
5✔
2646
                        vout: utxo.vout,
5✔
2647
                    },
5✔
2648
                    script_sig: Script::new(),
5✔
2649
                    sequence: 0xFFFFFFFD, // allow RBF
5✔
2650
                    witness: vec![],
5✔
2651
                };
5✔
2652
                tx.input.push(input);
5✔
2653
            }
5✔
2654

2655
            for (i, utxo) in utxos.iter().enumerate() {
5✔
2656
                let script_pub_key = utxo.script_pub_key.clone();
5✔
2657
                let sig_hash_all = 0x01;
5✔
2658

2659
                let (sig_hash, is_segwit) = if script_pub_key.as_bytes().len() == 22
5✔
UNCOV
2660
                    && script_pub_key.as_bytes()[0..2] == [0x00, 0x14]
×
2661
                {
2662
                    // p2wpkh
UNCOV
2663
                    (
×
UNCOV
2664
                        tx.segwit_signature_hash(i, &script_pub_key, utxo.amount, sig_hash_all),
×
UNCOV
2665
                        true,
×
UNCOV
2666
                    )
×
2667
                } else {
2668
                    // p2pkh
2669
                    (tx.signature_hash(i, &script_pub_key, sig_hash_all), false)
5✔
2670
                };
2671

2672
                let sig1_der = {
5✔
2673
                    let message = signer
5✔
2674
                        .sign_message(sig_hash.as_bytes())
5✔
2675
                        .expect("Unable to sign message");
5✔
2676
                    message
5✔
2677
                        .to_secp256k1_recoverable()
5✔
2678
                        .expect("Unable to get recoverable signature")
5✔
2679
                        .to_standard()
5✔
2680
                        .serialize_der()
5✔
2681
                };
2682

2683
                if is_segwit {
5✔
UNCOV
2684
                    // segwit
×
UNCOV
2685
                    public_key.set_compressed(true);
×
UNCOV
2686
                    tx.input[i].script_sig = Script::from(vec![]);
×
UNCOV
2687
                    tx.input[i].witness = vec![
×
UNCOV
2688
                        [&*sig1_der, &[sig_hash_all as u8][..]].concat().to_vec(),
×
UNCOV
2689
                        public_key.to_bytes(),
×
UNCOV
2690
                    ];
×
2691
                } else {
5✔
2692
                    // legacy scriptSig
5✔
2693
                    tx.input[i].script_sig = Builder::new()
5✔
2694
                        .push_slice(&[&*sig1_der, &[sig_hash_all as u8][..]].concat())
5✔
2695
                        .push_slice(&public_key.to_bytes())
5✔
2696
                        .into_script();
5✔
2697
                    tx.input[i].witness.clear();
5✔
2698
                }
5✔
2699
            }
2700

2701
            tx.input[index].clone()
5✔
2702
        }
5✔
2703

2704
        pub fn create_templated_leader_key_op() -> LeaderKeyRegisterOp {
4✔
2705
            LeaderKeyRegisterOp {
4✔
2706
                consensus_hash: ConsensusHash([0u8; 20]),
4✔
2707
                public_key: VRFPublicKey::from_private(
4✔
2708
                    &VRFPrivateKey::from_bytes(&[0u8; 32]).unwrap(),
4✔
2709
                ),
4✔
2710
                memo: vec![],
4✔
2711
                txid: Txid([3u8; 32]),
4✔
2712
                vtxindex: 0,
4✔
2713
                block_height: 1,
4✔
2714
                burn_header_hash: BurnchainHeaderHash([9u8; 32]),
4✔
2715
            }
4✔
2716
        }
4✔
2717

2718
        pub fn create_templated_pre_stx_op() -> PreStxOp {
4✔
2719
            PreStxOp {
4✔
2720
                output: StacksAddress::p2pkh_from_hash(false, Hash160::from_data(&[2u8; 20])),
4✔
2721
                txid: Txid([0u8; 32]),
4✔
2722
                vtxindex: 0,
4✔
2723
                block_height: 0,
4✔
2724
                burn_header_hash: BurnchainHeaderHash([0u8; 32]),
4✔
2725
            }
4✔
2726
        }
4✔
2727

UNCOV
2728
        pub fn create_follower_config() -> Config {
×
UNCOV
2729
            let mut config = Config::default();
×
UNCOV
2730
            config.node.miner = false;
×
UNCOV
2731
            config.burnchain.magic_bytes = "T3".as_bytes().into();
×
UNCOV
2732
            config.burnchain.username = None;
×
UNCOV
2733
            config.burnchain.password = None;
×
UNCOV
2734
            config.burnchain.peer_host = String::from("127.0.0.1");
×
UNCOV
2735
            config.burnchain.peer_port = 8333;
×
UNCOV
2736
            config.node.working_dir = format!("/tmp/follower");
×
UNCOV
2737
            config
×
UNCOV
2738
        }
×
2739
    }
2740

2741
    #[test]
UNCOV
2742
    fn test_get_satoshis_per_byte() {
×
UNCOV
2743
        let dir = temp_dir();
×
UNCOV
2744
        let file_path = dir.as_path().join("config.toml");
×
2745

UNCOV
2746
        let mut config = Config::default();
×
2747

UNCOV
2748
        let satoshis_per_byte = get_satoshis_per_byte(&config);
×
UNCOV
2749
        assert_eq!(satoshis_per_byte, DEFAULT_SATS_PER_VB);
×
2750

UNCOV
2751
        let mut file = File::create(&file_path).unwrap();
×
UNCOV
2752
        writeln!(file, "[burnchain]").unwrap();
×
UNCOV
2753
        writeln!(file, "satoshis_per_byte = 51").unwrap();
×
UNCOV
2754
        config.config_path = Some(file_path.to_str().unwrap().to_string());
×
2755

UNCOV
2756
        assert_eq!(get_satoshis_per_byte(&config), 51);
×
UNCOV
2757
    }
×
2758

2759
    /// Verify that we can build a valid Bitcoin transaction with multiple UTXOs.
2760
    /// Taken from production data.
2761
    /// Tests `serialize_tx()` and `send_block_commit_operation_at_burnchain_height()`
2762
    #[test]
UNCOV
2763
    fn test_multiple_inputs() {
×
UNCOV
2764
        let spend_utxos = vec![
×
UNCOV
2765
            UTXO {
×
UNCOV
2766
                txid: Sha256dHash::from_hex(
×
UNCOV
2767
                    "d3eafb3aba3cec925473550ed2e4d00bcb0d00744bb3212e4a8e72878909daee",
×
UNCOV
2768
                )
×
UNCOV
2769
                .unwrap(),
×
UNCOV
2770
                vout: 3,
×
UNCOV
2771
                script_pub_key: Builder::from(
×
UNCOV
2772
                    hex_bytes("76a9141dc27eba0247f8cc9575e7d45e50a0bc7e72427d88ac").unwrap(),
×
UNCOV
2773
                )
×
UNCOV
2774
                .into_script(),
×
UNCOV
2775
                amount: 42051,
×
UNCOV
2776
                confirmations: 1421,
×
UNCOV
2777
            },
×
UNCOV
2778
            UTXO {
×
UNCOV
2779
                txid: Sha256dHash::from_hex(
×
UNCOV
2780
                    "01132f2d4a98cc715624e033214c8d841098a1ee15b30188ab89589a320b3b24",
×
UNCOV
2781
                )
×
UNCOV
2782
                .unwrap(),
×
UNCOV
2783
                vout: 0,
×
UNCOV
2784
                script_pub_key: Builder::from(
×
UNCOV
2785
                    hex_bytes("76a9141dc27eba0247f8cc9575e7d45e50a0bc7e72427d88ac").unwrap(),
×
UNCOV
2786
                )
×
UNCOV
2787
                .into_script(),
×
UNCOV
2788
                amount: 326456,
×
UNCOV
2789
                confirmations: 1421,
×
UNCOV
2790
            },
×
2791
        ];
2792

2793
        // test serialize_tx()
UNCOV
2794
        let config = utils::create_miner_config();
×
2795

UNCOV
2796
        let mut btc_controller = BitcoinRegtestController::new(config, None);
×
UNCOV
2797
        let mut utxo_set = UTXOSet {
×
UNCOV
2798
            bhh: BurnchainHeaderHash([0x01; 32]),
×
UNCOV
2799
            utxos: spend_utxos.clone(),
×
UNCOV
2800
        };
×
UNCOV
2801
        let mut transaction = Transaction {
×
UNCOV
2802
            input: vec![],
×
UNCOV
2803
            output: vec![
×
UNCOV
2804
                TxOut {
×
UNCOV
2805
                    value: 0,
×
UNCOV
2806
                    script_pubkey: Builder::from(hex_bytes("6a4c5054335be88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32afd5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375000008a300010000059800015a").unwrap()).into_script(),
×
UNCOV
2807
                },
×
UNCOV
2808
                TxOut {
×
UNCOV
2809
                    value: 10000,
×
UNCOV
2810
                    script_pubkey: Builder::from(hex_bytes("76a914000000000000000000000000000000000000000088ac").unwrap()).into_script(),
×
UNCOV
2811
                },
×
UNCOV
2812
                TxOut {
×
UNCOV
2813
                    value: 10000,
×
UNCOV
2814
                    script_pubkey: Builder::from(hex_bytes("76a914000000000000000000000000000000000000000088ac").unwrap()).into_script(),
×
UNCOV
2815
                },
×
UNCOV
2816
            ],
×
UNCOV
2817
            version: 1,
×
UNCOV
2818
            lock_time: 0,
×
UNCOV
2819
        };
×
2820

UNCOV
2821
        let mut signer = BurnchainOpSigner::new(
×
UNCOV
2822
            Secp256k1PrivateKey::from_hex(
×
UNCOV
2823
                "9e446f6b0c6a96cf2190e54bcd5a8569c3e386f091605499464389b8d4e0bfc201",
×
2824
            )
UNCOV
2825
            .unwrap(),
×
2826
        );
UNCOV
2827
        assert!(btc_controller.serialize_tx(
×
UNCOV
2828
            StacksEpochId::Epoch25,
×
UNCOV
2829
            &mut transaction,
×
2830
            44950,
UNCOV
2831
            &mut utxo_set,
×
UNCOV
2832
            &mut signer,
×
2833
            true
2834
        ));
UNCOV
2835
        assert_eq!(transaction.output[3].value, 323557);
×
2836

2837
        // test send_block_commit_operation_at_burn_height()
UNCOV
2838
        let utxo_set = UTXOSet {
×
UNCOV
2839
            bhh: BurnchainHeaderHash([0x01; 32]),
×
UNCOV
2840
            utxos: spend_utxos,
×
UNCOV
2841
        };
×
2842

UNCOV
2843
        let commit_op = LeaderBlockCommitOp {
×
UNCOV
2844
            block_header_hash: BlockHeaderHash::from_hex(
×
UNCOV
2845
                "e88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32af",
×
UNCOV
2846
            )
×
UNCOV
2847
            .unwrap(),
×
UNCOV
2848
            new_seed: VRFSeed::from_hex(
×
UNCOV
2849
                "d5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375",
×
UNCOV
2850
            )
×
UNCOV
2851
            .unwrap(),
×
UNCOV
2852
            parent_block_ptr: 2211, // 0x000008a3
×
UNCOV
2853
            parent_vtxindex: 1,     // 0x0001
×
UNCOV
2854
            key_block_ptr: 1432,    // 0x00000598
×
UNCOV
2855
            key_vtxindex: 1,        // 0x0001
×
UNCOV
2856
            memo: vec![11],         // 0x5a >> 3
×
UNCOV
2857

×
UNCOV
2858
            burn_fee: 0,
×
UNCOV
2859
            input: (Txid([0x00; 32]), 0),
×
UNCOV
2860
            burn_parent_modulus: 2, // 0x5a & 0b111
×
UNCOV
2861

×
UNCOV
2862
            apparent_sender: BurnchainSigner("mgbpit8FvkVJ9kuXY8QSM5P7eibnhcEMBk".to_string()),
×
UNCOV
2863
            commit_outs: vec![
×
UNCOV
2864
                PoxAddress::Standard(StacksAddress::burn_address(false), None),
×
UNCOV
2865
                PoxAddress::Standard(StacksAddress::burn_address(false), None),
×
UNCOV
2866
            ],
×
UNCOV
2867

×
UNCOV
2868
            treatment: vec![],
×
UNCOV
2869
            sunset_burn: 0,
×
UNCOV
2870

×
UNCOV
2871
            txid: Txid([0x00; 32]),
×
UNCOV
2872
            vtxindex: 0,
×
UNCOV
2873
            block_height: 2212,
×
UNCOV
2874
            burn_header_hash: BurnchainHeaderHash([0x01; 32]),
×
UNCOV
2875
        };
×
2876

UNCOV
2877
        assert_eq!(to_hex(&commit_op.serialize_to_vec()), "5be88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32afd5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375000008a300010000059800015a".to_string());
×
2878

UNCOV
2879
        let leader_fees = LeaderBlockCommitFees {
×
UNCOV
2880
            sunset_fee: 0,
×
UNCOV
2881
            fee_rate: 50,
×
UNCOV
2882
            sortition_fee: 20000,
×
UNCOV
2883
            outputs_len: 2,
×
UNCOV
2884
            default_tx_size: 380,
×
UNCOV
2885
            spent_in_attempts: 0,
×
UNCOV
2886
            is_rbf_enabled: false,
×
UNCOV
2887
            final_size: 498,
×
UNCOV
2888
        };
×
2889

UNCOV
2890
        assert_eq!(leader_fees.amount_per_output(), 10000);
×
UNCOV
2891
        assert_eq!(leader_fees.total_spent(), 44900);
×
2892

UNCOV
2893
        let block_commit = btc_controller
×
UNCOV
2894
            .send_block_commit_operation_at_burnchain_height(
×
UNCOV
2895
                StacksEpochId::Epoch30,
×
UNCOV
2896
                commit_op,
×
UNCOV
2897
                &mut signer,
×
UNCOV
2898
                Some(utxo_set),
×
UNCOV
2899
                None,
×
UNCOV
2900
                leader_fees,
×
UNCOV
2901
                &[],
×
2902
                2212,
2903
            )
UNCOV
2904
            .unwrap();
×
2905

UNCOV
2906
        debug!("send_block_commit_operation:\n{block_commit:#?}");
×
UNCOV
2907
        assert_eq!(block_commit.output[3].value, 323507);
×
UNCOV
2908
        assert_eq!(serialize_hex(&block_commit).unwrap(), "0100000002eeda098987728e4a2e21b34b74000dcb0bd0e4d20e55735492ec3cba3afbead3030000006a4730440220558286e20e10ce31537f0625dae5cc62fac7961b9d2cf272c990de96323d7e2502202255adbea3d2e0509b80c5d8a3a4fe6397a87bcf18da1852740d5267d89a0cb20121035379aa40c02890d253cfa577964116eb5295570ae9f7287cbae5f2585f5b2c7cfdffffff243b0b329a5889ab8801b315eea19810848d4c2133e0245671cc984a2d2f1301000000006a47304402206d9f8de107f9e1eb15aafac66c2bb34331a7523260b30e18779257e367048d34022013c7dabb32a5c281aa00d405e2ccbd00f34f03a65b2336553a4acd6c52c251ef0121035379aa40c02890d253cfa577964116eb5295570ae9f7287cbae5f2585f5b2c7cfdffffff040000000000000000536a4c5054335be88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32afd5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375000008a300010000059800015a10270000000000001976a914000000000000000000000000000000000000000088ac10270000000000001976a914000000000000000000000000000000000000000088acb3ef0400000000001976a9141dc27eba0247f8cc9575e7d45e50a0bc7e72427d88ac00000000");
×
UNCOV
2909
    }
×
2910

2911
    #[test]
UNCOV
2912
    fn test_to_epoch_aware_pubkey() {
×
UNCOV
2913
        let mut config = utils::create_miner_config();
×
UNCOV
2914
        let pubkey = utils::create_miner1_pubkey();
×
2915

UNCOV
2916
        config.miner.segwit = false;
×
UNCOV
2917
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
×
2918

UNCOV
2919
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch20, &pubkey);
×
UNCOV
2920
        assert_eq!(
×
2921
            false,
UNCOV
2922
            reviewed.compressed(),
×
2923
            "Segwit disabled with Epoch < 2.1: not compressed"
2924
        );
UNCOV
2925
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch21, &pubkey);
×
UNCOV
2926
        assert_eq!(
×
2927
            false,
UNCOV
2928
            reviewed.compressed(),
×
2929
            "Segwit disabled with Epoch >= 2.1: not compressed"
2930
        );
2931

UNCOV
2932
        config.miner.segwit = true;
×
UNCOV
2933
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
×
2934

UNCOV
2935
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch20, &pubkey);
×
UNCOV
2936
        assert_eq!(
×
2937
            false,
UNCOV
2938
            reviewed.compressed(),
×
2939
            "Segwit enabled with Epoch < 2.1: not compressed"
2940
        );
UNCOV
2941
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch21, &pubkey);
×
UNCOV
2942
        assert_eq!(
×
2943
            true,
UNCOV
2944
            reviewed.compressed(),
×
2945
            "Segwit enabled with Epoch >= 2.1: compressed"
2946
        );
UNCOV
2947
    }
×
2948

2949
    #[test]
UNCOV
2950
    fn test_get_miner_address() {
×
UNCOV
2951
        let mut config = utils::create_miner_config();
×
UNCOV
2952
        let pub_key = utils::create_miner1_pubkey();
×
2953

UNCOV
2954
        config.miner.segwit = false;
×
UNCOV
2955
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
×
2956

UNCOV
2957
        let expected = utils::to_address_legacy(&pub_key);
×
UNCOV
2958
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch20, &pub_key);
×
UNCOV
2959
        assert_eq!(
×
2960
            expected, address,
2961
            "Segwit disabled with Epoch < 2.1: legacy addr"
2962
        );
2963

UNCOV
2964
        let expected = utils::to_address_legacy(&pub_key);
×
UNCOV
2965
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch21, &pub_key);
×
UNCOV
2966
        assert_eq!(
×
2967
            expected, address,
2968
            "Segwit disabled with Epoch >= 2.1: legacy addr"
2969
        );
2970

UNCOV
2971
        config.miner.segwit = true;
×
UNCOV
2972
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
×
2973

UNCOV
2974
        let expected = utils::to_address_legacy(&pub_key);
×
UNCOV
2975
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch20, &pub_key);
×
UNCOV
2976
        assert_eq!(
×
2977
            expected, address,
2978
            "Segwit enabled with Epoch < 2.1: legacy addr"
2979
        );
2980

UNCOV
2981
        let expected = utils::to_address_segwit_p2wpkh(&pub_key);
×
UNCOV
2982
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch21, &pub_key);
×
UNCOV
2983
        assert_eq!(
×
2984
            expected, address,
2985
            "Segwit enabled with Epoch >= 2.1: segwit addr"
2986
        );
UNCOV
2987
    }
×
2988

2989
    #[test]
UNCOV
2990
    fn test_instantiate_with_burnchain_on_follower_node_ok() {
×
UNCOV
2991
        let config = create_follower_config();
×
2992

UNCOV
2993
        let btc_controller = BitcoinRegtestController::with_burnchain(config, None, None, None);
×
2994

UNCOV
2995
        let result = panic::catch_unwind(AssertUnwindSafe(|| {
×
UNCOV
2996
            _ = btc_controller.get_rpc_client();
×
UNCOV
2997
        }));
×
UNCOV
2998
        assert!(
×
UNCOV
2999
            result.is_err(),
×
3000
            "Invoking any Bitcoin RPC related method should panic."
3001
        );
UNCOV
3002
    }
×
3003

3004
    #[test]
UNCOV
3005
    fn test_instantiate_with_burnchain_on_miner_node_ok() {
×
UNCOV
3006
        let config = create_miner_config();
×
3007

UNCOV
3008
        let btc_controller = BitcoinRegtestController::with_burnchain(config, None, None, None);
×
3009

UNCOV
3010
        let _ = btc_controller.get_rpc_client();
×
UNCOV
3011
        assert!(true, "Invoking any Bitcoin RPC related method should work.");
×
UNCOV
3012
    }
×
3013

3014
    #[test]
UNCOV
3015
    fn test_instantiate_with_burnchain_on_miner_node_failure() {
×
UNCOV
3016
        let mut config = create_miner_config();
×
UNCOV
3017
        config.burnchain.username = None;
×
UNCOV
3018
        config.burnchain.password = None;
×
3019

UNCOV
3020
        let result = panic::catch_unwind(|| {
×
UNCOV
3021
            _ = BitcoinRegtestController::with_burnchain(config, None, None, None);
×
UNCOV
3022
        });
×
UNCOV
3023
        assert!(
×
UNCOV
3024
            result.is_err(),
×
3025
            "Bitcoin RPC credentials are mandatory for miner node."
3026
        );
UNCOV
3027
    }
×
3028

3029
    #[test]
UNCOV
3030
    fn test_instantiate_new_dummy_on_follower_node_ok() {
×
UNCOV
3031
        let config = create_follower_config();
×
3032

UNCOV
3033
        let btc_controller = BitcoinRegtestController::new_dummy(config);
×
3034

UNCOV
3035
        let result = panic::catch_unwind(AssertUnwindSafe(|| {
×
UNCOV
3036
            _ = btc_controller.get_rpc_client();
×
UNCOV
3037
        }));
×
UNCOV
3038
        assert!(
×
UNCOV
3039
            result.is_err(),
×
3040
            "Invoking any Bitcoin RPC related method should panic."
3041
        );
UNCOV
3042
    }
×
3043

3044
    #[test]
UNCOV
3045
    fn test_instantiate_new_dummy_on_miner_node_ok() {
×
UNCOV
3046
        let config = create_miner_config();
×
3047

UNCOV
3048
        let btc_controller = BitcoinRegtestController::new_dummy(config);
×
3049

UNCOV
3050
        let _ = btc_controller.get_rpc_client();
×
UNCOV
3051
        assert!(true, "Invoking any Bitcoin RPC related method should work.");
×
UNCOV
3052
    }
×
3053

3054
    #[test]
UNCOV
3055
    fn test_instantiate_new_dummy_on_miner_node_failure() {
×
UNCOV
3056
        let mut config = create_miner_config();
×
UNCOV
3057
        config.burnchain.username = None;
×
UNCOV
3058
        config.burnchain.password = None;
×
3059

UNCOV
3060
        let result = panic::catch_unwind(|| {
×
UNCOV
3061
            _ = BitcoinRegtestController::new_dummy(config);
×
UNCOV
3062
        });
×
UNCOV
3063
        assert!(
×
UNCOV
3064
            result.is_err(),
×
3065
            "Bitcoin RPC credentials are mandatory for miner node."
3066
        );
UNCOV
3067
    }
×
3068

3069
    #[test]
3070
    #[ignore]
3071
    fn test_create_wallet_from_default_empty_name() {
1✔
3072
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3073
            return;
×
3074
        }
1✔
3075

3076
        let config = utils::create_miner_config();
1✔
3077

3078
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3079
        btcd_controller
1✔
3080
            .start_bitcoind()
1✔
3081
            .expect("bitcoind should be started!");
1✔
3082

3083
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3084

3085
        let wallets = btc_controller.list_wallets().unwrap();
1✔
3086
        assert_eq!(0, wallets.len());
1✔
3087

3088
        btc_controller
1✔
3089
            .create_wallet_if_dne()
1✔
3090
            .expect("Wallet should now exists!");
1✔
3091

3092
        let wallets = btc_controller.list_wallets().unwrap();
1✔
3093
        assert_eq!(1, wallets.len());
1✔
3094
        assert_eq!("".to_owned(), wallets[0]);
1✔
3095
    }
1✔
3096

3097
    #[test]
3098
    #[ignore]
3099
    fn test_create_wallet_from_custom_name() {
1✔
3100
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3101
            return;
×
3102
        }
1✔
3103

3104
        let mut config = utils::create_miner_config();
1✔
3105
        config.burnchain.wallet_name = String::from("mywallet");
1✔
3106

3107
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3108
        btcd_controller
1✔
3109
            .start_bitcoind()
1✔
3110
            .expect("bitcoind should be started!");
1✔
3111

3112
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3113

3114
        btc_controller
1✔
3115
            .create_wallet_if_dne()
1✔
3116
            .expect("Wallet should now exists!");
1✔
3117

3118
        let wallets = btc_controller.list_wallets().unwrap();
1✔
3119
        assert_eq!(1, wallets.len());
1✔
3120
        assert_eq!("mywallet".to_owned(), wallets[0]);
1✔
3121
    }
1✔
3122

3123
    #[test]
3124
    #[ignore]
3125
    fn test_retrieve_utxo_set_with_all_utxos() {
1✔
3126
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3127
            return;
×
3128
        }
1✔
3129

3130
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3131

3132
        let mut config = utils::create_miner_config();
1✔
3133
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3134

3135
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3136
        btcd_controller
1✔
3137
            .start_bitcoind()
1✔
3138
            .expect("Failed starting bitcoind");
1✔
3139

3140
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3141
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3142

3143
        let address = to_address_legacy(&miner_pubkey);
1✔
3144
        let utxo_set = btc_controller
1✔
3145
            .retrieve_utxo_set(&address, false, 0, &None, 0)
1✔
3146
            .expect("Failed to get utxos");
1✔
3147
        assert_eq!(btc_controller.get_block_hash(0), utxo_set.bhh);
1✔
3148
        assert_eq!(50, utxo_set.num_utxos());
1✔
3149
    }
1✔
3150

3151
    #[test]
3152
    #[ignore]
3153
    fn test_retrive_utxo_set_excluding_some_utxo() {
1✔
3154
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3155
            return;
×
3156
        }
1✔
3157

3158
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3159

3160
        let mut config = utils::create_miner_config();
1✔
3161
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3162

3163
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3164
        btcd_controller
1✔
3165
            .start_bitcoind()
1✔
3166
            .expect("Failed starting bitcoind");
1✔
3167

3168
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3169
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3170

3171
        let address = to_address_legacy(&miner_pubkey);
1✔
3172
        let mut all_utxos = btc_controller
1✔
3173
            .retrieve_utxo_set(&address, false, 0, &None, 0)
1✔
3174
            .expect("Failed to get utxos (50)");
1✔
3175

3176
        let filtered_utxos = btc_controller
1✔
3177
            .retrieve_utxo_set(&address, false, 0, &Some(all_utxos.clone()), 0)
1✔
3178
            .expect("Failed to get utxos");
1✔
3179
        assert_eq!(0, filtered_utxos.num_utxos(), "all utxos filtered out!");
1✔
3180

3181
        all_utxos.utxos.drain(0..10);
1✔
3182
        let filtered_utxos = btc_controller
1✔
3183
            .retrieve_utxo_set(&address, false, 0, &Some(all_utxos), 0)
1✔
3184
            .expect("Failed to get utxos");
1✔
3185
        assert_eq!(10, filtered_utxos.num_utxos(), "40 utxos filtered out!");
1✔
3186
    }
1✔
3187

3188
    #[test]
3189
    #[ignore]
3190
    fn test_list_unspent_with_max_utxos_config() {
1✔
3191
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3192
            return;
×
3193
        }
1✔
3194

3195
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3196

3197
        let mut config = utils::create_miner_config();
1✔
3198
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3199
        config.burnchain.max_unspent_utxos = Some(10);
1✔
3200

3201
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3202
        btcd_controller
1✔
3203
            .start_bitcoind()
1✔
3204
            .expect("Failed starting bitcoind");
1✔
3205

3206
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3207
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3208

3209
        let address = to_address_legacy(&miner_pubkey);
1✔
3210
        let utxos = btc_controller
1✔
3211
            .retrieve_utxo_set(&address, false, 1, &None, 0)
1✔
3212
            .expect("Failed to get utxos");
1✔
3213
        assert_eq!(10, utxos.num_utxos());
1✔
3214
    }
1✔
3215

3216
    #[test]
3217
    #[ignore]
3218
    fn test_get_all_utxos_with_confirmation() {
1✔
3219
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3220
            return;
×
3221
        }
1✔
3222

3223
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3224

3225
        let mut config = utils::create_miner_config();
1✔
3226
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3227

3228
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3229
        btcd_controller
1✔
3230
            .start_bitcoind()
1✔
3231
            .expect("bitcoind should be started!");
1✔
3232

3233
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3234

3235
        btc_controller.bootstrap_chain(100);
1✔
3236
        let utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3237
        assert_eq!(0, utxos.len());
1✔
3238

3239
        btc_controller.build_next_block(1);
1✔
3240
        let utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3241
        assert_eq!(1, utxos.len());
1✔
3242
        assert_eq!(101, utxos[0].confirmations);
1✔
3243
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3244

3245
        btc_controller.build_next_block(1);
1✔
3246
        let mut utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3247
        utxos.sort_by(|a, b| b.confirmations.cmp(&a.confirmations));
1✔
3248

3249
        assert_eq!(2, utxos.len());
1✔
3250
        assert_eq!(102, utxos[0].confirmations);
1✔
3251
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3252
        assert_eq!(101, utxos[1].confirmations);
1✔
3253
        assert_eq!(5_000_000_000, utxos[1].amount);
1✔
3254
    }
1✔
3255

3256
    #[test]
3257
    #[ignore]
3258
    fn test_get_all_utxos_for_other_pubkey() {
1✔
3259
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3260
            return;
×
3261
        }
1✔
3262

3263
        let miner1_pubkey = utils::create_miner1_pubkey();
1✔
3264
        let miner2_pubkey = utils::create_miner2_pubkey();
1✔
3265

3266
        let mut config = utils::create_miner_config();
1✔
3267
        config.burnchain.local_mining_public_key = Some(miner1_pubkey.to_hex());
1✔
3268

3269
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3270
        btcd_controller
1✔
3271
            .start_bitcoind()
1✔
3272
            .expect("bitcoind should be started!");
1✔
3273

3274
        let miner1_btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3275
        miner1_btc_controller.bootstrap_chain(1); // one utxo for miner_pubkey related address
1✔
3276

3277
        config.burnchain.local_mining_public_key = Some(miner2_pubkey.to_hex());
1✔
3278
        config.burnchain.wallet_name = "miner2_wallet".to_string();
1✔
3279
        let miner2_btc_controller = BitcoinRegtestController::new(config, None);
1✔
3280
        miner2_btc_controller.bootstrap_chain(102); // two utxo for other_pubkeys related address
1✔
3281

3282
        let utxos = miner1_btc_controller.get_all_utxos(&miner1_pubkey);
1✔
3283
        assert_eq!(1, utxos.len(), "miner1 see its own utxos");
1✔
3284

3285
        let utxos = miner1_btc_controller.get_all_utxos(&miner2_pubkey);
1✔
3286
        assert_eq!(2, utxos.len(), "miner1 see miner2 utxos");
1✔
3287

3288
        let utxos = miner2_btc_controller.get_all_utxos(&miner2_pubkey);
1✔
3289
        assert_eq!(2, utxos.len(), "miner2 see its own utxos");
1✔
3290

3291
        let utxos = miner2_btc_controller.get_all_utxos(&miner1_pubkey);
1✔
3292
        assert_eq!(1, utxos.len(), "miner2 see miner1 own utxos");
1✔
3293
    }
1✔
3294

3295
    #[test]
3296
    #[ignore]
3297
    fn test_get_utxos_ok_with_confirmation() {
1✔
3298
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3299
            return;
×
3300
        }
1✔
3301

3302
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3303

3304
        let mut config = utils::create_miner_config();
1✔
3305
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3306

3307
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3308
        btcd_controller
1✔
3309
            .start_bitcoind()
1✔
3310
            .expect("bitcoind should be started!");
1✔
3311

3312
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3313
        btc_controller.bootstrap_chain(101);
1✔
3314

3315
        let utxos_opt =
1✔
3316
            btc_controller.get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 1, None, 101);
1✔
3317
        let uxto_set = utxos_opt.expect("Shouldn't be None at height 101!");
1✔
3318

3319
        assert_eq!(btc_controller.get_block_hash(101), uxto_set.bhh);
1✔
3320
        assert_eq!(1, uxto_set.num_utxos());
1✔
3321
        assert_eq!(5_000_000_000, uxto_set.total_available());
1✔
3322
        let utxos = uxto_set.utxos;
1✔
3323
        assert_eq!(101, utxos[0].confirmations);
1✔
3324
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3325

3326
        btc_controller.build_next_block(1);
1✔
3327

3328
        let utxos_opt =
1✔
3329
            btc_controller.get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 1, None, 102);
1✔
3330
        let uxto_set = utxos_opt.expect("Shouldn't be None at height 102!");
1✔
3331

3332
        assert_eq!(btc_controller.get_block_hash(102), uxto_set.bhh);
1✔
3333
        assert_eq!(2, uxto_set.num_utxos());
1✔
3334
        assert_eq!(10_000_000_000, uxto_set.total_available());
1✔
3335
        let mut utxos = uxto_set.utxos;
1✔
3336
        utxos.sort_by(|a, b| b.confirmations.cmp(&a.confirmations));
1✔
3337
        assert_eq!(102, utxos[0].confirmations);
1✔
3338
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3339
        assert_eq!(101, utxos[1].confirmations);
1✔
3340
        assert_eq!(5_000_000_000, utxos[1].amount);
1✔
3341
    }
1✔
3342

3343
    #[test]
3344
    #[ignore]
3345
    fn test_get_utxos_none_due_to_filter_total_required() {
1✔
3346
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3347
            return;
×
3348
        }
1✔
3349

3350
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3351

3352
        let mut config = utils::create_miner_config();
1✔
3353
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3354

3355
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3356
        btcd_controller
1✔
3357
            .start_bitcoind()
1✔
3358
            .expect("bitcoind should be started!");
1✔
3359

3360
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3361
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3362

3363
        let too_much_required = 10_000_000_000;
1✔
3364
        let utxos = btc_controller.get_utxos(
1✔
3365
            StacksEpochId::Epoch31,
1✔
3366
            &miner_pubkey,
1✔
3367
            too_much_required,
1✔
3368
            None,
1✔
3369
            0,
3370
        );
3371
        assert!(utxos.is_none(), "None because too much required");
1✔
3372
    }
1✔
3373

3374
    #[test]
3375
    #[ignore]
3376
    fn test_get_utxos_none_due_to_filter_pubkey() {
1✔
3377
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3378
            return;
×
3379
        }
1✔
3380

3381
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3382

3383
        let mut config = utils::create_miner_config();
1✔
3384
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3385

3386
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3387
        btcd_controller
1✔
3388
            .start_bitcoind()
1✔
3389
            .expect("bitcoind should be started!");
1✔
3390

3391
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3392
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3393

3394
        let other_pubkey = utils::create_miner2_pubkey();
1✔
3395
        let utxos = btc_controller.get_utxos(StacksEpochId::Epoch31, &other_pubkey, 1, None, 0);
1✔
3396
        assert!(
1✔
3397
            utxos.is_none(),
1✔
3398
            "None because utxos for other pubkey don't exist"
3399
        );
3400
    }
1✔
3401

3402
    #[test]
3403
    #[ignore]
3404
    fn test_get_utxos_none_due_to_filter_utxo_exclusion() {
1✔
3405
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3406
            return;
×
3407
        }
1✔
3408

3409
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3410

3411
        let mut config = utils::create_miner_config();
1✔
3412
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3413

3414
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3415
        btcd_controller
1✔
3416
            .start_bitcoind()
1✔
3417
            .expect("bitcoind should be started!");
1✔
3418

3419
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3420
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3421

3422
        let existent_utxo = btc_controller
1✔
3423
            .get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 0, None, 0)
1✔
3424
            .expect("utxo set should exist");
1✔
3425
        let utxos = btc_controller.get_utxos(
1✔
3426
            StacksEpochId::Epoch31,
1✔
3427
            &miner_pubkey,
1✔
3428
            0,
3429
            Some(existent_utxo),
1✔
3430
            0,
3431
        );
3432
        assert!(
1✔
3433
            utxos.is_none(),
1✔
3434
            "None because filtering exclude existent utxo set"
3435
        );
3436
    }
1✔
3437

3438
    #[test]
3439
    #[ignore]
3440
    fn test_tx_confirmed_from_utxo_ok() {
1✔
3441
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3442
            return;
×
3443
        }
1✔
3444

3445
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3446

3447
        let mut config = utils::create_miner_config();
1✔
3448
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3449

3450
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3451
        btcd_controller
1✔
3452
            .start_bitcoind()
1✔
3453
            .expect("bitcoind should be started!");
1✔
3454

3455
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3456

3457
        btc_controller.bootstrap_chain(101);
1✔
3458
        let utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3459
        assert_eq!(1, utxos.len(), "One UTXO should be confirmed!");
1✔
3460

3461
        let txid = Txid::from_bitcoin_tx_hash(&utxos[0].txid);
1✔
3462
        assert!(
1✔
3463
            btc_controller.is_transaction_confirmed(&txid),
1✔
3464
            "UTXO tx should be confirmed!"
3465
        );
3466
    }
1✔
3467

3468
    #[test]
3469
    #[ignore]
3470
    fn test_import_public_key_ok() {
1✔
3471
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3472
            return;
×
3473
        }
1✔
3474

3475
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3476

3477
        let config = utils::create_miner_config();
1✔
3478

3479
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3480
        btcd_controller
1✔
3481
            .start_bitcoind()
1✔
3482
            .expect("bitcoind should be started!");
1✔
3483

3484
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3485
        btc_controller
1✔
3486
            .create_wallet_if_dne()
1✔
3487
            .expect("Wallet should be created!");
1✔
3488

3489
        let result = btc_controller.import_public_key(&miner_pubkey);
1✔
3490
        assert!(
1✔
3491
            result.is_ok(),
1✔
3492
            "Should be ok, got err instead: {:?}",
3493
            result.unwrap_err()
×
3494
        );
3495
    }
1✔
3496

3497
    #[test]
3498
    #[ignore]
3499
    fn test_import_public_key_twice_ok() {
1✔
3500
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3501
            return;
×
3502
        }
1✔
3503

3504
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3505

3506
        let config = utils::create_miner_config();
1✔
3507

3508
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3509
        btcd_controller
1✔
3510
            .start_bitcoind()
1✔
3511
            .expect("bitcoind should be started!");
1✔
3512

3513
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3514
        btc_controller
1✔
3515
            .create_wallet_if_dne()
1✔
3516
            .expect("Wallet should be created!");
1✔
3517

3518
        btc_controller
1✔
3519
            .import_public_key(&miner_pubkey)
1✔
3520
            .expect("Import should be ok: first time!");
1✔
3521

3522
        //ok, but it is basically a no-op
3523
        let result = btc_controller.import_public_key(&miner_pubkey);
1✔
3524
        assert!(
1✔
3525
            result.is_ok(),
1✔
3526
            "Should be ok, got err instead: {:?}",
3527
            result.unwrap_err()
×
3528
        );
3529
    }
1✔
3530

3531
    #[test]
3532
    #[ignore]
3533
    fn test_import_public_key_segwit_ok() {
1✔
3534
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3535
            return;
×
3536
        }
1✔
3537

3538
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3539

3540
        let mut config = utils::create_miner_config();
1✔
3541
        config.miner.segwit = true;
1✔
3542

3543
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3544
        btcd_controller
1✔
3545
            .start_bitcoind()
1✔
3546
            .expect("bitcoind should be started!");
1✔
3547

3548
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3549
        btc_controller
1✔
3550
            .create_wallet_if_dne()
1✔
3551
            .expect("Wallet should be created!");
1✔
3552

3553
        let result = btc_controller.import_public_key(&miner_pubkey);
1✔
3554
        assert!(
1✔
3555
            result.is_ok(),
1✔
3556
            "Should be ok, got err instead: {:?}",
UNCOV
3557
            result.unwrap_err()
×
3558
        );
3559
    }
1✔
3560

3561
    /// Tests related to Leader Block Commit operation
3562
    mod leader_commit_op {
3563
        use super::*;
3564

3565
        #[test]
3566
        #[ignore]
3567
        fn test_build_leader_block_commit_tx_ok_with_new_commit_op() {
1✔
3568
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3569
                return;
×
3570
            }
1✔
3571

3572
            let keychain = utils::create_keychain();
1✔
3573
            let miner_pubkey = keychain.get_pub_key();
1✔
3574
            let mut op_signer = keychain.generate_op_signer();
1✔
3575

3576
            let mut config = utils::create_miner_config();
1✔
3577
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3578
            config.burnchain.pox_reward_length = Some(11);
1✔
3579

3580
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3581
            btcd_controller
1✔
3582
                .start_bitcoind()
1✔
3583
                .expect("bitcoind should be started!");
1✔
3584

3585
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3586
            btc_controller
1✔
3587
                .connect_dbs()
1✔
3588
                .expect("Dbs initialization required!");
1✔
3589
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3590

3591
            let mut commit_op = utils::create_templated_commit_op();
1✔
3592
            commit_op.sunset_burn = 5_500;
1✔
3593
            commit_op.burn_fee = 110_000;
1✔
3594

3595
            let tx = btc_controller
1✔
3596
                .build_leader_block_commit_tx(
1✔
3597
                    StacksEpochId::Epoch31,
1✔
3598
                    commit_op.clone(),
1✔
3599
                    &mut op_signer,
1✔
3600
                )
3601
                .expect("Build leader block commit should work");
1✔
3602

3603
            assert!(op_signer.is_disposed());
1✔
3604

3605
            assert_eq!(1, tx.version);
1✔
3606
            assert_eq!(0, tx.lock_time);
1✔
3607
            assert_eq!(1, tx.input.len());
1✔
3608
            assert_eq!(4, tx.output.len());
1✔
3609

3610
            // utxos list contains the only existing utxo
3611
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3612
            let input_0 = utils::txin_at_index(&tx, &op_signer, &used_utxos, 0);
1✔
3613
            assert_eq!(input_0, tx.input[0]);
1✔
3614

3615
            let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes, 5_500);
1✔
3616
            let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_000);
1✔
3617
            let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_000);
1✔
3618
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 4_999_865_300);
1✔
3619
            assert_eq!(op_return, tx.output[0]);
1✔
3620
            assert_eq!(op_commit_1, tx.output[1]);
1✔
3621
            assert_eq!(op_commit_2, tx.output[2]);
1✔
3622
            assert_eq!(op_change, tx.output[3]);
1✔
3623
        }
1✔
3624

3625
        #[test]
3626
        #[ignore]
3627
        fn test_build_leader_block_commit_tx_fails_resub_same_commit_op_while_prev_not_confirmed() {
1✔
3628
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3629
                return;
×
3630
            }
1✔
3631

3632
            let keychain = utils::create_keychain();
1✔
3633
            let miner_pubkey = keychain.get_pub_key();
1✔
3634
            let mut op_signer = keychain.generate_op_signer();
1✔
3635

3636
            let mut config = utils::create_miner_config();
1✔
3637
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3638
            config.burnchain.pox_reward_length = Some(11);
1✔
3639

3640
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3641
            btcd_controller
1✔
3642
                .start_bitcoind()
1✔
3643
                .expect("bitcoind should be started!");
1✔
3644

3645
            let mut btc_controller = BitcoinRegtestController::new(config, None);
1✔
3646
            btc_controller
1✔
3647
                .connect_dbs()
1✔
3648
                .expect("Dbs initialization required!");
1✔
3649
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3650

3651
            let commit_op = utils::create_templated_commit_op();
1✔
3652

3653
            let _first_tx_ok = btc_controller
1✔
3654
                .build_leader_block_commit_tx(
1✔
3655
                    StacksEpochId::Epoch31,
1✔
3656
                    commit_op.clone(),
1✔
3657
                    &mut op_signer,
1✔
3658
                )
3659
                .expect("At first, building leader block commit should work");
1✔
3660

3661
            // re-submitting same commit while previous it is not confirmed by the burnchain
3662
            let resubmit = btc_controller.build_leader_block_commit_tx(
1✔
3663
                StacksEpochId::Epoch31,
1✔
3664
                commit_op,
1✔
3665
                &mut op_signer,
1✔
3666
            );
3667

3668
            assert!(resubmit.is_err());
1✔
3669
            assert_eq!(
1✔
3670
                BurnchainControllerError::IdenticalOperation,
3671
                resubmit.unwrap_err()
1✔
3672
            );
3673
        }
1✔
3674

3675
        #[test]
3676
        #[ignore]
3677
        fn test_build_leader_block_commit_tx_fails_resub_same_commit_op_while_prev_is_confirmed() {
1✔
3678
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3679
                return;
×
3680
            }
1✔
3681

3682
            let keychain = utils::create_keychain();
1✔
3683
            let miner_pubkey = keychain.get_pub_key();
1✔
3684
            let mut op_signer = keychain.generate_op_signer();
1✔
3685

3686
            let mut config = utils::create_miner_config();
1✔
3687
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3688
            config.burnchain.pox_reward_length = Some(11);
1✔
3689

3690
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3691
            btcd_controller
1✔
3692
                .start_bitcoind()
1✔
3693
                .expect("bitcoind should be started!");
1✔
3694

3695
            let mut btc_controller = BitcoinRegtestController::new(config, None);
1✔
3696
            btc_controller
1✔
3697
                .connect_dbs()
1✔
3698
                .expect("Dbs initialization required!");
1✔
3699
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3700

3701
            let commit_op = utils::create_templated_commit_op();
1✔
3702

3703
            let first_tx_ok = btc_controller
1✔
3704
                .build_leader_block_commit_tx(
1✔
3705
                    StacksEpochId::Epoch31,
1✔
3706
                    commit_op.clone(),
1✔
3707
                    &mut op_signer,
1✔
3708
                )
3709
                .expect("At first, building leader block commit should work");
1✔
3710

3711
            utils::mine_tx(&btc_controller, &first_tx_ok); // Now tx is confirmed
1✔
3712

3713
            // re-submitting same commit while previous it is confirmed by the burnchain
3714
            let resubmit = btc_controller.build_leader_block_commit_tx(
1✔
3715
                StacksEpochId::Epoch31,
1✔
3716
                commit_op,
1✔
3717
                &mut op_signer,
1✔
3718
            );
3719

3720
            assert!(resubmit.is_err());
1✔
3721
            assert_eq!(
1✔
3722
                BurnchainControllerError::IdenticalOperation,
3723
                resubmit.unwrap_err()
1✔
3724
            );
3725
        }
1✔
3726

3727
        #[test]
3728
        #[ignore]
3729
        fn test_build_leader_block_commit_tx_ok_while_prev_is_confirmed() {
1✔
3730
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3731
                return;
×
3732
            }
1✔
3733

3734
            let keychain = utils::create_keychain();
1✔
3735
            let miner_pubkey = keychain.get_pub_key();
1✔
3736
            let mut op_signer = keychain.generate_op_signer();
1✔
3737

3738
            let mut config = utils::create_miner_config();
1✔
3739
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3740
            config.burnchain.pox_reward_length = Some(11);
1✔
3741

3742
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3743
            btcd_controller
1✔
3744
                .start_bitcoind()
1✔
3745
                .expect("bitcoind should be started!");
1✔
3746

3747
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3748
            btc_controller
1✔
3749
                .connect_dbs()
1✔
3750
                .expect("Dbs initialization required!");
1✔
3751
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3752

3753
            let mut commit_op = utils::create_templated_commit_op();
1✔
3754
            commit_op.sunset_burn = 5_500;
1✔
3755
            commit_op.burn_fee = 110_000;
1✔
3756

3757
            let first_tx_ok = btc_controller
1✔
3758
                .build_leader_block_commit_tx(
1✔
3759
                    StacksEpochId::Epoch31,
1✔
3760
                    commit_op.clone(),
1✔
3761
                    &mut op_signer,
1✔
3762
                )
3763
                .expect("At first, building leader block commit should work");
1✔
3764

3765
            let first_txid = first_tx_ok.txid();
1✔
3766

3767
            // Now tx is confirmed: prev utxo is updated and one more utxo is generated
3768
            utils::mine_tx(&btc_controller, &first_tx_ok);
1✔
3769

3770
            // re-gen signer othewise fails because it will be disposed during previous commit tx.
3771
            let mut signer = keychain.generate_op_signer();
1✔
3772
            // Modify the commit operation payload slightly, so it no longer matches the confirmed version.
3773
            commit_op.burn_fee += 10;
1✔
3774

3775
            let new_tx = btc_controller
1✔
3776
                .build_leader_block_commit_tx(
1✔
3777
                    StacksEpochId::Epoch31,
1✔
3778
                    commit_op.clone(),
1✔
3779
                    &mut signer,
1✔
3780
                )
3781
                .expect("Commit tx should be created!");
1✔
3782

3783
            assert!(op_signer.is_disposed());
1✔
3784

3785
            assert_eq!(1, new_tx.version);
1✔
3786
            assert_eq!(0, new_tx.lock_time);
1✔
3787
            assert_eq!(1, new_tx.input.len());
1✔
3788
            assert_eq!(4, new_tx.output.len());
1✔
3789

3790
            // utxos list contains the sole utxo used by prev commit operation
3791
            // because has enough amount to cover the new commit
3792
            let used_utxos: Vec<UTXO> = btc_controller
1✔
3793
                .get_all_utxos(&miner_pubkey)
1✔
3794
                .into_iter()
1✔
3795
                .filter(|utxo| utxo.txid == first_txid)
2✔
3796
                .collect();
1✔
3797

3798
            let input_0 = utils::txin_at_index(&new_tx, &op_signer, &used_utxos, 0);
1✔
3799
            assert_eq!(input_0, new_tx.input[0]);
1✔
3800

3801
            let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes, 5_500);
1✔
3802
            let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_005);
1✔
3803
            let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_005);
1✔
3804
            let op_change = utils::txout_opdup_change_legacy(&mut signer, 4_999_730_590);
1✔
3805
            assert_eq!(op_return, new_tx.output[0]);
1✔
3806
            assert_eq!(op_commit_1, new_tx.output[1]);
1✔
3807
            assert_eq!(op_commit_2, new_tx.output[2]);
1✔
3808
            assert_eq!(op_change, new_tx.output[3]);
1✔
3809
        }
1✔
3810

3811
        #[test]
3812
        #[ignore]
3813
        fn test_build_leader_block_commit_tx_ok_rbf_while_prev_not_confirmed() {
1✔
3814
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3815
                return;
×
3816
            }
1✔
3817

3818
            let keychain = utils::create_keychain();
1✔
3819
            let miner_pubkey = keychain.get_pub_key();
1✔
3820
            let mut op_signer = keychain.generate_op_signer();
1✔
3821

3822
            let mut config = utils::create_miner_config();
1✔
3823
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3824
            config.burnchain.pox_reward_length = Some(11);
1✔
3825

3826
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3827
            btcd_controller
1✔
3828
                .start_bitcoind()
1✔
3829
                .expect("bitcoind should be started!");
1✔
3830

3831
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3832
            btc_controller
1✔
3833
                .connect_dbs()
1✔
3834
                .expect("Dbs initialization required!");
1✔
3835
            btc_controller.bootstrap_chain(101); // Now, one utxo exists
1✔
3836

3837
            let mut commit_op = utils::create_templated_commit_op();
1✔
3838
            commit_op.sunset_burn = 5_500;
1✔
3839
            commit_op.burn_fee = 110_000;
1✔
3840

3841
            let _first_tx_ok = btc_controller
1✔
3842
                .build_leader_block_commit_tx(
1✔
3843
                    StacksEpochId::Epoch31,
1✔
3844
                    commit_op.clone(),
1✔
3845
                    &mut op_signer,
1✔
3846
                )
3847
                .expect("At first, building leader block commit should work");
1✔
3848

3849
            //re-gen signer othewise fails because it will be disposed during previous commit tx.
3850
            let mut signer = keychain.generate_op_signer();
1✔
3851
            //small change to the commit op payload
3852
            commit_op.burn_fee += 10;
1✔
3853

3854
            let rbf_tx = btc_controller
1✔
3855
                .build_leader_block_commit_tx(
1✔
3856
                    StacksEpochId::Epoch31,
1✔
3857
                    commit_op.clone(),
1✔
3858
                    &mut signer,
1✔
3859
                )
3860
                .expect("Commit tx should be rbf-ed");
1✔
3861

3862
            assert!(op_signer.is_disposed());
1✔
3863

3864
            assert_eq!(1, rbf_tx.version);
1✔
3865
            assert_eq!(0, rbf_tx.lock_time);
1✔
3866
            assert_eq!(1, rbf_tx.input.len());
1✔
3867
            assert_eq!(4, rbf_tx.output.len());
1✔
3868

3869
            // utxos list contains the only existing utxo
3870
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3871

3872
            let input_0 = utils::txin_at_index(&rbf_tx, &op_signer, &used_utxos, 0);
1✔
3873
            assert_eq!(input_0, rbf_tx.input[0]);
1✔
3874

3875
            let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes, 5_500);
1✔
3876
            let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_005);
1✔
3877
            let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_005);
1✔
3878
            let op_change = utils::txout_opdup_change_legacy(&mut signer, 4_999_862_985);
1✔
3879
            assert_eq!(op_return, rbf_tx.output[0]);
1✔
3880
            assert_eq!(op_commit_1, rbf_tx.output[1]);
1✔
3881
            assert_eq!(op_commit_2, rbf_tx.output[2]);
1✔
3882
            assert_eq!(op_change, rbf_tx.output[3]);
1✔
3883
        }
1✔
3884

3885
        #[test]
3886
        #[ignore]
3887
        fn test_make_operation_leader_block_commit_tx_ok() {
1✔
3888
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3889
                return;
×
3890
            }
1✔
3891

3892
            let keychain = utils::create_keychain();
1✔
3893
            let miner_pubkey = keychain.get_pub_key();
1✔
3894
            let mut op_signer = keychain.generate_op_signer();
1✔
3895

3896
            let mut config = utils::create_miner_config();
1✔
3897
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3898
            config.burnchain.pox_reward_length = Some(11);
1✔
3899

3900
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3901
            btcd_controller
1✔
3902
                .start_bitcoind()
1✔
3903
                .expect("bitcoind should be started!");
1✔
3904

3905
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3906
            btc_controller
1✔
3907
                .connect_dbs()
1✔
3908
                .expect("Dbs initialization required!");
1✔
3909
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3910

3911
            let mut commit_op = utils::create_templated_commit_op();
1✔
3912
            commit_op.sunset_burn = 5_500;
1✔
3913
            commit_op.burn_fee = 110_000;
1✔
3914

3915
            let tx = btc_controller
1✔
3916
                .make_operation_tx(
1✔
3917
                    StacksEpochId::Epoch31,
1✔
3918
                    BlockstackOperationType::LeaderBlockCommit(commit_op),
1✔
3919
                    &mut op_signer,
1✔
3920
                )
3921
                .expect("Make op should work");
1✔
3922

3923
            assert!(op_signer.is_disposed());
1✔
3924

3925
            assert_eq!(
1✔
3926
                "1a74106bd760117892fbd90fca11646b4de46f99fd2b065c9e0706cfdcea0336",
3927
                tx.txid().to_string()
1✔
3928
            );
3929
        }
1✔
3930

3931
        #[test]
3932
        #[ignore]
3933
        fn test_submit_leader_block_commit_tx_ok() {
1✔
3934
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3935
                return;
×
3936
            }
1✔
3937

3938
            let keychain = utils::create_keychain();
1✔
3939
            let miner_pubkey = keychain.get_pub_key();
1✔
3940
            let mut op_signer = keychain.generate_op_signer();
1✔
3941

3942
            let mut config = utils::create_miner_config();
1✔
3943
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3944
            config.burnchain.pox_reward_length = Some(11);
1✔
3945

3946
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3947
            btcd_controller
1✔
3948
                .start_bitcoind()
1✔
3949
                .expect("bitcoind should be started!");
1✔
3950

3951
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3952
            btc_controller
1✔
3953
                .connect_dbs()
1✔
3954
                .expect("Dbs initialization required!");
1✔
3955
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3956

3957
            let mut commit_op = utils::create_templated_commit_op();
1✔
3958
            commit_op.sunset_burn = 5_500;
1✔
3959
            commit_op.burn_fee = 110_000;
1✔
3960

3961
            let tx_id = btc_controller
1✔
3962
                .submit_operation(
1✔
3963
                    StacksEpochId::Epoch31,
1✔
3964
                    BlockstackOperationType::LeaderBlockCommit(commit_op),
1✔
3965
                    &mut op_signer,
1✔
3966
                )
3967
                .expect("Submit op should work");
1✔
3968

3969
            assert!(op_signer.is_disposed());
1✔
3970

3971
            assert_eq!(
1✔
3972
                "1a74106bd760117892fbd90fca11646b4de46f99fd2b065c9e0706cfdcea0336",
3973
                tx_id.to_hex()
1✔
3974
            );
3975
        }
1✔
3976

3977
        #[test]
3978
        #[ignore]
3979
        fn test_submit_operation_block_commit_clears_ongoing_on_send_failure() {
1✔
3980
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
3981
                return;
×
3982
            }
1✔
3983

3984
            let keychain = utils::create_keychain();
1✔
3985
            let miner_pubkey = keychain.get_pub_key();
1✔
3986
            let mut op_signer = keychain.generate_op_signer();
1✔
3987

3988
            let mut config = utils::create_miner_config();
1✔
3989
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3990
            config.burnchain.pox_reward_length = Some(11);
1✔
3991

3992
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3993
            btcd_controller
1✔
3994
                .start_bitcoind()
1✔
3995
                .expect("bitcoind should be started!");
1✔
3996

3997
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3998
            btc_controller
1✔
3999
                .connect_dbs()
1✔
4000
                .expect("Dbs initialization required!");
1✔
4001
            btc_controller.bootstrap_chain(101);
1✔
4002

4003
            let mut commit_op = utils::create_templated_commit_op();
1✔
4004
            commit_op.sunset_burn = 5_500;
1✔
4005
            commit_op.burn_fee = 110_000;
1✔
4006

4007
            // First submit succeeds and sets ongoing_block_commit
4008
            btc_controller
1✔
4009
                .submit_operation(
1✔
4010
                    StacksEpochId::Epoch31,
1✔
4011
                    BlockstackOperationType::LeaderBlockCommit(commit_op.clone()),
1✔
4012
                    &mut op_signer,
1✔
4013
                )
4014
                .expect("First submit should succeed");
1✔
4015

4016
            assert!(
1✔
4017
                btc_controller.get_ongoing_commit().is_some(),
1✔
4018
                "ongoing_block_commit should be set after successful submit"
4019
            );
4020

4021
            // Corrupt the cached UTXOs so the RBF transaction references
4022
            // non-existent inputs, causing send_transaction to be rejected
4023
            // by bitcoind immediately.
4024
            let mut ongoing = btc_controller.get_ongoing_commit().unwrap();
1✔
4025
            for utxo in ongoing.utxos.utxos.iter_mut() {
1✔
4026
                utxo.txid = Sha256dHash::default();
1✔
4027
            }
1✔
4028
            btc_controller.set_ongoing_commit(Some(ongoing));
1✔
4029

4030
            // Second submit with different payload triggers the RBF path.
4031
            // make_operation_tx builds the tx using the corrupted UTXOs,
4032
            // then send_transaction fails because the inputs don't exist.
4033
            let mut op_signer = keychain.generate_op_signer();
1✔
4034
            commit_op.burn_fee += 10;
1✔
4035

4036
            let err = btc_controller
1✔
4037
                .submit_operation(
1✔
4038
                    StacksEpochId::Epoch31,
1✔
4039
                    BlockstackOperationType::LeaderBlockCommit(commit_op),
1✔
4040
                    &mut op_signer,
1✔
4041
                )
4042
                .unwrap_err();
1✔
4043

4044
            assert!(
1✔
UNCOV
4045
                matches!(
×
4046
                    err,
1✔
4047
                    BurnchainControllerError::TransactionSubmissionFailed(_)
4048
                ),
4049
                "Error should be TransactionSubmissionFailed, but was {err}"
4050
            );
4051
            assert!(
1✔
4052
                btc_controller.get_ongoing_commit().is_none(),
1✔
4053
                "ongoing_block_commit should be cleared after send failure"
4054
            );
4055
        }
1✔
4056
    }
4057

4058
    /// Tests related to Leader Key Register operation
4059
    mod leader_key_op {
4060
        use super::*;
4061

4062
        #[test]
4063
        #[ignore]
4064
        fn test_build_leader_key_tx_ok() {
1✔
4065
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
4066
                return;
×
4067
            }
1✔
4068

4069
            let keychain = utils::create_keychain();
1✔
4070
            let miner_pubkey = keychain.get_pub_key();
1✔
4071
            let mut op_signer = keychain.generate_op_signer();
1✔
4072

4073
            let mut config = utils::create_miner_config();
1✔
4074
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4075

4076
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4077
            btcd_controller
1✔
4078
                .start_bitcoind()
1✔
4079
                .expect("bitcoind should be started!");
1✔
4080

4081
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4082
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
4083

4084
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4085

4086
            let tx = btc_controller
1✔
4087
                .build_leader_key_register_tx(
1✔
4088
                    StacksEpochId::Epoch31,
1✔
4089
                    leader_key_op.clone(),
1✔
4090
                    &mut op_signer,
1✔
4091
                )
4092
                .expect("Build leader key should work");
1✔
4093

4094
            assert!(op_signer.is_disposed());
1✔
4095

4096
            assert_eq!(1, tx.version);
1✔
4097
            assert_eq!(0, tx.lock_time);
1✔
4098
            assert_eq!(1, tx.input.len());
1✔
4099
            assert_eq!(2, tx.output.len());
1✔
4100

4101
            // utxos list contains the only existing utxo
4102
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
4103
            let input_0 = utils::txin_at_index(&tx, &op_signer, &used_utxos, 0);
1✔
4104
            assert_eq!(input_0, tx.input[0]);
1✔
4105

4106
            let op_return = utils::txout_opreturn(&leader_key_op, &config.burnchain.magic_bytes, 0);
1✔
4107
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 4_999_980_000);
1✔
4108
            assert_eq!(op_return, tx.output[0]);
1✔
4109
            assert_eq!(op_change, tx.output[1]);
1✔
4110
        }
1✔
4111

4112
        #[test]
4113
        #[ignore]
4114
        fn test_build_leader_key_tx_fails_due_to_no_utxos() {
1✔
4115
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
4116
                return;
×
4117
            }
1✔
4118

4119
            let keychain = utils::create_keychain();
1✔
4120
            let miner_pubkey = keychain.get_pub_key();
1✔
4121
            let mut op_signer = keychain.generate_op_signer();
1✔
4122

4123
            let mut config = utils::create_miner_config();
1✔
4124
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4125

4126
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4127
            btcd_controller
1✔
4128
                .start_bitcoind()
1✔
4129
                .expect("bitcoind should be started!");
1✔
4130

4131
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4132
            btc_controller.bootstrap_chain(100); // no utxos exist
1✔
4133

4134
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4135

4136
            let error = btc_controller
1✔
4137
                .build_leader_key_register_tx(
1✔
4138
                    StacksEpochId::Epoch31,
1✔
4139
                    leader_key_op.clone(),
1✔
4140
                    &mut op_signer,
1✔
4141
                )
4142
                .expect_err("Leader key build should fail!");
1✔
4143

4144
            assert!(!op_signer.is_disposed());
1✔
4145
            assert_eq!(BurnchainControllerError::NoUTXOs, error);
1✔
4146
        }
1✔
4147

4148
        #[test]
4149
        #[ignore]
4150
        fn test_make_operation_leader_key_register_tx_ok() {
1✔
4151
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
4152
                return;
×
4153
            }
1✔
4154

4155
            let keychain = utils::create_keychain();
1✔
4156
            let miner_pubkey = keychain.get_pub_key();
1✔
4157
            let mut op_signer = keychain.generate_op_signer();
1✔
4158

4159
            let mut config = utils::create_miner_config();
1✔
4160
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4161
            config.burnchain.pox_reward_length = Some(11);
1✔
4162

4163
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4164
            btcd_controller
1✔
4165
                .start_bitcoind()
1✔
4166
                .expect("bitcoind should be started!");
1✔
4167

4168
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4169
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
4170

4171
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4172

4173
            let tx = btc_controller
1✔
4174
                .make_operation_tx(
1✔
4175
                    StacksEpochId::Epoch31,
1✔
4176
                    BlockstackOperationType::LeaderKeyRegister(leader_key_op),
1✔
4177
                    &mut op_signer,
1✔
4178
                )
4179
                .expect("Make op should work");
1✔
4180

4181
            assert!(op_signer.is_disposed());
1✔
4182

4183
            assert_eq!(
1✔
4184
                "4ecd7ba71bebd1aaed49dd63747ee424473f1c571bb9a576361607a669191024",
4185
                tx.txid().to_string()
1✔
4186
            );
4187
        }
1✔
4188

4189
        #[test]
4190
        #[ignore]
4191
        fn test_submit_operation_leader_key_register_tx_ok() {
1✔
4192
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
4193
                return;
×
4194
            }
1✔
4195

4196
            let keychain = utils::create_keychain();
1✔
4197
            let miner_pubkey = keychain.get_pub_key();
1✔
4198
            let mut op_signer = keychain.generate_op_signer();
1✔
4199

4200
            let mut config = utils::create_miner_config();
1✔
4201
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4202

4203
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4204
            btcd_controller
1✔
4205
                .start_bitcoind()
1✔
4206
                .expect("bitcoind should be started!");
1✔
4207

4208
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4209
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
4210

4211
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4212

4213
            let tx_id = btc_controller
1✔
4214
                .submit_operation(
1✔
4215
                    StacksEpochId::Epoch31,
1✔
4216
                    BlockstackOperationType::LeaderKeyRegister(leader_key_op),
1✔
4217
                    &mut op_signer,
1✔
4218
                )
4219
                .expect("Submit op should work");
1✔
4220

4221
            assert!(op_signer.is_disposed());
1✔
4222

4223
            assert_eq!(
1✔
4224
                "4ecd7ba71bebd1aaed49dd63747ee424473f1c571bb9a576361607a669191024",
4225
                tx_id.to_hex()
1✔
4226
            );
4227
        }
1✔
4228
    }
4229

4230
    /// Tests related to Pre Stacks operation
4231
    mod pre_stx_op {
4232
        use super::*;
4233

4234
        #[test]
4235
        #[ignore]
4236
        fn test_build_pre_stx_tx_ok() {
1✔
4237
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
4238
                return;
×
4239
            }
1✔
4240

4241
            let keychain = utils::create_keychain();
1✔
4242
            let miner_pubkey = keychain.get_pub_key();
1✔
4243
            let mut op_signer = keychain.generate_op_signer();
1✔
4244

4245
            let mut config = utils::create_miner_config();
1✔
4246
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4247

4248
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4249
            btcd_controller
1✔
4250
                .start_bitcoind()
1✔
4251
                .expect("bitcoind should be started!");
1✔
4252

4253
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4254
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
4255

4256
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4257
            pre_stx_op.output = keychain.get_address(false);
1✔
4258

4259
            let tx = btc_controller
1✔
4260
                .build_pre_stacks_tx(StacksEpochId::Epoch31, pre_stx_op.clone(), &mut op_signer)
1✔
4261
                .expect("Build leader key should work");
1✔
4262

4263
            assert!(op_signer.is_disposed());
1✔
4264

4265
            assert_eq!(1, tx.version);
1✔
4266
            assert_eq!(0, tx.lock_time);
1✔
4267
            assert_eq!(1, tx.input.len());
1✔
4268
            assert_eq!(3, tx.output.len());
1✔
4269

4270
            // utxos list contains the only existing utxo
4271
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
4272
            let input_0 = utils::txin_at_index(&tx, &op_signer, &used_utxos, 0);
1✔
4273
            assert_eq!(input_0, tx.input[0]);
1✔
4274

4275
            let op_return = utils::txout_opreturn(&pre_stx_op, &config.burnchain.magic_bytes, 0);
1✔
4276
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 24_500);
1✔
4277
            assert_eq!(op_return, tx.output[0]);
1✔
4278
            assert_eq!(op_change, tx.output[1]);
1✔
4279
        }
1✔
4280

4281
        #[test]
4282
        #[ignore]
4283
        fn test_build_pre_stx_tx_fails_due_to_no_utxos() {
1✔
4284
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
4285
                return;
×
4286
            }
1✔
4287

4288
            let keychain = utils::create_keychain();
1✔
4289
            let miner_pubkey = keychain.get_pub_key();
1✔
4290
            let mut op_signer = keychain.generate_op_signer();
1✔
4291

4292
            let mut config = utils::create_miner_config();
1✔
4293
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4294

4295
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4296
            btcd_controller
1✔
4297
                .start_bitcoind()
1✔
4298
                .expect("bitcoind should be started!");
1✔
4299

4300
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4301
            btc_controller.bootstrap_chain(100); // no utxo exists
1✔
4302

4303
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4304
            pre_stx_op.output = keychain.get_address(false);
1✔
4305

4306
            let error = btc_controller
1✔
4307
                .build_pre_stacks_tx(StacksEpochId::Epoch31, pre_stx_op.clone(), &mut op_signer)
1✔
4308
                .expect_err("Leader key build should fail!");
1✔
4309

4310
            assert!(!op_signer.is_disposed());
1✔
4311
            assert_eq!(BurnchainControllerError::NoUTXOs, error);
1✔
4312
        }
1✔
4313

4314
        #[test]
4315
        #[ignore]
4316
        fn test_make_operation_pre_stx_tx_ok() {
1✔
4317
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
4318
                return;
×
4319
            }
1✔
4320

4321
            let keychain = utils::create_keychain();
1✔
4322
            let miner_pubkey = keychain.get_pub_key();
1✔
4323
            let mut op_signer = keychain.generate_op_signer();
1✔
4324

4325
            let mut config = utils::create_miner_config();
1✔
4326
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4327

4328
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4329
            btcd_controller
1✔
4330
                .start_bitcoind()
1✔
4331
                .expect("bitcoind should be started!");
1✔
4332

4333
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4334
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
4335

4336
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4337
            pre_stx_op.output = keychain.get_address(false);
1✔
4338

4339
            let tx = btc_controller
1✔
4340
                .make_operation_tx(
1✔
4341
                    StacksEpochId::Epoch31,
1✔
4342
                    BlockstackOperationType::PreStx(pre_stx_op),
1✔
4343
                    &mut op_signer,
1✔
4344
                )
4345
                .expect("Make op should work");
1✔
4346

4347
            assert!(op_signer.is_disposed());
1✔
4348

4349
            assert_eq!(
1✔
4350
                "2d061c42c6f13a62fd9d80dc9fdcd19bdb4f9e4a07f786e42530c64c52ed9d1d",
4351
                tx.txid().to_string()
1✔
4352
            );
4353
        }
1✔
4354

4355
        #[test]
4356
        #[ignore]
4357
        fn test_submit_operation_pre_stx_tx_ok() {
1✔
4358
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
UNCOV
4359
                return;
×
4360
            }
1✔
4361

4362
            let keychain = utils::create_keychain();
1✔
4363
            let miner_pubkey = keychain.get_pub_key();
1✔
4364
            let mut op_signer = keychain.generate_op_signer();
1✔
4365

4366
            let mut config = utils::create_miner_config();
1✔
4367
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4368

4369
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4370
            btcd_controller
1✔
4371
                .start_bitcoind()
1✔
4372
                .expect("bitcoind should be started!");
1✔
4373

4374
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4375
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
4376

4377
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4378
            pre_stx_op.output = keychain.get_address(false);
1✔
4379

4380
            let tx_id = btc_controller
1✔
4381
                .submit_operation(
1✔
4382
                    StacksEpochId::Epoch31,
1✔
4383
                    BlockstackOperationType::PreStx(pre_stx_op),
1✔
4384
                    &mut op_signer,
1✔
4385
                )
4386
                .expect("submit op should work");
1✔
4387

4388
            assert!(op_signer.is_disposed());
1✔
4389

4390
            assert_eq!(
1✔
4391
                "2d061c42c6f13a62fd9d80dc9fdcd19bdb4f9e4a07f786e42530c64c52ed9d1d",
4392
                tx_id.to_hex()
1✔
4393
            );
4394
        }
1✔
4395
    }
4396
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc