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

stacks-network / stacks-core / 26399134379-1

25 May 2026 11:53AM UTC coverage: 85.974% (+0.02%) from 85.959%
26399134379-1

Pull #7233

github

e74aea
web-flow
Merge 32d5ba16e into 30629d416
Pull Request #7233: fix: prevent counting duplicate lockups in pox-5

48 of 57 new or added lines in 2 files covered. (84.21%)

6122 existing lines in 96 files now uncovered.

189662 of 220603 relevant lines covered (85.97%)

17663616.59 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

259
    pub fn register_replacement(&mut self, tx_size: u64) {
8,848✔
260
        let new_size = cmp::max(tx_size, self.final_size);
8,848✔
261
        if self.is_rbf_enabled {
8,848✔
262
            self.spent_in_attempts += new_size;
403✔
263
        }
8,445✔
264
        self.final_size = new_size;
8,848✔
265
    }
8,848✔
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,873✔
287
        match self {
7,873✔
288
            Ok(val) => val,
7,873✔
289
            Err(e) => {
×
290
                error!("Bitcoin RPC failure: {context} {e:?}");
×
291
                panic!();
×
292
            }
293
        }
294
    }
7,873✔
295

296
    fn ok_or_log_panic(self, context: &str) {
7,736✔
297
        _ = self.unwrap_or_log_panic(context);
7,736✔
298
    }
7,736✔
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 {
275✔
317
        BitcoinRegtestController::with_burnchain(config, coordinator_channel, None, None)
275✔
318
    }
275✔
319

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

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

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

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

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

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

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

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

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

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

426
        let rpc_client = Self::create_rpc_client_unchecked(&config);
209,661✔
427

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

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

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

459
    /// Get the default Burnchain instance from our config
460
    fn default_burnchain(&self) -> Burnchain {
927,004✔
461
        match &self.burnchain_config {
927,004✔
462
            Some(burnchain) => burnchain.clone(),
×
463
            None => self.config.get_burnchain(),
927,004✔
464
        }
465
    }
927,004✔
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 {
981,072✔
475
        match self.burnchain_config {
981,072✔
476
            Some(ref burnchain) => burnchain.clone(),
54,068✔
477
            None => self.default_burnchain(),
927,004✔
478
        }
479
    }
981,072✔
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> {
210,487✔
487
        config.node.miner.then(|| {
210,487✔
488
            BitcoinRpcClient::from_stx_config(&config)
210,466✔
489
                .expect("unable to instantiate the RPC client for miner node!")
210,466✔
490
        })
210,466✔
491
    }
210,487✔
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 {
62,625✔
502
        self.rpc_client
62,625✔
503
            .as_ref()
62,625✔
504
            .expect("BUG: BitcoinRpcClient is required, but it has not been configured properly!")
62,625✔
505
    }
62,625✔
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(
437,085✔
567
        &mut self,
437,085✔
568
        block_for_sortitions: bool,
437,085✔
569
        target_block_height_opt: Option<u64>,
437,085✔
570
    ) -> Result<(BurnchainTip, u64), BurnchainControllerError> {
437,085✔
571
        let coordinator_comms = match self.use_coordinator.as_ref() {
437,085✔
572
            Some(x) => x.clone(),
437,085✔
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();
437,085✔
582
        let (block_snapshot, burnchain_height, state_transition) = loop {
437,022✔
583
            if !self.should_keep_running() {
437,162✔
584
                return Err(BurnchainControllerError::CoordinatorClosed);
62✔
585
            }
437,100✔
586

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

597
                    // initialize the dbs...
598
                    self.sortdb_mut();
437,023✔
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 {
437,023✔
603
                        self.wait_for_sortitions(
436,447✔
604
                            coordinator_comms,
436,447✔
605
                            target_block_height_opt.unwrap_or(x.block_height),
436,447✔
606
                        )?;
1✔
607
                    }
576✔
608

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

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

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

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

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

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

676
        Ok((burnchain_tip, burnchain_height))
437,022✔
677
    }
437,085✔
678

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

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

707
        sleep_ms(1000);
61✔
708

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

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

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

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

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

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

766
        let utxos = if utxos.is_empty() {
9,285✔
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(),);
9,260✔
811
            utxos
9,260✔
812
        };
813

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

823
        Some(utxos)
9,271✔
824
    }
9,285✔
825

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

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

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

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

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

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

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

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

878
        increment_btc_ops_sent_counter();
276✔
879

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1484
        increment_btc_ops_sent_counter();
8,849✔
1485

1486
        Ok(tx)
8,849✔
1487
    }
9,737✔
1488

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

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

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

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

1513
            test_debug!("Ongoing Tx confirmed: {ongoing_tx_confirmed} - TXID: {txid}");
24,144✔
1514
            if ongoing_tx_confirmed {
24,144✔
1515
                if ongoing_op.payload == payload {
9,520✔
1516
                    info!("Abort attempt to re-submit confirmed LeaderBlockCommit");
1,558✔
1517
                    self.ongoing_block_commit = Some(ongoing_op);
1,558✔
1518
                    return Err(BurnchainControllerError::IdenticalOperation);
1,558✔
1519
                }
7,962✔
1520

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

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

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

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

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

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

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

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

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

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

1611
        res
403✔
1612
    }
25,153✔
1613

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

1621
        if self.config.miner.segwit && epoch_id >= StacksEpochId::Epoch21 {
35,173✔
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());
35,172✔
1627
            BitcoinAddress::from_bytes_legacy(
35,172✔
1628
                network_id,
35,172✔
1629
                LegacyBitcoinAddressType::PublicKeyHash,
35,172✔
1630
                &hash160.0,
35,172✔
1631
            )
1632
            .expect("Public key incorrect")
35,172✔
1633
        }
1634
    }
35,173✔
1635

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

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

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

1684
        Ok((transaction, utxos))
9,152✔
1685
    }
10,043✔
1686

1687
    #[allow(clippy::too_many_arguments)]
1688
    fn finalize_tx(
9,153✔
1689
        &mut self,
9,153✔
1690
        epoch_id: StacksEpochId,
9,153✔
1691
        tx: &mut Transaction,
9,153✔
1692
        spent_in_outputs: u64,
9,153✔
1693
        spent_in_rbf: u64,
9,153✔
1694
        min_tx_size: u64,
9,153✔
1695
        fee_rate: u64,
9,153✔
1696
        utxos_set: &mut UTXOSet,
9,153✔
1697
        signer: &mut BurnchainOpSigner,
9,153✔
1698
        force_change_output: bool,
9,153✔
1699
    ) {
9,153✔
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,836,112✔
1703
            if u1.confirmations != u2.confirmations {
6,836,096✔
1704
                u1.confirmations.cmp(&u2.confirmations)
6,836,095✔
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,836,096✔
1712

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

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

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

1780
            if total_consumed >= total_target {
18,310✔
1781
                break;
18,307✔
1782
            }
3✔
1783
        }
1784

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

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

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

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

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

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

1915
            if debug_ctr % 10 == 0 {
444,136✔
1916
                debug!(
437,302✔
1917
                    "Waiting until canonical sortition height reaches {height_to_wait} (currently {})",
1918
                    canonical_sortition_tip.block_height
1919
                );
1920
            }
6,834✔
1921
            debug_ctr += 1;
444,136✔
1922

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

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

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

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

1945
            // yield some time
1946
            sleep_ms(1000);
6,866✔
1947
        }
1948
    }
437,270✔
1949

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

1963
        let result = self
8,830✔
1964
            .get_rpc_client()
8,830✔
1965
            .generate_to_address(num_blocks, &address);
8,830✔
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,830✔
1976
            Ok(_) => {}
8,830✔
1977
            Err(e) => {
×
1978
                error!("Bitcoin RPC failure: error generating block {e:?}");
×
1979
                panic!();
×
1980
            }
1981
        }
1982
    }
8,830✔
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> {
5✔
2022
        self.config.burnchain.local_mining_public_key.clone()
5✔
2023
    }
5✔
2024

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

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

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

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

2078
    /// Build, sign, and broadcast a regular Bitcoin payment from the
2079
    /// miner address to `recipient`.
2080
    ///
2081
    /// Internally this is the same UTXO-selection + signing flow used
2082
    /// by `build_leader_block_commit_tx` / `build_leader_key_register_tx`
2083
    /// — `prepare_tx` picks miner UTXOs, then `finalize_tx` adds a
2084
    /// change output, fills inputs, and signs each one with the keychain
2085
    /// `BurnchainOpSigner` the caller supplies. The resulting tx is
2086
    /// broadcast via `sendrawtransaction`; the next regtest block (e.g.
2087
    /// `build_next_block(1)`) confirms it.
2088
    ///
2089
    /// Intended for tests that need the bondholder / a third party to
2090
    /// receive BTC on regtest without relying on bitcoind's wallet to
2091
    /// sign for the miner (the wallet is created with
2092
    /// `disable_private_keys=true`, so it can't).
2093
    #[cfg(test)]
2094
    pub fn send_btc(
2✔
2095
        &mut self,
2✔
2096
        epoch_id: StacksEpochId,
2✔
2097
        op_signer: &mut BurnchainOpSigner,
2✔
2098
        recipient: &BitcoinAddress,
2✔
2099
        amount: u64,
2✔
2100
    ) -> Result<Txid, BurnchainControllerError> {
2✔
2101
        let public_key = op_signer.get_public_key();
2✔
2102

2103
        let fee_rate = get_satoshis_per_byte(&self.config);
2✔
2104
        // Rough upper-bound for a 1-input, 2-output P2PKH/P2WPKH tx. The
2105
        // `finalize_tx` flow re-serializes to compute the real size; this
2106
        // is just for UTXO selection / change accounting.
2107
        let min_tx_size: u64 = 250;
2✔
2108
        let total_required = amount + min_tx_size * fee_rate;
2✔
2109

2110
        let (mut tx, mut utxos) =
2✔
2111
            self.prepare_tx(epoch_id, &public_key, total_required, None, None, 0)?;
2✔
2112

2113
        let recipient_output = match recipient {
2✔
NEW
2114
            BitcoinAddress::Legacy(legacy) => match legacy.addrtype {
×
2115
                LegacyBitcoinAddressType::PublicKeyHash => {
NEW
2116
                    LegacyBitcoinAddress::to_p2pkh_tx_out(&legacy.bytes, amount)
×
2117
                }
2118
                LegacyBitcoinAddressType::ScriptHash => {
NEW
2119
                    LegacyBitcoinAddress::to_p2sh_tx_out(&legacy.bytes, amount)
×
2120
                }
2121
            },
2122
            BitcoinAddress::Segwit(segwit) => match segwit {
2✔
2123
                SegwitBitcoinAddress::P2WPKH(_, bytes) => {
2✔
2124
                    SegwitBitcoinAddress::to_p2wpkh_tx_out(bytes, amount)
2✔
2125
                }
NEW
2126
                SegwitBitcoinAddress::P2WSH(_, bytes) => {
×
NEW
2127
                    SegwitBitcoinAddress::to_p2wsh_tx_out(bytes, amount)
×
2128
                }
NEW
2129
                SegwitBitcoinAddress::P2TR(_, bytes) => {
×
NEW
2130
                    SegwitBitcoinAddress::to_p2tr_tx_out(bytes, amount)
×
2131
                }
2132
            },
2133
        };
2134
        tx.output = vec![recipient_output];
2✔
2135

2136
        self.finalize_tx(
2✔
2137
            epoch_id,
2✔
2138
            &mut tx,
2✔
2139
            amount,
2✔
2140
            0,
2141
            min_tx_size,
2✔
2142
            fee_rate,
2✔
2143
            &mut utxos,
2✔
2144
            op_signer,
2✔
2145
            true,
2146
        );
2147

2148
        info!(
2✔
2149
            "Test send_btc: paying {amount} sats to {recipient}";
2150
            "fee_rate" => fee_rate,
2✔
2151
        );
2152

2153
        self.send_transaction(&tx)
2✔
2154
    }
2✔
2155

2156
    /// Produce `num_blocks` regtest bitcoin blocks, sending the bitcoin coinbase rewards
2157
    ///  to the bitcoin single sig addresses corresponding to `pks` in a round robin fashion.
2158
    #[cfg(test)]
2159
    pub fn bootstrap_chain_to_pks(&self, num_blocks: u64, pks: &[Secp256k1PublicKey]) {
270✔
2160
        info!("Creating wallet if it does not exist");
270✔
2161
        if let Err(e) = self.create_wallet_if_dne() {
270✔
2162
            error!("Error when creating wallet: {e:?}");
×
2163
        }
270✔
2164

2165
        for pk in pks {
309✔
2166
            debug!("Import public key '{}'", &pk.to_hex());
309✔
2167
            if let Err(e) = self.import_public_key(pk) {
309✔
2168
                warn!("Error when importing pubkey: {e:?}");
×
2169
            }
309✔
2170
        }
2171

2172
        if pks.len() == 1 {
270✔
2173
            // if we only have one pubkey, just generate all the blocks at once
2174
            let address = self.get_miner_address(StacksEpochId::Epoch21, &pks[0]);
231✔
2175
            debug!(
231✔
2176
                "Generate to address '{address}' for public key '{}'",
2177
                &pks[0].to_hex()
×
2178
            );
2179
            self.get_rpc_client()
231✔
2180
                .generate_to_address(num_blocks, &address)
231✔
2181
                .ok_or_log_panic("generating block");
231✔
2182
            return;
231✔
2183
        }
39✔
2184

2185
        // otherwise, round robin generate blocks
2186
        let num_blocks = num_blocks as usize;
39✔
2187
        for i in 0..num_blocks {
7,471✔
2188
            let pk = &pks[i % pks.len()];
7,471✔
2189
            let address = self.get_miner_address(StacksEpochId::Epoch21, pk);
7,471✔
2190
            if i < pks.len() {
7,471✔
2191
                debug!(
78✔
2192
                    "Generate to address '{}' for public key '{}'",
2193
                    address.to_string(),
×
2194
                    &pk.to_hex(),
×
2195
                );
2196
            }
7,393✔
2197
            self.get_rpc_client()
7,471✔
2198
                .generate_to_address(1, &address)
7,471✔
2199
                .ok_or_log_panic("generating block");
7,471✔
2200
        }
2201
    }
270✔
2202

2203
    /// Checks whether a transaction has been confirmed by the burnchain
2204
    ///
2205
    /// # Arguments
2206
    ///
2207
    /// * `txid` - The transaction ID to check (in big-endian order)
2208
    ///
2209
    /// # Returns
2210
    ///
2211
    /// * `true` if the transaction is confirmed (has at least one confirmation).
2212
    /// * `false` if the transaction is unconfirmed or could not be found.
2213
    pub fn is_transaction_confirmed(&self, txid: &Txid) -> bool {
16,219✔
2214
        match self
16,219✔
2215
            .get_rpc_client()
16,219✔
2216
            .get_transaction(self.get_wallet_name(), txid)
16,219✔
2217
        {
2218
            Ok(info) => info.confirmations > 0,
15,788✔
2219
            Err(e) => {
431✔
2220
                error!("Bitcoin RPC failure: checking tx confirmation {e:?}");
431✔
2221
                false
431✔
2222
            }
2223
        }
2224
    }
16,219✔
2225

2226
    /// Returns the configured wallet name from [`Config`].
2227
    fn get_wallet_name(&self) -> &String {
26,787✔
2228
        &self.config.burnchain.wallet_name
26,787✔
2229
    }
26,787✔
2230

2231
    /// Imports a public key into configured wallet by registering its
2232
    /// corresponding addresses as descriptors.
2233
    ///
2234
    /// This computes both **legacy (P2PKH)** and, if the miner is configured
2235
    /// with `segwit` enabled, also **SegWit (P2WPKH)** addresses, then imports
2236
    /// the related descriptors into the wallet.
2237
    pub fn import_public_key(
399✔
2238
        &self,
399✔
2239
        public_key: &Secp256k1PublicKey,
399✔
2240
    ) -> BitcoinRegtestControllerResult<()> {
399✔
2241
        let pkh = Hash160::from_data(&public_key.to_bytes())
399✔
2242
            .to_bytes()
399✔
2243
            .to_vec();
399✔
2244
        let (_, network_id) = self.config.burnchain.get_bitcoin_network();
399✔
2245

2246
        // import both the legacy and segwit variants of this public key
2247
        let mut addresses = vec![BitcoinAddress::from_bytes_legacy(
399✔
2248
            network_id,
399✔
2249
            LegacyBitcoinAddressType::PublicKeyHash,
399✔
2250
            &pkh,
399✔
2251
        )
2252
        .map_err(BitcoinRegtestControllerError::InvalidPublicKey)?];
399✔
2253

2254
        if self.config.miner.segwit {
399✔
2255
            addresses.push(
1✔
2256
                BitcoinAddress::from_bytes_segwit_p2wpkh(network_id, &pkh)
1✔
2257
                    .map_err(BitcoinRegtestControllerError::InvalidPublicKey)?,
1✔
2258
            );
2259
        }
398✔
2260

2261
        for address in addresses.into_iter() {
400✔
2262
            debug!(
400✔
2263
                "Import address {address} for public key {}",
2264
                public_key.to_hex()
×
2265
            );
2266

2267
            let descriptor = format!("addr({address})");
400✔
2268
            let info = self.get_rpc_client().get_descriptor_info(&descriptor)?;
400✔
2269

2270
            let descr_req = ImportDescriptorsRequest {
400✔
2271
                descriptor: format!("addr({address})#{}", info.checksum),
400✔
2272
                timestamp: Timestamp::Time(0),
400✔
2273
                internal: Some(true),
400✔
2274
            };
400✔
2275

2276
            self.get_rpc_client()
400✔
2277
                .import_descriptors(self.get_wallet_name(), &[&descr_req])?;
400✔
2278
        }
2279
        Ok(())
399✔
2280
    }
399✔
2281

2282
    /// Returns a copy of the given public key adjusted to the current epoch rules.
2283
    ///
2284
    /// In particular:
2285
    /// - For epochs **before** [`StacksEpochId::Epoch21`], the public key is returned
2286
    ///   unchanged.
2287
    /// - Starting with [`StacksEpochId::Epoch21`], if **SegWit** is enabled in the miner
2288
    ///   configuration, the key is forced into compressed form.
2289
    ///
2290
    /// # Arguments
2291
    /// * `epoch_id` — The epoch identifier to check against protocol upgrade rules.
2292
    /// * `public_key` — The original public key to adjust.
2293
    ///
2294
    /// # Returns
2295
    /// A [`Secp256k1PublicKey`] that is either the same as the input or compressed,
2296
    /// depending on the epoch and miner configuration.
2297
    fn to_epoch_aware_pubkey(
9,350✔
2298
        &self,
9,350✔
2299
        epoch_id: StacksEpochId,
9,350✔
2300
        public_key: &Secp256k1PublicKey,
9,350✔
2301
    ) -> Secp256k1PublicKey {
9,350✔
2302
        let mut reviewed = public_key.clone();
9,350✔
2303
        if self.config.miner.segwit && epoch_id >= StacksEpochId::Epoch21 {
9,350✔
2304
            reviewed.set_compressed(true);
1✔
2305
        }
9,349✔
2306
        return reviewed;
9,350✔
2307
    }
9,350✔
2308

2309
    /// Retrieves the set of UTXOs for a given address at a specific block height.
2310
    ///
2311
    /// This method queries all unspent outputs belonging to the provided address:
2312
    /// 1. Using a confirmation window of `0..=9_999_999` for the RPC call.
2313
    /// 2. Filtering out UTXOs that:
2314
    ///    - Are present in the optional exclusion set (matched by transaction ID).
2315
    ///    - Have an amount below the specified `minimum_sum_amount`.
2316
    ///
2317
    /// Note: The `block_height` is only used to retrieve the corresponding block hash
2318
    /// and does not affect which UTXOs are included in the result.
2319
    ///
2320
    /// # Arguments
2321
    /// - `address`: The Bitcoin address whose UTXOs should be retrieved.
2322
    /// - `include_unsafe`: Whether to include unsafe UTXOs.
2323
    /// - `minimum_sum_amount`: Minimum amount (in satoshis) that a UTXO must have to be included in the final set.
2324
    /// - `utxos_to_exclude`: Optional set of UTXOs to exclude from the final result.
2325
    /// - `block_height`: The block height at which to resolve the block hash used in the result.
2326
    ///
2327
    /// # Returns
2328
    /// A [`UTXOSet`] containing the filtered UTXOs and the block hash corresponding to `block_height`.
2329
    fn retrieve_utxo_set(
9,376✔
2330
        &self,
9,376✔
2331
        address: &BitcoinAddress,
9,376✔
2332
        include_unsafe: bool,
9,376✔
2333
        minimum_sum_amount: u64,
9,376✔
2334
        utxos_to_exclude: &Option<UTXOSet>,
9,376✔
2335
        block_height: u64,
9,376✔
2336
    ) -> BitcoinRpcClientResult<UTXOSet> {
9,376✔
2337
        let bhh = self.get_rpc_client().get_block_hash(block_height)?;
9,376✔
2338

2339
        const MIN_CONFIRMATIONS: u64 = 0;
2340
        const MAX_CONFIRMATIONS: u64 = 9_999_999;
2341
        let unspents = self.get_rpc_client().list_unspent(
9,375✔
2342
            &self.get_wallet_name(),
9,375✔
2343
            Some(MIN_CONFIRMATIONS),
9,375✔
2344
            Some(MAX_CONFIRMATIONS),
9,375✔
2345
            Some(&[address]),
9,375✔
2346
            Some(include_unsafe),
9,375✔
2347
            Some(minimum_sum_amount),
9,375✔
2348
            self.config.burnchain.max_unspent_utxos.clone(),
9,375✔
2349
        )?;
×
2350

2351
        let txids_to_exclude = utxos_to_exclude.as_ref().map_or_else(HashSet::new, |set| {
9,375✔
2352
            set.utxos
4✔
2353
                .iter()
4✔
2354
                .map(|utxo| Txid::from_bitcoin_tx_hash(&utxo.txid))
92✔
2355
                .collect()
4✔
2356
        });
4✔
2357

2358
        let utxos = unspents
9,375✔
2359
            .into_iter()
9,375✔
2360
            .filter(|each| !txids_to_exclude.contains(&each.txid))
992,144✔
2361
            .filter(|each| each.amount >= minimum_sum_amount)
992,054✔
2362
            .map(|each| UTXO {
9,375✔
2363
                txid: Txid::to_bitcoin_tx_hash(&each.txid),
992,027✔
2364
                vout: each.vout,
992,027✔
2365
                script_pub_key: each.script_pub_key,
992,027✔
2366
                amount: each.amount,
992,027✔
2367
                confirmations: each.confirmations,
992,027✔
2368
            })
992,027✔
2369
            .collect::<Vec<_>>();
9,375✔
2370
        Ok(UTXOSet { bhh, utxos })
9,375✔
2371
    }
9,376✔
2372
}
2373

2374
impl BurnchainController for BitcoinRegtestController {
2375
    fn sortdb_ref(&self) -> &SortitionDB {
2,247,589✔
2376
        self.db
2,247,589✔
2377
            .as_ref()
2,247,589✔
2378
            .expect("BUG: did not instantiate the burn DB")
2,247,589✔
2379
    }
2,247,589✔
2380

2381
    fn sortdb_mut(&mut self) -> &mut SortitionDB {
540,592✔
2382
        let burnchain = self.get_burnchain();
540,592✔
2383

2384
        let (db, burnchain_db) = burnchain.open_db(true).unwrap();
540,592✔
2385
        self.db = Some(db);
540,592✔
2386
        self.burnchain_db = Some(burnchain_db);
540,592✔
2387

2388
        match self.db {
540,592✔
2389
            Some(ref mut sortdb) => sortdb,
540,592✔
2390
            None => unreachable!(),
×
2391
        }
2392
    }
540,592✔
2393

2394
    fn get_chain_tip(&self) -> BurnchainTip {
×
2395
        match &self.chain_tip {
×
2396
            Some(chain_tip) => chain_tip.clone(),
×
2397
            None => {
2398
                unreachable!();
×
2399
            }
2400
        }
2401
    }
×
2402

2403
    fn get_headers_height(&self) -> u64 {
437,578✔
2404
        let (_, network_id) = self.config.burnchain.get_bitcoin_network();
437,578✔
2405
        let spv_client = SpvClient::new(
437,578✔
2406
            &self.config.get_spv_headers_file_path(),
437,578✔
2407
            0,
2408
            None,
437,578✔
2409
            network_id,
437,578✔
2410
            false,
2411
            false,
2412
        )
2413
        .expect("Unable to open burnchain headers DB");
437,578✔
2414
        spv_client
437,578✔
2415
            .get_headers_height()
437,578✔
2416
            .expect("Unable to query number of burnchain headers")
437,578✔
2417
    }
437,578✔
2418

2419
    fn connect_dbs(&mut self) -> Result<(), BurnchainControllerError> {
545✔
2420
        let burnchain = self.get_burnchain();
545✔
2421
        burnchain.connect_db(
545✔
2422
            true,
2423
            &self.indexer.get_first_block_header_hash()?,
545✔
2424
            self.indexer.get_first_block_header_timestamp()?,
545✔
2425
            self.indexer.get_stacks_epochs(),
545✔
2426
        )?;
×
2427
        Ok(())
545✔
2428
    }
545✔
2429

2430
    fn get_stacks_epochs(&self) -> EpochList {
538✔
2431
        self.indexer.get_stacks_epochs()
538✔
2432
    }
538✔
2433

2434
    fn start(
538✔
2435
        &mut self,
538✔
2436
        target_block_height_opt: Option<u64>,
538✔
2437
    ) -> Result<(BurnchainTip, u64), BurnchainControllerError> {
538✔
2438
        // if no target block height is given, just fetch the first burnchain block.
2439
        self.receive_blocks(false, target_block_height_opt.map_or_else(|| Some(1), Some))
538✔
2440
    }
538✔
2441

2442
    fn sync(
436,547✔
2443
        &mut self,
436,547✔
2444
        target_block_height_opt: Option<u64>,
436,547✔
2445
    ) -> Result<(BurnchainTip, u64), BurnchainControllerError> {
436,547✔
2446
        let (burnchain_tip, burnchain_height) = if self.config.burnchain.mode == "helium" {
436,547✔
2447
            // Helium: this node is responsible for mining new burnchain blocks
2448
            self.build_next_block(1);
×
2449
            self.receive_blocks(true, None)?
×
2450
        } else {
2451
            // Neon: this node is waiting on a block to be produced
2452
            self.receive_blocks(true, target_block_height_opt)?
436,547✔
2453
        };
2454

2455
        // Evaluate process_exit_at_block_height setting
2456
        if let Some(cap) = self.config.burnchain.process_exit_at_block_height {
436,485✔
2457
            if burnchain_tip.block_snapshot.block_height >= cap {
39✔
2458
                info!("Node succesfully reached the end of the ongoing {cap} blocks epoch!");
×
2459
                info!("This process will automatically terminate in 30s, restart your node for participating in the next epoch.");
×
2460
                sleep_ms(30000);
×
2461
                std::process::exit(0);
×
2462
            }
39✔
2463
        }
436,446✔
2464
        Ok((burnchain_tip, burnchain_height))
436,485✔
2465
    }
436,547✔
2466

2467
    /// Build and send a burnchain operation transaction.
2468
    /// Returns the [`Txid`] on success, [`BurnchainControllerError`] otherwise.
2469
    /// On [`BitcoinRegtestController::send_transaction`] failure for block commits,
2470
    /// clears `ongoing_block_commit` so the commit can be resubmitted.
2471
    fn submit_operation(
25,440✔
2472
        &mut self,
25,440✔
2473
        epoch_id: StacksEpochId,
25,440✔
2474
        operation: BlockstackOperationType,
25,440✔
2475
        op_signer: &mut BurnchainOpSigner,
25,440✔
2476
    ) -> Result<Txid, BurnchainControllerError> {
25,440✔
2477
        let is_block_commit = matches!(operation, BlockstackOperationType::LeaderBlockCommit(_));
25,440✔
2478
        let transaction = self.make_operation_tx(epoch_id, operation, op_signer)?;
25,440✔
2479
        self.send_transaction(&transaction).inspect_err(|_| {
9,137✔
2480
            if is_block_commit {
3✔
2481
                self.ongoing_block_commit = None;
3✔
2482
            }
3✔
2483
        })
3✔
2484
    }
25,440✔
2485

2486
    #[cfg(test)]
2487
    fn bootstrap_chain(&self, num_blocks: u64) {
128✔
2488
        let Some(ref local_mining_pubkey) = &self.config.burnchain.local_mining_public_key else {
128✔
2489
            warn!("No local mining pubkey while bootstrapping bitcoin regtest, will not generate bitcoin blocks");
1✔
2490
            return;
1✔
2491
        };
2492

2493
        // NOTE: miner address is whatever the miner's segwit setting says it is here
2494
        let mut local_mining_pubkey = Secp256k1PublicKey::from_hex(local_mining_pubkey).unwrap();
127✔
2495

2496
        if self.config.miner.segwit {
127✔
2497
            local_mining_pubkey.set_compressed(true);
×
2498
        }
127✔
2499

2500
        self.bootstrap_chain_to_pks(num_blocks, &[local_mining_pubkey])
127✔
2501
    }
128✔
2502
}
2503

2504
#[derive(Debug, Clone)]
2505
pub struct UTXOSet {
2506
    bhh: BurnchainHeaderHash,
2507
    utxos: Vec<UTXO>,
2508
}
2509

2510
impl UTXOSet {
2511
    pub fn is_empty(&self) -> bool {
9,309✔
2512
        self.utxos.len() == 0
9,309✔
2513
    }
9,309✔
2514

2515
    pub fn total_available(&self) -> u64 {
9,274✔
2516
        self.utxos.iter().map(|o| o.amount).sum()
9,274✔
2517
    }
9,274✔
2518

2519
    pub fn num_utxos(&self) -> usize {
6✔
2520
        self.utxos.len()
6✔
2521
    }
6✔
2522
}
2523

2524
#[derive(Clone, Debug, PartialEq)]
2525
pub struct UTXO {
2526
    pub txid: Sha256dHash,
2527
    pub vout: u32,
2528
    pub script_pub_key: Script,
2529
    pub amount: u64,
2530
    pub confirmations: u32,
2531
}
2532

2533
#[cfg(test)]
2534
mod tests {
2535
    use std::env::{self, temp_dir};
2536
    use std::fs::File;
2537
    use std::io::Write;
2538
    use std::panic::{self, AssertUnwindSafe};
2539

2540
    use stacks::burnchains::BurnchainSigner;
2541
    use stacks::config::DEFAULT_SATS_PER_VB;
2542
    use stacks_common::deps_common::bitcoin::blockdata::script::Builder;
2543
    use stacks_common::types::chainstate::{BlockHeaderHash, StacksAddress, VRFSeed};
2544
    use stacks_common::util::hash::to_hex;
2545
    use stacks_common::util::secp256k1::Secp256k1PrivateKey;
2546

2547
    use super::*;
2548
    use crate::burnchains::bitcoin::core_controller::BitcoinCoreController;
2549
    use crate::burnchains::bitcoin_regtest_controller::tests::utils::{
2550
        create_follower_config, create_miner_config, to_address_legacy,
2551
    };
2552
    use crate::Keychain;
2553

2554
    mod utils {
2555
        use std::net::TcpListener;
2556

2557
        use stacks::burnchains::MagicBytes;
2558
        use stacks::chainstate::burn::ConsensusHash;
2559
        use stacks::util::vrf::{VRFPrivateKey, VRFPublicKey};
2560

2561
        use super::*;
2562
        use crate::burnchains::bitcoin::core_controller::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
2563
        use crate::util::get_epoch_time_nanos;
2564

2565
        pub fn create_miner_config() -> Config {
38✔
2566
            let mut config = Config::default();
38✔
2567
            config.node.miner = true;
38✔
2568
            config.burnchain.magic_bytes = "T3".as_bytes().into();
38✔
2569
            config.burnchain.username = Some(String::from("user"));
38✔
2570
            config.burnchain.password = Some(String::from("12345"));
38✔
2571
            // overriding default "0.0.0.0" because doesn't play nicely on Windows.
2572
            config.burnchain.peer_host = String::from("127.0.0.1");
38✔
2573
            // avoiding peer port biding to reduce the number of ports to bind to.
2574
            config.burnchain.peer_port = BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
38✔
2575

2576
            //Ask the OS for a free port. Not guaranteed to stay free,
2577
            //after TcpListner is dropped, but good enough for testing
2578
            //and starting bitcoind right after config is created
2579
            let tmp_listener =
38✔
2580
                TcpListener::bind("127.0.0.1:0").expect("Failed to bind to get a free port");
38✔
2581
            let port = tmp_listener.local_addr().unwrap().port();
38✔
2582

2583
            config.burnchain.rpc_port = port;
38✔
2584

2585
            let now = get_epoch_time_nanos();
38✔
2586
            let dir = format!("/tmp/regtest-ctrl-{port}-{now}");
38✔
2587
            config.node.working_dir = dir;
38✔
2588

2589
            config
38✔
2590
        }
38✔
2591

2592
        pub fn create_keychain() -> Keychain {
16✔
2593
            create_keychain_with_seed(1)
16✔
2594
        }
16✔
2595

2596
        pub fn create_keychain_with_seed(value: u8) -> Keychain {
33✔
2597
            let seed = vec![value; 4];
33✔
2598
            let keychain = Keychain::default(seed);
33✔
2599
            keychain
33✔
2600
        }
33✔
2601

2602
        pub fn create_miner1_pubkey() -> Secp256k1PublicKey {
15✔
2603
            create_keychain_with_seed(1).get_pub_key()
15✔
2604
        }
15✔
2605

2606
        pub fn create_miner2_pubkey() -> Secp256k1PublicKey {
2✔
2607
            create_keychain_with_seed(2).get_pub_key()
2✔
2608
        }
2✔
2609

2610
        pub fn to_address_legacy(pub_key: &Secp256k1PublicKey) -> BitcoinAddress {
6✔
2611
            let hash160 = Hash160::from_data(&pub_key.to_bytes());
6✔
2612
            BitcoinAddress::from_bytes_legacy(
6✔
2613
                BitcoinNetworkType::Regtest,
6✔
2614
                LegacyBitcoinAddressType::PublicKeyHash,
6✔
2615
                &hash160.0,
6✔
2616
            )
2617
            .expect("Public key incorrect")
6✔
2618
        }
6✔
2619

2620
        pub fn to_address_segwit_p2wpkh(pub_key: &Secp256k1PublicKey) -> BitcoinAddress {
1✔
2621
            // pub_key.to_byte_compressed() equivalent to pub_key.set_compressed(true) + pub_key.to_bytes()
2622
            let hash160 = Hash160::from_data(&pub_key.to_bytes_compressed());
1✔
2623
            BitcoinAddress::from_bytes_segwit_p2wpkh(BitcoinNetworkType::Regtest, &hash160.0)
1✔
2624
                .expect("Public key incorrect")
1✔
2625
        }
1✔
2626

2627
        pub fn mine_tx(btc_controller: &BitcoinRegtestController, tx: &Transaction) {
2✔
2628
            btc_controller
2✔
2629
                .send_transaction(tx)
2✔
2630
                .expect("Tx should be sent to the burnchain!");
2✔
2631
            btc_controller.build_next_block(1); // Now tx is confirmed
2✔
2632
        }
2✔
2633

2634
        pub fn create_templated_commit_op() -> LeaderBlockCommitOp {
8✔
2635
            LeaderBlockCommitOp {
8✔
2636
                block_header_hash: BlockHeaderHash::from_hex(
8✔
2637
                    "e88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32af",
8✔
2638
                )
8✔
2639
                .unwrap(),
8✔
2640
                new_seed: VRFSeed::from_hex(
8✔
2641
                    "d5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375",
8✔
2642
                )
8✔
2643
                .unwrap(),
8✔
2644
                parent_block_ptr: 2211, // 0x000008a3
8✔
2645
                parent_vtxindex: 1,     // 0x0001
8✔
2646
                key_block_ptr: 1432,    // 0x00000598
8✔
2647
                key_vtxindex: 1,        // 0x0001
8✔
2648
                memo: vec![11],         // 0x5a >> 3
8✔
2649

8✔
2650
                burn_fee: 110_000, //relevant for fee calculation when sending the tx
8✔
2651
                input: (Txid([0x00; 32]), 0),
8✔
2652
                burn_parent_modulus: 2, // 0x5a & 0b111
8✔
2653

8✔
2654
                apparent_sender: BurnchainSigner("mgbpit8FvkVJ9kuXY8QSM5P7eibnhcEMBk".to_string()),
8✔
2655
                commit_outs: vec![
8✔
2656
                    PoxAddress::Standard(StacksAddress::burn_address(false), None),
8✔
2657
                    PoxAddress::Standard(StacksAddress::burn_address(false), None),
8✔
2658
                ],
8✔
2659

8✔
2660
                treatment: vec![],
8✔
2661
                sunset_burn: 5_500, //relevant for fee calculation when sending the tx
8✔
2662

8✔
2663
                txid: Txid([0x00; 32]),
8✔
2664
                vtxindex: 0,
8✔
2665
                block_height: 2212,
8✔
2666
                burn_header_hash: BurnchainHeaderHash([0x01; 32]),
8✔
2667
            }
8✔
2668
        }
8✔
2669

2670
        pub fn txout_opreturn<T: StacksMessageCodec>(
5✔
2671
            op: &T,
5✔
2672
            magic: &MagicBytes,
5✔
2673
            value: u64,
5✔
2674
        ) -> TxOut {
5✔
2675
            let op_bytes = {
5✔
2676
                let mut buffer = vec![];
5✔
2677
                let mut magic_bytes = magic.as_bytes().to_vec();
5✔
2678
                buffer.append(&mut magic_bytes);
5✔
2679
                op.consensus_serialize(&mut buffer)
5✔
2680
                    .expect("FATAL: invalid operation");
5✔
2681
                buffer
5✔
2682
            };
2683

2684
            TxOut {
5✔
2685
                value,
5✔
2686
                script_pubkey: Builder::new()
5✔
2687
                    .push_opcode(opcodes::All::OP_RETURN)
5✔
2688
                    .push_slice(&op_bytes)
5✔
2689
                    .into_script(),
5✔
2690
            }
5✔
2691
        }
5✔
2692

2693
        pub fn txout_opdup_commit_to(addr: &PoxAddress, amount: u64) -> TxOut {
6✔
2694
            addr.to_bitcoin_tx_out(amount)
6✔
2695
        }
6✔
2696

2697
        pub fn txout_opdup_change_legacy(signer: &mut BurnchainOpSigner, amount: u64) -> TxOut {
5✔
2698
            let public_key = signer.get_public_key();
5✔
2699
            let change_address_hash = Hash160::from_data(&public_key.to_bytes());
5✔
2700
            LegacyBitcoinAddress::to_p2pkh_tx_out(&change_address_hash, amount)
5✔
2701
        }
5✔
2702

2703
        pub fn txin_at_index(
5✔
2704
            complete_tx: &Transaction,
5✔
2705
            signer: &BurnchainOpSigner,
5✔
2706
            utxos: &[UTXO],
5✔
2707
            index: usize,
5✔
2708
        ) -> TxIn {
5✔
2709
            //Refresh op signer
2710
            let mut signer = signer.undisposed();
5✔
2711
            let mut public_key = signer.get_public_key();
5✔
2712

2713
            let mut tx = Transaction {
5✔
2714
                version: complete_tx.version,
5✔
2715
                lock_time: complete_tx.lock_time,
5✔
2716
                input: vec![],
5✔
2717
                output: complete_tx.output.clone(),
5✔
2718
            };
5✔
2719

2720
            for utxo in utxos.iter() {
5✔
2721
                let input = TxIn {
5✔
2722
                    previous_output: OutPoint {
5✔
2723
                        txid: utxo.txid.clone(),
5✔
2724
                        vout: utxo.vout,
5✔
2725
                    },
5✔
2726
                    script_sig: Script::new(),
5✔
2727
                    sequence: 0xFFFFFFFD, // allow RBF
5✔
2728
                    witness: vec![],
5✔
2729
                };
5✔
2730
                tx.input.push(input);
5✔
2731
            }
5✔
2732

2733
            for (i, utxo) in utxos.iter().enumerate() {
5✔
2734
                let script_pub_key = utxo.script_pub_key.clone();
5✔
2735
                let sig_hash_all = 0x01;
5✔
2736

2737
                let (sig_hash, is_segwit) = if script_pub_key.as_bytes().len() == 22
5✔
2738
                    && script_pub_key.as_bytes()[0..2] == [0x00, 0x14]
×
2739
                {
2740
                    // p2wpkh
2741
                    (
×
2742
                        tx.segwit_signature_hash(i, &script_pub_key, utxo.amount, sig_hash_all),
×
2743
                        true,
×
2744
                    )
×
2745
                } else {
2746
                    // p2pkh
2747
                    (tx.signature_hash(i, &script_pub_key, sig_hash_all), false)
5✔
2748
                };
2749

2750
                let sig1_der = {
5✔
2751
                    let message = signer
5✔
2752
                        .sign_message(sig_hash.as_bytes())
5✔
2753
                        .expect("Unable to sign message");
5✔
2754
                    message
5✔
2755
                        .to_secp256k1_recoverable()
5✔
2756
                        .expect("Unable to get recoverable signature")
5✔
2757
                        .to_standard()
5✔
2758
                        .serialize_der()
5✔
2759
                };
2760

2761
                if is_segwit {
5✔
2762
                    // segwit
×
2763
                    public_key.set_compressed(true);
×
2764
                    tx.input[i].script_sig = Script::from(vec![]);
×
2765
                    tx.input[i].witness = vec![
×
2766
                        [&*sig1_der, &[sig_hash_all as u8][..]].concat().to_vec(),
×
2767
                        public_key.to_bytes(),
×
2768
                    ];
×
2769
                } else {
5✔
2770
                    // legacy scriptSig
5✔
2771
                    tx.input[i].script_sig = Builder::new()
5✔
2772
                        .push_slice(&[&*sig1_der, &[sig_hash_all as u8][..]].concat())
5✔
2773
                        .push_slice(&public_key.to_bytes())
5✔
2774
                        .into_script();
5✔
2775
                    tx.input[i].witness.clear();
5✔
2776
                }
5✔
2777
            }
2778

2779
            tx.input[index].clone()
5✔
2780
        }
5✔
2781

2782
        pub fn create_templated_leader_key_op() -> LeaderKeyRegisterOp {
4✔
2783
            LeaderKeyRegisterOp {
4✔
2784
                consensus_hash: ConsensusHash([0u8; 20]),
4✔
2785
                public_key: VRFPublicKey::from_private(
4✔
2786
                    &VRFPrivateKey::from_bytes(&[0u8; 32]).unwrap(),
4✔
2787
                ),
4✔
2788
                memo: vec![],
4✔
2789
                txid: Txid([3u8; 32]),
4✔
2790
                vtxindex: 0,
4✔
2791
                block_height: 1,
4✔
2792
                burn_header_hash: BurnchainHeaderHash([9u8; 32]),
4✔
2793
            }
4✔
2794
        }
4✔
2795

2796
        pub fn create_templated_pre_stx_op() -> PreStxOp {
4✔
2797
            PreStxOp {
4✔
2798
                output: StacksAddress::p2pkh_from_hash(false, Hash160::from_data(&[2u8; 20])),
4✔
2799
                txid: Txid([0u8; 32]),
4✔
2800
                vtxindex: 0,
4✔
2801
                block_height: 0,
4✔
2802
                burn_header_hash: BurnchainHeaderHash([0u8; 32]),
4✔
2803
            }
4✔
2804
        }
4✔
2805

2806
        pub fn create_follower_config() -> Config {
2✔
2807
            let mut config = Config::default();
2✔
2808
            config.node.miner = false;
2✔
2809
            config.burnchain.magic_bytes = "T3".as_bytes().into();
2✔
2810
            config.burnchain.username = None;
2✔
2811
            config.burnchain.password = None;
2✔
2812
            config.burnchain.peer_host = String::from("127.0.0.1");
2✔
2813
            config.burnchain.peer_port = 8333;
2✔
2814
            config.node.working_dir = format!("/tmp/follower");
2✔
2815
            config
2✔
2816
        }
2✔
2817
    }
2818

2819
    #[test]
2820
    fn test_get_satoshis_per_byte() {
1✔
2821
        let dir = temp_dir();
1✔
2822
        let file_path = dir.as_path().join("config.toml");
1✔
2823

2824
        let mut config = Config::default();
1✔
2825

2826
        let satoshis_per_byte = get_satoshis_per_byte(&config);
1✔
2827
        assert_eq!(satoshis_per_byte, DEFAULT_SATS_PER_VB);
1✔
2828

2829
        let mut file = File::create(&file_path).unwrap();
1✔
2830
        writeln!(file, "[burnchain]").unwrap();
1✔
2831
        writeln!(file, "satoshis_per_byte = 51").unwrap();
1✔
2832
        config.config_path = Some(file_path.to_str().unwrap().to_string());
1✔
2833

2834
        assert_eq!(get_satoshis_per_byte(&config), 51);
1✔
2835
    }
1✔
2836

2837
    /// Verify that we can build a valid Bitcoin transaction with multiple UTXOs.
2838
    /// Taken from production data.
2839
    /// Tests `serialize_tx()` and `send_block_commit_operation_at_burnchain_height()`
2840
    #[test]
2841
    fn test_multiple_inputs() {
1✔
2842
        let spend_utxos = vec![
1✔
2843
            UTXO {
1✔
2844
                txid: Sha256dHash::from_hex(
1✔
2845
                    "d3eafb3aba3cec925473550ed2e4d00bcb0d00744bb3212e4a8e72878909daee",
1✔
2846
                )
1✔
2847
                .unwrap(),
1✔
2848
                vout: 3,
1✔
2849
                script_pub_key: Builder::from(
1✔
2850
                    hex_bytes("76a9141dc27eba0247f8cc9575e7d45e50a0bc7e72427d88ac").unwrap(),
1✔
2851
                )
1✔
2852
                .into_script(),
1✔
2853
                amount: 42051,
1✔
2854
                confirmations: 1421,
1✔
2855
            },
1✔
2856
            UTXO {
1✔
2857
                txid: Sha256dHash::from_hex(
1✔
2858
                    "01132f2d4a98cc715624e033214c8d841098a1ee15b30188ab89589a320b3b24",
1✔
2859
                )
1✔
2860
                .unwrap(),
1✔
2861
                vout: 0,
1✔
2862
                script_pub_key: Builder::from(
1✔
2863
                    hex_bytes("76a9141dc27eba0247f8cc9575e7d45e50a0bc7e72427d88ac").unwrap(),
1✔
2864
                )
1✔
2865
                .into_script(),
1✔
2866
                amount: 326456,
1✔
2867
                confirmations: 1421,
1✔
2868
            },
1✔
2869
        ];
2870

2871
        // test serialize_tx()
2872
        let config = utils::create_miner_config();
1✔
2873

2874
        let mut btc_controller = BitcoinRegtestController::new(config, None);
1✔
2875
        let mut utxo_set = UTXOSet {
1✔
2876
            bhh: BurnchainHeaderHash([0x01; 32]),
1✔
2877
            utxos: spend_utxos.clone(),
1✔
2878
        };
1✔
2879
        let mut transaction = Transaction {
1✔
2880
            input: vec![],
1✔
2881
            output: vec![
1✔
2882
                TxOut {
1✔
2883
                    value: 0,
1✔
2884
                    script_pubkey: Builder::from(hex_bytes("6a4c5054335be88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32afd5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375000008a300010000059800015a").unwrap()).into_script(),
1✔
2885
                },
1✔
2886
                TxOut {
1✔
2887
                    value: 10000,
1✔
2888
                    script_pubkey: Builder::from(hex_bytes("76a914000000000000000000000000000000000000000088ac").unwrap()).into_script(),
1✔
2889
                },
1✔
2890
                TxOut {
1✔
2891
                    value: 10000,
1✔
2892
                    script_pubkey: Builder::from(hex_bytes("76a914000000000000000000000000000000000000000088ac").unwrap()).into_script(),
1✔
2893
                },
1✔
2894
            ],
1✔
2895
            version: 1,
1✔
2896
            lock_time: 0,
1✔
2897
        };
1✔
2898

2899
        let mut signer = BurnchainOpSigner::new(
1✔
2900
            Secp256k1PrivateKey::from_hex(
1✔
2901
                "9e446f6b0c6a96cf2190e54bcd5a8569c3e386f091605499464389b8d4e0bfc201",
1✔
2902
            )
2903
            .unwrap(),
1✔
2904
        );
2905
        assert!(btc_controller.serialize_tx(
1✔
2906
            StacksEpochId::Epoch25,
1✔
2907
            &mut transaction,
1✔
2908
            44950,
2909
            &mut utxo_set,
1✔
2910
            &mut signer,
1✔
2911
            true
2912
        ));
2913
        assert_eq!(transaction.output[3].value, 323557);
1✔
2914

2915
        // test send_block_commit_operation_at_burn_height()
2916
        let utxo_set = UTXOSet {
1✔
2917
            bhh: BurnchainHeaderHash([0x01; 32]),
1✔
2918
            utxos: spend_utxos,
1✔
2919
        };
1✔
2920

2921
        let commit_op = LeaderBlockCommitOp {
1✔
2922
            block_header_hash: BlockHeaderHash::from_hex(
1✔
2923
                "e88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32af",
1✔
2924
            )
1✔
2925
            .unwrap(),
1✔
2926
            new_seed: VRFSeed::from_hex(
1✔
2927
                "d5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375",
1✔
2928
            )
1✔
2929
            .unwrap(),
1✔
2930
            parent_block_ptr: 2211, // 0x000008a3
1✔
2931
            parent_vtxindex: 1,     // 0x0001
1✔
2932
            key_block_ptr: 1432,    // 0x00000598
1✔
2933
            key_vtxindex: 1,        // 0x0001
1✔
2934
            memo: vec![11],         // 0x5a >> 3
1✔
2935

1✔
2936
            burn_fee: 0,
1✔
2937
            input: (Txid([0x00; 32]), 0),
1✔
2938
            burn_parent_modulus: 2, // 0x5a & 0b111
1✔
2939

1✔
2940
            apparent_sender: BurnchainSigner("mgbpit8FvkVJ9kuXY8QSM5P7eibnhcEMBk".to_string()),
1✔
2941
            commit_outs: vec![
1✔
2942
                PoxAddress::Standard(StacksAddress::burn_address(false), None),
1✔
2943
                PoxAddress::Standard(StacksAddress::burn_address(false), None),
1✔
2944
            ],
1✔
2945

1✔
2946
            treatment: vec![],
1✔
2947
            sunset_burn: 0,
1✔
2948

1✔
2949
            txid: Txid([0x00; 32]),
1✔
2950
            vtxindex: 0,
1✔
2951
            block_height: 2212,
1✔
2952
            burn_header_hash: BurnchainHeaderHash([0x01; 32]),
1✔
2953
        };
1✔
2954

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

2957
        let leader_fees = LeaderBlockCommitFees {
1✔
2958
            sunset_fee: 0,
1✔
2959
            fee_rate: 50,
1✔
2960
            sortition_fee: 20000,
1✔
2961
            outputs_len: 2,
1✔
2962
            default_tx_size: 380,
1✔
2963
            spent_in_attempts: 0,
1✔
2964
            is_rbf_enabled: false,
1✔
2965
            final_size: 498,
1✔
2966
        };
1✔
2967

2968
        assert_eq!(leader_fees.amount_per_output(), 10000);
1✔
2969
        assert_eq!(leader_fees.total_spent(), 44900);
1✔
2970

2971
        let block_commit = btc_controller
1✔
2972
            .send_block_commit_operation_at_burnchain_height(
1✔
2973
                StacksEpochId::Epoch30,
1✔
2974
                commit_op,
1✔
2975
                &mut signer,
1✔
2976
                Some(utxo_set),
1✔
2977
                None,
1✔
2978
                leader_fees,
1✔
2979
                &[],
1✔
2980
                2212,
2981
            )
2982
            .unwrap();
1✔
2983

2984
        debug!("send_block_commit_operation:\n{block_commit:#?}");
1✔
2985
        assert_eq!(block_commit.output[3].value, 323507);
1✔
2986
        assert_eq!(serialize_hex(&block_commit).unwrap(), "0100000002eeda098987728e4a2e21b34b74000dcb0bd0e4d20e55735492ec3cba3afbead3030000006a4730440220558286e20e10ce31537f0625dae5cc62fac7961b9d2cf272c990de96323d7e2502202255adbea3d2e0509b80c5d8a3a4fe6397a87bcf18da1852740d5267d89a0cb20121035379aa40c02890d253cfa577964116eb5295570ae9f7287cbae5f2585f5b2c7cfdffffff243b0b329a5889ab8801b315eea19810848d4c2133e0245671cc984a2d2f1301000000006a47304402206d9f8de107f9e1eb15aafac66c2bb34331a7523260b30e18779257e367048d34022013c7dabb32a5c281aa00d405e2ccbd00f34f03a65b2336553a4acd6c52c251ef0121035379aa40c02890d253cfa577964116eb5295570ae9f7287cbae5f2585f5b2c7cfdffffff040000000000000000536a4c5054335be88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32afd5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375000008a300010000059800015a10270000000000001976a914000000000000000000000000000000000000000088ac10270000000000001976a914000000000000000000000000000000000000000088acb3ef0400000000001976a9141dc27eba0247f8cc9575e7d45e50a0bc7e72427d88ac00000000");
1✔
2987
    }
1✔
2988

2989
    #[test]
2990
    fn test_to_epoch_aware_pubkey() {
1✔
2991
        let mut config = utils::create_miner_config();
1✔
2992
        let pubkey = utils::create_miner1_pubkey();
1✔
2993

2994
        config.miner.segwit = false;
1✔
2995
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
2996

2997
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch20, &pubkey);
1✔
2998
        assert_eq!(
1✔
2999
            false,
3000
            reviewed.compressed(),
1✔
3001
            "Segwit disabled with Epoch < 2.1: not compressed"
3002
        );
3003
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch21, &pubkey);
1✔
3004
        assert_eq!(
1✔
3005
            false,
3006
            reviewed.compressed(),
1✔
3007
            "Segwit disabled with Epoch >= 2.1: not compressed"
3008
        );
3009

3010
        config.miner.segwit = true;
1✔
3011
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3012

3013
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch20, &pubkey);
1✔
3014
        assert_eq!(
1✔
3015
            false,
3016
            reviewed.compressed(),
1✔
3017
            "Segwit enabled with Epoch < 2.1: not compressed"
3018
        );
3019
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch21, &pubkey);
1✔
3020
        assert_eq!(
1✔
3021
            true,
3022
            reviewed.compressed(),
1✔
3023
            "Segwit enabled with Epoch >= 2.1: compressed"
3024
        );
3025
    }
1✔
3026

3027
    #[test]
3028
    fn test_get_miner_address() {
1✔
3029
        let mut config = utils::create_miner_config();
1✔
3030
        let pub_key = utils::create_miner1_pubkey();
1✔
3031

3032
        config.miner.segwit = false;
1✔
3033
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3034

3035
        let expected = utils::to_address_legacy(&pub_key);
1✔
3036
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch20, &pub_key);
1✔
3037
        assert_eq!(
1✔
3038
            expected, address,
3039
            "Segwit disabled with Epoch < 2.1: legacy addr"
3040
        );
3041

3042
        let expected = utils::to_address_legacy(&pub_key);
1✔
3043
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch21, &pub_key);
1✔
3044
        assert_eq!(
1✔
3045
            expected, address,
3046
            "Segwit disabled with Epoch >= 2.1: legacy addr"
3047
        );
3048

3049
        config.miner.segwit = true;
1✔
3050
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3051

3052
        let expected = utils::to_address_legacy(&pub_key);
1✔
3053
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch20, &pub_key);
1✔
3054
        assert_eq!(
1✔
3055
            expected, address,
3056
            "Segwit enabled with Epoch < 2.1: legacy addr"
3057
        );
3058

3059
        let expected = utils::to_address_segwit_p2wpkh(&pub_key);
1✔
3060
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch21, &pub_key);
1✔
3061
        assert_eq!(
1✔
3062
            expected, address,
3063
            "Segwit enabled with Epoch >= 2.1: segwit addr"
3064
        );
3065
    }
1✔
3066

3067
    #[test]
3068
    fn test_instantiate_with_burnchain_on_follower_node_ok() {
1✔
3069
        let config = create_follower_config();
1✔
3070

3071
        let btc_controller = BitcoinRegtestController::with_burnchain(config, None, None, None);
1✔
3072

3073
        let result = panic::catch_unwind(AssertUnwindSafe(|| {
1✔
3074
            _ = btc_controller.get_rpc_client();
1✔
3075
        }));
1✔
3076
        assert!(
1✔
3077
            result.is_err(),
1✔
3078
            "Invoking any Bitcoin RPC related method should panic."
3079
        );
3080
    }
1✔
3081

3082
    #[test]
3083
    fn test_instantiate_with_burnchain_on_miner_node_ok() {
1✔
3084
        let config = create_miner_config();
1✔
3085

3086
        let btc_controller = BitcoinRegtestController::with_burnchain(config, None, None, None);
1✔
3087

3088
        let _ = btc_controller.get_rpc_client();
1✔
3089
        assert!(true, "Invoking any Bitcoin RPC related method should work.");
1✔
3090
    }
1✔
3091

3092
    #[test]
3093
    fn test_instantiate_with_burnchain_on_miner_node_failure() {
1✔
3094
        let mut config = create_miner_config();
1✔
3095
        config.burnchain.username = None;
1✔
3096
        config.burnchain.password = None;
1✔
3097

3098
        let result = panic::catch_unwind(|| {
1✔
3099
            _ = BitcoinRegtestController::with_burnchain(config, None, None, None);
1✔
3100
        });
1✔
3101
        assert!(
1✔
3102
            result.is_err(),
1✔
3103
            "Bitcoin RPC credentials are mandatory for miner node."
3104
        );
3105
    }
1✔
3106

3107
    #[test]
3108
    fn test_instantiate_new_dummy_on_follower_node_ok() {
1✔
3109
        let config = create_follower_config();
1✔
3110

3111
        let btc_controller = BitcoinRegtestController::new_dummy(config);
1✔
3112

3113
        let result = panic::catch_unwind(AssertUnwindSafe(|| {
1✔
3114
            _ = btc_controller.get_rpc_client();
1✔
3115
        }));
1✔
3116
        assert!(
1✔
3117
            result.is_err(),
1✔
3118
            "Invoking any Bitcoin RPC related method should panic."
3119
        );
3120
    }
1✔
3121

3122
    #[test]
3123
    fn test_instantiate_new_dummy_on_miner_node_ok() {
1✔
3124
        let config = create_miner_config();
1✔
3125

3126
        let btc_controller = BitcoinRegtestController::new_dummy(config);
1✔
3127

3128
        let _ = btc_controller.get_rpc_client();
1✔
3129
        assert!(true, "Invoking any Bitcoin RPC related method should work.");
1✔
3130
    }
1✔
3131

3132
    #[test]
3133
    fn test_instantiate_new_dummy_on_miner_node_failure() {
1✔
3134
        let mut config = create_miner_config();
1✔
3135
        config.burnchain.username = None;
1✔
3136
        config.burnchain.password = None;
1✔
3137

3138
        let result = panic::catch_unwind(|| {
1✔
3139
            _ = BitcoinRegtestController::new_dummy(config);
1✔
3140
        });
1✔
3141
        assert!(
1✔
3142
            result.is_err(),
1✔
3143
            "Bitcoin RPC credentials are mandatory for miner node."
3144
        );
3145
    }
1✔
3146

3147
    #[test]
3148
    #[ignore]
3149
    fn test_create_wallet_from_default_empty_name() {
1✔
3150
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3151
            return;
×
3152
        }
1✔
3153

3154
        let config = utils::create_miner_config();
1✔
3155

3156
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3157
        btcd_controller
1✔
3158
            .start_bitcoind()
1✔
3159
            .expect("bitcoind should be started!");
1✔
3160

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

3163
        let wallets = btc_controller.list_wallets().unwrap();
1✔
3164
        assert_eq!(0, wallets.len());
1✔
3165

3166
        btc_controller
1✔
3167
            .create_wallet_if_dne()
1✔
3168
            .expect("Wallet should now exists!");
1✔
3169

3170
        let wallets = btc_controller.list_wallets().unwrap();
1✔
3171
        assert_eq!(1, wallets.len());
1✔
3172
        assert_eq!("".to_owned(), wallets[0]);
1✔
3173
    }
1✔
3174

3175
    #[test]
3176
    #[ignore]
3177
    fn test_create_wallet_from_custom_name() {
1✔
3178
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3179
            return;
×
3180
        }
1✔
3181

3182
        let mut config = utils::create_miner_config();
1✔
3183
        config.burnchain.wallet_name = String::from("mywallet");
1✔
3184

3185
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3186
        btcd_controller
1✔
3187
            .start_bitcoind()
1✔
3188
            .expect("bitcoind should be started!");
1✔
3189

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

3192
        btc_controller
1✔
3193
            .create_wallet_if_dne()
1✔
3194
            .expect("Wallet should now exists!");
1✔
3195

3196
        let wallets = btc_controller.list_wallets().unwrap();
1✔
3197
        assert_eq!(1, wallets.len());
1✔
3198
        assert_eq!("mywallet".to_owned(), wallets[0]);
1✔
3199
    }
1✔
3200

3201
    #[test]
3202
    #[ignore]
3203
    fn test_retrieve_utxo_set_with_all_utxos() {
1✔
3204
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3205
            return;
×
3206
        }
1✔
3207

3208
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3209

3210
        let mut config = utils::create_miner_config();
1✔
3211
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3212

3213
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3214
        btcd_controller
1✔
3215
            .start_bitcoind()
1✔
3216
            .expect("Failed starting bitcoind");
1✔
3217

3218
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3219
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3220

3221
        let address = to_address_legacy(&miner_pubkey);
1✔
3222
        let utxo_set = btc_controller
1✔
3223
            .retrieve_utxo_set(&address, false, 0, &None, 0)
1✔
3224
            .expect("Failed to get utxos");
1✔
3225
        assert_eq!(btc_controller.get_block_hash(0), utxo_set.bhh);
1✔
3226
        assert_eq!(50, utxo_set.num_utxos());
1✔
3227
    }
1✔
3228

3229
    #[test]
3230
    #[ignore]
3231
    fn test_retrive_utxo_set_excluding_some_utxo() {
1✔
3232
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3233
            return;
×
3234
        }
1✔
3235

3236
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3237

3238
        let mut config = utils::create_miner_config();
1✔
3239
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3240

3241
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3242
        btcd_controller
1✔
3243
            .start_bitcoind()
1✔
3244
            .expect("Failed starting bitcoind");
1✔
3245

3246
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3247
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3248

3249
        let address = to_address_legacy(&miner_pubkey);
1✔
3250
        let mut all_utxos = btc_controller
1✔
3251
            .retrieve_utxo_set(&address, false, 0, &None, 0)
1✔
3252
            .expect("Failed to get utxos (50)");
1✔
3253

3254
        let filtered_utxos = btc_controller
1✔
3255
            .retrieve_utxo_set(&address, false, 0, &Some(all_utxos.clone()), 0)
1✔
3256
            .expect("Failed to get utxos");
1✔
3257
        assert_eq!(0, filtered_utxos.num_utxos(), "all utxos filtered out!");
1✔
3258

3259
        all_utxos.utxos.drain(0..10);
1✔
3260
        let filtered_utxos = btc_controller
1✔
3261
            .retrieve_utxo_set(&address, false, 0, &Some(all_utxos), 0)
1✔
3262
            .expect("Failed to get utxos");
1✔
3263
        assert_eq!(10, filtered_utxos.num_utxos(), "40 utxos filtered out!");
1✔
3264
    }
1✔
3265

3266
    #[test]
3267
    #[ignore]
3268
    fn test_list_unspent_with_max_utxos_config() {
1✔
3269
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3270
            return;
×
3271
        }
1✔
3272

3273
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3274

3275
        let mut config = utils::create_miner_config();
1✔
3276
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3277
        config.burnchain.max_unspent_utxos = Some(10);
1✔
3278

3279
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3280
        btcd_controller
1✔
3281
            .start_bitcoind()
1✔
3282
            .expect("Failed starting bitcoind");
1✔
3283

3284
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3285
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3286

3287
        let address = to_address_legacy(&miner_pubkey);
1✔
3288
        let utxos = btc_controller
1✔
3289
            .retrieve_utxo_set(&address, false, 1, &None, 0)
1✔
3290
            .expect("Failed to get utxos");
1✔
3291
        assert_eq!(10, utxos.num_utxos());
1✔
3292
    }
1✔
3293

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

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

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

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

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

3313
        btc_controller.bootstrap_chain(100);
1✔
3314
        let utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3315
        assert_eq!(0, utxos.len());
1✔
3316

3317
        btc_controller.build_next_block(1);
1✔
3318
        let utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3319
        assert_eq!(1, utxos.len());
1✔
3320
        assert_eq!(101, utxos[0].confirmations);
1✔
3321
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3322

3323
        btc_controller.build_next_block(1);
1✔
3324
        let mut utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3325
        utxos.sort_by(|a, b| b.confirmations.cmp(&a.confirmations));
1✔
3326

3327
        assert_eq!(2, utxos.len());
1✔
3328
        assert_eq!(102, utxos[0].confirmations);
1✔
3329
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3330
        assert_eq!(101, utxos[1].confirmations);
1✔
3331
        assert_eq!(5_000_000_000, utxos[1].amount);
1✔
3332
    }
1✔
3333

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

3341
        let miner1_pubkey = utils::create_miner1_pubkey();
1✔
3342
        let miner2_pubkey = utils::create_miner2_pubkey();
1✔
3343

3344
        let mut config = utils::create_miner_config();
1✔
3345
        config.burnchain.local_mining_public_key = Some(miner1_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 miner1_btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3353
        miner1_btc_controller.bootstrap_chain(1); // one utxo for miner_pubkey related address
1✔
3354

3355
        config.burnchain.local_mining_public_key = Some(miner2_pubkey.to_hex());
1✔
3356
        config.burnchain.wallet_name = "miner2_wallet".to_string();
1✔
3357
        let miner2_btc_controller = BitcoinRegtestController::new(config, None);
1✔
3358
        miner2_btc_controller.bootstrap_chain(102); // two utxo for other_pubkeys related address
1✔
3359

3360
        let utxos = miner1_btc_controller.get_all_utxos(&miner1_pubkey);
1✔
3361
        assert_eq!(1, utxos.len(), "miner1 see its own utxos");
1✔
3362

3363
        let utxos = miner1_btc_controller.get_all_utxos(&miner2_pubkey);
1✔
3364
        assert_eq!(2, utxos.len(), "miner1 see miner2 utxos");
1✔
3365

3366
        let utxos = miner2_btc_controller.get_all_utxos(&miner2_pubkey);
1✔
3367
        assert_eq!(2, utxos.len(), "miner2 see its own utxos");
1✔
3368

3369
        let utxos = miner2_btc_controller.get_all_utxos(&miner1_pubkey);
1✔
3370
        assert_eq!(1, utxos.len(), "miner2 see miner1 own utxos");
1✔
3371
    }
1✔
3372

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

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

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

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

3390
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3391
        btc_controller.bootstrap_chain(101);
1✔
3392

3393
        let utxos_opt =
1✔
3394
            btc_controller.get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 1, None, 101);
1✔
3395
        let uxto_set = utxos_opt.expect("Shouldn't be None at height 101!");
1✔
3396

3397
        assert_eq!(btc_controller.get_block_hash(101), uxto_set.bhh);
1✔
3398
        assert_eq!(1, uxto_set.num_utxos());
1✔
3399
        assert_eq!(5_000_000_000, uxto_set.total_available());
1✔
3400
        let utxos = uxto_set.utxos;
1✔
3401
        assert_eq!(101, utxos[0].confirmations);
1✔
3402
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3403

3404
        btc_controller.build_next_block(1);
1✔
3405

3406
        let utxos_opt =
1✔
3407
            btc_controller.get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 1, None, 102);
1✔
3408
        let uxto_set = utxos_opt.expect("Shouldn't be None at height 102!");
1✔
3409

3410
        assert_eq!(btc_controller.get_block_hash(102), uxto_set.bhh);
1✔
3411
        assert_eq!(2, uxto_set.num_utxos());
1✔
3412
        assert_eq!(10_000_000_000, uxto_set.total_available());
1✔
3413
        let mut utxos = uxto_set.utxos;
1✔
3414
        utxos.sort_by(|a, b| b.confirmations.cmp(&a.confirmations));
1✔
3415
        assert_eq!(102, utxos[0].confirmations);
1✔
3416
        assert_eq!(5_000_000_000, utxos[0].amount);
1✔
3417
        assert_eq!(101, utxos[1].confirmations);
1✔
3418
        assert_eq!(5_000_000_000, utxos[1].amount);
1✔
3419
    }
1✔
3420

3421
    #[test]
3422
    #[ignore]
3423
    fn test_get_utxos_none_due_to_filter_total_required() {
1✔
3424
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3425
            return;
×
3426
        }
1✔
3427

3428
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3429

3430
        let mut config = utils::create_miner_config();
1✔
3431
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3432

3433
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3434
        btcd_controller
1✔
3435
            .start_bitcoind()
1✔
3436
            .expect("bitcoind should be started!");
1✔
3437

3438
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3439
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3440

3441
        let too_much_required = 10_000_000_000;
1✔
3442
        let utxos = btc_controller.get_utxos(
1✔
3443
            StacksEpochId::Epoch31,
1✔
3444
            &miner_pubkey,
1✔
3445
            too_much_required,
1✔
3446
            None,
1✔
3447
            0,
3448
        );
3449
        assert!(utxos.is_none(), "None because too much required");
1✔
3450
    }
1✔
3451

3452
    #[test]
3453
    #[ignore]
3454
    fn test_get_utxos_none_due_to_filter_pubkey() {
1✔
3455
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3456
            return;
×
3457
        }
1✔
3458

3459
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3460

3461
        let mut config = utils::create_miner_config();
1✔
3462
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3463

3464
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3465
        btcd_controller
1✔
3466
            .start_bitcoind()
1✔
3467
            .expect("bitcoind should be started!");
1✔
3468

3469
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3470
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3471

3472
        let other_pubkey = utils::create_miner2_pubkey();
1✔
3473
        let utxos = btc_controller.get_utxos(StacksEpochId::Epoch31, &other_pubkey, 1, None, 0);
1✔
3474
        assert!(
1✔
3475
            utxos.is_none(),
1✔
3476
            "None because utxos for other pubkey don't exist"
3477
        );
3478
    }
1✔
3479

3480
    #[test]
3481
    #[ignore]
3482
    fn test_get_utxos_none_due_to_filter_utxo_exclusion() {
1✔
3483
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3484
            return;
×
3485
        }
1✔
3486

3487
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3488

3489
        let mut config = utils::create_miner_config();
1✔
3490
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3491

3492
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3493
        btcd_controller
1✔
3494
            .start_bitcoind()
1✔
3495
            .expect("bitcoind should be started!");
1✔
3496

3497
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3498
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3499

3500
        let existent_utxo = btc_controller
1✔
3501
            .get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 0, None, 0)
1✔
3502
            .expect("utxo set should exist");
1✔
3503
        let utxos = btc_controller.get_utxos(
1✔
3504
            StacksEpochId::Epoch31,
1✔
3505
            &miner_pubkey,
1✔
3506
            0,
3507
            Some(existent_utxo),
1✔
3508
            0,
3509
        );
3510
        assert!(
1✔
3511
            utxos.is_none(),
1✔
3512
            "None because filtering exclude existent utxo set"
3513
        );
3514
    }
1✔
3515

3516
    #[test]
3517
    #[ignore]
3518
    fn test_tx_confirmed_from_utxo_ok() {
1✔
3519
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3520
            return;
×
3521
        }
1✔
3522

3523
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3524

3525
        let mut config = utils::create_miner_config();
1✔
3526
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3527

3528
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3529
        btcd_controller
1✔
3530
            .start_bitcoind()
1✔
3531
            .expect("bitcoind should be started!");
1✔
3532

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

3535
        btc_controller.bootstrap_chain(101);
1✔
3536
        let utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3537
        assert_eq!(1, utxos.len(), "One UTXO should be confirmed!");
1✔
3538

3539
        let txid = Txid::from_bitcoin_tx_hash(&utxos[0].txid);
1✔
3540
        assert!(
1✔
3541
            btc_controller.is_transaction_confirmed(&txid),
1✔
3542
            "UTXO tx should be confirmed!"
3543
        );
3544
    }
1✔
3545

3546
    #[test]
3547
    #[ignore]
3548
    fn test_import_public_key_ok() {
1✔
3549
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3550
            return;
×
3551
        }
1✔
3552

3553
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3554

3555
        let config = utils::create_miner_config();
1✔
3556

3557
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3558
        btcd_controller
1✔
3559
            .start_bitcoind()
1✔
3560
            .expect("bitcoind should be started!");
1✔
3561

3562
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3563
        btc_controller
1✔
3564
            .create_wallet_if_dne()
1✔
3565
            .expect("Wallet should be created!");
1✔
3566

3567
        let result = btc_controller.import_public_key(&miner_pubkey);
1✔
3568
        assert!(
1✔
3569
            result.is_ok(),
1✔
3570
            "Should be ok, got err instead: {:?}",
3571
            result.unwrap_err()
×
3572
        );
3573
    }
1✔
3574

3575
    #[test]
3576
    #[ignore]
3577
    fn test_import_public_key_twice_ok() {
1✔
3578
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3579
            return;
×
3580
        }
1✔
3581

3582
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3583

3584
        let config = utils::create_miner_config();
1✔
3585

3586
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3587
        btcd_controller
1✔
3588
            .start_bitcoind()
1✔
3589
            .expect("bitcoind should be started!");
1✔
3590

3591
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3592
        btc_controller
1✔
3593
            .create_wallet_if_dne()
1✔
3594
            .expect("Wallet should be created!");
1✔
3595

3596
        btc_controller
1✔
3597
            .import_public_key(&miner_pubkey)
1✔
3598
            .expect("Import should be ok: first time!");
1✔
3599

3600
        //ok, but it is basically a no-op
3601
        let result = btc_controller.import_public_key(&miner_pubkey);
1✔
3602
        assert!(
1✔
3603
            result.is_ok(),
1✔
3604
            "Should be ok, got err instead: {:?}",
3605
            result.unwrap_err()
×
3606
        );
3607
    }
1✔
3608

3609
    #[test]
3610
    #[ignore]
3611
    fn test_import_public_key_segwit_ok() {
1✔
3612
        if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3613
            return;
×
3614
        }
1✔
3615

3616
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3617

3618
        let mut config = utils::create_miner_config();
1✔
3619
        config.miner.segwit = true;
1✔
3620

3621
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3622
        btcd_controller
1✔
3623
            .start_bitcoind()
1✔
3624
            .expect("bitcoind should be started!");
1✔
3625

3626
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3627
        btc_controller
1✔
3628
            .create_wallet_if_dne()
1✔
3629
            .expect("Wallet should be created!");
1✔
3630

3631
        let result = btc_controller.import_public_key(&miner_pubkey);
1✔
3632
        assert!(
1✔
3633
            result.is_ok(),
1✔
3634
            "Should be ok, got err instead: {:?}",
3635
            result.unwrap_err()
×
3636
        );
3637
    }
1✔
3638

3639
    /// Tests related to Leader Block Commit operation
3640
    mod leader_commit_op {
3641
        use super::*;
3642

3643
        #[test]
3644
        #[ignore]
3645
        fn test_build_leader_block_commit_tx_ok_with_new_commit_op() {
1✔
3646
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3647
                return;
×
3648
            }
1✔
3649

3650
            let keychain = utils::create_keychain();
1✔
3651
            let miner_pubkey = keychain.get_pub_key();
1✔
3652
            let mut op_signer = keychain.generate_op_signer();
1✔
3653

3654
            let mut config = utils::create_miner_config();
1✔
3655
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3656
            config.burnchain.pox_reward_length = Some(11);
1✔
3657

3658
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3659
            btcd_controller
1✔
3660
                .start_bitcoind()
1✔
3661
                .expect("bitcoind should be started!");
1✔
3662

3663
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3664
            btc_controller
1✔
3665
                .connect_dbs()
1✔
3666
                .expect("Dbs initialization required!");
1✔
3667
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3668

3669
            let mut commit_op = utils::create_templated_commit_op();
1✔
3670
            commit_op.sunset_burn = 5_500;
1✔
3671
            commit_op.burn_fee = 110_000;
1✔
3672

3673
            let tx = btc_controller
1✔
3674
                .build_leader_block_commit_tx(
1✔
3675
                    StacksEpochId::Epoch31,
1✔
3676
                    commit_op.clone(),
1✔
3677
                    &mut op_signer,
1✔
3678
                )
3679
                .expect("Build leader block commit should work");
1✔
3680

3681
            assert!(op_signer.is_disposed());
1✔
3682

3683
            assert_eq!(1, tx.version);
1✔
3684
            assert_eq!(0, tx.lock_time);
1✔
3685
            assert_eq!(1, tx.input.len());
1✔
3686
            assert_eq!(4, tx.output.len());
1✔
3687

3688
            // utxos list contains the only existing utxo
3689
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3690
            let input_0 = utils::txin_at_index(&tx, &op_signer, &used_utxos, 0);
1✔
3691
            assert_eq!(input_0, tx.input[0]);
1✔
3692

3693
            let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes, 5_500);
1✔
3694
            let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_000);
1✔
3695
            let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_000);
1✔
3696
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 4_999_865_300);
1✔
3697
            assert_eq!(op_return, tx.output[0]);
1✔
3698
            assert_eq!(op_commit_1, tx.output[1]);
1✔
3699
            assert_eq!(op_commit_2, tx.output[2]);
1✔
3700
            assert_eq!(op_change, tx.output[3]);
1✔
3701
        }
1✔
3702

3703
        #[test]
3704
        #[ignore]
3705
        fn test_build_leader_block_commit_tx_fails_resub_same_commit_op_while_prev_not_confirmed() {
1✔
3706
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3707
                return;
×
3708
            }
1✔
3709

3710
            let keychain = utils::create_keychain();
1✔
3711
            let miner_pubkey = keychain.get_pub_key();
1✔
3712
            let mut op_signer = keychain.generate_op_signer();
1✔
3713

3714
            let mut config = utils::create_miner_config();
1✔
3715
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3716
            config.burnchain.pox_reward_length = Some(11);
1✔
3717

3718
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3719
            btcd_controller
1✔
3720
                .start_bitcoind()
1✔
3721
                .expect("bitcoind should be started!");
1✔
3722

3723
            let mut btc_controller = BitcoinRegtestController::new(config, None);
1✔
3724
            btc_controller
1✔
3725
                .connect_dbs()
1✔
3726
                .expect("Dbs initialization required!");
1✔
3727
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3728

3729
            let commit_op = utils::create_templated_commit_op();
1✔
3730

3731
            let _first_tx_ok = btc_controller
1✔
3732
                .build_leader_block_commit_tx(
1✔
3733
                    StacksEpochId::Epoch31,
1✔
3734
                    commit_op.clone(),
1✔
3735
                    &mut op_signer,
1✔
3736
                )
3737
                .expect("At first, building leader block commit should work");
1✔
3738

3739
            // re-submitting same commit while previous it is not confirmed by the burnchain
3740
            let resubmit = btc_controller.build_leader_block_commit_tx(
1✔
3741
                StacksEpochId::Epoch31,
1✔
3742
                commit_op,
1✔
3743
                &mut op_signer,
1✔
3744
            );
3745

3746
            assert!(resubmit.is_err());
1✔
3747
            assert_eq!(
1✔
3748
                BurnchainControllerError::IdenticalOperation,
3749
                resubmit.unwrap_err()
1✔
3750
            );
3751
        }
1✔
3752

3753
        #[test]
3754
        #[ignore]
3755
        fn test_build_leader_block_commit_tx_fails_resub_same_commit_op_while_prev_is_confirmed() {
1✔
3756
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3757
                return;
×
3758
            }
1✔
3759

3760
            let keychain = utils::create_keychain();
1✔
3761
            let miner_pubkey = keychain.get_pub_key();
1✔
3762
            let mut op_signer = keychain.generate_op_signer();
1✔
3763

3764
            let mut config = utils::create_miner_config();
1✔
3765
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3766
            config.burnchain.pox_reward_length = Some(11);
1✔
3767

3768
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3769
            btcd_controller
1✔
3770
                .start_bitcoind()
1✔
3771
                .expect("bitcoind should be started!");
1✔
3772

3773
            let mut btc_controller = BitcoinRegtestController::new(config, None);
1✔
3774
            btc_controller
1✔
3775
                .connect_dbs()
1✔
3776
                .expect("Dbs initialization required!");
1✔
3777
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3778

3779
            let commit_op = utils::create_templated_commit_op();
1✔
3780

3781
            let first_tx_ok = btc_controller
1✔
3782
                .build_leader_block_commit_tx(
1✔
3783
                    StacksEpochId::Epoch31,
1✔
3784
                    commit_op.clone(),
1✔
3785
                    &mut op_signer,
1✔
3786
                )
3787
                .expect("At first, building leader block commit should work");
1✔
3788

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

3791
            // re-submitting same commit while previous it is confirmed by the burnchain
3792
            let resubmit = btc_controller.build_leader_block_commit_tx(
1✔
3793
                StacksEpochId::Epoch31,
1✔
3794
                commit_op,
1✔
3795
                &mut op_signer,
1✔
3796
            );
3797

3798
            assert!(resubmit.is_err());
1✔
3799
            assert_eq!(
1✔
3800
                BurnchainControllerError::IdenticalOperation,
3801
                resubmit.unwrap_err()
1✔
3802
            );
3803
        }
1✔
3804

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

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

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

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

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

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

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

3843
            let first_txid = first_tx_ok.txid();
1✔
3844

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

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

3853
            let new_tx = btc_controller
1✔
3854
                .build_leader_block_commit_tx(
1✔
3855
                    StacksEpochId::Epoch31,
1✔
3856
                    commit_op.clone(),
1✔
3857
                    &mut signer,
1✔
3858
                )
3859
                .expect("Commit tx should be created!");
1✔
3860

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

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

3868
            // utxos list contains the sole utxo used by prev commit operation
3869
            // because has enough amount to cover the new commit
3870
            let used_utxos: Vec<UTXO> = btc_controller
1✔
3871
                .get_all_utxos(&miner_pubkey)
1✔
3872
                .into_iter()
1✔
3873
                .filter(|utxo| utxo.txid == first_txid)
2✔
3874
                .collect();
1✔
3875

3876
            let input_0 = utils::txin_at_index(&new_tx, &op_signer, &used_utxos, 0);
1✔
3877
            assert_eq!(input_0, new_tx.input[0]);
1✔
3878

3879
            let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes, 5_500);
1✔
3880
            let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_005);
1✔
3881
            let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_005);
1✔
3882
            let op_change = utils::txout_opdup_change_legacy(&mut signer, 4_999_730_590);
1✔
3883
            assert_eq!(op_return, new_tx.output[0]);
1✔
3884
            assert_eq!(op_commit_1, new_tx.output[1]);
1✔
3885
            assert_eq!(op_commit_2, new_tx.output[2]);
1✔
3886
            assert_eq!(op_change, new_tx.output[3]);
1✔
3887
        }
1✔
3888

3889
        #[test]
3890
        #[ignore]
3891
        fn test_build_leader_block_commit_tx_ok_rbf_while_prev_not_confirmed() {
1✔
3892
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3893
                return;
×
3894
            }
1✔
3895

3896
            let keychain = utils::create_keychain();
1✔
3897
            let miner_pubkey = keychain.get_pub_key();
1✔
3898
            let mut op_signer = keychain.generate_op_signer();
1✔
3899

3900
            let mut config = utils::create_miner_config();
1✔
3901
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3902
            config.burnchain.pox_reward_length = Some(11);
1✔
3903

3904
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3905
            btcd_controller
1✔
3906
                .start_bitcoind()
1✔
3907
                .expect("bitcoind should be started!");
1✔
3908

3909
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3910
            btc_controller
1✔
3911
                .connect_dbs()
1✔
3912
                .expect("Dbs initialization required!");
1✔
3913
            btc_controller.bootstrap_chain(101); // Now, one utxo exists
1✔
3914

3915
            let mut commit_op = utils::create_templated_commit_op();
1✔
3916
            commit_op.sunset_burn = 5_500;
1✔
3917
            commit_op.burn_fee = 110_000;
1✔
3918

3919
            let _first_tx_ok = btc_controller
1✔
3920
                .build_leader_block_commit_tx(
1✔
3921
                    StacksEpochId::Epoch31,
1✔
3922
                    commit_op.clone(),
1✔
3923
                    &mut op_signer,
1✔
3924
                )
3925
                .expect("At first, building leader block commit should work");
1✔
3926

3927
            //re-gen signer othewise fails because it will be disposed during previous commit tx.
3928
            let mut signer = keychain.generate_op_signer();
1✔
3929
            //small change to the commit op payload
3930
            commit_op.burn_fee += 10;
1✔
3931

3932
            let rbf_tx = btc_controller
1✔
3933
                .build_leader_block_commit_tx(
1✔
3934
                    StacksEpochId::Epoch31,
1✔
3935
                    commit_op.clone(),
1✔
3936
                    &mut signer,
1✔
3937
                )
3938
                .expect("Commit tx should be rbf-ed");
1✔
3939

3940
            assert!(op_signer.is_disposed());
1✔
3941

3942
            assert_eq!(1, rbf_tx.version);
1✔
3943
            assert_eq!(0, rbf_tx.lock_time);
1✔
3944
            assert_eq!(1, rbf_tx.input.len());
1✔
3945
            assert_eq!(4, rbf_tx.output.len());
1✔
3946

3947
            // utxos list contains the only existing utxo
3948
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3949

3950
            let input_0 = utils::txin_at_index(&rbf_tx, &op_signer, &used_utxos, 0);
1✔
3951
            assert_eq!(input_0, rbf_tx.input[0]);
1✔
3952

3953
            let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes, 5_500);
1✔
3954
            let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_005);
1✔
3955
            let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_005);
1✔
3956
            let op_change = utils::txout_opdup_change_legacy(&mut signer, 4_999_862_985);
1✔
3957
            assert_eq!(op_return, rbf_tx.output[0]);
1✔
3958
            assert_eq!(op_commit_1, rbf_tx.output[1]);
1✔
3959
            assert_eq!(op_commit_2, rbf_tx.output[2]);
1✔
3960
            assert_eq!(op_change, rbf_tx.output[3]);
1✔
3961
        }
1✔
3962

3963
        #[test]
3964
        #[ignore]
3965
        fn test_make_operation_leader_block_commit_tx_ok() {
1✔
3966
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
3967
                return;
×
3968
            }
1✔
3969

3970
            let keychain = utils::create_keychain();
1✔
3971
            let miner_pubkey = keychain.get_pub_key();
1✔
3972
            let mut op_signer = keychain.generate_op_signer();
1✔
3973

3974
            let mut config = utils::create_miner_config();
1✔
3975
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3976
            config.burnchain.pox_reward_length = Some(11);
1✔
3977

3978
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3979
            btcd_controller
1✔
3980
                .start_bitcoind()
1✔
3981
                .expect("bitcoind should be started!");
1✔
3982

3983
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3984
            btc_controller
1✔
3985
                .connect_dbs()
1✔
3986
                .expect("Dbs initialization required!");
1✔
3987
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
3988

3989
            let mut commit_op = utils::create_templated_commit_op();
1✔
3990
            commit_op.sunset_burn = 5_500;
1✔
3991
            commit_op.burn_fee = 110_000;
1✔
3992

3993
            let tx = btc_controller
1✔
3994
                .make_operation_tx(
1✔
3995
                    StacksEpochId::Epoch31,
1✔
3996
                    BlockstackOperationType::LeaderBlockCommit(commit_op),
1✔
3997
                    &mut op_signer,
1✔
3998
                )
3999
                .expect("Make op should work");
1✔
4000

4001
            assert!(op_signer.is_disposed());
1✔
4002

4003
            assert_eq!(
1✔
4004
                "1a74106bd760117892fbd90fca11646b4de46f99fd2b065c9e0706cfdcea0336",
4005
                tx.txid().to_string()
1✔
4006
            );
4007
        }
1✔
4008

4009
        #[test]
4010
        #[ignore]
4011
        fn test_submit_leader_block_commit_tx_ok() {
1✔
4012
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4013
                return;
×
4014
            }
1✔
4015

4016
            let keychain = utils::create_keychain();
1✔
4017
            let miner_pubkey = keychain.get_pub_key();
1✔
4018
            let mut op_signer = keychain.generate_op_signer();
1✔
4019

4020
            let mut config = utils::create_miner_config();
1✔
4021
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4022
            config.burnchain.pox_reward_length = Some(11);
1✔
4023

4024
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4025
            btcd_controller
1✔
4026
                .start_bitcoind()
1✔
4027
                .expect("bitcoind should be started!");
1✔
4028

4029
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4030
            btc_controller
1✔
4031
                .connect_dbs()
1✔
4032
                .expect("Dbs initialization required!");
1✔
4033
            btc_controller.bootstrap_chain(101); // now, one utxo exists
1✔
4034

4035
            let mut commit_op = utils::create_templated_commit_op();
1✔
4036
            commit_op.sunset_burn = 5_500;
1✔
4037
            commit_op.burn_fee = 110_000;
1✔
4038

4039
            let tx_id = btc_controller
1✔
4040
                .submit_operation(
1✔
4041
                    StacksEpochId::Epoch31,
1✔
4042
                    BlockstackOperationType::LeaderBlockCommit(commit_op),
1✔
4043
                    &mut op_signer,
1✔
4044
                )
4045
                .expect("Submit op should work");
1✔
4046

4047
            assert!(op_signer.is_disposed());
1✔
4048

4049
            assert_eq!(
1✔
4050
                "1a74106bd760117892fbd90fca11646b4de46f99fd2b065c9e0706cfdcea0336",
4051
                tx_id.to_hex()
1✔
4052
            );
4053
        }
1✔
4054

4055
        #[test]
4056
        #[ignore]
4057
        fn test_submit_operation_block_commit_clears_ongoing_on_send_failure() {
1✔
4058
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4059
                return;
×
4060
            }
1✔
4061

4062
            let keychain = utils::create_keychain();
1✔
4063
            let miner_pubkey = keychain.get_pub_key();
1✔
4064
            let mut op_signer = keychain.generate_op_signer();
1✔
4065

4066
            let mut config = utils::create_miner_config();
1✔
4067
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4068
            config.burnchain.pox_reward_length = Some(11);
1✔
4069

4070
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4071
            btcd_controller
1✔
4072
                .start_bitcoind()
1✔
4073
                .expect("bitcoind should be started!");
1✔
4074

4075
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4076
            btc_controller
1✔
4077
                .connect_dbs()
1✔
4078
                .expect("Dbs initialization required!");
1✔
4079
            btc_controller.bootstrap_chain(101);
1✔
4080

4081
            let mut commit_op = utils::create_templated_commit_op();
1✔
4082
            commit_op.sunset_burn = 5_500;
1✔
4083
            commit_op.burn_fee = 110_000;
1✔
4084

4085
            // First submit succeeds and sets ongoing_block_commit
4086
            btc_controller
1✔
4087
                .submit_operation(
1✔
4088
                    StacksEpochId::Epoch31,
1✔
4089
                    BlockstackOperationType::LeaderBlockCommit(commit_op.clone()),
1✔
4090
                    &mut op_signer,
1✔
4091
                )
4092
                .expect("First submit should succeed");
1✔
4093

4094
            assert!(
1✔
4095
                btc_controller.get_ongoing_commit().is_some(),
1✔
4096
                "ongoing_block_commit should be set after successful submit"
4097
            );
4098

4099
            // Corrupt the cached UTXOs so the RBF transaction references
4100
            // non-existent inputs, causing send_transaction to be rejected
4101
            // by bitcoind immediately.
4102
            let mut ongoing = btc_controller.get_ongoing_commit().unwrap();
1✔
4103
            for utxo in ongoing.utxos.utxos.iter_mut() {
1✔
4104
                utxo.txid = Sha256dHash::default();
1✔
4105
            }
1✔
4106
            btc_controller.set_ongoing_commit(Some(ongoing));
1✔
4107

4108
            // Second submit with different payload triggers the RBF path.
4109
            // make_operation_tx builds the tx using the corrupted UTXOs,
4110
            // then send_transaction fails because the inputs don't exist.
4111
            let mut op_signer = keychain.generate_op_signer();
1✔
4112
            commit_op.burn_fee += 10;
1✔
4113

4114
            let err = btc_controller
1✔
4115
                .submit_operation(
1✔
4116
                    StacksEpochId::Epoch31,
1✔
4117
                    BlockstackOperationType::LeaderBlockCommit(commit_op),
1✔
4118
                    &mut op_signer,
1✔
4119
                )
4120
                .unwrap_err();
1✔
4121

4122
            assert!(
1✔
4123
                matches!(
×
4124
                    err,
1✔
4125
                    BurnchainControllerError::TransactionSubmissionFailed(_)
4126
                ),
4127
                "Error should be TransactionSubmissionFailed, but was {err}"
4128
            );
4129
            assert!(
1✔
4130
                btc_controller.get_ongoing_commit().is_none(),
1✔
4131
                "ongoing_block_commit should be cleared after send failure"
4132
            );
4133
        }
1✔
4134
    }
4135

4136
    /// Tests related to Leader Key Register operation
4137
    mod leader_key_op {
4138
        use super::*;
4139

4140
        #[test]
4141
        #[ignore]
4142
        fn test_build_leader_key_tx_ok() {
1✔
4143
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4144
                return;
×
4145
            }
1✔
4146

4147
            let keychain = utils::create_keychain();
1✔
4148
            let miner_pubkey = keychain.get_pub_key();
1✔
4149
            let mut op_signer = keychain.generate_op_signer();
1✔
4150

4151
            let mut config = utils::create_miner_config();
1✔
4152
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4153

4154
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4155
            btcd_controller
1✔
4156
                .start_bitcoind()
1✔
4157
                .expect("bitcoind should be started!");
1✔
4158

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

4162
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4163

4164
            let tx = btc_controller
1✔
4165
                .build_leader_key_register_tx(
1✔
4166
                    StacksEpochId::Epoch31,
1✔
4167
                    leader_key_op.clone(),
1✔
4168
                    &mut op_signer,
1✔
4169
                )
4170
                .expect("Build leader key should work");
1✔
4171

4172
            assert!(op_signer.is_disposed());
1✔
4173

4174
            assert_eq!(1, tx.version);
1✔
4175
            assert_eq!(0, tx.lock_time);
1✔
4176
            assert_eq!(1, tx.input.len());
1✔
4177
            assert_eq!(2, tx.output.len());
1✔
4178

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

4184
            let op_return = utils::txout_opreturn(&leader_key_op, &config.burnchain.magic_bytes, 0);
1✔
4185
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 4_999_980_000);
1✔
4186
            assert_eq!(op_return, tx.output[0]);
1✔
4187
            assert_eq!(op_change, tx.output[1]);
1✔
4188
        }
1✔
4189

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

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

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

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

4209
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4210
            btc_controller.bootstrap_chain(100); // no utxos exist
1✔
4211

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

4214
            let error = btc_controller
1✔
4215
                .build_leader_key_register_tx(
1✔
4216
                    StacksEpochId::Epoch31,
1✔
4217
                    leader_key_op.clone(),
1✔
4218
                    &mut op_signer,
1✔
4219
                )
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_leader_key_register_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
            config.burnchain.pox_reward_length = Some(11);
1✔
4240

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

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

4249
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4250

4251
            let tx = btc_controller
1✔
4252
                .make_operation_tx(
1✔
4253
                    StacksEpochId::Epoch31,
1✔
4254
                    BlockstackOperationType::LeaderKeyRegister(leader_key_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
                "4ecd7ba71bebd1aaed49dd63747ee424473f1c571bb9a576361607a669191024",
4263
                tx.txid().to_string()
1✔
4264
            );
4265
        }
1✔
4266

4267
        #[test]
4268
        #[ignore]
4269
        fn test_submit_operation_leader_key_register_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 leader_key_op = utils::create_templated_leader_key_op();
1✔
4290

4291
            let tx_id = btc_controller
1✔
4292
                .submit_operation(
1✔
4293
                    StacksEpochId::Epoch31,
1✔
4294
                    BlockstackOperationType::LeaderKeyRegister(leader_key_op),
1✔
4295
                    &mut op_signer,
1✔
4296
                )
4297
                .expect("Submit op should work");
1✔
4298

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

4301
            assert_eq!(
1✔
4302
                "4ecd7ba71bebd1aaed49dd63747ee424473f1c571bb9a576361607a669191024",
4303
                tx_id.to_hex()
1✔
4304
            );
4305
        }
1✔
4306
    }
4307

4308
    /// Tests related to Pre Stacks operation
4309
    mod pre_stx_op {
4310
        use super::*;
4311

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

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

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

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

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

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

4337
            let tx = btc_controller
1✔
4338
                .build_pre_stacks_tx(StacksEpochId::Epoch31, pre_stx_op.clone(), &mut op_signer)
1✔
4339
                .expect("Build leader key should work");
1✔
4340

4341
            assert!(op_signer.is_disposed());
1✔
4342

4343
            assert_eq!(1, tx.version);
1✔
4344
            assert_eq!(0, tx.lock_time);
1✔
4345
            assert_eq!(1, tx.input.len());
1✔
4346
            assert_eq!(3, tx.output.len());
1✔
4347

4348
            // utxos list contains the only existing utxo
4349
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
4350
            let input_0 = utils::txin_at_index(&tx, &op_signer, &used_utxos, 0);
1✔
4351
            assert_eq!(input_0, tx.input[0]);
1✔
4352

4353
            let op_return = utils::txout_opreturn(&pre_stx_op, &config.burnchain.magic_bytes, 0);
1✔
4354
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 24_500);
1✔
4355
            assert_eq!(op_return, tx.output[0]);
1✔
4356
            assert_eq!(op_change, tx.output[1]);
1✔
4357
        }
1✔
4358

4359
        #[test]
4360
        #[ignore]
4361
        fn test_build_pre_stx_tx_fails_due_to_no_utxos() {
1✔
4362
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4363
                return;
×
4364
            }
1✔
4365

4366
            let keychain = utils::create_keychain();
1✔
4367
            let miner_pubkey = keychain.get_pub_key();
1✔
4368
            let mut op_signer = keychain.generate_op_signer();
1✔
4369

4370
            let mut config = utils::create_miner_config();
1✔
4371
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4372

4373
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4374
            btcd_controller
1✔
4375
                .start_bitcoind()
1✔
4376
                .expect("bitcoind should be started!");
1✔
4377

4378
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4379
            btc_controller.bootstrap_chain(100); // no utxo exists
1✔
4380

4381
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4382
            pre_stx_op.output = keychain.get_address(false);
1✔
4383

4384
            let error = btc_controller
1✔
4385
                .build_pre_stacks_tx(StacksEpochId::Epoch31, pre_stx_op.clone(), &mut op_signer)
1✔
4386
                .expect_err("Leader key build should fail!");
1✔
4387

4388
            assert!(!op_signer.is_disposed());
1✔
4389
            assert_eq!(BurnchainControllerError::NoUTXOs, error);
1✔
4390
        }
1✔
4391

4392
        #[test]
4393
        #[ignore]
4394
        fn test_make_operation_pre_stx_tx_ok() {
1✔
4395
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4396
                return;
×
4397
            }
1✔
4398

4399
            let keychain = utils::create_keychain();
1✔
4400
            let miner_pubkey = keychain.get_pub_key();
1✔
4401
            let mut op_signer = keychain.generate_op_signer();
1✔
4402

4403
            let mut config = utils::create_miner_config();
1✔
4404
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4405

4406
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4407
            btcd_controller
1✔
4408
                .start_bitcoind()
1✔
4409
                .expect("bitcoind should be started!");
1✔
4410

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

4414
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4415
            pre_stx_op.output = keychain.get_address(false);
1✔
4416

4417
            let tx = btc_controller
1✔
4418
                .make_operation_tx(
1✔
4419
                    StacksEpochId::Epoch31,
1✔
4420
                    BlockstackOperationType::PreStx(pre_stx_op),
1✔
4421
                    &mut op_signer,
1✔
4422
                )
4423
                .expect("Make op should work");
1✔
4424

4425
            assert!(op_signer.is_disposed());
1✔
4426

4427
            assert_eq!(
1✔
4428
                "2d061c42c6f13a62fd9d80dc9fdcd19bdb4f9e4a07f786e42530c64c52ed9d1d",
4429
                tx.txid().to_string()
1✔
4430
            );
4431
        }
1✔
4432

4433
        #[test]
4434
        #[ignore]
4435
        fn test_submit_operation_pre_stx_tx_ok() {
1✔
4436
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4437
                return;
×
4438
            }
1✔
4439

4440
            let keychain = utils::create_keychain();
1✔
4441
            let miner_pubkey = keychain.get_pub_key();
1✔
4442
            let mut op_signer = keychain.generate_op_signer();
1✔
4443

4444
            let mut config = utils::create_miner_config();
1✔
4445
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4446

4447
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4448
            btcd_controller
1✔
4449
                .start_bitcoind()
1✔
4450
                .expect("bitcoind should be started!");
1✔
4451

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

4455
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4456
            pre_stx_op.output = keychain.get_address(false);
1✔
4457

4458
            let tx_id = btc_controller
1✔
4459
                .submit_operation(
1✔
4460
                    StacksEpochId::Epoch31,
1✔
4461
                    BlockstackOperationType::PreStx(pre_stx_op),
1✔
4462
                    &mut op_signer,
1✔
4463
                )
4464
                .expect("submit op should work");
1✔
4465

4466
            assert!(op_signer.is_disposed());
1✔
4467

4468
            assert_eq!(
1✔
4469
                "2d061c42c6f13a62fd9d80dc9fdcd19bdb4f9e4a07f786e42530c64c52ed9d1d",
4470
                tx_id.to_hex()
1✔
4471
            );
4472
        }
1✔
4473
    }
4474
}
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