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

stacks-network / stacks-core / 23525981672

25 Mar 2026 05:10AM UTC coverage: 85.738% (-0.1%) from 85.84%
23525981672

Pull #7036

github

web-flow
Merge 12a0358c3 into 91ed8c178
Pull Request #7036: Feat/improved const callable

28 of 29 new or added lines in 5 files covered. (96.55%)

317 existing lines in 41 files now uncovered.

187347 of 218510 relevant lines covered (85.74%)

17241084.7 hits per line

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

92.35
/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 {
175,469✔
125
    let (network, _) = config.get_bitcoin_network();
175,469✔
126
    let mut params = BurnchainParameters::from_params(&config.chain, &network)
175,469✔
127
        .expect("Bitcoin network unsupported");
175,469✔
128
    if let Some(first_burn_block_height) = config.first_burn_block_height {
175,469✔
129
        params.first_block_height = first_burn_block_height;
×
130
    }
175,469✔
131
    params
175,469✔
132
}
175,469✔
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,282✔
138
    config: &Config,
1,282✔
139
    should_keep_running: Option<Arc<AtomicBool>>,
1,282✔
140
) -> BitcoinIndexer {
1,282✔
141
    let burnchain_params = burnchain_params_from_config(&config.burnchain);
1,282✔
142
    let indexer_config = {
1,282✔
143
        let burnchain_config = config.burnchain.clone();
1,282✔
144
        BitcoinIndexerConfig {
1,282✔
145
            peer_host: burnchain_config.peer_host,
1,282✔
146
            peer_port: burnchain_config.peer_port,
1,282✔
147
            rpc_port: burnchain_config.rpc_port,
1,282✔
148
            rpc_ssl: burnchain_config.rpc_ssl,
1,282✔
149
            username: burnchain_config.username,
1,282✔
150
            password: burnchain_config.password,
1,282✔
151
            timeout: burnchain_config.timeout,
1,282✔
152
            socket_timeout: burnchain_config.socket_timeout,
1,282✔
153
            spv_headers_path: config.get_spv_headers_file_path(),
1,282✔
154
            first_block: burnchain_params.first_block_height,
1,282✔
155
            magic_bytes: burnchain_config.magic_bytes,
1,282✔
156
            epochs: burnchain_config.epochs,
1,282✔
157
        }
1,282✔
158
    };
159

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

259
    pub fn register_replacement(&mut self, tx_size: u64) {
8,217✔
260
        let new_size = cmp::max(tx_size, self.final_size);
8,217✔
261
        if self.is_rbf_enabled {
8,217✔
262
            self.spent_in_attempts += new_size;
316✔
263
        }
7,901✔
264
        self.final_size = new_size;
8,217✔
265
    }
8,217✔
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,724✔
297
        _ = self.unwrap_or_log_panic(context);
7,724✔
298
    }
7,724✔
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 {
263✔
317
        BitcoinRegtestController::with_burnchain(config, coordinator_channel, None, None)
263✔
318
    }
263✔
319

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

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

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

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

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

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

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

379
        Self {
793✔
380
            use_coordinator: coordinator_channel,
793✔
381
            config,
793✔
382
            indexer: burnchain_indexer,
793✔
383
            db: None,
793✔
384
            burnchain_db: None,
793✔
385
            chain_tip: None,
793✔
386
            burnchain_config: burnchain,
793✔
387
            ongoing_block_commit: None,
793✔
388
            should_keep_running,
793✔
389
            rpc_client,
793✔
390
        }
793✔
391
    }
793✔
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 {
172,972✔
398
        let burnchain_params = burnchain_params_from_config(&config.burnchain);
172,972✔
399

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

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

426
        let rpc_client = Self::create_rpc_client_unchecked(&config);
172,972✔
427

428
        Self {
172,972✔
429
            use_coordinator: None,
172,972✔
430
            config,
172,972✔
431
            indexer: burnchain_indexer,
172,972✔
432
            db: None,
172,972✔
433
            burnchain_db: None,
172,972✔
434
            chain_tip: None,
172,972✔
435
            burnchain_config: None,
172,972✔
436
            ongoing_block_commit: None,
172,972✔
437
            should_keep_running: None,
172,972✔
438
            rpc_client,
172,972✔
439
        }
172,972✔
440
    }
172,972✔
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 {
172,454✔
444
        let mut ret = Self::new_dummy(config);
172,454✔
445
        ret.ongoing_block_commit = ongoing;
172,454✔
446
        ret
172,454✔
447
    }
172,454✔
448

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

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

459
    /// Get the default Burnchain instance from our config
460
    fn default_burnchain(&self) -> Burnchain {
808,203✔
461
        match &self.burnchain_config {
808,203✔
462
            Some(burnchain) => burnchain.clone(),
×
463
            None => self.config.get_burnchain(),
808,203✔
464
        }
465
    }
808,203✔
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 {
859,935✔
475
        match self.burnchain_config {
859,935✔
476
            Some(ref burnchain) => burnchain.clone(),
51,732✔
477
            None => self.default_burnchain(),
808,203✔
478
        }
479
    }
859,935✔
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> {
173,765✔
487
        config.node.miner.then(|| {
173,765✔
488
            BitcoinRpcClient::from_stx_config(&config)
173,744✔
489
                .expect("unable to instantiate the RPC client for miner node!")
173,744✔
490
        })
173,744✔
491
    }
173,765✔
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 {
59,103✔
502
        self.rpc_client
59,103✔
503
            .as_ref()
59,103✔
504
            .expect("BUG: BitcoinRpcClient is required, but it has not been configured properly!")
59,103✔
505
    }
59,103✔
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(
379,023✔
567
        &mut self,
379,023✔
568
        block_for_sortitions: bool,
379,023✔
569
        target_block_height_opt: Option<u64>,
379,023✔
570
    ) -> Result<(BurnchainTip, u64), BurnchainControllerError> {
379,023✔
571
        let coordinator_comms = match self.use_coordinator.as_ref() {
379,023✔
572
            Some(x) => x.clone(),
379,023✔
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();
379,023✔
582
        let (block_snapshot, burnchain_height, state_transition) = loop {
378,962✔
583
            if !self.should_keep_running() {
379,097✔
584
                return Err(BurnchainControllerError::CoordinatorClosed);
58✔
585
            }
379,039✔
586

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

597
                    // initialize the dbs...
598
                    self.sortdb_mut();
378,963✔
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 {
378,963✔
603
                        self.wait_for_sortitions(
378,413✔
604
                            coordinator_comms,
378,413✔
605
                            target_block_height_opt.unwrap_or(x.block_height),
378,413✔
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 =
378,962✔
611
                        SortitionDB::get_canonical_burn_chain_tip(self.sortdb_ref().conn())
378,962✔
612
                            .expect("Sortition DB error.");
378,962✔
613

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

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

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

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

668
        if self.config.burnchain.fault_injection_burnchain_block_delay > 0 && received {
378,962✔
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
        }
378,962✔
675

676
        Ok((burnchain_tip, burnchain_height))
378,962✔
677
    }
379,023✔
678

679
    fn should_keep_running(&self) -> bool {
386,073✔
680
        match self.should_keep_running {
386,073✔
681
            Some(ref should_keep_running) => should_keep_running.load(Ordering::SeqCst),
386,073✔
682
            _ => true,
×
683
        }
684
    }
386,073✔
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> {
33✔
694
        const EPOCH: StacksEpochId = StacksEpochId::Epoch21;
695
        let address = self.get_miner_address(EPOCH, public_key);
33✔
696
        let pub_key_rev = self.to_epoch_aware_pubkey(EPOCH, public_key);
33✔
697

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

707
        sleep_ms(1000);
33✔
708

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

714
    /// Retrieve all loaded wallets.
715
    pub fn list_wallets(&self) -> BitcoinRegtestControllerResult<Vec<String>> {
763✔
716
        Ok(self.get_rpc_client().list_wallets()?)
763✔
717
    }
763✔
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<()> {
760✔
722
        let wallets = self.list_wallets()?;
760✔
723
        let wallet = self.get_wallet_name();
760✔
724
        if !wallets.contains(wallet) {
760✔
725
            self.get_rpc_client().create_wallet(wallet, Some(true))?
259✔
726
        }
501✔
727
        Ok(())
760✔
728
    }
760✔
729

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

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

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

753
            // Perform request
754
            match result {
8,706✔
755
                Ok(utxos) => {
8,706✔
756
                    break utxos;
8,706✔
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,706✔
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,681✔
811
            utxos
8,681✔
812
        };
813

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

823
        Some(utxos)
8,692✔
824
    }
8,706✔
825

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

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

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

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

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

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

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

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

878
        increment_btc_ops_sent_counter();
265✔
879

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

885
        Ok(tx)
265✔
886
    }
266✔
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,217✔
1351
        #[cfg(test)]
1352
        {
1353
            if let Some(set_bytes) = *TEST_MAGIC_BYTES
8,217✔
1354
                .lock()
8,217✔
1355
                .expect("FATAL: test magic bytes mutex poisoned")
8,217✔
1356
            {
1357
                return set_bytes.to_vec();
1✔
1358
            }
8,216✔
1359
        }
1360
        self.config.burnchain.magic_bytes.as_bytes().to_vec()
8,216✔
1361
    }
8,217✔
1362

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

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

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

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

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

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

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

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

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

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

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

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

1484
        increment_btc_ops_sent_counter();
8,217✔
1485

1486
        Ok(tx)
8,217✔
1487
    }
9,198✔
1488

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

1503
        let ongoing_op = self.ongoing_block_commit.take().unwrap();
22,473✔
1504

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

1508
        for txid in ongoing_op.txids.iter() {
22,748✔
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);
22,748✔
1511
            let ongoing_tx_confirmed = mined_op.is_some() || self.is_transaction_confirmed(txid);
22,748✔
1512

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

1521
                debug!("Was able to retrieve confirmation of ongoing burnchain TXID - {txid}");
7,439✔
1522
                let res = self.send_block_commit_operation(
7,439✔
1523
                    epoch_id,
7,439✔
1524
                    payload,
7,439✔
1525
                    signer,
7,439✔
1526
                    None,
7,439✔
1527
                    None,
7,439✔
1528
                    None,
7,439✔
1529
                    &[],
7,439✔
1530
                );
1531
                return res;
7,439✔
1532
            } else {
1533
                debug!("Was unable to retrieve ongoing TXID - {txid}");
13,991✔
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;
13,716✔
1539
        let mut burn_chain_tip = burnchain_db
13,716✔
1540
            .get_canonical_chain_tip()
13,716✔
1541
            .map_err(|_| BurnchainControllerError::BurnchainError)?;
13,716✔
1542
        let mut found_last_mined_at = false;
13,716✔
1543
        while traversal_depth < UTXO_CACHE_STALENESS_LIMIT {
13,780✔
1544
            if burn_chain_tip.block_hash == ongoing_op.utxos.bhh {
13,776✔
1545
                found_last_mined_at = true;
13,712✔
1546
                break;
13,712✔
1547
            }
64✔
1548

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

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

1559
        if !found_last_mined_at {
13,716✔
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
        }
13,712✔
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
13,712✔
1571
            > (get_satoshis_per_byte(&self.config) * get_max_rbf(&self.config) / 100)
13,712✔
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
        }
13,712✔
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 {
13,712✔
1587
            info!("Abort attempt to re-submit identical LeaderBlockCommit");
13,396✔
1588
            self.ongoing_block_commit = Some(ongoing_op);
13,396✔
1589
            return Err(BurnchainControllerError::IdenticalOperation);
13,396✔
1590
        }
316✔
1591

1592
        // If we reach this point, we are attempting to RBF the ongoing operation (2)
1593
        info!(
316✔
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(
316✔
1598
            epoch_id,
316✔
1599
            payload,
316✔
1600
            signer,
316✔
1601
            Some(ongoing_op.utxos.clone()),
316✔
1602
            None,
316✔
1603
            Some(ongoing_op.fees.clone()),
316✔
1604
            &ongoing_op.txids,
316✔
1605
        );
1606

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

1611
        res
316✔
1612
    }
23,911✔
1613

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

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

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

1656
            // Fetch some UTXOs
1657
            let addr = self.get_miner_address(epoch_id, public_key);
8,193✔
1658
            match self.get_utxos(
8,193✔
1659
                epoch_id,
8,193✔
1660
                public_key,
8,193✔
1661
                total_required,
8,193✔
1662
                utxos_to_exclude,
8,193✔
1663
                block_height,
8,193✔
1664
            ) {
8,193✔
1665
                Some(utxos) => utxos,
8,191✔
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,508✔
1678
            input: vec![],
8,508✔
1679
            output: vec![],
8,508✔
1680
            version: 1,
8,508✔
1681
            lock_time: 0,
8,508✔
1682
        };
8,508✔
1683

1684
        Ok((transaction, utxos))
8,508✔
1685
    }
9,491✔
1686

1687
    #[allow(clippy::too_many_arguments)]
1688
    fn finalize_tx(
8,509✔
1689
        &mut self,
8,509✔
1690
        epoch_id: StacksEpochId,
8,509✔
1691
        tx: &mut Transaction,
8,509✔
1692
        spent_in_outputs: u64,
8,509✔
1693
        spent_in_rbf: u64,
8,509✔
1694
        min_tx_size: u64,
8,509✔
1695
        fee_rate: u64,
8,509✔
1696
        utxos_set: &mut UTXOSet,
8,509✔
1697
        signer: &mut BurnchainOpSigner,
8,509✔
1698
        force_change_output: bool,
8,509✔
1699
    ) {
8,509✔
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,322,832✔
1703
            if u1.confirmations != u2.confirmations {
6,322,818✔
1704
                u1.confirmations.cmp(&u2.confirmations)
6,322,817✔
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.
1709
                u1.amount.cmp(&u2.amount)
1✔
1710
            }
1711
        });
6,322,818✔
1712

1713
        let tx_size = {
8,509✔
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,509✔
1717
                0
8,193✔
1718
            } else {
1719
                spent_in_rbf + min_tx_size // we're spending 1 sat / byte in RBF
316✔
1720
            };
1721
            let mut tx_cloned = tx.clone();
8,509✔
1722
            let mut utxos_cloned = utxos_set.clone();
8,509✔
1723
            self.serialize_tx(
8,509✔
1724
                epoch_id,
8,509✔
1725
                &mut tx_cloned,
8,509✔
1726
                spent_in_outputs + min_tx_size * fee_rate + estimated_rbf,
8,509✔
1727
                &mut utxos_cloned,
8,509✔
1728
                signer,
8,509✔
1729
                force_change_output,
8,509✔
1730
            );
1731
            let serialized_tx = serialize(&tx_cloned).expect("BUG: failed to serialize to a vec");
8,509✔
1732
            cmp::max(min_tx_size, serialized_tx.len() as u64)
8,509✔
1733
        };
1734

1735
        let rbf_fee = if spent_in_rbf == 0 {
8,509✔
1736
            0
8,193✔
1737
        } else {
1738
            spent_in_rbf + tx_size // we're spending 1 sat / byte in RBF
316✔
1739
        };
1740
        self.serialize_tx(
8,509✔
1741
            epoch_id,
8,509✔
1742
            tx,
8,509✔
1743
            spent_in_outputs + tx_size * fee_rate + rbf_fee,
8,509✔
1744
            utxos_set,
8,509✔
1745
            signer,
8,509✔
1746
            force_change_output,
8,509✔
1747
        );
1748
        signer.dispose();
8,509✔
1749
    }
8,509✔
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,019✔
1756
        &mut self,
17,019✔
1757
        epoch_id: StacksEpochId,
17,019✔
1758
        tx: &mut Transaction,
17,019✔
1759
        tx_cost: u64,
17,019✔
1760
        utxos_set: &mut UTXOSet,
17,019✔
1761
        signer: &mut BurnchainOpSigner,
17,019✔
1762
        force_change_output: bool,
17,019✔
1763
    ) -> bool {
17,019✔
1764
        let mut public_key = signer.get_public_key();
17,019✔
1765

1766
        let total_target = if force_change_output {
17,019✔
1767
            tx_cost + DUST_UTXO_LIMIT
16,965✔
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,019✔
1774
        let mut available_utxos = vec![];
17,019✔
1775
        available_utxos.append(&mut utxos_set.utxos);
17,019✔
1776
        for utxo in available_utxos.into_iter() {
17,022✔
1777
            total_consumed += utxo.amount;
17,022✔
1778
            utxos_set.utxos.push(utxo);
17,022✔
1779

1780
            if total_consumed >= total_target {
17,022✔
1781
                break;
17,019✔
1782
            }
3✔
1783
        }
1784

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

1790
        // Append the change output
1791
        let value = total_consumed - tx_cost;
17,019✔
1792
        debug!(
17,019✔
1793
            "Payments value: {value:?}, total_consumed: {total_consumed:?}, total_spent: {total_target:?}"
1794
        );
1795
        if value >= DUST_UTXO_LIMIT {
17,019✔
1796
            let change_output = if self.config.miner.segwit && epoch_id >= StacksEpochId::Epoch21 {
17,010✔
1797
                // p2wpkh
UNCOV
1798
                public_key.set_compressed(true);
×
UNCOV
1799
                let change_address_hash = Hash160::from_data(&public_key.to_bytes());
×
UNCOV
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,010✔
1804
                LegacyBitcoinAddress::to_p2pkh_tx_out(&change_address_hash, value)
17,010✔
1805
            };
1806
            tx.output.push(change_output);
17,010✔
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,022✔
1813
            let input = TxIn {
17,022✔
1814
                previous_output: OutPoint {
17,022✔
1815
                    txid: utxo.txid.clone(),
17,022✔
1816
                    vout: utxo.vout,
17,022✔
1817
                },
17,022✔
1818
                script_sig: Script::new(),
17,022✔
1819
                sequence: 0xFFFFFFFD, // allow RBF
17,022✔
1820
                witness: vec![],
17,022✔
1821
            };
17,022✔
1822
            tx.input.push(input);
17,022✔
1823
        }
17,022✔
1824
        for (i, utxo) in utxos_set.utxos.iter().enumerate() {
17,022✔
1825
            let script_pub_key = utxo.script_pub_key.clone();
17,022✔
1826
            let sig_hash_all = 0x01;
17,022✔
1827

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

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

1852
            if is_segwit {
17,022✔
UNCOV
1853
                // segwit
×
UNCOV
1854
                public_key.set_compressed(true);
×
UNCOV
1855
                tx.input[i].script_sig = Script::from(vec![]);
×
UNCOV
1856
                tx.input[i].witness = vec![
×
UNCOV
1857
                    [&*sig1_der, &[sig_hash_all as u8][..]].concat().to_vec(),
×
UNCOV
1858
                    public_key.to_bytes(),
×
UNCOV
1859
                ];
×
1860
            } else {
17,022✔
1861
                // legacy scriptSig
17,022✔
1862
                tx.input[i].script_sig = Builder::new()
17,022✔
1863
                    .push_slice(&[&*sig1_der, &[sig_hash_all as u8][..]].concat())
17,022✔
1864
                    .push_slice(&public_key.to_bytes())
17,022✔
1865
                    .into_script();
17,022✔
1866
                tx.input[i].witness.clear();
17,022✔
1867
            }
17,022✔
1868
        }
1869
        true
17,019✔
1870
    }
17,019✔
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,498✔
1884
        debug!(
8,498✔
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,498✔
1892
            .send_raw_transaction(tx, Some(UNCAPPED_FEE), Some(MAX_BURN_AMOUNT))
8,498✔
1893
            .map(|txid| {
8,498✔
1894
                debug!("Transaction {txid} sent successfully");
8,494✔
1895
                txid
8,494✔
1896
            })
8,494✔
1897
            .map_err(|e| {
8,498✔
1898
                error!("Bitcoin RPC error: transaction submission failed - {e:?}");
4✔
1899
                BurnchainControllerError::TransactionSubmissionFailed(format!("{e:?}"))
4✔
1900
            })
4✔
1901
    }
8,498✔
1902

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

1915
            if debug_ctr % 10 == 0 {
386,179✔
1916
                debug!(
379,233✔
1917
                    "Waiting until canonical sortition height reaches {height_to_wait} (currently {})",
1918
                    canonical_sortition_tip.block_height
1919
                );
1920
            }
6,946✔
1921
            debug_ctr += 1;
386,179✔
1922

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

1930
                return Ok(BurnchainTip {
379,203✔
1931
                    block_snapshot: canonical_sortition_tip,
379,203✔
1932
                    received_at: Instant::now(),
379,203✔
1933
                    state_transition,
379,203✔
1934
                });
379,203✔
1935
            }
6,976✔
1936

1937
            if !self.should_keep_running() {
6,976✔
1938
                return Err(BurnchainControllerError::CoordinatorClosed);
1✔
1939
            }
6,975✔
1940

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

1945
            // yield some time
1946
            sleep_ms(1000);
6,975✔
1947
        }
1948
    }
379,204✔
1949

1950
    /// Instruct a regtest Bitcoin node to build the next block.
1951
    pub fn build_next_block(&self, num_blocks: u64) {
8,202✔
1952
        debug!("Generate {num_blocks} block(s)");
8,202✔
1953
        let public_key_bytes = match &self.config.burnchain.local_mining_public_key {
8,202✔
1954
            Some(public_key) => hex_bytes(public_key).expect("Invalid byte sequence"),
8,202✔
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,202✔
1960
            .expect("FATAL: invalid public key bytes");
8,202✔
1961
        let address = self.get_miner_address(StacksEpochId::Epoch21, &public_key);
8,202✔
1962

1963
        let result = self
8,202✔
1964
            .get_rpc_client()
8,202✔
1965
            .generate_to_address(num_blocks, &address);
8,202✔
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,202✔
1976
            Ok(_) => {}
8,202✔
1977
            Err(e) => {
×
1978
                error!("Bitcoin RPC failure: error generating block {e:?}");
×
1979
                panic!();
×
1980
            }
1981
        }
1982
    }
8,202✔
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)]
UNCOV
2026
    pub fn set_mining_pubkey(&mut self, pubkey: String) -> Option<String> {
×
UNCOV
2027
        let old_key = self.config.burnchain.local_mining_public_key.take();
×
UNCOV
2028
        self.config.burnchain.local_mining_public_key = Some(pubkey);
×
UNCOV
2029
        old_key
×
UNCOV
2030
    }
×
2031

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

2037
    // TODO: add tests from mutation testing results #4866
2038
    #[cfg_attr(test, mutants::skip)]
2039
    fn make_operation_tx(
24,190✔
2040
        &mut self,
24,190✔
2041
        epoch_id: StacksEpochId,
24,190✔
2042
        operation: BlockstackOperationType,
24,190✔
2043
        op_signer: &mut BurnchainOpSigner,
24,190✔
2044
    ) -> Result<Transaction, BurnchainControllerError> {
24,190✔
2045
        match operation {
24,190✔
2046
            BlockstackOperationType::LeaderBlockCommit(payload) => {
23,902✔
2047
                self.build_leader_block_commit_tx(epoch_id, payload, op_signer)
23,902✔
2048
            }
2049
            BlockstackOperationType::LeaderKeyRegister(payload) => {
264✔
2050
                self.build_leader_key_register_tx(epoch_id, payload, op_signer)
264✔
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
    }
24,190✔
2069

2070
    /// Retrieves a raw [`Transaction`] by its [`Txid`]
2071
    #[cfg(test)]
2072
    pub fn get_raw_transaction(&self, txid: &Txid) -> Transaction {
15✔
2073
        self.get_rpc_client()
15✔
2074
            .get_raw_transaction(txid)
15✔
2075
            .unwrap_or_log_panic("retrieve raw tx")
15✔
2076
    }
15✔
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]) {
258✔
2082
        info!("Creating wallet if it does not exist");
258✔
2083
        if let Err(e) = self.create_wallet_if_dne() {
258✔
2084
            error!("Error when creating wallet: {e:?}");
×
2085
        }
258✔
2086

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

2094
        if pks.len() == 1 {
258✔
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]);
219✔
2097
            debug!(
219✔
2098
                "Generate to address '{address}' for public key '{}'",
2099
                &pks[0].to_hex()
×
2100
            );
2101
            self.get_rpc_client()
219✔
2102
                .generate_to_address(num_blocks, &address)
219✔
2103
                .ok_or_log_panic("generating block");
219✔
2104
            return;
219✔
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
    }
258✔
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 {
15,347✔
2136
        match self
15,347✔
2137
            .get_rpc_client()
15,347✔
2138
            .get_transaction(self.get_wallet_name(), txid)
15,347✔
2139
        {
2140
            Ok(info) => info.confirmations > 0,
14,860✔
2141
            Err(e) => {
487✔
2142
                error!("Bitcoin RPC failure: checking tx confirmation {e:?}");
487✔
2143
                false
487✔
2144
            }
2145
        }
2146
    }
15,347✔
2147

2148
    /// Returns the configured wallet name from [`Config`].
2149
    fn get_wallet_name(&self) -> &String {
25,236✔
2150
        &self.config.burnchain.wallet_name
25,236✔
2151
    }
25,236✔
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(
359✔
2160
        &self,
359✔
2161
        public_key: &Secp256k1PublicKey,
359✔
2162
    ) -> BitcoinRegtestControllerResult<()> {
359✔
2163
        let pkh = Hash160::from_data(&public_key.to_bytes())
359✔
2164
            .to_bytes()
359✔
2165
            .to_vec();
359✔
2166
        let (_, network_id) = self.config.burnchain.get_bitcoin_network();
359✔
2167

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

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

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

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

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

2198
            self.get_rpc_client()
360✔
2199
                .import_descriptors(self.get_wallet_name(), &[&descr_req])?;
360✔
2200
        }
2201
        Ok(())
359✔
2202
    }
359✔
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,743✔
2220
        &self,
8,743✔
2221
        epoch_id: StacksEpochId,
8,743✔
2222
        public_key: &Secp256k1PublicKey,
8,743✔
2223
    ) -> Secp256k1PublicKey {
8,743✔
2224
        let mut reviewed = public_key.clone();
8,743✔
2225
        if self.config.miner.segwit && epoch_id >= StacksEpochId::Epoch21 {
8,743✔
2226
            reviewed.set_compressed(true);
1✔
2227
        }
8,742✔
2228
        return reviewed;
8,743✔
2229
    }
8,743✔
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,769✔
2252
        &self,
8,769✔
2253
        address: &BitcoinAddress,
8,769✔
2254
        include_unsafe: bool,
8,769✔
2255
        minimum_sum_amount: u64,
8,769✔
2256
        utxos_to_exclude: &Option<UTXOSet>,
8,769✔
2257
        block_height: u64,
8,769✔
2258
    ) -> BitcoinRpcClientResult<UTXOSet> {
8,769✔
2259
        let bhh = self.get_rpc_client().get_block_hash(block_height)?;
8,769✔
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,769✔
2264
            &self.get_wallet_name(),
8,769✔
2265
            Some(MIN_CONFIRMATIONS),
8,769✔
2266
            Some(MAX_CONFIRMATIONS),
8,769✔
2267
            Some(&[address]),
8,769✔
2268
            Some(include_unsafe),
8,769✔
2269
            Some(minimum_sum_amount),
8,769✔
2270
            self.config.burnchain.max_unspent_utxos.clone(),
8,769✔
2271
        )?;
×
2272

2273
        let txids_to_exclude = utxos_to_exclude.as_ref().map_or_else(HashSet::new, |set| {
8,769✔
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,769✔
2281
            .into_iter()
8,769✔
2282
            .filter(|each| !txids_to_exclude.contains(&each.txid))
915,156✔
2283
            .filter(|each| each.amount >= minimum_sum_amount)
915,066✔
2284
            .map(|each| UTXO {
8,769✔
2285
                txid: Txid::to_bitcoin_tx_hash(&each.txid),
915,039✔
2286
                vout: each.vout,
915,039✔
2287
                script_pub_key: each.script_pub_key,
915,039✔
2288
                amount: each.amount,
915,039✔
2289
                confirmations: each.confirmations,
915,039✔
2290
            })
915,039✔
2291
            .collect::<Vec<_>>();
8,769✔
2292
        Ok(UTXOSet { bhh, utxos })
8,769✔
2293
    }
8,769✔
2294
}
2295

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

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

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

2310
        match self.db {
477,716✔
2311
            Some(ref mut sortdb) => sortdb,
477,716✔
UNCOV
2312
            None => unreachable!(),
×
2313
        }
2314
    }
477,716✔
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 {
379,494✔
2326
        let (_, network_id) = self.config.burnchain.get_bitcoin_network();
379,494✔
2327
        let spv_client = SpvClient::new(
379,494✔
2328
            &self.config.get_spv_headers_file_path(),
379,494✔
2329
            0,
2330
            None,
379,494✔
2331
            network_id,
379,494✔
2332
            false,
2333
            false,
2334
        )
2335
        .expect("Unable to open burnchain headers DB");
379,494✔
2336
        spv_client
379,494✔
2337
            .get_headers_height()
379,494✔
2338
            .expect("Unable to query number of burnchain headers")
379,494✔
2339
    }
379,494✔
2340

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

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

2356
    fn start(
517✔
2357
        &mut self,
517✔
2358
        target_block_height_opt: Option<u64>,
517✔
2359
    ) -> Result<(BurnchainTip, u64), BurnchainControllerError> {
517✔
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))
517✔
2362
    }
517✔
2363

2364
    fn sync(
378,506✔
2365
        &mut self,
378,506✔
2366
        target_block_height_opt: Option<u64>,
378,506✔
2367
    ) -> Result<(BurnchainTip, u64), BurnchainControllerError> {
378,506✔
2368
        let (burnchain_tip, burnchain_height) = if self.config.burnchain.mode == "helium" {
378,506✔
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)?
378,506✔
2375
        };
2376

2377
        // Evaluate process_exit_at_block_height setting
2378
        if let Some(cap) = self.config.burnchain.process_exit_at_block_height {
378,447✔
2379
            if burnchain_tip.block_snapshot.block_height >= cap {
35✔
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
            }
35✔
2385
        }
378,412✔
2386
        Ok((burnchain_tip, burnchain_height))
378,447✔
2387
    }
378,506✔
2388

2389
    // returns true if the operation was submitted successfully, false otherwise
2390
    fn submit_operation(
24,187✔
2391
        &mut self,
24,187✔
2392
        epoch_id: StacksEpochId,
24,187✔
2393
        operation: BlockstackOperationType,
24,187✔
2394
        op_signer: &mut BurnchainOpSigner,
24,187✔
2395
    ) -> Result<Txid, BurnchainControllerError> {
24,187✔
2396
        let transaction = self.make_operation_tx(epoch_id, operation, op_signer)?;
24,187✔
2397
        self.send_transaction(&transaction)
8,494✔
2398
    }
24,187✔
2399

2400
    #[cfg(test)]
2401
    fn bootstrap_chain(&self, num_blocks: u64) {
120✔
2402
        let Some(ref local_mining_pubkey) = &self.config.burnchain.local_mining_public_key else {
120✔
2403
            warn!("No local mining pubkey while bootstrapping bitcoin regtest, will not generate bitcoin blocks");
1✔
2404
            return;
1✔
2405
        };
2406

2407
        // NOTE: miner address is whatever the miner's segwit setting says it is here
2408
        let mut local_mining_pubkey = Secp256k1PublicKey::from_hex(local_mining_pubkey).unwrap();
119✔
2409

2410
        if self.config.miner.segwit {
119✔
UNCOV
2411
            local_mining_pubkey.set_compressed(true);
×
2412
        }
119✔
2413

2414
        self.bootstrap_chain_to_pks(num_blocks, &[local_mining_pubkey])
119✔
2415
    }
120✔
2416
}
2417

2418
#[derive(Debug, Clone)]
2419
pub struct UTXOSet {
2420
    bhh: BurnchainHeaderHash,
2421
    utxos: Vec<UTXO>,
2422
}
2423

2424
impl UTXOSet {
2425
    pub fn is_empty(&self) -> bool {
8,731✔
2426
        self.utxos.len() == 0
8,731✔
2427
    }
8,731✔
2428

2429
    pub fn total_available(&self) -> u64 {
8,696✔
2430
        self.utxos.iter().map(|o| o.amount).sum()
8,696✔
2431
    }
8,696✔
2432

2433
    pub fn num_utxos(&self) -> usize {
6✔
2434
        self.utxos.len()
6✔
2435
    }
6✔
2436
}
2437

2438
#[derive(Clone, Debug, PartialEq)]
2439
pub struct UTXO {
2440
    pub txid: Sha256dHash,
2441
    pub vout: u32,
2442
    pub script_pub_key: Script,
2443
    pub amount: u64,
2444
    pub confirmations: u32,
2445
}
2446

2447
#[cfg(test)]
2448
mod tests {
2449
    use std::env::{self, temp_dir};
2450
    use std::fs::File;
2451
    use std::io::Write;
2452
    use std::panic::{self, AssertUnwindSafe};
2453

2454
    use stacks::burnchains::BurnchainSigner;
2455
    use stacks::config::DEFAULT_SATS_PER_VB;
2456
    use stacks_common::deps_common::bitcoin::blockdata::script::Builder;
2457
    use stacks_common::types::chainstate::{BlockHeaderHash, StacksAddress, VRFSeed};
2458
    use stacks_common::util::hash::to_hex;
2459
    use stacks_common::util::secp256k1::Secp256k1PrivateKey;
2460

2461
    use super::*;
2462
    use crate::burnchains::bitcoin::core_controller::BitcoinCoreController;
2463
    use crate::burnchains::bitcoin_regtest_controller::tests::utils::{
2464
        create_follower_config, create_miner_config, to_address_legacy,
2465
    };
2466
    use crate::Keychain;
2467

2468
    mod utils {
2469
        use std::net::TcpListener;
2470

2471
        use stacks::burnchains::MagicBytes;
2472
        use stacks::chainstate::burn::ConsensusHash;
2473
        use stacks::util::vrf::{VRFPrivateKey, VRFPublicKey};
2474

2475
        use super::*;
2476
        use crate::burnchains::bitcoin::core_controller::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
2477
        use crate::util::get_epoch_time_nanos;
2478

2479
        pub fn create_miner_config() -> Config {
37✔
2480
            let mut config = Config::default();
37✔
2481
            config.node.miner = true;
37✔
2482
            config.burnchain.magic_bytes = "T3".as_bytes().into();
37✔
2483
            config.burnchain.username = Some(String::from("user"));
37✔
2484
            config.burnchain.password = Some(String::from("12345"));
37✔
2485
            // overriding default "0.0.0.0" because doesn't play nicely on Windows.
2486
            config.burnchain.peer_host = String::from("127.0.0.1");
37✔
2487
            // avoiding peer port biding to reduce the number of ports to bind to.
2488
            config.burnchain.peer_port = BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
37✔
2489

2490
            //Ask the OS for a free port. Not guaranteed to stay free,
2491
            //after TcpListner is dropped, but good enough for testing
2492
            //and starting bitcoind right after config is created
2493
            let tmp_listener =
37✔
2494
                TcpListener::bind("127.0.0.1:0").expect("Failed to bind to get a free port");
37✔
2495
            let port = tmp_listener.local_addr().unwrap().port();
37✔
2496

2497
            config.burnchain.rpc_port = port;
37✔
2498

2499
            let now = get_epoch_time_nanos();
37✔
2500
            let dir = format!("/tmp/regtest-ctrl-{port}-{now}");
37✔
2501
            config.node.working_dir = dir;
37✔
2502

2503
            config
37✔
2504
        }
37✔
2505

2506
        pub fn create_keychain() -> Keychain {
15✔
2507
            create_keychain_with_seed(1)
15✔
2508
        }
15✔
2509

2510
        pub fn create_keychain_with_seed(value: u8) -> Keychain {
32✔
2511
            let seed = vec![value; 4];
32✔
2512
            let keychain = Keychain::default(seed);
32✔
2513
            keychain
32✔
2514
        }
32✔
2515

2516
        pub fn create_miner1_pubkey() -> Secp256k1PublicKey {
15✔
2517
            create_keychain_with_seed(1).get_pub_key()
15✔
2518
        }
15✔
2519

2520
        pub fn create_miner2_pubkey() -> Secp256k1PublicKey {
2✔
2521
            create_keychain_with_seed(2).get_pub_key()
2✔
2522
        }
2✔
2523

2524
        pub fn to_address_legacy(pub_key: &Secp256k1PublicKey) -> BitcoinAddress {
6✔
2525
            let hash160 = Hash160::from_data(&pub_key.to_bytes());
6✔
2526
            BitcoinAddress::from_bytes_legacy(
6✔
2527
                BitcoinNetworkType::Regtest,
6✔
2528
                LegacyBitcoinAddressType::PublicKeyHash,
6✔
2529
                &hash160.0,
6✔
2530
            )
2531
            .expect("Public key incorrect")
6✔
2532
        }
6✔
2533

2534
        pub fn to_address_segwit_p2wpkh(pub_key: &Secp256k1PublicKey) -> BitcoinAddress {
1✔
2535
            // pub_key.to_byte_compressed() equivalent to pub_key.set_compressed(true) + pub_key.to_bytes()
2536
            let hash160 = Hash160::from_data(&pub_key.to_bytes_compressed());
1✔
2537
            BitcoinAddress::from_bytes_segwit_p2wpkh(BitcoinNetworkType::Regtest, &hash160.0)
1✔
2538
                .expect("Public key incorrect")
1✔
2539
        }
1✔
2540

2541
        pub fn mine_tx(btc_controller: &BitcoinRegtestController, tx: &Transaction) {
2✔
2542
            btc_controller
2✔
2543
                .send_transaction(tx)
2✔
2544
                .expect("Tx should be sent to the burnchain!");
2✔
2545
            btc_controller.build_next_block(1); // Now tx is confirmed
2✔
2546
        }
2✔
2547

2548
        pub fn create_templated_commit_op() -> LeaderBlockCommitOp {
7✔
2549
            LeaderBlockCommitOp {
7✔
2550
                block_header_hash: BlockHeaderHash::from_hex(
7✔
2551
                    "e88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32af",
7✔
2552
                )
7✔
2553
                .unwrap(),
7✔
2554
                new_seed: VRFSeed::from_hex(
7✔
2555
                    "d5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375",
7✔
2556
                )
7✔
2557
                .unwrap(),
7✔
2558
                parent_block_ptr: 2211, // 0x000008a3
7✔
2559
                parent_vtxindex: 1,     // 0x0001
7✔
2560
                key_block_ptr: 1432,    // 0x00000598
7✔
2561
                key_vtxindex: 1,        // 0x0001
7✔
2562
                memo: vec![11],         // 0x5a >> 3
7✔
2563

7✔
2564
                burn_fee: 110_000, //relevant for fee calculation when sending the tx
7✔
2565
                input: (Txid([0x00; 32]), 0),
7✔
2566
                burn_parent_modulus: 2, // 0x5a & 0b111
7✔
2567

7✔
2568
                apparent_sender: BurnchainSigner("mgbpit8FvkVJ9kuXY8QSM5P7eibnhcEMBk".to_string()),
7✔
2569
                commit_outs: vec![
7✔
2570
                    PoxAddress::Standard(StacksAddress::burn_address(false), None),
7✔
2571
                    PoxAddress::Standard(StacksAddress::burn_address(false), None),
7✔
2572
                ],
7✔
2573

7✔
2574
                treatment: vec![],
7✔
2575
                sunset_burn: 5_500, //relevant for fee calculation when sending the tx
7✔
2576

7✔
2577
                txid: Txid([0x00; 32]),
7✔
2578
                vtxindex: 0,
7✔
2579
                block_height: 2212,
7✔
2580
                burn_header_hash: BurnchainHeaderHash([0x01; 32]),
7✔
2581
            }
7✔
2582
        }
7✔
2583

2584
        pub fn txout_opreturn<T: StacksMessageCodec>(
5✔
2585
            op: &T,
5✔
2586
            magic: &MagicBytes,
5✔
2587
            value: u64,
5✔
2588
        ) -> TxOut {
5✔
2589
            let op_bytes = {
5✔
2590
                let mut buffer = vec![];
5✔
2591
                let mut magic_bytes = magic.as_bytes().to_vec();
5✔
2592
                buffer.append(&mut magic_bytes);
5✔
2593
                op.consensus_serialize(&mut buffer)
5✔
2594
                    .expect("FATAL: invalid operation");
5✔
2595
                buffer
5✔
2596
            };
2597

2598
            TxOut {
5✔
2599
                value,
5✔
2600
                script_pubkey: Builder::new()
5✔
2601
                    .push_opcode(opcodes::All::OP_RETURN)
5✔
2602
                    .push_slice(&op_bytes)
5✔
2603
                    .into_script(),
5✔
2604
            }
5✔
2605
        }
5✔
2606

2607
        pub fn txout_opdup_commit_to(addr: &PoxAddress, amount: u64) -> TxOut {
6✔
2608
            addr.to_bitcoin_tx_out(amount)
6✔
2609
        }
6✔
2610

2611
        pub fn txout_opdup_change_legacy(signer: &mut BurnchainOpSigner, amount: u64) -> TxOut {
5✔
2612
            let public_key = signer.get_public_key();
5✔
2613
            let change_address_hash = Hash160::from_data(&public_key.to_bytes());
5✔
2614
            LegacyBitcoinAddress::to_p2pkh_tx_out(&change_address_hash, amount)
5✔
2615
        }
5✔
2616

2617
        pub fn txin_at_index(
5✔
2618
            complete_tx: &Transaction,
5✔
2619
            signer: &BurnchainOpSigner,
5✔
2620
            utxos: &[UTXO],
5✔
2621
            index: usize,
5✔
2622
        ) -> TxIn {
5✔
2623
            //Refresh op signer
2624
            let mut signer = signer.undisposed();
5✔
2625
            let mut public_key = signer.get_public_key();
5✔
2626

2627
            let mut tx = Transaction {
5✔
2628
                version: complete_tx.version,
5✔
2629
                lock_time: complete_tx.lock_time,
5✔
2630
                input: vec![],
5✔
2631
                output: complete_tx.output.clone(),
5✔
2632
            };
5✔
2633

2634
            for utxo in utxos.iter() {
5✔
2635
                let input = TxIn {
5✔
2636
                    previous_output: OutPoint {
5✔
2637
                        txid: utxo.txid.clone(),
5✔
2638
                        vout: utxo.vout,
5✔
2639
                    },
5✔
2640
                    script_sig: Script::new(),
5✔
2641
                    sequence: 0xFFFFFFFD, // allow RBF
5✔
2642
                    witness: vec![],
5✔
2643
                };
5✔
2644
                tx.input.push(input);
5✔
2645
            }
5✔
2646

2647
            for (i, utxo) in utxos.iter().enumerate() {
5✔
2648
                let script_pub_key = utxo.script_pub_key.clone();
5✔
2649
                let sig_hash_all = 0x01;
5✔
2650

2651
                let (sig_hash, is_segwit) = if script_pub_key.as_bytes().len() == 22
5✔
2652
                    && script_pub_key.as_bytes()[0..2] == [0x00, 0x14]
×
2653
                {
2654
                    // p2wpkh
2655
                    (
×
2656
                        tx.segwit_signature_hash(i, &script_pub_key, utxo.amount, sig_hash_all),
×
2657
                        true,
×
2658
                    )
×
2659
                } else {
2660
                    // p2pkh
2661
                    (tx.signature_hash(i, &script_pub_key, sig_hash_all), false)
5✔
2662
                };
2663

2664
                let sig1_der = {
5✔
2665
                    let message = signer
5✔
2666
                        .sign_message(sig_hash.as_bytes())
5✔
2667
                        .expect("Unable to sign message");
5✔
2668
                    message
5✔
2669
                        .to_secp256k1_recoverable()
5✔
2670
                        .expect("Unable to get recoverable signature")
5✔
2671
                        .to_standard()
5✔
2672
                        .serialize_der()
5✔
2673
                };
2674

2675
                if is_segwit {
5✔
2676
                    // segwit
×
2677
                    public_key.set_compressed(true);
×
2678
                    tx.input[i].script_sig = Script::from(vec![]);
×
2679
                    tx.input[i].witness = vec![
×
2680
                        [&*sig1_der, &[sig_hash_all as u8][..]].concat().to_vec(),
×
2681
                        public_key.to_bytes(),
×
2682
                    ];
×
2683
                } else {
5✔
2684
                    // legacy scriptSig
5✔
2685
                    tx.input[i].script_sig = Builder::new()
5✔
2686
                        .push_slice(&[&*sig1_der, &[sig_hash_all as u8][..]].concat())
5✔
2687
                        .push_slice(&public_key.to_bytes())
5✔
2688
                        .into_script();
5✔
2689
                    tx.input[i].witness.clear();
5✔
2690
                }
5✔
2691
            }
2692

2693
            tx.input[index].clone()
5✔
2694
        }
5✔
2695

2696
        pub fn create_templated_leader_key_op() -> LeaderKeyRegisterOp {
4✔
2697
            LeaderKeyRegisterOp {
4✔
2698
                consensus_hash: ConsensusHash([0u8; 20]),
4✔
2699
                public_key: VRFPublicKey::from_private(
4✔
2700
                    &VRFPrivateKey::from_bytes(&[0u8; 32]).unwrap(),
4✔
2701
                ),
4✔
2702
                memo: vec![],
4✔
2703
                txid: Txid([3u8; 32]),
4✔
2704
                vtxindex: 0,
4✔
2705
                block_height: 1,
4✔
2706
                burn_header_hash: BurnchainHeaderHash([9u8; 32]),
4✔
2707
            }
4✔
2708
        }
4✔
2709

2710
        pub fn create_templated_pre_stx_op() -> PreStxOp {
4✔
2711
            PreStxOp {
4✔
2712
                output: StacksAddress::p2pkh_from_hash(false, Hash160::from_data(&[2u8; 20])),
4✔
2713
                txid: Txid([0u8; 32]),
4✔
2714
                vtxindex: 0,
4✔
2715
                block_height: 0,
4✔
2716
                burn_header_hash: BurnchainHeaderHash([0u8; 32]),
4✔
2717
            }
4✔
2718
        }
4✔
2719

2720
        pub fn create_follower_config() -> Config {
2✔
2721
            let mut config = Config::default();
2✔
2722
            config.node.miner = false;
2✔
2723
            config.burnchain.magic_bytes = "T3".as_bytes().into();
2✔
2724
            config.burnchain.username = None;
2✔
2725
            config.burnchain.password = None;
2✔
2726
            config.burnchain.peer_host = String::from("127.0.0.1");
2✔
2727
            config.burnchain.peer_port = 8333;
2✔
2728
            config.node.working_dir = format!("/tmp/follower");
2✔
2729
            config
2✔
2730
        }
2✔
2731
    }
2732

2733
    #[test]
2734
    fn test_get_satoshis_per_byte() {
1✔
2735
        let dir = temp_dir();
1✔
2736
        let file_path = dir.as_path().join("config.toml");
1✔
2737

2738
        let mut config = Config::default();
1✔
2739

2740
        let satoshis_per_byte = get_satoshis_per_byte(&config);
1✔
2741
        assert_eq!(satoshis_per_byte, DEFAULT_SATS_PER_VB);
1✔
2742

2743
        let mut file = File::create(&file_path).unwrap();
1✔
2744
        writeln!(file, "[burnchain]").unwrap();
1✔
2745
        writeln!(file, "satoshis_per_byte = 51").unwrap();
1✔
2746
        config.config_path = Some(file_path.to_str().unwrap().to_string());
1✔
2747

2748
        assert_eq!(get_satoshis_per_byte(&config), 51);
1✔
2749
    }
1✔
2750

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

2785
        // test serialize_tx()
2786
        let config = utils::create_miner_config();
1✔
2787

2788
        let mut btc_controller = BitcoinRegtestController::new(config, None);
1✔
2789
        let mut utxo_set = UTXOSet {
1✔
2790
            bhh: BurnchainHeaderHash([0x01; 32]),
1✔
2791
            utxos: spend_utxos.clone(),
1✔
2792
        };
1✔
2793
        let mut transaction = Transaction {
1✔
2794
            input: vec![],
1✔
2795
            output: vec![
1✔
2796
                TxOut {
1✔
2797
                    value: 0,
1✔
2798
                    script_pubkey: Builder::from(hex_bytes("6a4c5054335be88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32afd5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375000008a300010000059800015a").unwrap()).into_script(),
1✔
2799
                },
1✔
2800
                TxOut {
1✔
2801
                    value: 10000,
1✔
2802
                    script_pubkey: Builder::from(hex_bytes("76a914000000000000000000000000000000000000000088ac").unwrap()).into_script(),
1✔
2803
                },
1✔
2804
                TxOut {
1✔
2805
                    value: 10000,
1✔
2806
                    script_pubkey: Builder::from(hex_bytes("76a914000000000000000000000000000000000000000088ac").unwrap()).into_script(),
1✔
2807
                },
1✔
2808
            ],
1✔
2809
            version: 1,
1✔
2810
            lock_time: 0,
1✔
2811
        };
1✔
2812

2813
        let mut signer = BurnchainOpSigner::new(
1✔
2814
            Secp256k1PrivateKey::from_hex(
1✔
2815
                "9e446f6b0c6a96cf2190e54bcd5a8569c3e386f091605499464389b8d4e0bfc201",
1✔
2816
            )
2817
            .unwrap(),
1✔
2818
        );
2819
        assert!(btc_controller.serialize_tx(
1✔
2820
            StacksEpochId::Epoch25,
1✔
2821
            &mut transaction,
1✔
2822
            44950,
2823
            &mut utxo_set,
1✔
2824
            &mut signer,
1✔
2825
            true
2826
        ));
2827
        assert_eq!(transaction.output[3].value, 323557);
1✔
2828

2829
        // test send_block_commit_operation_at_burn_height()
2830
        let utxo_set = UTXOSet {
1✔
2831
            bhh: BurnchainHeaderHash([0x01; 32]),
1✔
2832
            utxos: spend_utxos,
1✔
2833
        };
1✔
2834

2835
        let commit_op = LeaderBlockCommitOp {
1✔
2836
            block_header_hash: BlockHeaderHash::from_hex(
1✔
2837
                "e88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32af",
1✔
2838
            )
1✔
2839
            .unwrap(),
1✔
2840
            new_seed: VRFSeed::from_hex(
1✔
2841
                "d5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375",
1✔
2842
            )
1✔
2843
            .unwrap(),
1✔
2844
            parent_block_ptr: 2211, // 0x000008a3
1✔
2845
            parent_vtxindex: 1,     // 0x0001
1✔
2846
            key_block_ptr: 1432,    // 0x00000598
1✔
2847
            key_vtxindex: 1,        // 0x0001
1✔
2848
            memo: vec![11],         // 0x5a >> 3
1✔
2849

1✔
2850
            burn_fee: 0,
1✔
2851
            input: (Txid([0x00; 32]), 0),
1✔
2852
            burn_parent_modulus: 2, // 0x5a & 0b111
1✔
2853

1✔
2854
            apparent_sender: BurnchainSigner("mgbpit8FvkVJ9kuXY8QSM5P7eibnhcEMBk".to_string()),
1✔
2855
            commit_outs: vec![
1✔
2856
                PoxAddress::Standard(StacksAddress::burn_address(false), None),
1✔
2857
                PoxAddress::Standard(StacksAddress::burn_address(false), None),
1✔
2858
            ],
1✔
2859

1✔
2860
            treatment: vec![],
1✔
2861
            sunset_burn: 0,
1✔
2862

1✔
2863
            txid: Txid([0x00; 32]),
1✔
2864
            vtxindex: 0,
1✔
2865
            block_height: 2212,
1✔
2866
            burn_header_hash: BurnchainHeaderHash([0x01; 32]),
1✔
2867
        };
1✔
2868

2869
        assert_eq!(to_hex(&commit_op.serialize_to_vec()), "5be88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32afd5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375000008a300010000059800015a".to_string());
1✔
2870

2871
        let leader_fees = LeaderBlockCommitFees {
1✔
2872
            sunset_fee: 0,
1✔
2873
            fee_rate: 50,
1✔
2874
            sortition_fee: 20000,
1✔
2875
            outputs_len: 2,
1✔
2876
            default_tx_size: 380,
1✔
2877
            spent_in_attempts: 0,
1✔
2878
            is_rbf_enabled: false,
1✔
2879
            final_size: 498,
1✔
2880
        };
1✔
2881

2882
        assert_eq!(leader_fees.amount_per_output(), 10000);
1✔
2883
        assert_eq!(leader_fees.total_spent(), 44900);
1✔
2884

2885
        let block_commit = btc_controller
1✔
2886
            .send_block_commit_operation_at_burnchain_height(
1✔
2887
                StacksEpochId::Epoch30,
1✔
2888
                commit_op,
1✔
2889
                &mut signer,
1✔
2890
                Some(utxo_set),
1✔
2891
                None,
1✔
2892
                leader_fees,
1✔
2893
                &[],
1✔
2894
                2212,
2895
            )
2896
            .unwrap();
1✔
2897

2898
        debug!("send_block_commit_operation:\n{block_commit:#?}");
1✔
2899
        assert_eq!(block_commit.output[3].value, 323507);
1✔
2900
        assert_eq!(serialize_hex(&block_commit).unwrap(), "0100000002eeda098987728e4a2e21b34b74000dcb0bd0e4d20e55735492ec3cba3afbead3030000006a4730440220558286e20e10ce31537f0625dae5cc62fac7961b9d2cf272c990de96323d7e2502202255adbea3d2e0509b80c5d8a3a4fe6397a87bcf18da1852740d5267d89a0cb20121035379aa40c02890d253cfa577964116eb5295570ae9f7287cbae5f2585f5b2c7cfdffffff243b0b329a5889ab8801b315eea19810848d4c2133e0245671cc984a2d2f1301000000006a47304402206d9f8de107f9e1eb15aafac66c2bb34331a7523260b30e18779257e367048d34022013c7dabb32a5c281aa00d405e2ccbd00f34f03a65b2336553a4acd6c52c251ef0121035379aa40c02890d253cfa577964116eb5295570ae9f7287cbae5f2585f5b2c7cfdffffff040000000000000000536a4c5054335be88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32afd5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375000008a300010000059800015a10270000000000001976a914000000000000000000000000000000000000000088ac10270000000000001976a914000000000000000000000000000000000000000088acb3ef0400000000001976a9141dc27eba0247f8cc9575e7d45e50a0bc7e72427d88ac00000000");
1✔
2901
    }
1✔
2902

2903
    #[test]
2904
    fn test_to_epoch_aware_pubkey() {
1✔
2905
        let mut config = utils::create_miner_config();
1✔
2906
        let pubkey = utils::create_miner1_pubkey();
1✔
2907

2908
        config.miner.segwit = false;
1✔
2909
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
2910

2911
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch20, &pubkey);
1✔
2912
        assert_eq!(
1✔
2913
            false,
2914
            reviewed.compressed(),
1✔
2915
            "Segwit disabled with Epoch < 2.1: not compressed"
2916
        );
2917
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch21, &pubkey);
1✔
2918
        assert_eq!(
1✔
2919
            false,
2920
            reviewed.compressed(),
1✔
2921
            "Segwit disabled with Epoch >= 2.1: not compressed"
2922
        );
2923

2924
        config.miner.segwit = true;
1✔
2925
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
2926

2927
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch20, &pubkey);
1✔
2928
        assert_eq!(
1✔
2929
            false,
2930
            reviewed.compressed(),
1✔
2931
            "Segwit enabled with Epoch < 2.1: not compressed"
2932
        );
2933
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch21, &pubkey);
1✔
2934
        assert_eq!(
1✔
2935
            true,
2936
            reviewed.compressed(),
1✔
2937
            "Segwit enabled with Epoch >= 2.1: compressed"
2938
        );
2939
    }
1✔
2940

2941
    #[test]
2942
    fn test_get_miner_address() {
1✔
2943
        let mut config = utils::create_miner_config();
1✔
2944
        let pub_key = utils::create_miner1_pubkey();
1✔
2945

2946
        config.miner.segwit = false;
1✔
2947
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
2948

2949
        let expected = utils::to_address_legacy(&pub_key);
1✔
2950
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch20, &pub_key);
1✔
2951
        assert_eq!(
1✔
2952
            expected, address,
2953
            "Segwit disabled with Epoch < 2.1: legacy addr"
2954
        );
2955

2956
        let expected = utils::to_address_legacy(&pub_key);
1✔
2957
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch21, &pub_key);
1✔
2958
        assert_eq!(
1✔
2959
            expected, address,
2960
            "Segwit disabled with Epoch >= 2.1: legacy addr"
2961
        );
2962

2963
        config.miner.segwit = true;
1✔
2964
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
2965

2966
        let expected = utils::to_address_legacy(&pub_key);
1✔
2967
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch20, &pub_key);
1✔
2968
        assert_eq!(
1✔
2969
            expected, address,
2970
            "Segwit enabled with Epoch < 2.1: legacy addr"
2971
        );
2972

2973
        let expected = utils::to_address_segwit_p2wpkh(&pub_key);
1✔
2974
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch21, &pub_key);
1✔
2975
        assert_eq!(
1✔
2976
            expected, address,
2977
            "Segwit enabled with Epoch >= 2.1: segwit addr"
2978
        );
2979
    }
1✔
2980

2981
    #[test]
2982
    fn test_instantiate_with_burnchain_on_follower_node_ok() {
1✔
2983
        let config = create_follower_config();
1✔
2984

2985
        let btc_controller = BitcoinRegtestController::with_burnchain(config, None, None, None);
1✔
2986

2987
        let result = panic::catch_unwind(AssertUnwindSafe(|| {
1✔
2988
            _ = btc_controller.get_rpc_client();
1✔
2989
        }));
1✔
2990
        assert!(
1✔
2991
            result.is_err(),
1✔
2992
            "Invoking any Bitcoin RPC related method should panic."
2993
        );
2994
    }
1✔
2995

2996
    #[test]
2997
    fn test_instantiate_with_burnchain_on_miner_node_ok() {
1✔
2998
        let config = create_miner_config();
1✔
2999

3000
        let btc_controller = BitcoinRegtestController::with_burnchain(config, None, None, None);
1✔
3001

3002
        let _ = btc_controller.get_rpc_client();
1✔
3003
        assert!(true, "Invoking any Bitcoin RPC related method should work.");
1✔
3004
    }
1✔
3005

3006
    #[test]
3007
    fn test_instantiate_with_burnchain_on_miner_node_failure() {
1✔
3008
        let mut config = create_miner_config();
1✔
3009
        config.burnchain.username = None;
1✔
3010
        config.burnchain.password = None;
1✔
3011

3012
        let result = panic::catch_unwind(|| {
1✔
3013
            _ = BitcoinRegtestController::with_burnchain(config, None, None, None);
1✔
3014
        });
1✔
3015
        assert!(
1✔
3016
            result.is_err(),
1✔
3017
            "Bitcoin RPC credentials are mandatory for miner node."
3018
        );
3019
    }
1✔
3020

3021
    #[test]
3022
    fn test_instantiate_new_dummy_on_follower_node_ok() {
1✔
3023
        let config = create_follower_config();
1✔
3024

3025
        let btc_controller = BitcoinRegtestController::new_dummy(config);
1✔
3026

3027
        let result = panic::catch_unwind(AssertUnwindSafe(|| {
1✔
3028
            _ = btc_controller.get_rpc_client();
1✔
3029
        }));
1✔
3030
        assert!(
1✔
3031
            result.is_err(),
1✔
3032
            "Invoking any Bitcoin RPC related method should panic."
3033
        );
3034
    }
1✔
3035

3036
    #[test]
3037
    fn test_instantiate_new_dummy_on_miner_node_ok() {
1✔
3038
        let config = create_miner_config();
1✔
3039

3040
        let btc_controller = BitcoinRegtestController::new_dummy(config);
1✔
3041

3042
        let _ = btc_controller.get_rpc_client();
1✔
3043
        assert!(true, "Invoking any Bitcoin RPC related method should work.");
1✔
3044
    }
1✔
3045

3046
    #[test]
3047
    fn test_instantiate_new_dummy_on_miner_node_failure() {
1✔
3048
        let mut config = create_miner_config();
1✔
3049
        config.burnchain.username = None;
1✔
3050
        config.burnchain.password = None;
1✔
3051

3052
        let result = panic::catch_unwind(|| {
1✔
3053
            _ = BitcoinRegtestController::new_dummy(config);
1✔
3054
        });
1✔
3055
        assert!(
1✔
3056
            result.is_err(),
1✔
3057
            "Bitcoin RPC credentials are mandatory for miner node."
3058
        );
3059
    }
1✔
3060

3061
    #[test]
3062
    #[ignore]
3063
    fn test_create_wallet_from_default_empty_name() {
1✔
3064
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3065
            return;
×
3066
        }
1✔
3067

3068
        let config = utils::create_miner_config();
1✔
3069

3070
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3071
        btcd_controller
1✔
3072
            .start_bitcoind()
1✔
3073
            .expect("bitcoind should be started!");
1✔
3074

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

3077
        let wallets = btc_controller.list_wallets().unwrap();
1✔
3078
        assert_eq!(0, wallets.len());
1✔
3079

3080
        btc_controller
1✔
3081
            .create_wallet_if_dne()
1✔
3082
            .expect("Wallet should now exists!");
1✔
3083

3084
        let wallets = btc_controller.list_wallets().unwrap();
1✔
3085
        assert_eq!(1, wallets.len());
1✔
3086
        assert_eq!("".to_owned(), wallets[0]);
1✔
3087
    }
1✔
3088

3089
    #[test]
3090
    #[ignore]
3091
    fn test_create_wallet_from_custom_name() {
1✔
3092
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3093
            return;
×
3094
        }
1✔
3095

3096
        let mut config = utils::create_miner_config();
1✔
3097
        config.burnchain.wallet_name = String::from("mywallet");
1✔
3098

3099
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3100
        btcd_controller
1✔
3101
            .start_bitcoind()
1✔
3102
            .expect("bitcoind should be started!");
1✔
3103

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

3106
        btc_controller
1✔
3107
            .create_wallet_if_dne()
1✔
3108
            .expect("Wallet should now exists!");
1✔
3109

3110
        let wallets = btc_controller.list_wallets().unwrap();
1✔
3111
        assert_eq!(1, wallets.len());
1✔
3112
        assert_eq!("mywallet".to_owned(), wallets[0]);
1✔
3113
    }
1✔
3114

3115
    #[test]
3116
    #[ignore]
3117
    fn test_retrieve_utxo_set_with_all_utxos() {
1✔
3118
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3119
            return;
×
3120
        }
1✔
3121

3122
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3123

3124
        let mut config = utils::create_miner_config();
1✔
3125
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3126

3127
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3128
        btcd_controller
1✔
3129
            .start_bitcoind()
1✔
3130
            .expect("Failed starting bitcoind");
1✔
3131

3132
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3133
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3134

3135
        let address = to_address_legacy(&miner_pubkey);
1✔
3136
        let utxo_set = btc_controller
1✔
3137
            .retrieve_utxo_set(&address, false, 0, &None, 0)
1✔
3138
            .expect("Failed to get utxos");
1✔
3139
        assert_eq!(btc_controller.get_block_hash(0), utxo_set.bhh);
1✔
3140
        assert_eq!(50, utxo_set.num_utxos());
1✔
3141
    }
1✔
3142

3143
    #[test]
3144
    #[ignore]
3145
    fn test_retrive_utxo_set_excluding_some_utxo() {
1✔
3146
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3147
            return;
×
3148
        }
1✔
3149

3150
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3151

3152
        let mut config = utils::create_miner_config();
1✔
3153
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3154

3155
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3156
        btcd_controller
1✔
3157
            .start_bitcoind()
1✔
3158
            .expect("Failed starting bitcoind");
1✔
3159

3160
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3161
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3162

3163
        let address = to_address_legacy(&miner_pubkey);
1✔
3164
        let mut all_utxos = btc_controller
1✔
3165
            .retrieve_utxo_set(&address, false, 0, &None, 0)
1✔
3166
            .expect("Failed to get utxos (50)");
1✔
3167

3168
        let filtered_utxos = btc_controller
1✔
3169
            .retrieve_utxo_set(&address, false, 0, &Some(all_utxos.clone()), 0)
1✔
3170
            .expect("Failed to get utxos");
1✔
3171
        assert_eq!(0, filtered_utxos.num_utxos(), "all utxos filtered out!");
1✔
3172

3173
        all_utxos.utxos.drain(0..10);
1✔
3174
        let filtered_utxos = btc_controller
1✔
3175
            .retrieve_utxo_set(&address, false, 0, &Some(all_utxos), 0)
1✔
3176
            .expect("Failed to get utxos");
1✔
3177
        assert_eq!(10, filtered_utxos.num_utxos(), "40 utxos filtered out!");
1✔
3178
    }
1✔
3179

3180
    #[test]
3181
    #[ignore]
3182
    fn test_list_unspent_with_max_utxos_config() {
1✔
3183
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3184
            return;
×
3185
        }
1✔
3186

3187
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3188

3189
        let mut config = utils::create_miner_config();
1✔
3190
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3191
        config.burnchain.max_unspent_utxos = Some(10);
1✔
3192

3193
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3194
        btcd_controller
1✔
3195
            .start_bitcoind()
1✔
3196
            .expect("Failed starting bitcoind");
1✔
3197

3198
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3199
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3200

3201
        let address = to_address_legacy(&miner_pubkey);
1✔
3202
        let utxos = btc_controller
1✔
3203
            .retrieve_utxo_set(&address, false, 1, &None, 0)
1✔
3204
            .expect("Failed to get utxos");
1✔
3205
        assert_eq!(10, utxos.num_utxos());
1✔
3206
    }
1✔
3207

3208
    #[test]
3209
    #[ignore]
3210
    fn test_get_all_utxos_with_confirmation() {
1✔
3211
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3212
            return;
×
3213
        }
1✔
3214

3215
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3216

3217
        let mut config = utils::create_miner_config();
1✔
3218
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3219

3220
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3221
        btcd_controller
1✔
3222
            .start_bitcoind()
1✔
3223
            .expect("bitcoind should be started!");
1✔
3224

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

3227
        btc_controller.bootstrap_chain(100);
1✔
3228
        let utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3229
        assert_eq!(0, utxos.len());
1✔
3230

3231
        btc_controller.build_next_block(1);
1✔
3232
        let utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3233
        assert_eq!(1, utxos.len());
1✔
3234
        assert_eq!(101, utxos[0].confirmations);
1✔
3235
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3236

3237
        btc_controller.build_next_block(1);
1✔
3238
        let mut utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3239
        utxos.sort_by(|a, b| b.confirmations.cmp(&a.confirmations));
1✔
3240

3241
        assert_eq!(2, utxos.len());
1✔
3242
        assert_eq!(102, utxos[0].confirmations);
1✔
3243
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3244
        assert_eq!(101, utxos[1].confirmations);
1✔
3245
        assert_eq!(5_000_000_000, utxos[1].amount);
1✔
3246
    }
1✔
3247

3248
    #[test]
3249
    #[ignore]
3250
    fn test_get_all_utxos_for_other_pubkey() {
1✔
3251
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3252
            return;
×
3253
        }
1✔
3254

3255
        let miner1_pubkey = utils::create_miner1_pubkey();
1✔
3256
        let miner2_pubkey = utils::create_miner2_pubkey();
1✔
3257

3258
        let mut config = utils::create_miner_config();
1✔
3259
        config.burnchain.local_mining_public_key = Some(miner1_pubkey.to_hex());
1✔
3260

3261
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3262
        btcd_controller
1✔
3263
            .start_bitcoind()
1✔
3264
            .expect("bitcoind should be started!");
1✔
3265

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

3269
        config.burnchain.local_mining_public_key = Some(miner2_pubkey.to_hex());
1✔
3270
        config.burnchain.wallet_name = "miner2_wallet".to_string();
1✔
3271
        let miner2_btc_controller = BitcoinRegtestController::new(config, None);
1✔
3272
        miner2_btc_controller.bootstrap_chain(102); // two utxo for other_pubkeys related address
1✔
3273

3274
        let utxos = miner1_btc_controller.get_all_utxos(&miner1_pubkey);
1✔
3275
        assert_eq!(1, utxos.len(), "miner1 see its own utxos");
1✔
3276

3277
        let utxos = miner1_btc_controller.get_all_utxos(&miner2_pubkey);
1✔
3278
        assert_eq!(2, utxos.len(), "miner1 see miner2 utxos");
1✔
3279

3280
        let utxos = miner2_btc_controller.get_all_utxos(&miner2_pubkey);
1✔
3281
        assert_eq!(2, utxos.len(), "miner2 see its own utxos");
1✔
3282

3283
        let utxos = miner2_btc_controller.get_all_utxos(&miner1_pubkey);
1✔
3284
        assert_eq!(1, utxos.len(), "miner2 see miner1 own utxos");
1✔
3285
    }
1✔
3286

3287
    #[test]
3288
    #[ignore]
3289
    fn test_get_utxos_ok_with_confirmation() {
1✔
3290
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3291
            return;
×
3292
        }
1✔
3293

3294
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3295

3296
        let mut config = utils::create_miner_config();
1✔
3297
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3298

3299
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3300
        btcd_controller
1✔
3301
            .start_bitcoind()
1✔
3302
            .expect("bitcoind should be started!");
1✔
3303

3304
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3305
        btc_controller.bootstrap_chain(101);
1✔
3306

3307
        let utxos_opt =
1✔
3308
            btc_controller.get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 1, None, 101);
1✔
3309
        let uxto_set = utxos_opt.expect("Shouldn't be None at height 101!");
1✔
3310

3311
        assert_eq!(btc_controller.get_block_hash(101), uxto_set.bhh);
1✔
3312
        assert_eq!(1, uxto_set.num_utxos());
1✔
3313
        assert_eq!(5_000_000_000, uxto_set.total_available());
1✔
3314
        let utxos = uxto_set.utxos;
1✔
3315
        assert_eq!(101, utxos[0].confirmations);
1✔
3316
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3317

3318
        btc_controller.build_next_block(1);
1✔
3319

3320
        let utxos_opt =
1✔
3321
            btc_controller.get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 1, None, 102);
1✔
3322
        let uxto_set = utxos_opt.expect("Shouldn't be None at height 102!");
1✔
3323

3324
        assert_eq!(btc_controller.get_block_hash(102), uxto_set.bhh);
1✔
3325
        assert_eq!(2, uxto_set.num_utxos());
1✔
3326
        assert_eq!(10_000_000_000, uxto_set.total_available());
1✔
3327
        let mut utxos = uxto_set.utxos;
1✔
3328
        utxos.sort_by(|a, b| b.confirmations.cmp(&a.confirmations));
1✔
3329
        assert_eq!(102, utxos[0].confirmations);
1✔
3330
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3331
        assert_eq!(101, utxos[1].confirmations);
1✔
3332
        assert_eq!(5_000_000_000, utxos[1].amount);
1✔
3333
    }
1✔
3334

3335
    #[test]
3336
    #[ignore]
3337
    fn test_get_utxos_none_due_to_filter_total_required() {
1✔
3338
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3339
            return;
×
3340
        }
1✔
3341

3342
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3343

3344
        let mut config = utils::create_miner_config();
1✔
3345
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3346

3347
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3348
        btcd_controller
1✔
3349
            .start_bitcoind()
1✔
3350
            .expect("bitcoind should be started!");
1✔
3351

3352
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3353
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3354

3355
        let too_much_required = 10_000_000_000;
1✔
3356
        let utxos = btc_controller.get_utxos(
1✔
3357
            StacksEpochId::Epoch31,
1✔
3358
            &miner_pubkey,
1✔
3359
            too_much_required,
1✔
3360
            None,
1✔
3361
            0,
3362
        );
3363
        assert!(utxos.is_none(), "None because too much required");
1✔
3364
    }
1✔
3365

3366
    #[test]
3367
    #[ignore]
3368
    fn test_get_utxos_none_due_to_filter_pubkey() {
1✔
3369
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3370
            return;
×
3371
        }
1✔
3372

3373
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3374

3375
        let mut config = utils::create_miner_config();
1✔
3376
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3377

3378
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3379
        btcd_controller
1✔
3380
            .start_bitcoind()
1✔
3381
            .expect("bitcoind should be started!");
1✔
3382

3383
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3384
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3385

3386
        let other_pubkey = utils::create_miner2_pubkey();
1✔
3387
        let utxos = btc_controller.get_utxos(StacksEpochId::Epoch31, &other_pubkey, 1, None, 0);
1✔
3388
        assert!(
1✔
3389
            utxos.is_none(),
1✔
3390
            "None because utxos for other pubkey don't exist"
3391
        );
3392
    }
1✔
3393

3394
    #[test]
3395
    #[ignore]
3396
    fn test_get_utxos_none_due_to_filter_utxo_exclusion() {
1✔
3397
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3398
            return;
×
3399
        }
1✔
3400

3401
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3402

3403
        let mut config = utils::create_miner_config();
1✔
3404
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3405

3406
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3407
        btcd_controller
1✔
3408
            .start_bitcoind()
1✔
3409
            .expect("bitcoind should be started!");
1✔
3410

3411
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3412
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3413

3414
        let existent_utxo = btc_controller
1✔
3415
            .get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 0, None, 0)
1✔
3416
            .expect("utxo set should exist");
1✔
3417
        let utxos = btc_controller.get_utxos(
1✔
3418
            StacksEpochId::Epoch31,
1✔
3419
            &miner_pubkey,
1✔
3420
            0,
3421
            Some(existent_utxo),
1✔
3422
            0,
3423
        );
3424
        assert!(
1✔
3425
            utxos.is_none(),
1✔
3426
            "None because filtering exclude existent utxo set"
3427
        );
3428
    }
1✔
3429

3430
    #[test]
3431
    #[ignore]
3432
    fn test_tx_confirmed_from_utxo_ok() {
1✔
3433
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3434
            return;
×
3435
        }
1✔
3436

3437
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3438

3439
        let mut config = utils::create_miner_config();
1✔
3440
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3441

3442
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3443
        btcd_controller
1✔
3444
            .start_bitcoind()
1✔
3445
            .expect("bitcoind should be started!");
1✔
3446

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

3449
        btc_controller.bootstrap_chain(101);
1✔
3450
        let utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3451
        assert_eq!(1, utxos.len(), "One UTXO should be confirmed!");
1✔
3452

3453
        let txid = Txid::from_bitcoin_tx_hash(&utxos[0].txid);
1✔
3454
        assert!(
1✔
3455
            btc_controller.is_transaction_confirmed(&txid),
1✔
3456
            "UTXO tx should be confirmed!"
3457
        );
3458
    }
1✔
3459

3460
    #[test]
3461
    #[ignore]
3462
    fn test_import_public_key_ok() {
1✔
3463
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3464
            return;
×
3465
        }
1✔
3466

3467
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3468

3469
        let config = utils::create_miner_config();
1✔
3470

3471
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3472
        btcd_controller
1✔
3473
            .start_bitcoind()
1✔
3474
            .expect("bitcoind should be started!");
1✔
3475

3476
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3477
        btc_controller
1✔
3478
            .create_wallet_if_dne()
1✔
3479
            .expect("Wallet should be created!");
1✔
3480

3481
        let result = btc_controller.import_public_key(&miner_pubkey);
1✔
3482
        assert!(
1✔
3483
            result.is_ok(),
1✔
3484
            "Should be ok, got err instead: {:?}",
3485
            result.unwrap_err()
×
3486
        );
3487
    }
1✔
3488

3489
    #[test]
3490
    #[ignore]
3491
    fn test_import_public_key_twice_ok() {
1✔
3492
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3493
            return;
×
3494
        }
1✔
3495

3496
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3497

3498
        let config = utils::create_miner_config();
1✔
3499

3500
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3501
        btcd_controller
1✔
3502
            .start_bitcoind()
1✔
3503
            .expect("bitcoind should be started!");
1✔
3504

3505
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3506
        btc_controller
1✔
3507
            .create_wallet_if_dne()
1✔
3508
            .expect("Wallet should be created!");
1✔
3509

3510
        btc_controller
1✔
3511
            .import_public_key(&miner_pubkey)
1✔
3512
            .expect("Import should be ok: first time!");
1✔
3513

3514
        //ok, but it is basically a no-op
3515
        let result = btc_controller.import_public_key(&miner_pubkey);
1✔
3516
        assert!(
1✔
3517
            result.is_ok(),
1✔
3518
            "Should be ok, got err instead: {:?}",
3519
            result.unwrap_err()
×
3520
        );
3521
    }
1✔
3522

3523
    #[test]
3524
    #[ignore]
3525
    fn test_import_public_key_segwit_ok() {
1✔
3526
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3527
            return;
×
3528
        }
1✔
3529

3530
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3531

3532
        let mut config = utils::create_miner_config();
1✔
3533
        config.miner.segwit = true;
1✔
3534

3535
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3536
        btcd_controller
1✔
3537
            .start_bitcoind()
1✔
3538
            .expect("bitcoind should be started!");
1✔
3539

3540
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3541
        btc_controller
1✔
3542
            .create_wallet_if_dne()
1✔
3543
            .expect("Wallet should be created!");
1✔
3544

3545
        let result = btc_controller.import_public_key(&miner_pubkey);
1✔
3546
        assert!(
1✔
3547
            result.is_ok(),
1✔
3548
            "Should be ok, got err instead: {:?}",
3549
            result.unwrap_err()
×
3550
        );
3551
    }
1✔
3552

3553
    /// Tests related to Leader Block Commit operation
3554
    mod leader_commit_op {
3555
        use super::*;
3556

3557
        #[test]
3558
        #[ignore]
3559
        fn test_build_leader_block_commit_tx_ok_with_new_commit_op() {
1✔
3560
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3561
                return;
×
3562
            }
1✔
3563

3564
            let keychain = utils::create_keychain();
1✔
3565
            let miner_pubkey = keychain.get_pub_key();
1✔
3566
            let mut op_signer = keychain.generate_op_signer();
1✔
3567

3568
            let mut config = utils::create_miner_config();
1✔
3569
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3570
            config.burnchain.pox_reward_length = Some(11);
1✔
3571

3572
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3573
            btcd_controller
1✔
3574
                .start_bitcoind()
1✔
3575
                .expect("bitcoind should be started!");
1✔
3576

3577
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3578
            btc_controller
1✔
3579
                .connect_dbs()
1✔
3580
                .expect("Dbs initialization required!");
1✔
3581
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3582

3583
            let mut commit_op = utils::create_templated_commit_op();
1✔
3584
            commit_op.sunset_burn = 5_500;
1✔
3585
            commit_op.burn_fee = 110_000;
1✔
3586

3587
            let tx = btc_controller
1✔
3588
                .build_leader_block_commit_tx(
1✔
3589
                    StacksEpochId::Epoch31,
1✔
3590
                    commit_op.clone(),
1✔
3591
                    &mut op_signer,
1✔
3592
                )
3593
                .expect("Build leader block commit should work");
1✔
3594

3595
            assert!(op_signer.is_disposed());
1✔
3596

3597
            assert_eq!(1, tx.version);
1✔
3598
            assert_eq!(0, tx.lock_time);
1✔
3599
            assert_eq!(1, tx.input.len());
1✔
3600
            assert_eq!(4, tx.output.len());
1✔
3601

3602
            // utxos list contains the only existing utxo
3603
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3604
            let input_0 = utils::txin_at_index(&tx, &op_signer, &used_utxos, 0);
1✔
3605
            assert_eq!(input_0, tx.input[0]);
1✔
3606

3607
            let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes, 5_500);
1✔
3608
            let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_000);
1✔
3609
            let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_000);
1✔
3610
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 4_999_865_300);
1✔
3611
            assert_eq!(op_return, tx.output[0]);
1✔
3612
            assert_eq!(op_commit_1, tx.output[1]);
1✔
3613
            assert_eq!(op_commit_2, tx.output[2]);
1✔
3614
            assert_eq!(op_change, tx.output[3]);
1✔
3615
        }
1✔
3616

3617
        #[test]
3618
        #[ignore]
3619
        fn test_build_leader_block_commit_tx_fails_resub_same_commit_op_while_prev_not_confirmed() {
1✔
3620
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3621
                return;
×
3622
            }
1✔
3623

3624
            let keychain = utils::create_keychain();
1✔
3625
            let miner_pubkey = keychain.get_pub_key();
1✔
3626
            let mut op_signer = keychain.generate_op_signer();
1✔
3627

3628
            let mut config = utils::create_miner_config();
1✔
3629
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3630
            config.burnchain.pox_reward_length = Some(11);
1✔
3631

3632
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3633
            btcd_controller
1✔
3634
                .start_bitcoind()
1✔
3635
                .expect("bitcoind should be started!");
1✔
3636

3637
            let mut btc_controller = BitcoinRegtestController::new(config, None);
1✔
3638
            btc_controller
1✔
3639
                .connect_dbs()
1✔
3640
                .expect("Dbs initialization required!");
1✔
3641
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3642

3643
            let commit_op = utils::create_templated_commit_op();
1✔
3644

3645
            let _first_tx_ok = btc_controller
1✔
3646
                .build_leader_block_commit_tx(
1✔
3647
                    StacksEpochId::Epoch31,
1✔
3648
                    commit_op.clone(),
1✔
3649
                    &mut op_signer,
1✔
3650
                )
3651
                .expect("At first, building leader block commit should work");
1✔
3652

3653
            // re-submitting same commit while previous it is not confirmed by the burnchain
3654
            let resubmit = btc_controller.build_leader_block_commit_tx(
1✔
3655
                StacksEpochId::Epoch31,
1✔
3656
                commit_op,
1✔
3657
                &mut op_signer,
1✔
3658
            );
3659

3660
            assert!(resubmit.is_err());
1✔
3661
            assert_eq!(
1✔
3662
                BurnchainControllerError::IdenticalOperation,
3663
                resubmit.unwrap_err()
1✔
3664
            );
3665
        }
1✔
3666

3667
        #[test]
3668
        #[ignore]
3669
        fn test_build_leader_block_commit_tx_fails_resub_same_commit_op_while_prev_is_confirmed() {
1✔
3670
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3671
                return;
×
3672
            }
1✔
3673

3674
            let keychain = utils::create_keychain();
1✔
3675
            let miner_pubkey = keychain.get_pub_key();
1✔
3676
            let mut op_signer = keychain.generate_op_signer();
1✔
3677

3678
            let mut config = utils::create_miner_config();
1✔
3679
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3680
            config.burnchain.pox_reward_length = Some(11);
1✔
3681

3682
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3683
            btcd_controller
1✔
3684
                .start_bitcoind()
1✔
3685
                .expect("bitcoind should be started!");
1✔
3686

3687
            let mut btc_controller = BitcoinRegtestController::new(config, None);
1✔
3688
            btc_controller
1✔
3689
                .connect_dbs()
1✔
3690
                .expect("Dbs initialization required!");
1✔
3691
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3692

3693
            let commit_op = utils::create_templated_commit_op();
1✔
3694

3695
            let first_tx_ok = btc_controller
1✔
3696
                .build_leader_block_commit_tx(
1✔
3697
                    StacksEpochId::Epoch31,
1✔
3698
                    commit_op.clone(),
1✔
3699
                    &mut op_signer,
1✔
3700
                )
3701
                .expect("At first, building leader block commit should work");
1✔
3702

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

3705
            // re-submitting same commit while previous it is confirmed by the burnchain
3706
            let resubmit = btc_controller.build_leader_block_commit_tx(
1✔
3707
                StacksEpochId::Epoch31,
1✔
3708
                commit_op,
1✔
3709
                &mut op_signer,
1✔
3710
            );
3711

3712
            assert!(resubmit.is_err());
1✔
3713
            assert_eq!(
1✔
3714
                BurnchainControllerError::IdenticalOperation,
3715
                resubmit.unwrap_err()
1✔
3716
            );
3717
        }
1✔
3718

3719
        #[test]
3720
        #[ignore]
3721
        fn test_build_leader_block_commit_tx_ok_while_prev_is_confirmed() {
1✔
3722
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3723
                return;
×
3724
            }
1✔
3725

3726
            let keychain = utils::create_keychain();
1✔
3727
            let miner_pubkey = keychain.get_pub_key();
1✔
3728
            let mut op_signer = keychain.generate_op_signer();
1✔
3729

3730
            let mut config = utils::create_miner_config();
1✔
3731
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3732
            config.burnchain.pox_reward_length = Some(11);
1✔
3733

3734
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3735
            btcd_controller
1✔
3736
                .start_bitcoind()
1✔
3737
                .expect("bitcoind should be started!");
1✔
3738

3739
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3740
            btc_controller
1✔
3741
                .connect_dbs()
1✔
3742
                .expect("Dbs initialization required!");
1✔
3743
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3744

3745
            let mut commit_op = utils::create_templated_commit_op();
1✔
3746
            commit_op.sunset_burn = 5_500;
1✔
3747
            commit_op.burn_fee = 110_000;
1✔
3748

3749
            let first_tx_ok = btc_controller
1✔
3750
                .build_leader_block_commit_tx(
1✔
3751
                    StacksEpochId::Epoch31,
1✔
3752
                    commit_op.clone(),
1✔
3753
                    &mut op_signer,
1✔
3754
                )
3755
                .expect("At first, building leader block commit should work");
1✔
3756

3757
            let first_txid = first_tx_ok.txid();
1✔
3758

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

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

3767
            let new_tx = btc_controller
1✔
3768
                .build_leader_block_commit_tx(
1✔
3769
                    StacksEpochId::Epoch31,
1✔
3770
                    commit_op.clone(),
1✔
3771
                    &mut signer,
1✔
3772
                )
3773
                .expect("Commit tx should be created!");
1✔
3774

3775
            assert!(op_signer.is_disposed());
1✔
3776

3777
            assert_eq!(1, new_tx.version);
1✔
3778
            assert_eq!(0, new_tx.lock_time);
1✔
3779
            assert_eq!(1, new_tx.input.len());
1✔
3780
            assert_eq!(4, new_tx.output.len());
1✔
3781

3782
            // utxos list contains the sole utxo used by prev commit operation
3783
            // because has enough amount to cover the new commit
3784
            let used_utxos: Vec<UTXO> = btc_controller
1✔
3785
                .get_all_utxos(&miner_pubkey)
1✔
3786
                .into_iter()
1✔
3787
                .filter(|utxo| utxo.txid == first_txid)
2✔
3788
                .collect();
1✔
3789

3790
            let input_0 = utils::txin_at_index(&new_tx, &op_signer, &used_utxos, 0);
1✔
3791
            assert_eq!(input_0, new_tx.input[0]);
1✔
3792

3793
            let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes, 5_500);
1✔
3794
            let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_005);
1✔
3795
            let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_005);
1✔
3796
            let op_change = utils::txout_opdup_change_legacy(&mut signer, 4_999_730_590);
1✔
3797
            assert_eq!(op_return, new_tx.output[0]);
1✔
3798
            assert_eq!(op_commit_1, new_tx.output[1]);
1✔
3799
            assert_eq!(op_commit_2, new_tx.output[2]);
1✔
3800
            assert_eq!(op_change, new_tx.output[3]);
1✔
3801
        }
1✔
3802

3803
        #[test]
3804
        #[ignore]
3805
        fn test_build_leader_block_commit_tx_ok_rbf_while_prev_not_confirmed() {
1✔
3806
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3807
                return;
×
3808
            }
1✔
3809

3810
            let keychain = utils::create_keychain();
1✔
3811
            let miner_pubkey = keychain.get_pub_key();
1✔
3812
            let mut op_signer = keychain.generate_op_signer();
1✔
3813

3814
            let mut config = utils::create_miner_config();
1✔
3815
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3816
            config.burnchain.pox_reward_length = Some(11);
1✔
3817

3818
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3819
            btcd_controller
1✔
3820
                .start_bitcoind()
1✔
3821
                .expect("bitcoind should be started!");
1✔
3822

3823
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3824
            btc_controller
1✔
3825
                .connect_dbs()
1✔
3826
                .expect("Dbs initialization required!");
1✔
3827
            btc_controller.bootstrap_chain(101); // Now, one utxo exists
1✔
3828

3829
            let mut commit_op = utils::create_templated_commit_op();
1✔
3830
            commit_op.sunset_burn = 5_500;
1✔
3831
            commit_op.burn_fee = 110_000;
1✔
3832

3833
            let _first_tx_ok = btc_controller
1✔
3834
                .build_leader_block_commit_tx(
1✔
3835
                    StacksEpochId::Epoch31,
1✔
3836
                    commit_op.clone(),
1✔
3837
                    &mut op_signer,
1✔
3838
                )
3839
                .expect("At first, building leader block commit should work");
1✔
3840

3841
            //re-gen signer othewise fails because it will be disposed during previous commit tx.
3842
            let mut signer = keychain.generate_op_signer();
1✔
3843
            //small change to the commit op payload
3844
            commit_op.burn_fee += 10;
1✔
3845

3846
            let rbf_tx = btc_controller
1✔
3847
                .build_leader_block_commit_tx(
1✔
3848
                    StacksEpochId::Epoch31,
1✔
3849
                    commit_op.clone(),
1✔
3850
                    &mut signer,
1✔
3851
                )
3852
                .expect("Commit tx should be rbf-ed");
1✔
3853

3854
            assert!(op_signer.is_disposed());
1✔
3855

3856
            assert_eq!(1, rbf_tx.version);
1✔
3857
            assert_eq!(0, rbf_tx.lock_time);
1✔
3858
            assert_eq!(1, rbf_tx.input.len());
1✔
3859
            assert_eq!(4, rbf_tx.output.len());
1✔
3860

3861
            // utxos list contains the only existing utxo
3862
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3863

3864
            let input_0 = utils::txin_at_index(&rbf_tx, &op_signer, &used_utxos, 0);
1✔
3865
            assert_eq!(input_0, rbf_tx.input[0]);
1✔
3866

3867
            let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes, 5_500);
1✔
3868
            let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_005);
1✔
3869
            let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_005);
1✔
3870
            let op_change = utils::txout_opdup_change_legacy(&mut signer, 4_999_862_985);
1✔
3871
            assert_eq!(op_return, rbf_tx.output[0]);
1✔
3872
            assert_eq!(op_commit_1, rbf_tx.output[1]);
1✔
3873
            assert_eq!(op_commit_2, rbf_tx.output[2]);
1✔
3874
            assert_eq!(op_change, rbf_tx.output[3]);
1✔
3875
        }
1✔
3876

3877
        #[test]
3878
        #[ignore]
3879
        fn test_make_operation_leader_block_commit_tx_ok() {
1✔
3880
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3881
                return;
×
3882
            }
1✔
3883

3884
            let keychain = utils::create_keychain();
1✔
3885
            let miner_pubkey = keychain.get_pub_key();
1✔
3886
            let mut op_signer = keychain.generate_op_signer();
1✔
3887

3888
            let mut config = utils::create_miner_config();
1✔
3889
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3890
            config.burnchain.pox_reward_length = Some(11);
1✔
3891

3892
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3893
            btcd_controller
1✔
3894
                .start_bitcoind()
1✔
3895
                .expect("bitcoind should be started!");
1✔
3896

3897
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3898
            btc_controller
1✔
3899
                .connect_dbs()
1✔
3900
                .expect("Dbs initialization required!");
1✔
3901
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3902

3903
            let mut commit_op = utils::create_templated_commit_op();
1✔
3904
            commit_op.sunset_burn = 5_500;
1✔
3905
            commit_op.burn_fee = 110_000;
1✔
3906

3907
            let tx = btc_controller
1✔
3908
                .make_operation_tx(
1✔
3909
                    StacksEpochId::Epoch31,
1✔
3910
                    BlockstackOperationType::LeaderBlockCommit(commit_op),
1✔
3911
                    &mut op_signer,
1✔
3912
                )
3913
                .expect("Make op should work");
1✔
3914

3915
            assert!(op_signer.is_disposed());
1✔
3916

3917
            assert_eq!(
1✔
3918
                "1a74106bd760117892fbd90fca11646b4de46f99fd2b065c9e0706cfdcea0336",
3919
                tx.txid().to_string()
1✔
3920
            );
3921
        }
1✔
3922

3923
        #[test]
3924
        #[ignore]
3925
        fn test_submit_leader_block_commit_tx_ok() {
1✔
3926
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3927
                return;
×
3928
            }
1✔
3929

3930
            let keychain = utils::create_keychain();
1✔
3931
            let miner_pubkey = keychain.get_pub_key();
1✔
3932
            let mut op_signer = keychain.generate_op_signer();
1✔
3933

3934
            let mut config = utils::create_miner_config();
1✔
3935
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3936
            config.burnchain.pox_reward_length = Some(11);
1✔
3937

3938
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3939
            btcd_controller
1✔
3940
                .start_bitcoind()
1✔
3941
                .expect("bitcoind should be started!");
1✔
3942

3943
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3944
            btc_controller
1✔
3945
                .connect_dbs()
1✔
3946
                .expect("Dbs initialization required!");
1✔
3947
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3948

3949
            let mut commit_op = utils::create_templated_commit_op();
1✔
3950
            commit_op.sunset_burn = 5_500;
1✔
3951
            commit_op.burn_fee = 110_000;
1✔
3952

3953
            let tx_id = btc_controller
1✔
3954
                .submit_operation(
1✔
3955
                    StacksEpochId::Epoch31,
1✔
3956
                    BlockstackOperationType::LeaderBlockCommit(commit_op),
1✔
3957
                    &mut op_signer,
1✔
3958
                )
3959
                .expect("Submit op should work");
1✔
3960

3961
            assert!(op_signer.is_disposed());
1✔
3962

3963
            assert_eq!(
1✔
3964
                "1a74106bd760117892fbd90fca11646b4de46f99fd2b065c9e0706cfdcea0336",
3965
                tx_id.to_hex()
1✔
3966
            );
3967
        }
1✔
3968
    }
3969

3970
    /// Tests related to Leader Key Register operation
3971
    mod leader_key_op {
3972
        use super::*;
3973

3974
        #[test]
3975
        #[ignore]
3976
        fn test_build_leader_key_tx_ok() {
1✔
3977
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3978
                return;
×
3979
            }
1✔
3980

3981
            let keychain = utils::create_keychain();
1✔
3982
            let miner_pubkey = keychain.get_pub_key();
1✔
3983
            let mut op_signer = keychain.generate_op_signer();
1✔
3984

3985
            let mut config = utils::create_miner_config();
1✔
3986
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3987

3988
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3989
            btcd_controller
1✔
3990
                .start_bitcoind()
1✔
3991
                .expect("bitcoind should be started!");
1✔
3992

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

3996
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
3997

3998
            let tx = btc_controller
1✔
3999
                .build_leader_key_register_tx(
1✔
4000
                    StacksEpochId::Epoch31,
1✔
4001
                    leader_key_op.clone(),
1✔
4002
                    &mut op_signer,
1✔
4003
                )
4004
                .expect("Build leader key should work");
1✔
4005

4006
            assert!(op_signer.is_disposed());
1✔
4007

4008
            assert_eq!(1, tx.version);
1✔
4009
            assert_eq!(0, tx.lock_time);
1✔
4010
            assert_eq!(1, tx.input.len());
1✔
4011
            assert_eq!(2, tx.output.len());
1✔
4012

4013
            // utxos list contains the only existing utxo
4014
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
4015
            let input_0 = utils::txin_at_index(&tx, &op_signer, &used_utxos, 0);
1✔
4016
            assert_eq!(input_0, tx.input[0]);
1✔
4017

4018
            let op_return = utils::txout_opreturn(&leader_key_op, &config.burnchain.magic_bytes, 0);
1✔
4019
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 4_999_980_000);
1✔
4020
            assert_eq!(op_return, tx.output[0]);
1✔
4021
            assert_eq!(op_change, tx.output[1]);
1✔
4022
        }
1✔
4023

4024
        #[test]
4025
        #[ignore]
4026
        fn test_build_leader_key_tx_fails_due_to_no_utxos() {
1✔
4027
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4028
                return;
×
4029
            }
1✔
4030

4031
            let keychain = utils::create_keychain();
1✔
4032
            let miner_pubkey = keychain.get_pub_key();
1✔
4033
            let mut op_signer = keychain.generate_op_signer();
1✔
4034

4035
            let mut config = utils::create_miner_config();
1✔
4036
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4037

4038
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4039
            btcd_controller
1✔
4040
                .start_bitcoind()
1✔
4041
                .expect("bitcoind should be started!");
1✔
4042

4043
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4044
            btc_controller.bootstrap_chain(100); // no utxos exist
1✔
4045

4046
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4047

4048
            let error = btc_controller
1✔
4049
                .build_leader_key_register_tx(
1✔
4050
                    StacksEpochId::Epoch31,
1✔
4051
                    leader_key_op.clone(),
1✔
4052
                    &mut op_signer,
1✔
4053
                )
4054
                .expect_err("Leader key build should fail!");
1✔
4055

4056
            assert!(!op_signer.is_disposed());
1✔
4057
            assert_eq!(BurnchainControllerError::NoUTXOs, error);
1✔
4058
        }
1✔
4059

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

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

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

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

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

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

4085
            let tx = btc_controller
1✔
4086
                .make_operation_tx(
1✔
4087
                    StacksEpochId::Epoch31,
1✔
4088
                    BlockstackOperationType::LeaderKeyRegister(leader_key_op),
1✔
4089
                    &mut op_signer,
1✔
4090
                )
4091
                .expect("Make op should work");
1✔
4092

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

4095
            assert_eq!(
1✔
4096
                "4ecd7ba71bebd1aaed49dd63747ee424473f1c571bb9a576361607a669191024",
4097
                tx.txid().to_string()
1✔
4098
            );
4099
        }
1✔
4100

4101
        #[test]
4102
        #[ignore]
4103
        fn test_submit_operation_leader_key_register_tx_ok() {
1✔
4104
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4105
                return;
×
4106
            }
1✔
4107

4108
            let keychain = utils::create_keychain();
1✔
4109
            let miner_pubkey = keychain.get_pub_key();
1✔
4110
            let mut op_signer = keychain.generate_op_signer();
1✔
4111

4112
            let mut config = utils::create_miner_config();
1✔
4113
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4114

4115
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4116
            btcd_controller
1✔
4117
                .start_bitcoind()
1✔
4118
                .expect("bitcoind should be started!");
1✔
4119

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

4123
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4124

4125
            let tx_id = btc_controller
1✔
4126
                .submit_operation(
1✔
4127
                    StacksEpochId::Epoch31,
1✔
4128
                    BlockstackOperationType::LeaderKeyRegister(leader_key_op),
1✔
4129
                    &mut op_signer,
1✔
4130
                )
4131
                .expect("Submit op should work");
1✔
4132

4133
            assert!(op_signer.is_disposed());
1✔
4134

4135
            assert_eq!(
1✔
4136
                "4ecd7ba71bebd1aaed49dd63747ee424473f1c571bb9a576361607a669191024",
4137
                tx_id.to_hex()
1✔
4138
            );
4139
        }
1✔
4140
    }
4141

4142
    /// Tests related to Pre Stacks operation
4143
    mod pre_stx_op {
4144
        use super::*;
4145

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

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

4157
            let mut config = utils::create_miner_config();
1✔
4158
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4159

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

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

4168
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4169
            pre_stx_op.output = keychain.get_address(false);
1✔
4170

4171
            let tx = btc_controller
1✔
4172
                .build_pre_stacks_tx(StacksEpochId::Epoch31, pre_stx_op.clone(), &mut op_signer)
1✔
4173
                .expect("Build leader key should work");
1✔
4174

4175
            assert!(op_signer.is_disposed());
1✔
4176

4177
            assert_eq!(1, tx.version);
1✔
4178
            assert_eq!(0, tx.lock_time);
1✔
4179
            assert_eq!(1, tx.input.len());
1✔
4180
            assert_eq!(3, tx.output.len());
1✔
4181

4182
            // utxos list contains the only existing utxo
4183
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
4184
            let input_0 = utils::txin_at_index(&tx, &op_signer, &used_utxos, 0);
1✔
4185
            assert_eq!(input_0, tx.input[0]);
1✔
4186

4187
            let op_return = utils::txout_opreturn(&pre_stx_op, &config.burnchain.magic_bytes, 0);
1✔
4188
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 24_500);
1✔
4189
            assert_eq!(op_return, tx.output[0]);
1✔
4190
            assert_eq!(op_change, tx.output[1]);
1✔
4191
        }
1✔
4192

4193
        #[test]
4194
        #[ignore]
4195
        fn test_build_pre_stx_tx_fails_due_to_no_utxos() {
1✔
4196
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4197
                return;
×
4198
            }
1✔
4199

4200
            let keychain = utils::create_keychain();
1✔
4201
            let miner_pubkey = keychain.get_pub_key();
1✔
4202
            let mut op_signer = keychain.generate_op_signer();
1✔
4203

4204
            let mut config = utils::create_miner_config();
1✔
4205
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4206

4207
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4208
            btcd_controller
1✔
4209
                .start_bitcoind()
1✔
4210
                .expect("bitcoind should be started!");
1✔
4211

4212
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4213
            btc_controller.bootstrap_chain(100); // no utxo exists
1✔
4214

4215
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4216
            pre_stx_op.output = keychain.get_address(false);
1✔
4217

4218
            let error = btc_controller
1✔
4219
                .build_pre_stacks_tx(StacksEpochId::Epoch31, pre_stx_op.clone(), &mut op_signer)
1✔
4220
                .expect_err("Leader key build should fail!");
1✔
4221

4222
            assert!(!op_signer.is_disposed());
1✔
4223
            assert_eq!(BurnchainControllerError::NoUTXOs, error);
1✔
4224
        }
1✔
4225

4226
        #[test]
4227
        #[ignore]
4228
        fn test_make_operation_pre_stx_tx_ok() {
1✔
4229
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4230
                return;
×
4231
            }
1✔
4232

4233
            let keychain = utils::create_keychain();
1✔
4234
            let miner_pubkey = keychain.get_pub_key();
1✔
4235
            let mut op_signer = keychain.generate_op_signer();
1✔
4236

4237
            let mut config = utils::create_miner_config();
1✔
4238
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4239

4240
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4241
            btcd_controller
1✔
4242
                .start_bitcoind()
1✔
4243
                .expect("bitcoind should be started!");
1✔
4244

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

4248
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4249
            pre_stx_op.output = keychain.get_address(false);
1✔
4250

4251
            let tx = btc_controller
1✔
4252
                .make_operation_tx(
1✔
4253
                    StacksEpochId::Epoch31,
1✔
4254
                    BlockstackOperationType::PreStx(pre_stx_op),
1✔
4255
                    &mut op_signer,
1✔
4256
                )
4257
                .expect("Make op should work");
1✔
4258

4259
            assert!(op_signer.is_disposed());
1✔
4260

4261
            assert_eq!(
1✔
4262
                "2d061c42c6f13a62fd9d80dc9fdcd19bdb4f9e4a07f786e42530c64c52ed9d1d",
4263
                tx.txid().to_string()
1✔
4264
            );
4265
        }
1✔
4266

4267
        #[test]
4268
        #[ignore]
4269
        fn test_submit_operation_pre_stx_tx_ok() {
1✔
4270
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4271
                return;
×
4272
            }
1✔
4273

4274
            let keychain = utils::create_keychain();
1✔
4275
            let miner_pubkey = keychain.get_pub_key();
1✔
4276
            let mut op_signer = keychain.generate_op_signer();
1✔
4277

4278
            let mut config = utils::create_miner_config();
1✔
4279
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4280

4281
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4282
            btcd_controller
1✔
4283
                .start_bitcoind()
1✔
4284
                .expect("bitcoind should be started!");
1✔
4285

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

4289
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4290
            pre_stx_op.output = keychain.get_address(false);
1✔
4291

4292
            let tx_id = btc_controller
1✔
4293
                .submit_operation(
1✔
4294
                    StacksEpochId::Epoch31,
1✔
4295
                    BlockstackOperationType::PreStx(pre_stx_op),
1✔
4296
                    &mut op_signer,
1✔
4297
                )
4298
                .expect("submit op should work");
1✔
4299

4300
            assert!(op_signer.is_disposed());
1✔
4301

4302
            assert_eq!(
1✔
4303
                "2d061c42c6f13a62fd9d80dc9fdcd19bdb4f9e4a07f786e42530c64c52ed9d1d",
4304
                tx_id.to_hex()
1✔
4305
            );
4306
        }
1✔
4307
    }
4308
}
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