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

stacks-network / stacks-core / 23505074926

24 Mar 2026 06:07PM UTC coverage: 85.693% (-0.02%) from 85.712%
23505074926

Pull #6976

github

53bcbc
web-flow
Merge 93427aa0f into 1e36cefa9
Pull Request #6976: fix: clear the ongoing block commit if txn fails

58 of 60 new or added lines in 1 file covered. (96.67%)

119 existing lines in 24 files now uncovered.

186541 of 217685 relevant lines covered (85.69%)

17249623.5 hits per line

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

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

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

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

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

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

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

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

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

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

122
// TODO: add tests from mutation testing results #4862
123
#[cfg_attr(test, mutants::skip)]
124
pub fn burnchain_params_from_config(config: &BurnchainConfig) -> BurnchainParameters {
175,976✔
125
    let (network, _) = config.get_bitcoin_network();
175,976✔
126
    let mut params = BurnchainParameters::from_params(&config.chain, &network)
175,976✔
127
        .expect("Bitcoin network unsupported");
175,976✔
128
    if let Some(first_burn_block_height) = config.first_burn_block_height {
175,976✔
129
        params.first_block_height = first_burn_block_height;
×
130
    }
175,976✔
131
    params
175,976✔
132
}
175,976✔
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,284✔
138
    config: &Config,
1,284✔
139
    should_keep_running: Option<Arc<AtomicBool>>,
1,284✔
140
) -> BitcoinIndexer {
1,284✔
141
    let burnchain_params = burnchain_params_from_config(&config.burnchain);
1,284✔
142
    let indexer_config = {
1,284✔
143
        let burnchain_config = config.burnchain.clone();
1,284✔
144
        BitcoinIndexerConfig {
1,284✔
145
            peer_host: burnchain_config.peer_host,
1,284✔
146
            peer_port: burnchain_config.peer_port,
1,284✔
147
            rpc_port: burnchain_config.rpc_port,
1,284✔
148
            rpc_ssl: burnchain_config.rpc_ssl,
1,284✔
149
            username: burnchain_config.username,
1,284✔
150
            password: burnchain_config.password,
1,284✔
151
            timeout: burnchain_config.timeout,
1,284✔
152
            socket_timeout: burnchain_config.socket_timeout,
1,284✔
153
            spv_headers_path: config.get_spv_headers_file_path(),
1,284✔
154
            first_block: burnchain_params.first_block_height,
1,284✔
155
            magic_bytes: burnchain_config.magic_bytes,
1,284✔
156
            epochs: burnchain_config.epochs,
1,284✔
157
        }
1,284✔
158
    };
159

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

426
        let rpc_client = Self::create_rpc_client_unchecked(&config);
173,475✔
427

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

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

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

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

459
    /// Get the default Burnchain instance from our config
460
    fn default_burnchain(&self) -> Burnchain {
824,768✔
461
        match &self.burnchain_config {
824,768✔
462
            Some(burnchain) => burnchain.clone(),
×
463
            None => self.config.get_burnchain(),
824,768✔
464
        }
465
    }
824,768✔
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 {
873,890✔
475
        match self.burnchain_config {
873,890✔
476
            Some(ref burnchain) => burnchain.clone(),
49,122✔
477
            None => self.default_burnchain(),
824,768✔
478
        }
479
    }
873,890✔
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> {
174,270✔
487
        config.node.miner.then(|| {
174,270✔
488
            BitcoinRpcClient::from_stx_config(&config)
174,249✔
489
                .expect("unable to instantiate the RPC client for miner node!")
174,249✔
490
        })
174,249✔
491
    }
174,270✔
492

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

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

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

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

566
    fn receive_blocks(
385,744✔
567
        &mut self,
385,744✔
568
        block_for_sortitions: bool,
385,744✔
569
        target_block_height_opt: Option<u64>,
385,744✔
570
    ) -> Result<(BurnchainTip, u64), BurnchainControllerError> {
385,744✔
571
        let coordinator_comms = match self.use_coordinator.as_ref() {
385,744✔
572
            Some(x) => x.clone(),
385,744✔
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();
385,744✔
582
        let (block_snapshot, burnchain_height, state_transition) = loop {
385,700✔
583
            if !self.should_keep_running() {
385,801✔
584
                return Err(BurnchainControllerError::CoordinatorClosed);
41✔
585
            }
385,760✔
586

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

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

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

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

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

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

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

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

676
        Ok((burnchain_tip, burnchain_height))
385,700✔
677
    }
385,744✔
678

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

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

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

707
        sleep_ms(1000);
32✔
708

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

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

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

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

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

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

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

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

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

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

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

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

823
        Some(utxos)
8,750✔
824
    }
8,764✔
825

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

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

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

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

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

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

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

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

878
        increment_btc_ops_sent_counter();
266✔
879

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

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

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

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

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

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

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

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

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

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

1009
        increment_btc_ops_sent_counter();
4✔
1010

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

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

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

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

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

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

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

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

1094
        increment_btc_ops_sent_counter();
2✔
1095

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

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

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

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

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

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

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

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

1171
        increment_btc_ops_sent_counter();
2✔
1172

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

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

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

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

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

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

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

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

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

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

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

1250
        increment_btc_ops_sent_counter();
15✔
1251

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

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

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

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

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

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

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

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

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

1340
        increment_btc_ops_sent_counter();
4✔
1341

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1484
        increment_btc_ops_sent_counter();
8,270✔
1485

1486
        Ok(tx)
8,270✔
1487
    }
9,235✔
1488

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

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

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

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

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

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

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

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

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

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

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

1611
        res
313✔
1612
    }
24,069✔
1613

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

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

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

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

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

1684
        Ok((transaction, utxos))
8,560✔
1685
    }
9,529✔
1686

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1915
            if debug_ctr % 10 == 0 {
392,807✔
1916
                debug!(
385,969✔
1917
                    "Waiting until canonical sortition height reaches {height_to_wait} (currently {})",
1918
                    canonical_sortition_tip.block_height
1919
                );
1920
            }
6,838✔
1921
            debug_ctr += 1;
392,807✔
1922

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

1930
                return Ok(BurnchainTip {
385,940✔
1931
                    block_snapshot: canonical_sortition_tip,
385,940✔
1932
                    received_at: Instant::now(),
385,940✔
1933
                    state_transition,
385,940✔
1934
                });
385,940✔
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
    }
385,941✔
1949

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2389
    // returns true if the operation was submitted successfully, false otherwise
2390
    fn submit_operation(
24,346✔
2391
        &mut self,
24,346✔
2392
        epoch_id: StacksEpochId,
24,346✔
2393
        operation: BlockstackOperationType,
24,346✔
2394
        op_signer: &mut BurnchainOpSigner,
24,346✔
2395
    ) -> Result<Txid, BurnchainControllerError> {
24,346✔
2396
        let transaction = self.make_operation_tx(epoch_id, operation, op_signer)?;
24,346✔
2397
        self.send_transaction(&transaction).map_err(|e| {
8,548✔
2398
            // If the transaction was never successfully submitted,
2399
            // clear the ongoing block commit so it can be resubmitted.
2400
            self.ongoing_block_commit = None;
2✔
2401
            e
2✔
2402
        })
2✔
2403
    }
24,346✔
2404

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

2412
        // NOTE: miner address is whatever the miner's segwit setting says it is here
2413
        let mut local_mining_pubkey = Secp256k1PublicKey::from_hex(local_mining_pubkey).unwrap();
120✔
2414

2415
        if self.config.miner.segwit {
120✔
2416
            local_mining_pubkey.set_compressed(true);
×
2417
        }
120✔
2418

2419
        self.bootstrap_chain_to_pks(num_blocks, &[local_mining_pubkey])
120✔
2420
    }
121✔
2421
}
2422

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

2429
impl UTXOSet {
2430
    pub fn is_empty(&self) -> bool {
8,787✔
2431
        self.utxos.len() == 0
8,787✔
2432
    }
8,787✔
2433

2434
    pub fn total_available(&self) -> u64 {
8,752✔
2435
        self.utxos.iter().map(|o| o.amount).sum()
8,752✔
2436
    }
8,752✔
2437

2438
    pub fn num_utxos(&self) -> usize {
6✔
2439
        self.utxos.len()
6✔
2440
    }
6✔
2441
}
2442

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

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

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

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

2473
    mod utils {
2474
        use std::net::TcpListener;
2475

2476
        use stacks::burnchains::MagicBytes;
2477
        use stacks::chainstate::burn::ConsensusHash;
2478
        use stacks::util::vrf::{VRFPrivateKey, VRFPublicKey};
2479

2480
        use super::*;
2481
        use crate::burnchains::bitcoin::core_controller::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
2482
        use crate::util::get_epoch_time_nanos;
2483

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

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

2502
            config.burnchain.rpc_port = port;
37✔
2503

2504
            let now = get_epoch_time_nanos();
37✔
2505
            let dir = format!("/tmp/regtest-ctrl-{port}-{now}");
37✔
2506
            config.node.working_dir = dir;
37✔
2507

2508
            config
37✔
2509
        }
37✔
2510

2511
        pub fn create_keychain() -> Keychain {
15✔
2512
            create_keychain_with_seed(1)
15✔
2513
        }
15✔
2514

2515
        pub fn create_keychain_with_seed(value: u8) -> Keychain {
32✔
2516
            let seed = vec![value; 4];
32✔
2517
            let keychain = Keychain::default(seed);
32✔
2518
            keychain
32✔
2519
        }
32✔
2520

2521
        pub fn create_miner1_pubkey() -> Secp256k1PublicKey {
15✔
2522
            create_keychain_with_seed(1).get_pub_key()
15✔
2523
        }
15✔
2524

2525
        pub fn create_miner2_pubkey() -> Secp256k1PublicKey {
2✔
2526
            create_keychain_with_seed(2).get_pub_key()
2✔
2527
        }
2✔
2528

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

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

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

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

7✔
2569
                burn_fee: 110_000, //relevant for fee calculation when sending the tx
7✔
2570
                input: (Txid([0x00; 32]), 0),
7✔
2571
                burn_parent_modulus: 2, // 0x5a & 0b111
7✔
2572

7✔
2573
                apparent_sender: BurnchainSigner("mgbpit8FvkVJ9kuXY8QSM5P7eibnhcEMBk".to_string()),
7✔
2574
                commit_outs: vec![
7✔
2575
                    PoxAddress::Standard(StacksAddress::burn_address(false), None),
7✔
2576
                    PoxAddress::Standard(StacksAddress::burn_address(false), None),
7✔
2577
                ],
7✔
2578

7✔
2579
                treatment: vec![],
7✔
2580
                sunset_burn: 5_500, //relevant for fee calculation when sending the tx
7✔
2581

7✔
2582
                txid: Txid([0x00; 32]),
7✔
2583
                vtxindex: 0,
7✔
2584
                block_height: 2212,
7✔
2585
                burn_header_hash: BurnchainHeaderHash([0x01; 32]),
7✔
2586
            }
7✔
2587
        }
7✔
2588

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

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

2612
        pub fn txout_opdup_commit_to(addr: &PoxAddress, amount: u64) -> TxOut {
6✔
2613
            addr.to_bitcoin_tx_out(amount)
6✔
2614
        }
6✔
2615

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

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

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

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

2652
            for (i, utxo) in utxos.iter().enumerate() {
5✔
2653
                let script_pub_key = utxo.script_pub_key.clone();
5✔
2654
                let sig_hash_all = 0x01;
5✔
2655

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

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

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

2698
            tx.input[index].clone()
5✔
2699
        }
5✔
2700

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

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

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

2738
    #[test]
2739
    fn test_get_satoshis_per_byte() {
1✔
2740
        let dir = temp_dir();
1✔
2741
        let file_path = dir.as_path().join("config.toml");
1✔
2742

2743
        let mut config = Config::default();
1✔
2744

2745
        let satoshis_per_byte = get_satoshis_per_byte(&config);
1✔
2746
        assert_eq!(satoshis_per_byte, DEFAULT_SATS_PER_VB);
1✔
2747

2748
        let mut file = File::create(&file_path).unwrap();
1✔
2749
        writeln!(file, "[burnchain]").unwrap();
1✔
2750
        writeln!(file, "satoshis_per_byte = 51").unwrap();
1✔
2751
        config.config_path = Some(file_path.to_str().unwrap().to_string());
1✔
2752

2753
        assert_eq!(get_satoshis_per_byte(&config), 51);
1✔
2754
    }
1✔
2755

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

2790
        // test serialize_tx()
2791
        let config = utils::create_miner_config();
1✔
2792

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

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

2834
        // test send_block_commit_operation_at_burn_height()
2835
        let utxo_set = UTXOSet {
1✔
2836
            bhh: BurnchainHeaderHash([0x01; 32]),
1✔
2837
            utxos: spend_utxos,
1✔
2838
        };
1✔
2839

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

1✔
2855
            burn_fee: 0,
1✔
2856
            input: (Txid([0x00; 32]), 0),
1✔
2857
            burn_parent_modulus: 2, // 0x5a & 0b111
1✔
2858

1✔
2859
            apparent_sender: BurnchainSigner("mgbpit8FvkVJ9kuXY8QSM5P7eibnhcEMBk".to_string()),
1✔
2860
            commit_outs: vec![
1✔
2861
                PoxAddress::Standard(StacksAddress::burn_address(false), None),
1✔
2862
                PoxAddress::Standard(StacksAddress::burn_address(false), None),
1✔
2863
            ],
1✔
2864

1✔
2865
            treatment: vec![],
1✔
2866
            sunset_burn: 0,
1✔
2867

1✔
2868
            txid: Txid([0x00; 32]),
1✔
2869
            vtxindex: 0,
1✔
2870
            block_height: 2212,
1✔
2871
            burn_header_hash: BurnchainHeaderHash([0x01; 32]),
1✔
2872
        };
1✔
2873

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

2876
        let leader_fees = LeaderBlockCommitFees {
1✔
2877
            sunset_fee: 0,
1✔
2878
            fee_rate: 50,
1✔
2879
            sortition_fee: 20000,
1✔
2880
            outputs_len: 2,
1✔
2881
            default_tx_size: 380,
1✔
2882
            spent_in_attempts: 0,
1✔
2883
            is_rbf_enabled: false,
1✔
2884
            final_size: 498,
1✔
2885
        };
1✔
2886

2887
        assert_eq!(leader_fees.amount_per_output(), 10000);
1✔
2888
        assert_eq!(leader_fees.total_spent(), 44900);
1✔
2889

2890
        let block_commit = btc_controller
1✔
2891
            .send_block_commit_operation_at_burnchain_height(
1✔
2892
                StacksEpochId::Epoch30,
1✔
2893
                commit_op,
1✔
2894
                &mut signer,
1✔
2895
                Some(utxo_set),
1✔
2896
                None,
1✔
2897
                leader_fees,
1✔
2898
                &[],
1✔
2899
                2212,
2900
            )
2901
            .unwrap();
1✔
2902

2903
        debug!("send_block_commit_operation:\n{block_commit:#?}");
1✔
2904
        assert_eq!(block_commit.output[3].value, 323507);
1✔
2905
        assert_eq!(serialize_hex(&block_commit).unwrap(), "0100000002eeda098987728e4a2e21b34b74000dcb0bd0e4d20e55735492ec3cba3afbead3030000006a4730440220558286e20e10ce31537f0625dae5cc62fac7961b9d2cf272c990de96323d7e2502202255adbea3d2e0509b80c5d8a3a4fe6397a87bcf18da1852740d5267d89a0cb20121035379aa40c02890d253cfa577964116eb5295570ae9f7287cbae5f2585f5b2c7cfdffffff243b0b329a5889ab8801b315eea19810848d4c2133e0245671cc984a2d2f1301000000006a47304402206d9f8de107f9e1eb15aafac66c2bb34331a7523260b30e18779257e367048d34022013c7dabb32a5c281aa00d405e2ccbd00f34f03a65b2336553a4acd6c52c251ef0121035379aa40c02890d253cfa577964116eb5295570ae9f7287cbae5f2585f5b2c7cfdffffff040000000000000000536a4c5054335be88c3d30cb59a142f83de3b27f897a43bbb0f13316911bb98a3229973dae32afd5b9f21bc1f40f24e2c101ecd13c55b8619e5e03dad81de2c62a1cc1d8c1b375000008a300010000059800015a10270000000000001976a914000000000000000000000000000000000000000088ac10270000000000001976a914000000000000000000000000000000000000000088acb3ef0400000000001976a9141dc27eba0247f8cc9575e7d45e50a0bc7e72427d88ac00000000");
1✔
2906
    }
1✔
2907

2908
    #[test]
2909
    fn test_to_epoch_aware_pubkey() {
1✔
2910
        let mut config = utils::create_miner_config();
1✔
2911
        let pubkey = utils::create_miner1_pubkey();
1✔
2912

2913
        config.miner.segwit = false;
1✔
2914
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
2915

2916
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch20, &pubkey);
1✔
2917
        assert_eq!(
1✔
2918
            false,
2919
            reviewed.compressed(),
1✔
2920
            "Segwit disabled with Epoch < 2.1: not compressed"
2921
        );
2922
        let reviewed = btc_controller.to_epoch_aware_pubkey(StacksEpochId::Epoch21, &pubkey);
1✔
2923
        assert_eq!(
1✔
2924
            false,
2925
            reviewed.compressed(),
1✔
2926
            "Segwit disabled with Epoch >= 2.1: not compressed"
2927
        );
2928

2929
        config.miner.segwit = true;
1✔
2930
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
2931

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

2946
    #[test]
2947
    fn test_get_miner_address() {
1✔
2948
        let mut config = utils::create_miner_config();
1✔
2949
        let pub_key = utils::create_miner1_pubkey();
1✔
2950

2951
        config.miner.segwit = false;
1✔
2952
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
2953

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

2961
        let expected = utils::to_address_legacy(&pub_key);
1✔
2962
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch21, &pub_key);
1✔
2963
        assert_eq!(
1✔
2964
            expected, address,
2965
            "Segwit disabled with Epoch >= 2.1: legacy addr"
2966
        );
2967

2968
        config.miner.segwit = true;
1✔
2969
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
2970

2971
        let expected = utils::to_address_legacy(&pub_key);
1✔
2972
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch20, &pub_key);
1✔
2973
        assert_eq!(
1✔
2974
            expected, address,
2975
            "Segwit enabled with Epoch < 2.1: legacy addr"
2976
        );
2977

2978
        let expected = utils::to_address_segwit_p2wpkh(&pub_key);
1✔
2979
        let address = btc_controller.get_miner_address(StacksEpochId::Epoch21, &pub_key);
1✔
2980
        assert_eq!(
1✔
2981
            expected, address,
2982
            "Segwit enabled with Epoch >= 2.1: segwit addr"
2983
        );
2984
    }
1✔
2985

2986
    #[test]
2987
    fn test_instantiate_with_burnchain_on_follower_node_ok() {
1✔
2988
        let config = create_follower_config();
1✔
2989

2990
        let btc_controller = BitcoinRegtestController::with_burnchain(config, None, None, None);
1✔
2991

2992
        let result = panic::catch_unwind(AssertUnwindSafe(|| {
1✔
2993
            _ = btc_controller.get_rpc_client();
1✔
2994
        }));
1✔
2995
        assert!(
1✔
2996
            result.is_err(),
1✔
2997
            "Invoking any Bitcoin RPC related method should panic."
2998
        );
2999
    }
1✔
3000

3001
    #[test]
3002
    fn test_instantiate_with_burnchain_on_miner_node_ok() {
1✔
3003
        let config = create_miner_config();
1✔
3004

3005
        let btc_controller = BitcoinRegtestController::with_burnchain(config, None, None, None);
1✔
3006

3007
        let _ = btc_controller.get_rpc_client();
1✔
3008
        assert!(true, "Invoking any Bitcoin RPC related method should work.");
1✔
3009
    }
1✔
3010

3011
    #[test]
3012
    fn test_instantiate_with_burnchain_on_miner_node_failure() {
1✔
3013
        let mut config = create_miner_config();
1✔
3014
        config.burnchain.username = None;
1✔
3015
        config.burnchain.password = None;
1✔
3016

3017
        let result = panic::catch_unwind(|| {
1✔
3018
            _ = BitcoinRegtestController::with_burnchain(config, None, None, None);
1✔
3019
        });
1✔
3020
        assert!(
1✔
3021
            result.is_err(),
1✔
3022
            "Bitcoin RPC credentials are mandatory for miner node."
3023
        );
3024
    }
1✔
3025

3026
    #[test]
3027
    fn test_instantiate_new_dummy_on_follower_node_ok() {
1✔
3028
        let config = create_follower_config();
1✔
3029

3030
        let btc_controller = BitcoinRegtestController::new_dummy(config);
1✔
3031

3032
        let result = panic::catch_unwind(AssertUnwindSafe(|| {
1✔
3033
            _ = btc_controller.get_rpc_client();
1✔
3034
        }));
1✔
3035
        assert!(
1✔
3036
            result.is_err(),
1✔
3037
            "Invoking any Bitcoin RPC related method should panic."
3038
        );
3039
    }
1✔
3040

3041
    #[test]
3042
    fn test_instantiate_new_dummy_on_miner_node_ok() {
1✔
3043
        let config = create_miner_config();
1✔
3044

3045
        let btc_controller = BitcoinRegtestController::new_dummy(config);
1✔
3046

3047
        let _ = btc_controller.get_rpc_client();
1✔
3048
        assert!(true, "Invoking any Bitcoin RPC related method should work.");
1✔
3049
    }
1✔
3050

3051
    #[test]
3052
    fn test_instantiate_new_dummy_on_miner_node_failure() {
1✔
3053
        let mut config = create_miner_config();
1✔
3054
        config.burnchain.username = None;
1✔
3055
        config.burnchain.password = None;
1✔
3056

3057
        let result = panic::catch_unwind(|| {
1✔
3058
            _ = BitcoinRegtestController::new_dummy(config);
1✔
3059
        });
1✔
3060
        assert!(
1✔
3061
            result.is_err(),
1✔
3062
            "Bitcoin RPC credentials are mandatory for miner node."
3063
        );
3064
    }
1✔
3065

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

3073
        let config = utils::create_miner_config();
1✔
3074

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

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

3082
        let wallets = btc_controller.list_wallets().unwrap();
1✔
3083
        assert_eq!(0, wallets.len());
1✔
3084

3085
        btc_controller
1✔
3086
            .create_wallet_if_dne()
1✔
3087
            .expect("Wallet should now exists!");
1✔
3088

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

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

3101
        let mut config = utils::create_miner_config();
1✔
3102
        config.burnchain.wallet_name = String::from("mywallet");
1✔
3103

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

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

3111
        btc_controller
1✔
3112
            .create_wallet_if_dne()
1✔
3113
            .expect("Wallet should now exists!");
1✔
3114

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

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

3127
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3128

3129
        let mut config = utils::create_miner_config();
1✔
3130
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3131

3132
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3133
        btcd_controller
1✔
3134
            .start_bitcoind()
1✔
3135
            .expect("Failed starting bitcoind");
1✔
3136

3137
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3138
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3139

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

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

3155
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3156

3157
        let mut config = utils::create_miner_config();
1✔
3158
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3159

3160
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3161
        btcd_controller
1✔
3162
            .start_bitcoind()
1✔
3163
            .expect("Failed starting bitcoind");
1✔
3164

3165
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3166
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3167

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

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

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

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

3192
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3193

3194
        let mut config = utils::create_miner_config();
1✔
3195
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3196
        config.burnchain.max_unspent_utxos = Some(10);
1✔
3197

3198
        let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
3199
        btcd_controller
1✔
3200
            .start_bitcoind()
1✔
3201
            .expect("Failed starting bitcoind");
1✔
3202

3203
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3204
        btc_controller.bootstrap_chain(150); //produces 50 spendable utxos
1✔
3205

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

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

3220
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3221

3222
        let mut config = utils::create_miner_config();
1✔
3223
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3224

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

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

3232
        btc_controller.bootstrap_chain(100);
1✔
3233
        let utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3234
        assert_eq!(0, utxos.len());
1✔
3235

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

3242
        btc_controller.build_next_block(1);
1✔
3243
        let mut utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3244
        utxos.sort_by(|a, b| b.confirmations.cmp(&a.confirmations));
1✔
3245

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

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

3260
        let miner1_pubkey = utils::create_miner1_pubkey();
1✔
3261
        let miner2_pubkey = utils::create_miner2_pubkey();
1✔
3262

3263
        let mut config = utils::create_miner_config();
1✔
3264
        config.burnchain.local_mining_public_key = Some(miner1_pubkey.to_hex());
1✔
3265

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

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

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

3279
        let utxos = miner1_btc_controller.get_all_utxos(&miner1_pubkey);
1✔
3280
        assert_eq!(1, utxos.len(), "miner1 see its own utxos");
1✔
3281

3282
        let utxos = miner1_btc_controller.get_all_utxos(&miner2_pubkey);
1✔
3283
        assert_eq!(2, utxos.len(), "miner1 see miner2 utxos");
1✔
3284

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

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

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

3299
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3300

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

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

3309
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3310
        btc_controller.bootstrap_chain(101);
1✔
3311

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

3316
        assert_eq!(btc_controller.get_block_hash(101), uxto_set.bhh);
1✔
3317
        assert_eq!(1, uxto_set.num_utxos());
1✔
3318
        assert_eq!(5_000_000_000, uxto_set.total_available());
1✔
3319
        let utxos = uxto_set.utxos;
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

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

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

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

3347
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3348

3349
        let mut config = utils::create_miner_config();
1✔
3350
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3351

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

3357
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3358
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3359

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

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

3378
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3379

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

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

3388
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3389
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3390

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

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

3406
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3407

3408
        let mut config = utils::create_miner_config();
1✔
3409
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3410

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

3416
        let btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
3417
        btc_controller.bootstrap_chain(101); // one utxo exists
1✔
3418

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

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

3442
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3443

3444
        let mut config = utils::create_miner_config();
1✔
3445
        config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3446

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

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

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

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

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

3472
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3473

3474
        let config = utils::create_miner_config();
1✔
3475

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

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

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

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

3501
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3502

3503
        let config = utils::create_miner_config();
1✔
3504

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

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

3515
        btc_controller
1✔
3516
            .import_public_key(&miner_pubkey)
1✔
3517
            .expect("Import should be ok: first time!");
1✔
3518

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

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

3535
        let miner_pubkey = utils::create_miner1_pubkey();
1✔
3536

3537
        let mut config = utils::create_miner_config();
1✔
3538
        config.miner.segwit = true;
1✔
3539

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

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

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

3558
    /// Tests related to Leader Block Commit operation
3559
    mod leader_commit_op {
3560
        use super::*;
3561

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

3569
            let keychain = utils::create_keychain();
1✔
3570
            let miner_pubkey = keychain.get_pub_key();
1✔
3571
            let mut op_signer = keychain.generate_op_signer();
1✔
3572

3573
            let mut config = utils::create_miner_config();
1✔
3574
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3575
            config.burnchain.pox_reward_length = Some(11);
1✔
3576

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

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

3588
            let mut commit_op = utils::create_templated_commit_op();
1✔
3589
            commit_op.sunset_burn = 5_500;
1✔
3590
            commit_op.burn_fee = 110_000;
1✔
3591

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

3600
            assert!(op_signer.is_disposed());
1✔
3601

3602
            assert_eq!(1, tx.version);
1✔
3603
            assert_eq!(0, tx.lock_time);
1✔
3604
            assert_eq!(1, tx.input.len());
1✔
3605
            assert_eq!(4, tx.output.len());
1✔
3606

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

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

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

3629
            let keychain = utils::create_keychain();
1✔
3630
            let miner_pubkey = keychain.get_pub_key();
1✔
3631
            let mut op_signer = keychain.generate_op_signer();
1✔
3632

3633
            let mut config = utils::create_miner_config();
1✔
3634
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3635
            config.burnchain.pox_reward_length = Some(11);
1✔
3636

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

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

3648
            let commit_op = utils::create_templated_commit_op();
1✔
3649

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

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

3665
            assert!(resubmit.is_err());
1✔
3666
            assert_eq!(
1✔
3667
                BurnchainControllerError::IdenticalOperation,
3668
                resubmit.unwrap_err()
1✔
3669
            );
3670
        }
1✔
3671

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

3679
            let keychain = utils::create_keychain();
1✔
3680
            let miner_pubkey = keychain.get_pub_key();
1✔
3681
            let mut op_signer = keychain.generate_op_signer();
1✔
3682

3683
            let mut config = utils::create_miner_config();
1✔
3684
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3685
            config.burnchain.pox_reward_length = Some(11);
1✔
3686

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

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

3698
            let commit_op = utils::create_templated_commit_op();
1✔
3699

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

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

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

3717
            assert!(resubmit.is_err());
1✔
3718
            assert_eq!(
1✔
3719
                BurnchainControllerError::IdenticalOperation,
3720
                resubmit.unwrap_err()
1✔
3721
            );
3722
        }
1✔
3723

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

3731
            let keychain = utils::create_keychain();
1✔
3732
            let miner_pubkey = keychain.get_pub_key();
1✔
3733
            let mut op_signer = keychain.generate_op_signer();
1✔
3734

3735
            let mut config = utils::create_miner_config();
1✔
3736
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3737
            config.burnchain.pox_reward_length = Some(11);
1✔
3738

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

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

3750
            let mut commit_op = utils::create_templated_commit_op();
1✔
3751
            commit_op.sunset_burn = 5_500;
1✔
3752
            commit_op.burn_fee = 110_000;
1✔
3753

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

3762
            let first_txid = first_tx_ok.txid();
1✔
3763

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

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

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

3780
            assert!(op_signer.is_disposed());
1✔
3781

3782
            assert_eq!(1, new_tx.version);
1✔
3783
            assert_eq!(0, new_tx.lock_time);
1✔
3784
            assert_eq!(1, new_tx.input.len());
1✔
3785
            assert_eq!(4, new_tx.output.len());
1✔
3786

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

3795
            let input_0 = utils::txin_at_index(&new_tx, &op_signer, &used_utxos, 0);
1✔
3796
            assert_eq!(input_0, new_tx.input[0]);
1✔
3797

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

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

3815
            let keychain = utils::create_keychain();
1✔
3816
            let miner_pubkey = keychain.get_pub_key();
1✔
3817
            let mut op_signer = keychain.generate_op_signer();
1✔
3818

3819
            let mut config = utils::create_miner_config();
1✔
3820
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3821
            config.burnchain.pox_reward_length = Some(11);
1✔
3822

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

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

3834
            let mut commit_op = utils::create_templated_commit_op();
1✔
3835
            commit_op.sunset_burn = 5_500;
1✔
3836
            commit_op.burn_fee = 110_000;
1✔
3837

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

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

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

3859
            assert!(op_signer.is_disposed());
1✔
3860

3861
            assert_eq!(1, rbf_tx.version);
1✔
3862
            assert_eq!(0, rbf_tx.lock_time);
1✔
3863
            assert_eq!(1, rbf_tx.input.len());
1✔
3864
            assert_eq!(4, rbf_tx.output.len());
1✔
3865

3866
            // utxos list contains the only existing utxo
3867
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
3868

3869
            let input_0 = utils::txin_at_index(&rbf_tx, &op_signer, &used_utxos, 0);
1✔
3870
            assert_eq!(input_0, rbf_tx.input[0]);
1✔
3871

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

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

3889
            let keychain = utils::create_keychain();
1✔
3890
            let miner_pubkey = keychain.get_pub_key();
1✔
3891
            let mut op_signer = keychain.generate_op_signer();
1✔
3892

3893
            let mut config = utils::create_miner_config();
1✔
3894
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3895
            config.burnchain.pox_reward_length = Some(11);
1✔
3896

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

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

3908
            let mut commit_op = utils::create_templated_commit_op();
1✔
3909
            commit_op.sunset_burn = 5_500;
1✔
3910
            commit_op.burn_fee = 110_000;
1✔
3911

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

3920
            assert!(op_signer.is_disposed());
1✔
3921

3922
            assert_eq!(
1✔
3923
                "1a74106bd760117892fbd90fca11646b4de46f99fd2b065c9e0706cfdcea0336",
3924
                tx.txid().to_string()
1✔
3925
            );
3926
        }
1✔
3927

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

3935
            let keychain = utils::create_keychain();
1✔
3936
            let miner_pubkey = keychain.get_pub_key();
1✔
3937
            let mut op_signer = keychain.generate_op_signer();
1✔
3938

3939
            let mut config = utils::create_miner_config();
1✔
3940
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
3941
            config.burnchain.pox_reward_length = Some(11);
1✔
3942

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

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

3954
            let mut commit_op = utils::create_templated_commit_op();
1✔
3955
            commit_op.sunset_burn = 5_500;
1✔
3956
            commit_op.burn_fee = 110_000;
1✔
3957

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

3966
            assert!(op_signer.is_disposed());
1✔
3967

3968
            assert_eq!(
1✔
3969
                "1a74106bd760117892fbd90fca11646b4de46f99fd2b065c9e0706cfdcea0336",
3970
                tx_id.to_hex()
1✔
3971
            );
3972
        }
1✔
3973
    }
3974

3975
    /// Tests related to Leader Key Register operation
3976
    mod leader_key_op {
3977
        use super::*;
3978

3979
        #[test]
3980
        #[ignore]
3981
        fn test_build_leader_key_tx_ok() {
1✔
3982
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
NEW
3983
                return;
×
3984
            }
1✔
3985

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

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

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

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

4001
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4002

4003
            let tx = btc_controller
1✔
4004
                .build_leader_key_register_tx(
1✔
4005
                    StacksEpochId::Epoch31,
1✔
4006
                    leader_key_op.clone(),
1✔
4007
                    &mut op_signer,
1✔
4008
                )
4009
                .expect("Build leader key should work");
1✔
4010

4011
            assert!(op_signer.is_disposed());
1✔
4012

4013
            assert_eq!(1, tx.version);
1✔
4014
            assert_eq!(0, tx.lock_time);
1✔
4015
            assert_eq!(1, tx.input.len());
1✔
4016
            assert_eq!(2, tx.output.len());
1✔
4017

4018
            // utxos list contains the only existing utxo
4019
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
4020
            let input_0 = utils::txin_at_index(&tx, &op_signer, &used_utxos, 0);
1✔
4021
            assert_eq!(input_0, tx.input[0]);
1✔
4022

4023
            let op_return = utils::txout_opreturn(&leader_key_op, &config.burnchain.magic_bytes, 0);
1✔
4024
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 4_999_980_000);
1✔
4025
            assert_eq!(op_return, tx.output[0]);
1✔
4026
            assert_eq!(op_change, tx.output[1]);
1✔
4027
        }
1✔
4028

4029
        #[test]
4030
        #[ignore]
4031
        fn test_build_leader_key_tx_fails_due_to_no_utxos() {
1✔
4032
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
NEW
4033
                return;
×
4034
            }
1✔
4035

4036
            let keychain = utils::create_keychain();
1✔
4037
            let miner_pubkey = keychain.get_pub_key();
1✔
4038
            let mut op_signer = keychain.generate_op_signer();
1✔
4039

4040
            let mut config = utils::create_miner_config();
1✔
4041
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4042

4043
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4044
            btcd_controller
1✔
4045
                .start_bitcoind()
1✔
4046
                .expect("bitcoind should be started!");
1✔
4047

4048
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4049
            btc_controller.bootstrap_chain(100); // no utxos exist
1✔
4050

4051
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4052

4053
            let error = btc_controller
1✔
4054
                .build_leader_key_register_tx(
1✔
4055
                    StacksEpochId::Epoch31,
1✔
4056
                    leader_key_op.clone(),
1✔
4057
                    &mut op_signer,
1✔
4058
                )
4059
                .expect_err("Leader key build should fail!");
1✔
4060

4061
            assert!(!op_signer.is_disposed());
1✔
4062
            assert_eq!(BurnchainControllerError::NoUTXOs, error);
1✔
4063
        }
1✔
4064

4065
        #[test]
4066
        #[ignore]
4067
        fn test_make_operation_leader_key_register_tx_ok() {
1✔
4068
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4069
                return;
×
4070
            }
1✔
4071

4072
            let keychain = utils::create_keychain();
1✔
4073
            let miner_pubkey = keychain.get_pub_key();
1✔
4074
            let mut op_signer = keychain.generate_op_signer();
1✔
4075

4076
            let mut config = utils::create_miner_config();
1✔
4077
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4078
            config.burnchain.pox_reward_length = Some(11);
1✔
4079

4080
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4081
            btcd_controller
1✔
4082
                .start_bitcoind()
1✔
4083
                .expect("bitcoind should be started!");
1✔
4084

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

4088
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4089

4090
            let tx = btc_controller
1✔
4091
                .make_operation_tx(
1✔
4092
                    StacksEpochId::Epoch31,
1✔
4093
                    BlockstackOperationType::LeaderKeyRegister(leader_key_op),
1✔
4094
                    &mut op_signer,
1✔
4095
                )
4096
                .expect("Make op should work");
1✔
4097

4098
            assert!(op_signer.is_disposed());
1✔
4099

4100
            assert_eq!(
1✔
4101
                "4ecd7ba71bebd1aaed49dd63747ee424473f1c571bb9a576361607a669191024",
4102
                tx.txid().to_string()
1✔
4103
            );
4104
        }
1✔
4105

4106
        #[test]
4107
        #[ignore]
4108
        fn test_submit_operation_leader_key_register_tx_ok() {
1✔
4109
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4110
                return;
×
4111
            }
1✔
4112

4113
            let keychain = utils::create_keychain();
1✔
4114
            let miner_pubkey = keychain.get_pub_key();
1✔
4115
            let mut op_signer = keychain.generate_op_signer();
1✔
4116

4117
            let mut config = utils::create_miner_config();
1✔
4118
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4119

4120
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4121
            btcd_controller
1✔
4122
                .start_bitcoind()
1✔
4123
                .expect("bitcoind should be started!");
1✔
4124

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

4128
            let leader_key_op = utils::create_templated_leader_key_op();
1✔
4129

4130
            let tx_id = btc_controller
1✔
4131
                .submit_operation(
1✔
4132
                    StacksEpochId::Epoch31,
1✔
4133
                    BlockstackOperationType::LeaderKeyRegister(leader_key_op),
1✔
4134
                    &mut op_signer,
1✔
4135
                )
4136
                .expect("Submit op should work");
1✔
4137

4138
            assert!(op_signer.is_disposed());
1✔
4139

4140
            assert_eq!(
1✔
4141
                "4ecd7ba71bebd1aaed49dd63747ee424473f1c571bb9a576361607a669191024",
4142
                tx_id.to_hex()
1✔
4143
            );
4144
        }
1✔
4145
    }
4146

4147
    /// Tests related to Pre Stacks operation
4148
    mod pre_stx_op {
4149
        use super::*;
4150

4151
        #[test]
4152
        #[ignore]
4153
        fn test_build_pre_stx_tx_ok() {
1✔
4154
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4155
                return;
×
4156
            }
1✔
4157

4158
            let keychain = utils::create_keychain();
1✔
4159
            let miner_pubkey = keychain.get_pub_key();
1✔
4160
            let mut op_signer = keychain.generate_op_signer();
1✔
4161

4162
            let mut config = utils::create_miner_config();
1✔
4163
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4164

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

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

4173
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4174
            pre_stx_op.output = keychain.get_address(false);
1✔
4175

4176
            let tx = btc_controller
1✔
4177
                .build_pre_stacks_tx(StacksEpochId::Epoch31, pre_stx_op.clone(), &mut op_signer)
1✔
4178
                .expect("Build leader key should work");
1✔
4179

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

4182
            assert_eq!(1, tx.version);
1✔
4183
            assert_eq!(0, tx.lock_time);
1✔
4184
            assert_eq!(1, tx.input.len());
1✔
4185
            assert_eq!(3, tx.output.len());
1✔
4186

4187
            // utxos list contains the only existing utxo
4188
            let used_utxos = btc_controller.get_all_utxos(&miner_pubkey);
1✔
4189
            let input_0 = utils::txin_at_index(&tx, &op_signer, &used_utxos, 0);
1✔
4190
            assert_eq!(input_0, tx.input[0]);
1✔
4191

4192
            let op_return = utils::txout_opreturn(&pre_stx_op, &config.burnchain.magic_bytes, 0);
1✔
4193
            let op_change = utils::txout_opdup_change_legacy(&mut op_signer, 24_500);
1✔
4194
            assert_eq!(op_return, tx.output[0]);
1✔
4195
            assert_eq!(op_change, tx.output[1]);
1✔
4196
        }
1✔
4197

4198
        #[test]
4199
        #[ignore]
4200
        fn test_build_pre_stx_tx_fails_due_to_no_utxos() {
1✔
4201
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4202
                return;
×
4203
            }
1✔
4204

4205
            let keychain = utils::create_keychain();
1✔
4206
            let miner_pubkey = keychain.get_pub_key();
1✔
4207
            let mut op_signer = keychain.generate_op_signer();
1✔
4208

4209
            let mut config = utils::create_miner_config();
1✔
4210
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4211

4212
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4213
            btcd_controller
1✔
4214
                .start_bitcoind()
1✔
4215
                .expect("bitcoind should be started!");
1✔
4216

4217
            let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
1✔
4218
            btc_controller.bootstrap_chain(100); // no utxo exists
1✔
4219

4220
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4221
            pre_stx_op.output = keychain.get_address(false);
1✔
4222

4223
            let error = btc_controller
1✔
4224
                .build_pre_stacks_tx(StacksEpochId::Epoch31, pre_stx_op.clone(), &mut op_signer)
1✔
4225
                .expect_err("Leader key build should fail!");
1✔
4226

4227
            assert!(!op_signer.is_disposed());
1✔
4228
            assert_eq!(BurnchainControllerError::NoUTXOs, error);
1✔
4229
        }
1✔
4230

4231
        #[test]
4232
        #[ignore]
4233
        fn test_make_operation_pre_stx_tx_ok() {
1✔
4234
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4235
                return;
×
4236
            }
1✔
4237

4238
            let keychain = utils::create_keychain();
1✔
4239
            let miner_pubkey = keychain.get_pub_key();
1✔
4240
            let mut op_signer = keychain.generate_op_signer();
1✔
4241

4242
            let mut config = utils::create_miner_config();
1✔
4243
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4244

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

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

4253
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4254
            pre_stx_op.output = keychain.get_address(false);
1✔
4255

4256
            let tx = btc_controller
1✔
4257
                .make_operation_tx(
1✔
4258
                    StacksEpochId::Epoch31,
1✔
4259
                    BlockstackOperationType::PreStx(pre_stx_op),
1✔
4260
                    &mut op_signer,
1✔
4261
                )
4262
                .expect("Make op should work");
1✔
4263

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

4266
            assert_eq!(
1✔
4267
                "2d061c42c6f13a62fd9d80dc9fdcd19bdb4f9e4a07f786e42530c64c52ed9d1d",
4268
                tx.txid().to_string()
1✔
4269
            );
4270
        }
1✔
4271

4272
        #[test]
4273
        #[ignore]
4274
        fn test_submit_operation_pre_stx_tx_ok() {
1✔
4275
            if env::var("BITCOIND_TEST") != Ok("1".into()) {
1✔
4276
                return;
×
4277
            }
1✔
4278

4279
            let keychain = utils::create_keychain();
1✔
4280
            let miner_pubkey = keychain.get_pub_key();
1✔
4281
            let mut op_signer = keychain.generate_op_signer();
1✔
4282

4283
            let mut config = utils::create_miner_config();
1✔
4284
            config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
1✔
4285

4286
            let mut btcd_controller = BitcoinCoreController::from_stx_config(&config);
1✔
4287
            btcd_controller
1✔
4288
                .start_bitcoind()
1✔
4289
                .expect("bitcoind should be started!");
1✔
4290

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

4294
            let mut pre_stx_op = utils::create_templated_pre_stx_op();
1✔
4295
            pre_stx_op.output = keychain.get_address(false);
1✔
4296

4297
            let tx_id = btc_controller
1✔
4298
                .submit_operation(
1✔
4299
                    StacksEpochId::Epoch31,
1✔
4300
                    BlockstackOperationType::PreStx(pre_stx_op),
1✔
4301
                    &mut op_signer,
1✔
4302
                )
4303
                .expect("submit op should work");
1✔
4304

4305
            assert!(op_signer.is_disposed());
1✔
4306

4307
            assert_eq!(
1✔
4308
                "2d061c42c6f13a62fd9d80dc9fdcd19bdb4f9e4a07f786e42530c64c52ed9d1d",
4309
                tx_id.to_hex()
1✔
4310
            );
4311
        }
1✔
4312
    }
4313
}
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