Skip to content

Commit e5ad64f

Browse files
committed
chore: Allow optional config to command_replay_mock_mining()
1 parent 674cb5f commit e5ad64f

File tree

3 files changed

+95
-25
lines changed

3 files changed

+95
-25
lines changed

stackslib/src/cli.rs

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
//! Subcommands used by `stacks-inspect` binary
1818
19+
use std::cell::LazyCell;
1920
use std::path::PathBuf;
2021
use std::time::Instant;
2122
use std::{env, fs, io, process, thread};
@@ -39,10 +40,40 @@ use crate::clarity_vm::clarity::ClarityInstance;
3940
use crate::core::*;
4041
use crate::util_lib::db::IndexDBTx;
4142

43+
/// Can be used with CLI commands to support non-mainnet chainstate
44+
/// Allows integration testing of these functions
45+
pub struct StacksChainConfig {
46+
pub chain_id: u32,
47+
pub first_block_height: u64,
48+
pub first_burn_header_hash: BurnchainHeaderHash,
49+
pub first_burn_header_timestamp: u64,
50+
pub pox_constants: PoxConstants,
51+
pub epochs: Vec<StacksEpoch>,
52+
}
53+
54+
impl StacksChainConfig {
55+
pub fn default_mainnet() -> Self {
56+
Self {
57+
chain_id: CHAIN_ID_MAINNET,
58+
first_block_height: BITCOIN_MAINNET_FIRST_BLOCK_HEIGHT,
59+
first_burn_header_hash: BurnchainHeaderHash::from_hex(BITCOIN_MAINNET_FIRST_BLOCK_HASH)
60+
.unwrap(),
61+
first_burn_header_timestamp: BITCOIN_MAINNET_FIRST_BLOCK_TIMESTAMP.into(),
62+
pox_constants: PoxConstants::mainnet_default(),
63+
epochs: STACKS_EPOCHS_MAINNET.to_vec(),
64+
}
65+
}
66+
}
67+
68+
const STACKS_CHAIN_CONFIG_DEFAULT_MAINNET: LazyCell<StacksChainConfig> =
69+
LazyCell::new(StacksChainConfig::default_mainnet);
70+
4271
/// Replay blocks from chainstate database
43-
/// Takes args in CLI format: `<command-name> [args...]`
4472
/// Terminates on error using `process::exit()`
45-
pub fn command_replay_block(argv: &[String]) {
73+
///
74+
/// Arguments:
75+
/// - `argv`: Args in CLI format: `<command-name> [args...]`
76+
pub fn command_replay_block(argv: &[String], conf: Option<&StacksChainConfig>) {
4677
let print_help_and_exit = || -> ! {
4778
let n = &argv[0];
4879
eprintln!("Usage:");
@@ -110,15 +141,18 @@ pub fn command_replay_block(argv: &[String]) {
110141
if i % 100 == 0 {
111142
println!("Checked {i}...");
112143
}
113-
replay_staging_block(db_path, index_block_hash);
144+
replay_staging_block(db_path, index_block_hash, conf);
114145
}
115146
println!("Finished. run_time_seconds = {}", start.elapsed().as_secs());
116147
}
117148

118149
/// Replay mock mined blocks from JSON files
119-
/// Takes args in CLI format: `<command-name> [args...]`
120150
/// Terminates on error using `process::exit()`
121-
pub fn command_replay_mock_mining(argv: &[String]) {
151+
///
152+
/// Arguments:
153+
/// - `argv`: Args in CLI format: `<command-name> [args...]`
154+
/// - `conf`: Optional config for running on non-mainnet chainstate
155+
pub fn command_replay_mock_mining(argv: &[String], conf: Option<&StacksChainConfig>) {
122156
let print_help_and_exit = || -> ! {
123157
let n = &argv[0];
124158
eprintln!("Usage:");
@@ -202,28 +236,36 @@ pub fn command_replay_mock_mining(argv: &[String]) {
202236
"block_height" => bh,
203237
"block" => ?block
204238
);
205-
replay_mock_mined_block(&db_path, block);
239+
replay_mock_mined_block(&db_path, block, conf);
206240
}
207241
}
208242

209243
/// Fetch and process a `StagingBlock` from database and call `replay_block()` to validate
210-
fn replay_staging_block(db_path: &str, index_block_hash_hex: &str) {
244+
fn replay_staging_block(
245+
db_path: &str,
246+
index_block_hash_hex: &str,
247+
conf: Option<&StacksChainConfig>,
248+
) {
211249
let block_id = StacksBlockId::from_hex(index_block_hash_hex).unwrap();
212250
let chain_state_path = format!("{db_path}/chainstate/");
213251
let sort_db_path = format!("{db_path}/burnchain/sortition");
214252
let burn_db_path = format!("{db_path}/burnchain/burnchain.sqlite");
215253
let burnchain_blocks_db = BurnchainDB::open(&burn_db_path, false).unwrap();
216254

255+
let default_conf = STACKS_CHAIN_CONFIG_DEFAULT_MAINNET;
256+
let conf = conf.unwrap_or(&default_conf);
257+
258+
let mainnet = conf.chain_id == CHAIN_ID_MAINNET;
217259
let (mut chainstate, _) =
218-
StacksChainState::open(true, CHAIN_ID_MAINNET, &chain_state_path, None).unwrap();
260+
StacksChainState::open(mainnet, conf.chain_id, &chain_state_path, None).unwrap();
219261

220262
let mut sortdb = SortitionDB::connect(
221263
&sort_db_path,
222-
BITCOIN_MAINNET_FIRST_BLOCK_HEIGHT,
223-
&BurnchainHeaderHash::from_hex(BITCOIN_MAINNET_FIRST_BLOCK_HASH).unwrap(),
224-
BITCOIN_MAINNET_FIRST_BLOCK_TIMESTAMP.into(),
225-
STACKS_EPOCHS_MAINNET.as_ref(),
226-
PoxConstants::mainnet_default(),
264+
conf.first_block_height,
265+
&conf.first_burn_header_hash,
266+
conf.first_burn_header_timestamp,
267+
&conf.epochs,
268+
conf.pox_constants.clone(),
227269
None,
228270
true,
229271
)
@@ -277,22 +319,30 @@ fn replay_staging_block(db_path: &str, index_block_hash_hex: &str) {
277319
}
278320

279321
/// Process a mock mined block and call `replay_block()` to validate
280-
fn replay_mock_mined_block(db_path: &str, block: AssembledAnchorBlock) {
322+
fn replay_mock_mined_block(
323+
db_path: &str,
324+
block: AssembledAnchorBlock,
325+
conf: Option<&StacksChainConfig>,
326+
) {
281327
let chain_state_path = format!("{db_path}/chainstate/");
282328
let sort_db_path = format!("{db_path}/burnchain/sortition");
283329
let burn_db_path = format!("{db_path}/burnchain/burnchain.sqlite");
284330
let burnchain_blocks_db = BurnchainDB::open(&burn_db_path, false).unwrap();
285331

332+
let default_conf = STACKS_CHAIN_CONFIG_DEFAULT_MAINNET;
333+
let conf = conf.unwrap_or(&default_conf);
334+
335+
let mainnet = conf.chain_id == CHAIN_ID_MAINNET;
286336
let (mut chainstate, _) =
287-
StacksChainState::open(true, CHAIN_ID_MAINNET, &chain_state_path, None).unwrap();
337+
StacksChainState::open(mainnet, conf.chain_id, &chain_state_path, None).unwrap();
288338

289339
let mut sortdb = SortitionDB::connect(
290340
&sort_db_path,
291-
BITCOIN_MAINNET_FIRST_BLOCK_HEIGHT,
292-
&BurnchainHeaderHash::from_hex(BITCOIN_MAINNET_FIRST_BLOCK_HASH).unwrap(),
293-
BITCOIN_MAINNET_FIRST_BLOCK_TIMESTAMP.into(),
294-
STACKS_EPOCHS_MAINNET.as_ref(),
295-
PoxConstants::mainnet_default(),
341+
conf.first_block_height,
342+
&conf.first_burn_header_hash,
343+
conf.first_burn_header_timestamp,
344+
&conf.epochs,
345+
conf.pox_constants.clone(),
296346
None,
297347
true,
298348
)

stackslib/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,12 +1255,12 @@ simulating a miner.
12551255
}
12561256

12571257
if argv[1] == "replay-block" {
1258-
cli::command_replay_block(&argv[1..]);
1258+
cli::command_replay_block(&argv[1..], None);
12591259
process::exit(0);
12601260
}
12611261

12621262
if argv[1] == "replay-mock-mining" {
1263-
cli::command_replay_mock_mining(&argv[1..]);
1263+
cli::command_replay_mock_mining(&argv[1..], None);
12641264
process::exit(0);
12651265
}
12661266

testnet/stacks-node/src/tests/neon_integrations.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::sync::{mpsc, Arc};
55
use std::time::{Duration, Instant};
66
use std::{cmp, env, fs, io, thread};
77

8+
use clarity::consts::BITCOIN_REGTEST_FIRST_BLOCK_TIMESTAMP;
89
use clarity::vm::ast::stack_depth_checker::AST_CALL_STACK_DEPTH_BUFFER;
910
use clarity::vm::ast::ASTRules;
1011
use clarity::vm::costs::ExecutionCost;
@@ -38,7 +39,7 @@ use stacks::chainstate::stacks::{
3839
StacksPublicKey, StacksTransaction, TransactionContractCall, TransactionPayload,
3940
};
4041
use stacks::clarity_cli::vm_execute as execute;
41-
use stacks::cli;
42+
use stacks::cli::{self, StacksChainConfig};
4243
use stacks::codec::StacksMessageCodec;
4344
use stacks::core::mempool::MemPoolWalkTxTypes;
4445
use stacks::core::{
@@ -12627,7 +12628,13 @@ fn mock_miner_replay() {
1262712628
.start_bitcoind()
1262812629
.expect("Failed starting bitcoind");
1262912630

12630-
let mut btc_regtest_controller = BitcoinRegtestController::new(conf.clone(), None);
12631+
let burnchain_config = Burnchain::regtest(&conf.get_burn_db_path());
12632+
let mut btc_regtest_controller = BitcoinRegtestController::with_burnchain(
12633+
conf.clone(),
12634+
None,
12635+
Some(burnchain_config.clone()),
12636+
None,
12637+
);
1263112638

1263212639
btc_regtest_controller.bootstrap_chain(201);
1263312640

@@ -12756,9 +12763,22 @@ fn mock_miner_replay() {
1275612763
let blocks_dir = blocks_dir.into_os_string().into_string().unwrap();
1275712764
let db_path = format!("{}/neon", conf.node.working_dir);
1275812765
let args: Vec<String> = vec!["replay-mock-mining".into(), db_path, blocks_dir];
12766+
let SortitionDB {
12767+
first_block_height,
12768+
first_burn_header_hash,
12769+
..
12770+
} = *btc_regtest_controller.sortdb_mut();
12771+
let replay_config = StacksChainConfig {
12772+
chain_id: conf.burnchain.chain_id,
12773+
first_block_height,
12774+
first_burn_header_hash,
12775+
first_burn_header_timestamp: BITCOIN_REGTEST_FIRST_BLOCK_TIMESTAMP.into(),
12776+
pox_constants: burnchain_config.pox_constants,
12777+
epochs: conf.burnchain.epochs.expect("Missing `epochs` in config"),
12778+
};
1275912779

1276012780
info!("Replaying mock mined blocks...");
12761-
cli::command_replay_mock_mining(&args);
12781+
cli::command_replay_mock_mining(&args, Some(&replay_config));
1276212782

1276312783
// ---------- Test finished, clean up ----------
1276412784

0 commit comments

Comments
 (0)