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

stacks-network / stacks-core / 24511638676

16 Apr 2026 01:00PM UTC coverage: 62.273% (-23.4%) from 85.712%
24511638676

Pull #7155

github

d50351
web-flow
Merge 7eacb9950 into 946825b4d
Pull Request #7155: chore: remove mocknet network mode

5 of 10 new or added lines in 5 files covered. (50.0%)

53916 existing lines in 296 files now uncovered.

135738 of 217973 relevant lines covered (62.27%)

13531219.27 hits per line

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

9.69
/stacks-node/src/node.rs
1
use std::collections::{HashMap, HashSet};
2
use std::net::SocketAddr;
3
use std::thread::JoinHandle;
4
use std::{env, thread, time};
5

6
use rand::RngCore;
7
use stacks::burnchains::bitcoin::BitcoinNetworkType;
8
use stacks::burnchains::db::BurnchainDB;
9
use stacks::burnchains::{PoxConstants, Txid};
10
use stacks::chainstate::burn::db::sortdb::SortitionDB;
11
use stacks::chainstate::burn::operations::leader_block_commit::{
12
    RewardSetInfo, BURN_BLOCK_MINED_AT_MODULUS,
13
};
14
use stacks::chainstate::burn::operations::{
15
    BlockstackOperationType, LeaderBlockCommitOp, LeaderKeyRegisterOp,
16
};
17
use stacks::chainstate::burn::ConsensusHash;
18
use stacks::chainstate::stacks::address::PoxAddress;
19
use stacks::chainstate::stacks::db::{
20
    ChainStateBootData, ChainstateAccountBalance, ChainstateAccountLockup, ChainstateBNSName,
21
    ChainstateBNSNamespace, ClarityTx, StacksChainState, StacksEpochReceipt, StacksHeaderInfo,
22
};
23
use stacks::chainstate::stacks::events::{
24
    StacksTransactionEvent, StacksTransactionReceipt, TransactionOrigin,
25
};
26
use stacks::chainstate::stacks::{
27
    CoinbasePayload, StacksBlock, StacksMicroblock, StacksTransaction, StacksTransactionSigner,
28
    TransactionAnchorMode, TransactionPayload, TransactionVersion,
29
};
30
use stacks::core::mempool::MemPoolDB;
31
use stacks::core::{EpochList, STACKS_EPOCH_2_1_MARKER};
32
use stacks::cost_estimates::metrics::UnitMetric;
33
use stacks::cost_estimates::UnitEstimator;
34
use stacks::net::atlas::{AtlasConfig, AtlasDB, AttachmentInstance};
35
use stacks::net::db::PeerDB;
36
use stacks::net::p2p::PeerNetwork;
37
use stacks::net::stackerdb::StackerDBs;
38
use stacks::net::RPCHandlerArgs;
39
use stacks::util_lib::strings::UrlString;
40
use stacks_common::types::chainstate::{BlockHeaderHash, BurnchainHeaderHash, TrieHash, VRFSeed};
41
use stacks_common::types::net::PeerAddress;
42
use stacks_common::util::get_epoch_time_secs;
43
use stacks_common::util::hash::Sha256Sum;
44
use stacks_common::util::secp256k1::Secp256k1PrivateKey;
45
use stacks_common::util::vrf::VRFPublicKey;
46

47
use super::{BurnchainController, BurnchainTip, Config, EventDispatcher, Keychain, Tenure};
48
use crate::burnchains::make_bitcoin_indexer;
49
use crate::genesis_data::USE_TEST_GENESIS_CHAINSTATE;
50
use crate::run_loop;
51
use crate::run_loop::RegisteredKey;
52

53
#[derive(Debug, Clone)]
54
pub struct ChainTip {
55
    pub metadata: StacksHeaderInfo,
56
    pub block: StacksBlock,
57
    pub receipts: Vec<StacksTransactionReceipt>,
58
}
59

60
impl ChainTip {
61
    pub fn genesis(
420✔
62
        first_burnchain_block_hash: &BurnchainHeaderHash,
420✔
63
        first_burnchain_block_height: u64,
420✔
64
        first_burnchain_block_timestamp: u64,
420✔
65
    ) -> ChainTip {
420✔
66
        ChainTip {
420✔
67
            metadata: StacksHeaderInfo::genesis(
420✔
68
                TrieHash([0u8; 32]),
420✔
69
                first_burnchain_block_hash,
420✔
70
                first_burnchain_block_height as u32,
420✔
71
                first_burnchain_block_timestamp,
420✔
72
            ),
420✔
73
            block: StacksBlock::genesis_block(),
420✔
74
            receipts: vec![],
420✔
75
        }
420✔
76
    }
420✔
77
}
78

79
/// Node is a structure modelising an active node working on the stacks chain.
80
pub struct Node {
81
    pub chain_state: StacksChainState,
82
    pub config: Config,
83
    active_registered_key: Option<RegisteredKey>,
84
    bootstraping_chain: bool,
85
    pub burnchain_tip: Option<BurnchainTip>,
86
    pub chain_tip: Option<ChainTip>,
87
    keychain: Keychain,
88
    last_sortitioned_block: Option<BurnchainTip>,
89
    event_dispatcher: EventDispatcher,
90
    nonce: u64,
91
    leader_key_registers: HashSet<Txid>,
92
    block_commits: HashSet<Txid>,
93
}
94

95
pub fn get_account_lockups(
273✔
96
    use_test_chainstate_data: bool,
273✔
97
) -> Box<dyn Iterator<Item = ChainstateAccountLockup>> {
273✔
98
    Box::new(
273✔
99
        stx_genesis::GenesisData::new(use_test_chainstate_data)
273✔
100
            .read_lockups()
273✔
101
            .map(|item| ChainstateAccountLockup {
273✔
102
                address: item.address,
5,460✔
103
                amount: item.amount,
5,460✔
104
                block_height: item.block_height,
5,460✔
105
            }),
5,460✔
106
    )
107
}
273✔
108

109
pub fn get_account_balances(
273✔
110
    use_test_chainstate_data: bool,
273✔
111
) -> Box<dyn Iterator<Item = ChainstateAccountBalance>> {
273✔
112
    Box::new(
273✔
113
        stx_genesis::GenesisData::new(use_test_chainstate_data)
273✔
114
            .read_balances()
273✔
115
            .map(|item| ChainstateAccountBalance {
273✔
116
                address: item.address,
6,552✔
117
                amount: item.amount,
6,552✔
118
            }),
6,552✔
119
    )
120
}
273✔
121

122
pub fn get_namespaces(
273✔
123
    use_test_chainstate_data: bool,
273✔
124
) -> Box<dyn Iterator<Item = ChainstateBNSNamespace>> {
273✔
125
    Box::new(
273✔
126
        stx_genesis::GenesisData::new(use_test_chainstate_data)
273✔
127
            .read_namespaces()
273✔
128
            .map(|item| ChainstateBNSNamespace {
273✔
129
                namespace_id: item.namespace_id,
1,365✔
130
                importer: item.importer,
1,365✔
131
                buckets: item.buckets,
1,365✔
132
                base: item.base as u64,
1,365✔
133
                coeff: item.coeff as u64,
1,365✔
134
                nonalpha_discount: item.nonalpha_discount as u64,
1,365✔
135
                no_vowel_discount: item.no_vowel_discount as u64,
1,365✔
136
                lifetime: item.lifetime as u64,
1,365✔
137
            }),
1,365✔
138
    )
139
}
273✔
140

141
pub fn get_names(use_test_chainstate_data: bool) -> Box<dyn Iterator<Item = ChainstateBNSName>> {
273✔
142
    Box::new(
273✔
143
        stx_genesis::GenesisData::new(use_test_chainstate_data)
273✔
144
            .read_names()
273✔
145
            .map(|item| ChainstateBNSName {
273✔
146
                fully_qualified_name: item.fully_qualified_name,
3,276✔
147
                owner: item.owner,
3,276✔
148
                zonefile_hash: item.zonefile_hash,
3,276✔
149
            }),
3,276✔
150
    )
151
}
273✔
152

153
// This function is called for helium.
154
#[allow(clippy::too_many_arguments)]
UNCOV
155
fn spawn_peer(
×
UNCOV
156
    is_mainnet: bool,
×
UNCOV
157
    chain_id: u32,
×
UNCOV
158
    mut this: PeerNetwork,
×
UNCOV
159
    p2p_sock: &SocketAddr,
×
UNCOV
160
    rpc_sock: &SocketAddr,
×
UNCOV
161
    burn_db_path: String,
×
UNCOV
162
    stacks_chainstate_path: String,
×
UNCOV
163
    pox_consts: PoxConstants,
×
UNCOV
164
    event_dispatcher: EventDispatcher,
×
UNCOV
165
    exit_at_block_height: Option<u64>,
×
UNCOV
166
    genesis_chainstate_hash: Sha256Sum,
×
UNCOV
167
    poll_timeout: u64,
×
UNCOV
168
    config: Config,
×
UNCOV
169
) -> JoinHandle<()> {
×
UNCOV
170
    this.bind(p2p_sock, rpc_sock).unwrap();
×
UNCOV
171
    let server_thread = thread::spawn(move || {
×
172
        // create estimators, metric instances for RPC handler
UNCOV
173
        let cost_estimator = config
×
UNCOV
174
            .make_cost_estimator()
×
UNCOV
175
            .unwrap_or_else(|| Box::new(UnitEstimator));
×
UNCOV
176
        let metric = config
×
UNCOV
177
            .make_cost_metric()
×
UNCOV
178
            .unwrap_or_else(|| Box::new(UnitMetric));
×
UNCOV
179
        let fee_estimator = config.make_fee_estimator();
×
180

UNCOV
181
        let handler_args = RPCHandlerArgs {
×
UNCOV
182
            exit_at_block_height,
×
UNCOV
183
            cost_estimator: Some(cost_estimator.as_ref()),
×
UNCOV
184
            cost_metric: Some(metric.as_ref()),
×
UNCOV
185
            fee_estimator: fee_estimator.as_ref().map(|x| x.as_ref()),
×
UNCOV
186
            genesis_chainstate_hash,
×
UNCOV
187
            ..RPCHandlerArgs::default()
×
188
        };
189

190
        loop {
UNCOV
191
            let sortdb = match SortitionDB::open(
×
UNCOV
192
                &burn_db_path,
×
UNCOV
193
                false,
×
UNCOV
194
                pox_consts.clone(),
×
UNCOV
195
                Some(config.node.get_marf_opts()),
×
UNCOV
196
            ) {
×
UNCOV
197
                Ok(x) => x,
×
198
                Err(e) => {
×
199
                    warn!("Error while connecting burnchain db in peer loop: {e}");
×
200
                    thread::sleep(time::Duration::from_secs(1));
×
201
                    continue;
×
202
                }
203
            };
UNCOV
204
            let (mut chainstate, _) = match StacksChainState::open(
×
UNCOV
205
                is_mainnet,
×
UNCOV
206
                chain_id,
×
UNCOV
207
                &stacks_chainstate_path,
×
UNCOV
208
                Some(config.node.get_marf_opts()),
×
UNCOV
209
            ) {
×
UNCOV
210
                Ok(x) => x,
×
211
                Err(e) => {
×
212
                    warn!("Error while connecting chainstate db in peer loop: {e}");
×
213
                    thread::sleep(time::Duration::from_secs(1));
×
214
                    continue;
×
215
                }
216
            };
217

UNCOV
218
            let estimator = Box::new(UnitEstimator);
×
UNCOV
219
            let metric = Box::new(UnitMetric);
×
220

UNCOV
221
            let mut mem_pool = match MemPoolDB::open(
×
UNCOV
222
                is_mainnet,
×
UNCOV
223
                chain_id,
×
UNCOV
224
                &stacks_chainstate_path,
×
UNCOV
225
                estimator,
×
UNCOV
226
                metric,
×
UNCOV
227
            ) {
×
UNCOV
228
                Ok(x) => x,
×
229
                Err(e) => {
×
230
                    warn!("Error while connecting to mempool db in peer loop: {e}");
×
231
                    thread::sleep(time::Duration::from_secs(1));
×
232
                    continue;
×
233
                }
234
            };
235

UNCOV
236
            let indexer = make_bitcoin_indexer(&config, None);
×
237

UNCOV
238
            let net_result = this
×
UNCOV
239
                .run(
×
UNCOV
240
                    &indexer,
×
UNCOV
241
                    &sortdb,
×
UNCOV
242
                    &mut chainstate,
×
UNCOV
243
                    &mut mem_pool,
×
UNCOV
244
                    None,
×
245
                    false,
246
                    false,
UNCOV
247
                    poll_timeout,
×
UNCOV
248
                    &handler_args,
×
UNCOV
249
                    config.node.txindex,
×
250
                )
UNCOV
251
                .unwrap();
×
UNCOV
252
            if net_result.has_transactions() {
×
UNCOV
253
                event_dispatcher.process_new_mempool_txs(net_result.transactions())
×
UNCOV
254
            }
×
255
            // Dispatch retrieved attachments, if any.
UNCOV
256
            if net_result.has_attachments() {
×
257
                event_dispatcher.process_new_attachments(&net_result.attachments);
×
UNCOV
258
            }
×
259
        }
260
    });
UNCOV
261
    server_thread
×
UNCOV
262
}
×
263

264
// Check if the small test genesis chainstate data should be used.
265
// First check env var, then config file, then use default.
266
pub fn use_test_genesis_chainstate(config: &Config) -> bool {
1,017✔
267
    if env::var("BLOCKSTACK_USE_TEST_GENESIS_CHAINSTATE") == Ok("1".to_string()) {
1,017✔
268
        true
×
269
    } else if let Some(use_test_genesis_chainstate) = config.node.use_test_genesis_chainstate {
1,017✔
270
        use_test_genesis_chainstate
×
271
    } else {
272
        USE_TEST_GENESIS_CHAINSTATE
1,017✔
273
    }
274
}
1,017✔
275

276
impl Node {
277
    /// Instantiate and initialize a new node, given a config
UNCOV
278
    pub fn new(config: Config, boot_block_exec: Box<dyn FnOnce(&mut ClarityTx)>) -> Self {
×
NEW
279
        let use_test_genesis_data = if config.burnchain.mode == "helium" {
×
UNCOV
280
            use_test_genesis_chainstate(&config)
×
281
        } else {
282
            USE_TEST_GENESIS_CHAINSTATE
×
283
        };
284

UNCOV
285
        let keychain = Keychain::default(config.node.seed.clone());
×
286

UNCOV
287
        let initial_balances = config
×
UNCOV
288
            .initial_balances
×
UNCOV
289
            .iter()
×
UNCOV
290
            .map(|e| (e.address.clone(), e.amount))
×
UNCOV
291
            .collect();
×
UNCOV
292
        let pox_constants = match config.burnchain.get_bitcoin_network() {
×
293
            (_, BitcoinNetworkType::Mainnet) => PoxConstants::mainnet_default(),
×
294
            (_, BitcoinNetworkType::Testnet) => PoxConstants::testnet_default(),
×
UNCOV
295
            (_, BitcoinNetworkType::Regtest) => PoxConstants::regtest_default(),
×
296
        };
297

UNCOV
298
        let mut boot_data = ChainStateBootData {
×
UNCOV
299
            initial_balances,
×
UNCOV
300
            first_burnchain_block_hash: BurnchainHeaderHash::zero(),
×
301
            first_burnchain_block_height: 0,
302
            first_burnchain_block_timestamp: 0,
UNCOV
303
            pox_constants,
×
UNCOV
304
            post_flight_callback: Some(boot_block_exec),
×
UNCOV
305
            get_bulk_initial_lockups: Some(Box::new(move || {
×
UNCOV
306
                get_account_lockups(use_test_genesis_data)
×
UNCOV
307
            })),
×
UNCOV
308
            get_bulk_initial_balances: Some(Box::new(move || {
×
UNCOV
309
                get_account_balances(use_test_genesis_data)
×
UNCOV
310
            })),
×
UNCOV
311
            get_bulk_initial_namespaces: Some(Box::new(move || {
×
UNCOV
312
                get_namespaces(use_test_genesis_data)
×
UNCOV
313
            })),
×
UNCOV
314
            get_bulk_initial_names: Some(Box::new(move || get_names(use_test_genesis_data))),
×
315
        };
316

UNCOV
317
        let chain_state_result = StacksChainState::open_and_exec(
×
UNCOV
318
            config.is_mainnet(),
×
UNCOV
319
            config.burnchain.chain_id,
×
UNCOV
320
            &config.get_chainstate_path_str(),
×
UNCOV
321
            Some(&mut boot_data),
×
UNCOV
322
            Some(config.node.get_marf_opts()),
×
323
        );
324

UNCOV
325
        let (chain_state, receipts) = match chain_state_result {
×
UNCOV
326
            Ok(res) => res,
×
327
            Err(err) => panic!(
×
328
                "Error while opening chain state at path {}: {err:?}",
329
                config.get_chainstate_path_str()
×
330
            ),
331
        };
332

UNCOV
333
        let estimator = Box::new(UnitEstimator);
×
UNCOV
334
        let metric = Box::new(UnitMetric);
×
335

336
        // avoid race to create condition on mempool db
UNCOV
337
        let _mem_pool = MemPoolDB::open(
×
UNCOV
338
            config.is_mainnet(),
×
UNCOV
339
            config.burnchain.chain_id,
×
UNCOV
340
            &chain_state.root_path,
×
UNCOV
341
            estimator,
×
UNCOV
342
            metric,
×
343
        )
UNCOV
344
        .expect("FATAL: failed to initiate mempool");
×
345

UNCOV
346
        let mut event_dispatcher = EventDispatcher::new(config.get_working_dir());
×
347

UNCOV
348
        for observer in &config.events_observers {
×
349
            event_dispatcher.register_observer(observer);
×
350
        }
×
UNCOV
351
        event_dispatcher.process_pending_payloads();
×
352

UNCOV
353
        let burnchain_config = config.get_burnchain();
×
354

355
        // instantiate DBs
UNCOV
356
        let _burnchain_db = BurnchainDB::connect(
×
UNCOV
357
            &burnchain_config.get_burnchaindb_path(),
×
UNCOV
358
            &burnchain_config,
×
359
            true,
360
        )
UNCOV
361
        .expect("FATAL: failed to connect to burnchain DB");
×
362

UNCOV
363
        run_loop::announce_boot_receipts(
×
UNCOV
364
            &mut event_dispatcher,
×
UNCOV
365
            &chain_state,
×
UNCOV
366
            &burnchain_config.pox_constants,
×
UNCOV
367
            &receipts,
×
368
        );
369

UNCOV
370
        Self {
×
UNCOV
371
            active_registered_key: None,
×
UNCOV
372
            bootstraping_chain: false,
×
UNCOV
373
            chain_state,
×
UNCOV
374
            chain_tip: None,
×
UNCOV
375
            keychain,
×
UNCOV
376
            last_sortitioned_block: None,
×
UNCOV
377
            config,
×
UNCOV
378
            burnchain_tip: None,
×
UNCOV
379
            nonce: 0,
×
UNCOV
380
            event_dispatcher,
×
UNCOV
381
            leader_key_registers: HashSet::new(),
×
UNCOV
382
            block_commits: HashSet::new(),
×
UNCOV
383
        }
×
UNCOV
384
    }
×
385

UNCOV
386
    fn make_atlas_config() -> AtlasConfig {
×
UNCOV
387
        AtlasConfig::new(false)
×
UNCOV
388
    }
×
389

UNCOV
390
    pub fn make_atlas_db(&self) -> AtlasDB {
×
UNCOV
391
        AtlasDB::connect(
×
UNCOV
392
            Self::make_atlas_config(),
×
UNCOV
393
            &self.config.get_atlas_db_file_path(),
×
394
            true,
395
        )
UNCOV
396
        .unwrap()
×
UNCOV
397
    }
×
398

399
    // This function is used for helium.
UNCOV
400
    pub fn spawn_peer_server(&mut self) {
×
401
        // we can call _open_ here rather than _connect_, since connect is first called in
402
        //   make_genesis_block
UNCOV
403
        let burnchain = self.config.get_burnchain();
×
UNCOV
404
        let sortdb = SortitionDB::open(
×
UNCOV
405
            &self.config.get_burn_db_file_path(),
×
406
            true,
UNCOV
407
            burnchain.pox_constants.clone(),
×
UNCOV
408
            Some(self.config.node.get_marf_opts()),
×
409
        )
UNCOV
410
        .expect("Error while instantiating burnchain db");
×
411

UNCOV
412
        let epochs_vec = SortitionDB::get_stacks_epochs(sortdb.conn())
×
UNCOV
413
            .expect("Error while loading stacks epochs");
×
UNCOV
414
        let epochs = EpochList::new(&epochs_vec);
×
415

UNCOV
416
        Config::assert_valid_epoch_settings(&burnchain, &epochs);
×
417

UNCOV
418
        let view = {
×
UNCOV
419
            let sortition_tip = SortitionDB::get_canonical_burn_chain_tip(sortdb.conn())
×
UNCOV
420
                .expect("Failed to get sortition tip");
×
UNCOV
421
            SortitionDB::get_burnchain_view(&sortdb.index_conn(), &burnchain, &sortition_tip)
×
UNCOV
422
                .unwrap()
×
423
        };
424

425
        // create a new peerdb
UNCOV
426
        let data_url = UrlString::try_from(self.config.node.data_url.to_string()).unwrap();
×
427

UNCOV
428
        let initial_neighbors = self.config.node.bootstrap_node.clone();
×
429

UNCOV
430
        println!("BOOTSTRAP WITH {initial_neighbors:?}");
×
431

UNCOV
432
        let rpc_sock: SocketAddr =
×
UNCOV
433
            self.config.node.rpc_bind.parse().unwrap_or_else(|_| {
×
434
                panic!("Failed to parse socket: {}", &self.config.node.rpc_bind)
×
435
            });
UNCOV
436
        let p2p_sock: SocketAddr =
×
UNCOV
437
            self.config.node.p2p_bind.parse().unwrap_or_else(|_| {
×
438
                panic!("Failed to parse socket: {}", &self.config.node.p2p_bind)
×
439
            });
UNCOV
440
        let p2p_addr: SocketAddr = self.config.node.p2p_address.parse().unwrap_or_else(|_| {
×
441
            panic!("Failed to parse socket: {}", &self.config.node.p2p_address)
×
442
        });
UNCOV
443
        let node_privkey = {
×
UNCOV
444
            let mut re_hashed_seed = self.config.node.local_peer_seed.clone();
×
UNCOV
445
            let my_private_key = loop {
×
UNCOV
446
                match Secp256k1PrivateKey::from_slice(&re_hashed_seed[..]) {
×
UNCOV
447
                    Ok(sk) => break sk,
×
448
                    Err(_) => {
449
                        re_hashed_seed = Sha256Sum::from_data(&re_hashed_seed[..])
×
450
                            .as_bytes()
×
451
                            .to_vec()
×
452
                    }
453
                }
454
            };
UNCOV
455
            my_private_key
×
456
        };
457

UNCOV
458
        let mut peerdb = PeerDB::connect(
×
UNCOV
459
            &self.config.get_peer_db_file_path(),
×
460
            true,
UNCOV
461
            self.config.burnchain.chain_id,
×
UNCOV
462
            burnchain.network_id,
×
UNCOV
463
            Some(node_privkey),
×
UNCOV
464
            self.config.connection_options.private_key_lifetime,
×
UNCOV
465
            PeerAddress::from_socketaddr(&p2p_addr),
×
UNCOV
466
            p2p_sock.port(),
×
UNCOV
467
            data_url,
×
UNCOV
468
            &[],
×
UNCOV
469
            Some(&initial_neighbors),
×
UNCOV
470
            &[],
×
471
        )
UNCOV
472
        .unwrap();
×
473

UNCOV
474
        println!("DENY NEIGHBORS {:?}", &self.config.node.deny_nodes);
×
475
        {
UNCOV
476
            let tx = peerdb.tx_begin().unwrap();
×
UNCOV
477
            for denied in self.config.node.deny_nodes.iter() {
×
478
                PeerDB::set_deny_peer(
×
479
                    &tx,
×
480
                    denied.addr.network_id,
×
481
                    &denied.addr.addrbytes,
×
482
                    denied.addr.port,
×
483
                    get_epoch_time_secs() + 24 * 365 * 3600,
×
484
                )
×
485
                .unwrap();
×
486
            }
×
UNCOV
487
            tx.commit().unwrap();
×
488
        }
UNCOV
489
        let atlasdb = self.make_atlas_db();
×
490

UNCOV
491
        let stackerdbs =
×
UNCOV
492
            StackerDBs::connect(&self.config.get_stacker_db_file_path(), true).unwrap();
×
493

UNCOV
494
        let local_peer = match PeerDB::get_local_peer(peerdb.conn()) {
×
UNCOV
495
            Ok(local_peer) => local_peer,
×
496
            _ => panic!("Unable to retrieve local peer"),
×
497
        };
498

UNCOV
499
        let event_dispatcher = self.event_dispatcher.clone();
×
UNCOV
500
        let exit_at_block_height = self.config.burnchain.process_exit_at_block_height;
×
UNCOV
501
        let burnchain_db = burnchain
×
UNCOV
502
            .open_burnchain_db(false)
×
UNCOV
503
            .expect("Failed to open burnchain DB");
×
504

UNCOV
505
        let p2p_net = PeerNetwork::new(
×
UNCOV
506
            peerdb,
×
UNCOV
507
            atlasdb,
×
UNCOV
508
            stackerdbs,
×
UNCOV
509
            burnchain_db,
×
UNCOV
510
            local_peer,
×
UNCOV
511
            self.config.burnchain.peer_version,
×
UNCOV
512
            burnchain.clone(),
×
UNCOV
513
            view,
×
UNCOV
514
            self.config.connection_options.clone(),
×
UNCOV
515
            HashMap::new(),
×
UNCOV
516
            epochs,
×
517
        );
UNCOV
518
        let _join_handle = spawn_peer(
×
UNCOV
519
            self.config.is_mainnet(),
×
UNCOV
520
            self.config.burnchain.chain_id,
×
UNCOV
521
            p2p_net,
×
UNCOV
522
            &p2p_sock,
×
UNCOV
523
            &rpc_sock,
×
UNCOV
524
            self.config.get_burn_db_file_path(),
×
UNCOV
525
            self.config.get_chainstate_path_str(),
×
UNCOV
526
            burnchain.pox_constants,
×
UNCOV
527
            event_dispatcher,
×
UNCOV
528
            exit_at_block_height,
×
UNCOV
529
            Sha256Sum::from_hex(stx_genesis::GENESIS_CHAINSTATE_HASH).unwrap(),
×
530
            1000,
UNCOV
531
            self.config.clone(),
×
532
        );
533

UNCOV
534
        info!("Start HTTP server on: {}", &self.config.node.rpc_bind);
×
UNCOV
535
        info!("Start P2P server on: {}", &self.config.node.p2p_bind);
×
UNCOV
536
    }
×
537

UNCOV
538
    pub fn setup(&mut self, burnchain_controller: &mut Box<dyn BurnchainController>) {
×
539
        // Register a new key
UNCOV
540
        let burnchain_tip = burnchain_controller.get_chain_tip();
×
UNCOV
541
        let (vrf_pk, _) = self
×
UNCOV
542
            .keychain
×
UNCOV
543
            .make_vrf_keypair(burnchain_tip.block_snapshot.block_height);
×
UNCOV
544
        let consensus_hash = burnchain_tip.block_snapshot.consensus_hash;
×
545

UNCOV
546
        let burnchain = self.config.get_burnchain();
×
547

UNCOV
548
        let sortdb = SortitionDB::open(
×
UNCOV
549
            &self.config.get_burn_db_file_path(),
×
550
            true,
UNCOV
551
            burnchain.pox_constants.clone(),
×
UNCOV
552
            Some(self.config.node.get_marf_opts()),
×
553
        )
UNCOV
554
        .expect("Error while opening sortition db");
×
555

UNCOV
556
        let epochs = SortitionDB::get_stacks_epochs(sortdb.conn())
×
UNCOV
557
            .expect("FATAL: failed to read sortition DB");
×
558

UNCOV
559
        Config::assert_valid_epoch_settings(&burnchain, &epochs);
×
560

UNCOV
561
        let cur_epoch =
×
UNCOV
562
            SortitionDB::get_stacks_epoch(sortdb.conn(), burnchain_tip.block_snapshot.block_height)
×
UNCOV
563
                .expect("FATAL: failed to read sortition DB")
×
UNCOV
564
                .expect("FATAL: no epoch defined");
×
565

UNCOV
566
        let key_reg_op = self.generate_leader_key_register_op(vrf_pk, consensus_hash);
×
UNCOV
567
        let mut op_signer = self.keychain.generate_op_signer();
×
UNCOV
568
        let key_txid = burnchain_controller
×
UNCOV
569
            .submit_operation(cur_epoch.epoch_id, key_reg_op, &mut op_signer)
×
UNCOV
570
            .expect("FATAL: failed to submit leader key register operation");
×
571

UNCOV
572
        self.leader_key_registers.insert(key_txid);
×
UNCOV
573
    }
×
574

575
    /// Process an state coming from the burnchain, by extracting the validated KeyRegisterOp
576
    /// and inspecting if a sortition was won.
UNCOV
577
    pub fn process_burnchain_state(
×
UNCOV
578
        &mut self,
×
UNCOV
579
        burnchain_tip: &BurnchainTip,
×
UNCOV
580
    ) -> (Option<BurnchainTip>, bool) {
×
UNCOV
581
        let mut new_key = None;
×
UNCOV
582
        let mut last_sortitioned_block = None;
×
UNCOV
583
        let mut won_sortition = false;
×
UNCOV
584
        let ops = &burnchain_tip.state_transition.accepted_ops;
×
585

UNCOV
586
        for op in ops.iter() {
×
UNCOV
587
            match op {
×
UNCOV
588
                BlockstackOperationType::LeaderKeyRegister(ref op) => {
×
UNCOV
589
                    if self.leader_key_registers.contains(&op.txid) {
×
UNCOV
590
                        // Registered key has been mined
×
UNCOV
591
                        new_key = Some(RegisteredKey {
×
UNCOV
592
                            vrf_public_key: op.public_key.clone(),
×
UNCOV
593
                            block_height: op.block_height,
×
UNCOV
594
                            op_vtxindex: op.vtxindex,
×
UNCOV
595
                            target_block_height: op.block_height - 1,
×
UNCOV
596
                            memo: op.memo.clone(),
×
UNCOV
597
                        });
×
UNCOV
598
                    }
×
599
                }
UNCOV
600
                BlockstackOperationType::LeaderBlockCommit(ref op) => {
×
UNCOV
601
                    if op.txid == burnchain_tip.block_snapshot.winning_block_txid {
×
UNCOV
602
                        last_sortitioned_block = Some(burnchain_tip.clone());
×
UNCOV
603
                        if self.block_commits.contains(&op.txid) {
×
UNCOV
604
                            won_sortition = true;
×
UNCOV
605
                        }
×
606
                    }
×
607
                }
608
                _ => {
×
609
                    // no-op, ops are not supported / produced at this point.
×
610
                }
×
611
            }
612
        }
613

614
        // Update the active key so we use the latest registered key.
UNCOV
615
        if new_key.is_some() {
×
UNCOV
616
            self.active_registered_key = new_key;
×
UNCOV
617
        }
×
618

619
        // Update last_sortitioned_block so we keep a reference to the latest
620
        // block including a sortition.
UNCOV
621
        if last_sortitioned_block.is_some() {
×
UNCOV
622
            self.last_sortitioned_block = last_sortitioned_block;
×
UNCOV
623
        }
×
624

625
        // Keep a pointer of the burnchain's chain tip.
UNCOV
626
        self.burnchain_tip = Some(burnchain_tip.clone());
×
627

UNCOV
628
        (self.last_sortitioned_block.clone(), won_sortition)
×
UNCOV
629
    }
×
630

631
    /// Prepares the node to run a tenure consisting in bootstraping the chain.
632
    ///
633
    /// Will internally call initiate_new_tenure().
UNCOV
634
    pub fn initiate_genesis_tenure(&mut self, burnchain_tip: &BurnchainTip) -> Option<Tenure> {
×
635
        // Set the `bootstraping_chain` flag, that will be unset once the
636
        // bootstraping tenure ran successfully (process_tenure).
UNCOV
637
        self.bootstraping_chain = true;
×
638

UNCOV
639
        self.last_sortitioned_block = Some(burnchain_tip.clone());
×
640

UNCOV
641
        self.initiate_new_tenure()
×
UNCOV
642
    }
×
643

644
    /// Constructs and returns an instance of Tenure, that can be run
645
    /// on an isolated thread and discarded or canceled without corrupting the
646
    /// chain state of the node.
UNCOV
647
    pub fn initiate_new_tenure(&mut self) -> Option<Tenure> {
×
648
        // Get the latest registered key
UNCOV
649
        let registered_key = match &self.active_registered_key {
×
650
            None => {
651
                // We're continuously registering new keys, as such, this branch
652
                // should be unreachable.
653
                unreachable!()
×
654
            }
UNCOV
655
            Some(ref key) => key,
×
656
        };
657

UNCOV
658
        let burnchain = self.config.get_burnchain();
×
UNCOV
659
        let sortdb = SortitionDB::open(
×
UNCOV
660
            &self.config.get_burn_db_file_path(),
×
661
            true,
UNCOV
662
            burnchain.pox_constants,
×
UNCOV
663
            Some(self.config.node.get_marf_opts()),
×
664
        )
UNCOV
665
        .expect("Error while opening sortition db");
×
UNCOV
666
        let tip = SortitionDB::get_canonical_burn_chain_tip(sortdb.conn())
×
UNCOV
667
            .expect("FATAL: failed to query canonical burn chain tip");
×
668

669
        // Generates a proof out of the sortition hash provided in the params.
UNCOV
670
        let Some(vrf_proof) = self.keychain.generate_proof(
×
UNCOV
671
            registered_key.target_block_height,
×
UNCOV
672
            tip.sortition_hash.as_bytes(),
×
UNCOV
673
        ) else {
×
674
            warn!("Failed to generate VRF proof, will be unable to initiate new tenure");
×
675
            return None;
×
676
        };
677

678
        // Generates a new secret key for signing the trail of microblocks
679
        // of the upcoming tenure.
UNCOV
680
        let microblock_secret_key = self.keychain.get_microblock_key(tip.block_height);
×
681

682
        // Get the stack's chain tip
UNCOV
683
        let chain_tip = match self.bootstraping_chain {
×
UNCOV
684
            true => ChainTip::genesis(&BurnchainHeaderHash::zero(), 0, 0),
×
UNCOV
685
            false => match &self.chain_tip {
×
UNCOV
686
                Some(chain_tip) => chain_tip.clone(),
×
687
                None => unreachable!(),
×
688
            },
689
        };
690

UNCOV
691
        let estimator = self
×
UNCOV
692
            .config
×
UNCOV
693
            .make_cost_estimator()
×
UNCOV
694
            .unwrap_or_else(|| Box::new(UnitEstimator));
×
UNCOV
695
        let metric = self
×
UNCOV
696
            .config
×
UNCOV
697
            .make_cost_metric()
×
UNCOV
698
            .unwrap_or_else(|| Box::new(UnitMetric));
×
699

UNCOV
700
        let mem_pool = MemPoolDB::open(
×
UNCOV
701
            self.config.is_mainnet(),
×
UNCOV
702
            self.config.burnchain.chain_id,
×
UNCOV
703
            &self.chain_state.root_path,
×
UNCOV
704
            estimator,
×
UNCOV
705
            metric,
×
706
        )
UNCOV
707
        .expect("FATAL: failed to open mempool");
×
708

709
        // Construct the coinbase transaction - 1st txn that should be handled and included in
710
        // the upcoming tenure.
UNCOV
711
        let coinbase_tx = self.generate_coinbase_tx(self.config.is_mainnet());
×
712

UNCOV
713
        let burn_fee_cap = self.config.burnchain.burn_fee_cap;
×
714

UNCOV
715
        let block_to_build_upon = match &self.last_sortitioned_block {
×
716
            None => unreachable!(),
×
UNCOV
717
            Some(block) => block.clone(),
×
718
        };
719

720
        // Construct the upcoming tenure
UNCOV
721
        let tenure = Tenure::new(
×
UNCOV
722
            chain_tip,
×
UNCOV
723
            coinbase_tx,
×
UNCOV
724
            self.config.clone(),
×
UNCOV
725
            mem_pool,
×
UNCOV
726
            microblock_secret_key,
×
UNCOV
727
            block_to_build_upon,
×
UNCOV
728
            vrf_proof,
×
UNCOV
729
            burn_fee_cap,
×
730
        );
731

UNCOV
732
        Some(tenure)
×
UNCOV
733
    }
×
734

UNCOV
735
    pub fn commit_artifacts(
×
UNCOV
736
        &mut self,
×
UNCOV
737
        anchored_block_from_ongoing_tenure: &StacksBlock,
×
UNCOV
738
        burnchain_tip: &BurnchainTip,
×
UNCOV
739
        burnchain_controller: &mut Box<dyn BurnchainController>,
×
UNCOV
740
        burn_fee: u64,
×
UNCOV
741
    ) {
×
UNCOV
742
        if self.active_registered_key.is_some() {
×
UNCOV
743
            let registered_key = self.active_registered_key.clone().unwrap();
×
744

UNCOV
745
            let Some(vrf_proof) = self.keychain.generate_proof(
×
UNCOV
746
                registered_key.target_block_height,
×
UNCOV
747
                burnchain_tip.block_snapshot.sortition_hash.as_bytes(),
×
UNCOV
748
            ) else {
×
749
                warn!("Failed to generate VRF proof, will be unable to mine commits");
×
750
                return;
×
751
            };
752

UNCOV
753
            let op = self.generate_block_commit_op(
×
UNCOV
754
                anchored_block_from_ongoing_tenure.header.block_hash(),
×
UNCOV
755
                burn_fee,
×
UNCOV
756
                &registered_key,
×
UNCOV
757
                burnchain_tip,
×
UNCOV
758
                VRFSeed::from_proof(&vrf_proof),
×
759
            );
760

UNCOV
761
            let burnchain = self.config.get_burnchain();
×
UNCOV
762
            let sortdb = SortitionDB::open(
×
UNCOV
763
                &self.config.get_burn_db_file_path(),
×
764
                true,
UNCOV
765
                burnchain.pox_constants,
×
UNCOV
766
                Some(self.config.node.get_marf_opts()),
×
767
            )
UNCOV
768
            .expect("Error while opening sortition db");
×
769

UNCOV
770
            let cur_epoch = SortitionDB::get_stacks_epoch(
×
UNCOV
771
                sortdb.conn(),
×
UNCOV
772
                burnchain_tip.block_snapshot.block_height,
×
773
            )
UNCOV
774
            .expect("FATAL: failed to read sortition DB")
×
UNCOV
775
            .expect("FATAL: no epoch defined");
×
776

UNCOV
777
            let mut op_signer = self.keychain.generate_op_signer();
×
UNCOV
778
            let txid = burnchain_controller
×
UNCOV
779
                .submit_operation(cur_epoch.epoch_id, op, &mut op_signer)
×
UNCOV
780
                .expect("FATAL: failed to submit block-commit");
×
781

UNCOV
782
            self.block_commits.insert(txid);
×
783
        } else {
784
            warn!("No leader key active!");
×
785
        }
UNCOV
786
    }
×
787

788
    /// Process artifacts from the tenure.
789
    /// At this point, we're modifying the chainstate, and merging the artifacts from the previous tenure.
UNCOV
790
    pub fn process_tenure(
×
UNCOV
791
        &mut self,
×
UNCOV
792
        anchored_block: &StacksBlock,
×
UNCOV
793
        consensus_hash: &ConsensusHash,
×
UNCOV
794
        microblocks: Vec<StacksMicroblock>,
×
UNCOV
795
        db: &mut SortitionDB,
×
UNCOV
796
        atlas_db: &mut AtlasDB,
×
UNCOV
797
    ) -> ChainTip {
×
UNCOV
798
        let _parent_consensus_hash = {
×
799
            // look up parent consensus hash
UNCOV
800
            let ic = db.index_conn();
×
UNCOV
801
            let parent_consensus_hash = StacksChainState::get_parent_consensus_hash(
×
UNCOV
802
                &ic,
×
UNCOV
803
                &anchored_block.header.parent_block,
×
UNCOV
804
                consensus_hash,
×
805
            )
UNCOV
806
            .unwrap_or_else(|_| {
×
807
                panic!(
×
808
                    "BUG: could not query chainstate to find parent consensus hash of {consensus_hash}/{}",
809
                    &anchored_block.block_hash()
×
810
                )
811
            })
UNCOV
812
            .unwrap_or_else(|| {
×
813
                panic!(
×
814
                    "BUG: no such parent of block {consensus_hash}/{}",
815
                    &anchored_block.block_hash()
×
816
                )
817
            });
818

819
            // Preprocess the anchored block
UNCOV
820
            self.chain_state
×
UNCOV
821
                .preprocess_anchored_block(
×
UNCOV
822
                    &ic,
×
UNCOV
823
                    consensus_hash,
×
UNCOV
824
                    anchored_block,
×
UNCOV
825
                    &parent_consensus_hash,
×
826
                    0,
827
                )
UNCOV
828
                .unwrap();
×
829

830
            // Preprocess the microblocks
UNCOV
831
            for microblock in microblocks.iter() {
×
832
                let res = self
×
833
                    .chain_state
×
834
                    .preprocess_streamed_microblock(
×
835
                        consensus_hash,
×
836
                        &anchored_block.block_hash(),
×
837
                        microblock,
×
838
                    )
839
                    .unwrap();
×
840
                if !res {
×
841
                    warn!(
×
842
                        "Unhandled error while pre-processing microblock {}",
843
                        microblock.header.block_hash()
×
844
                    );
845
                }
×
846
            }
847

UNCOV
848
            parent_consensus_hash
×
849
        };
850

UNCOV
851
        let atlas_config = Self::make_atlas_config();
×
UNCOV
852
        let mut processed_blocks = vec![];
×
853
        loop {
UNCOV
854
            let mut process_blocks_at_tip = {
×
UNCOV
855
                let tx = db.tx_begin_at_tip();
×
UNCOV
856
                self.chain_state
×
UNCOV
857
                    .process_blocks(tx, 1, Some(&self.event_dispatcher))
×
858
            };
UNCOV
859
            match process_blocks_at_tip {
×
860
                Err(e) => panic!("Error while processing block - {e:?}"),
×
UNCOV
861
                Ok(ref mut blocks) => {
×
UNCOV
862
                    if blocks.is_empty() {
×
UNCOV
863
                        break;
×
864
                    } else {
UNCOV
865
                        for block in blocks.iter() {
×
UNCOV
866
                            if let (Some(epoch_receipt), _) = block {
×
UNCOV
867
                                let attachments_instances =
×
UNCOV
868
                                    self.get_attachment_instances(epoch_receipt, &atlas_config);
×
UNCOV
869
                                if !attachments_instances.is_empty() {
×
870
                                    for new_attachment in attachments_instances.into_iter() {
×
871
                                        if let Err(e) =
×
872
                                            atlas_db.queue_attachment_instance(&new_attachment)
×
873
                                        {
874
                                            warn!(
×
875
                                                "Atlas: Error writing attachment instance to DB";
876
                                                "err" => ?e,
877
                                                "index_block_hash" => %new_attachment.index_block_hash,
878
                                                "contract_id" => %new_attachment.contract_id,
879
                                                "attachment_index" => %new_attachment.attachment_index,
880
                                            );
881
                                        }
×
882
                                    }
UNCOV
883
                                }
×
884
                            }
×
885
                        }
886

UNCOV
887
                        processed_blocks.append(blocks);
×
888
                    }
889
                }
890
            }
891
        }
892

893
        // todo(ludo): yikes but good enough in the context of helium:
894
        // we only expect 1 block.
UNCOV
895
        let processed_block = processed_blocks[0].clone().0.unwrap();
×
896

UNCOV
897
        let mut cost_estimator = self.config.make_cost_estimator();
×
UNCOV
898
        let mut fee_estimator = self.config.make_fee_estimator();
×
899

UNCOV
900
        let stacks_epoch =
×
UNCOV
901
            SortitionDB::get_stacks_epoch_by_epoch_id(db.conn(), &processed_block.evaluated_epoch)
×
UNCOV
902
                .expect("FATAL: could not query sortition DB for epochs")
×
UNCOV
903
                .expect("Could not find a stacks epoch.");
×
UNCOV
904
        if let Some(estimator) = cost_estimator.as_mut() {
×
UNCOV
905
            estimator.notify_block(
×
UNCOV
906
                &processed_block.tx_receipts,
×
UNCOV
907
                &stacks_epoch.block_limit,
×
UNCOV
908
                &stacks_epoch.epoch_id,
×
UNCOV
909
            );
×
UNCOV
910
        }
×
911

UNCOV
912
        if let Some(estimator) = fee_estimator.as_mut() {
×
UNCOV
913
            if let Err(e) = estimator.notify_block(&processed_block, &stacks_epoch.block_limit) {
×
914
                warn!("FeeEstimator failed to process block receipt";
×
915
                      "stacks_block_hash" => %processed_block.header.anchored_header.block_hash(),
×
916
                      "stacks_height" => %processed_block.header.stacks_block_height,
917
                      "error" => %e);
UNCOV
918
            }
×
919
        }
×
920

921
        // Handle events
UNCOV
922
        let receipts = processed_block.tx_receipts;
×
UNCOV
923
        let metadata = processed_block.header;
×
UNCOV
924
        let block: StacksBlock = {
×
UNCOV
925
            let block_path = StacksChainState::get_block_path(
×
UNCOV
926
                &self.chain_state.blocks_path,
×
UNCOV
927
                &metadata.consensus_hash,
×
UNCOV
928
                &metadata.anchored_header.block_hash(),
×
929
            )
UNCOV
930
            .unwrap();
×
UNCOV
931
            StacksChainState::consensus_load(&block_path).unwrap()
×
932
        };
933

UNCOV
934
        let chain_tip = ChainTip {
×
UNCOV
935
            metadata,
×
UNCOV
936
            block,
×
UNCOV
937
            receipts,
×
UNCOV
938
        };
×
UNCOV
939
        self.chain_tip = Some(chain_tip.clone());
×
940

941
        // Unset the `bootstraping_chain` flag.
UNCOV
942
        if self.bootstraping_chain {
×
UNCOV
943
            self.bootstraping_chain = false;
×
UNCOV
944
        }
×
945

UNCOV
946
        chain_tip
×
UNCOV
947
    }
×
948

UNCOV
949
    pub fn get_attachment_instances(
×
UNCOV
950
        &self,
×
UNCOV
951
        epoch_receipt: &StacksEpochReceipt,
×
UNCOV
952
        atlas_config: &AtlasConfig,
×
UNCOV
953
    ) -> HashSet<AttachmentInstance> {
×
UNCOV
954
        let mut attachments_instances = HashSet::new();
×
UNCOV
955
        for receipt in epoch_receipt.tx_receipts.iter() {
×
UNCOV
956
            if let TransactionOrigin::Stacks(ref transaction) = receipt.transaction {
×
UNCOV
957
                if let TransactionPayload::ContractCall(ref contract_call) = transaction.payload {
×
UNCOV
958
                    let contract_id = contract_call.to_clarity_contract_id();
×
UNCOV
959
                    if atlas_config.contracts.contains(&contract_id) {
×
960
                        for event in receipt.events.iter() {
×
961
                            if let StacksTransactionEvent::SmartContractEvent(ref event_data) =
×
962
                                event
×
963
                            {
964
                                let res = AttachmentInstance::try_new_from_value(
×
965
                                    &event_data.value,
×
966
                                    &contract_id,
×
967
                                    epoch_receipt.header.index_block_hash(),
×
968
                                    epoch_receipt.header.stacks_block_height,
×
969
                                    receipt.transaction.txid(),
×
970
                                    self.chain_tip
×
971
                                        .as_ref()
×
972
                                        .map(|t| t.metadata.stacks_block_height),
×
973
                                );
974
                                if let Some(attachment_instance) = res {
×
975
                                    attachments_instances.insert(attachment_instance);
×
976
                                }
×
977
                            }
×
978
                        }
UNCOV
979
                    }
×
UNCOV
980
                }
×
981
            }
×
982
        }
UNCOV
983
        attachments_instances
×
UNCOV
984
    }
×
985

986
    /// Constructs and returns a LeaderKeyRegisterOp out of the provided params
UNCOV
987
    fn generate_leader_key_register_op(
×
UNCOV
988
        &mut self,
×
UNCOV
989
        vrf_public_key: VRFPublicKey,
×
UNCOV
990
        consensus_hash: ConsensusHash,
×
UNCOV
991
    ) -> BlockstackOperationType {
×
UNCOV
992
        let mut txid_bytes = [0u8; 32];
×
UNCOV
993
        let mut rng = rand::thread_rng();
×
UNCOV
994
        rng.fill_bytes(&mut txid_bytes);
×
UNCOV
995
        let txid = Txid(txid_bytes);
×
996

UNCOV
997
        BlockstackOperationType::LeaderKeyRegister(LeaderKeyRegisterOp {
×
UNCOV
998
            public_key: vrf_public_key,
×
UNCOV
999
            memo: vec![],
×
UNCOV
1000
            consensus_hash,
×
UNCOV
1001
            vtxindex: 1,
×
UNCOV
1002
            txid,
×
UNCOV
1003
            block_height: 0,
×
UNCOV
1004
            burn_header_hash: BurnchainHeaderHash::zero(),
×
UNCOV
1005
        })
×
UNCOV
1006
    }
×
1007

1008
    /// Constructs and returns a LeaderBlockCommitOp out of the provided params
UNCOV
1009
    fn generate_block_commit_op(
×
UNCOV
1010
        &mut self,
×
UNCOV
1011
        block_header_hash: BlockHeaderHash,
×
UNCOV
1012
        burn_fee: u64,
×
UNCOV
1013
        key: &RegisteredKey,
×
UNCOV
1014
        burnchain_tip: &BurnchainTip,
×
UNCOV
1015
        vrf_seed: VRFSeed,
×
UNCOV
1016
    ) -> BlockstackOperationType {
×
UNCOV
1017
        let winning_tx_vtindex = burnchain_tip.get_winning_tx_index().unwrap_or(0);
×
1018

UNCOV
1019
        let (parent_block_ptr, parent_vtxindex) = match self.bootstraping_chain {
×
UNCOV
1020
            true => (0, 0), // parent_block_ptr and parent_vtxindex should both be 0 on block #1
×
UNCOV
1021
            false => (
×
UNCOV
1022
                burnchain_tip.block_snapshot.block_height as u32,
×
UNCOV
1023
                winning_tx_vtindex as u16,
×
UNCOV
1024
            ),
×
1025
        };
1026

UNCOV
1027
        let burnchain = self.config.get_burnchain();
×
UNCOV
1028
        let commit_outs = if burnchain_tip.block_snapshot.block_height + 1
×
UNCOV
1029
            < burnchain.pox_constants.sunset_end
×
UNCOV
1030
            && !burnchain.is_in_prepare_phase(burnchain_tip.block_snapshot.block_height + 1)
×
1031
        {
UNCOV
1032
            RewardSetInfo::into_commit_outs(None, self.config.is_mainnet())
×
1033
        } else {
UNCOV
1034
            vec![PoxAddress::standard_burn_address(self.config.is_mainnet())]
×
1035
        };
1036

UNCOV
1037
        let burn_parent_modulus =
×
UNCOV
1038
            (burnchain_tip.block_snapshot.block_height % BURN_BLOCK_MINED_AT_MODULUS) as u8;
×
1039

UNCOV
1040
        let mut txid_bytes = [0u8; 32];
×
UNCOV
1041
        let mut rng = rand::thread_rng();
×
UNCOV
1042
        rng.fill_bytes(&mut txid_bytes);
×
UNCOV
1043
        let txid = Txid(txid_bytes);
×
1044

UNCOV
1045
        BlockstackOperationType::LeaderBlockCommit(LeaderBlockCommitOp {
×
UNCOV
1046
            treatment: vec![],
×
UNCOV
1047
            sunset_burn: 0,
×
UNCOV
1048
            block_header_hash,
×
UNCOV
1049
            burn_fee,
×
UNCOV
1050
            input: (Txid([0; 32]), 0),
×
UNCOV
1051
            apparent_sender: self.keychain.get_burnchain_signer(),
×
UNCOV
1052
            key_block_ptr: key.block_height as u32,
×
UNCOV
1053
            key_vtxindex: key.op_vtxindex as u16,
×
UNCOV
1054
            memo: vec![STACKS_EPOCH_2_1_MARKER],
×
UNCOV
1055
            new_seed: vrf_seed,
×
UNCOV
1056
            parent_block_ptr,
×
UNCOV
1057
            parent_vtxindex,
×
UNCOV
1058
            vtxindex: 2,
×
UNCOV
1059
            txid,
×
UNCOV
1060
            commit_outs,
×
UNCOV
1061
            block_height: 0,
×
UNCOV
1062
            burn_header_hash: BurnchainHeaderHash::zero(),
×
UNCOV
1063
            burn_parent_modulus,
×
UNCOV
1064
        })
×
UNCOV
1065
    }
×
1066

1067
    // Constructs a coinbase transaction
UNCOV
1068
    fn generate_coinbase_tx(&mut self, is_mainnet: bool) -> StacksTransaction {
×
UNCOV
1069
        let mut tx_auth = self.keychain.get_transaction_auth().unwrap();
×
UNCOV
1070
        tx_auth.set_origin_nonce(self.nonce);
×
1071

UNCOV
1072
        let version = if is_mainnet {
×
1073
            TransactionVersion::Mainnet
×
1074
        } else {
UNCOV
1075
            TransactionVersion::Testnet
×
1076
        };
UNCOV
1077
        let mut tx = StacksTransaction::new(
×
UNCOV
1078
            version,
×
UNCOV
1079
            tx_auth,
×
UNCOV
1080
            TransactionPayload::Coinbase(CoinbasePayload([0u8; 32]), None, None),
×
1081
        );
UNCOV
1082
        tx.chain_id = self.config.burnchain.chain_id;
×
UNCOV
1083
        tx.anchor_mode = TransactionAnchorMode::OnChainOnly;
×
UNCOV
1084
        let mut tx_signer = StacksTransactionSigner::new(&tx);
×
UNCOV
1085
        self.keychain.sign_as_origin(&mut tx_signer);
×
1086

1087
        // Increment nonce
UNCOV
1088
        self.nonce += 1;
×
1089

UNCOV
1090
        tx_signer.get_tx().unwrap()
×
UNCOV
1091
    }
×
1092
}
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