Skip to content

Commit f949691

Browse files
committed
feat: Neon mock miner can write blocks to files
1 parent d8d3337 commit f949691

File tree

1 file changed

+58
-23
lines changed

1 file changed

+58
-23
lines changed

testnet/stacks-node/src/neon_node.rs

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,14 @@
140140
use std::cmp;
141141
use std::cmp::Ordering as CmpOrdering;
142142
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
143-
use std::io::{Read, Write};
143+
use std::fs::{self, File};
144+
use std::io::{BufWriter, Read, Write};
144145
use std::net::SocketAddr;
146+
use std::path::Path;
145147
use std::sync::mpsc::{Receiver, TrySendError};
146148
use std::thread::JoinHandle;
147149
use std::time::Duration;
148-
use std::{fs, mem, thread};
150+
use std::{mem, thread};
149151

150152
use clarity::vm::ast::ASTRules;
151153
use clarity::vm::costs::ExecutionCost;
@@ -237,7 +239,7 @@ pub(crate) enum MinerThreadResult {
237239
/// Fully-assembled Stacks anchored, block as well as some extra metadata pertaining to how it was
238240
/// linked to the burnchain and what view(s) the miner had of the burnchain before and after
239241
/// completing the block.
240-
#[derive(Clone)]
242+
#[derive(Clone, Serialize)]
241243
pub struct AssembledAnchorBlock {
242244
/// Consensus hash of the parent Stacks block
243245
parent_consensus_hash: ConsensusHash,
@@ -255,6 +257,28 @@ pub struct AssembledAnchorBlock {
255257
tenure_begin: u128,
256258
}
257259

260+
/// Write any `serde_json` object to a file
261+
/// TODO: Move this somewhere else
262+
pub fn serialize_json_to_file<J, P>(json: &J, filepath: P) -> Result<(), std::io::Error>
263+
where
264+
J: ?Sized + serde::Serialize,
265+
P: AsRef<Path>,
266+
{
267+
let file = File::create(filepath)?;
268+
let mut writer = BufWriter::new(file);
269+
serde_json::to_writer(&mut writer, json)?;
270+
writer.flush()
271+
}
272+
273+
impl AssembledAnchorBlock {
274+
pub fn serialize_to_file<P>(&self, filepath: P) -> Result<(), std::io::Error>
275+
where
276+
P: AsRef<Path>,
277+
{
278+
serialize_json_to_file(self, filepath)
279+
}
280+
}
281+
258282
/// Miner chain tip, on top of which to build microblocks
259283
#[derive(Debug, Clone, PartialEq)]
260284
pub struct MinerTip {
@@ -2567,28 +2591,44 @@ impl BlockMinerThread {
25672591
"attempt" => attempt
25682592
);
25692593

2594+
let NodeConfig {
2595+
mock_mining,
2596+
mock_mining_output_dir,
2597+
..
2598+
} = self.config.get_node_config(false);
2599+
25702600
let res = bitcoin_controller.submit_operation(target_epoch_id, op, &mut op_signer, attempt);
2601+
let assembled_block = AssembledAnchorBlock {
2602+
parent_consensus_hash: parent_block_info.parent_consensus_hash,
2603+
my_burn_hash: cur_burn_chain_tip.burn_header_hash,
2604+
my_block_height: cur_burn_chain_tip.block_height,
2605+
orig_burn_hash: self.burn_block.burn_header_hash,
2606+
anchored_block,
2607+
attempt,
2608+
tenure_begin,
2609+
};
25712610
if res.is_none() {
25722611
self.failed_to_submit_last_attempt = true;
2573-
if !self.config.get_node_config(false).mock_mining {
2612+
if mock_mining {
2613+
debug!("Relayer: Mock-mining enabled; not sending Bitcoin transaction");
2614+
if let Some(dir) = mock_mining_output_dir {
2615+
let stacks_block_height = assembled_block.anchored_block.header.total_work.work;
2616+
let filename = format!("{stacks_block_height}.json");
2617+
let filepath = dir.join(filename);
2618+
assembled_block
2619+
.serialize_to_file(&filepath)
2620+
.unwrap_or_else(|e| panic!("Failed to write to file '{filepath:?}': {e}"));
2621+
}
2622+
} else {
25742623
warn!("Relayer: Failed to submit Bitcoin transaction");
25752624
return None;
25762625
}
2577-
debug!("Relayer: Mock-mining enabled; not sending Bitcoin transaction");
25782626
} else {
25792627
self.failed_to_submit_last_attempt = false;
25802628
}
25812629

25822630
Some(MinerThreadResult::Block(
2583-
AssembledAnchorBlock {
2584-
parent_consensus_hash: parent_block_info.parent_consensus_hash,
2585-
my_burn_hash: cur_burn_chain_tip.burn_header_hash,
2586-
my_block_height: cur_burn_chain_tip.block_height,
2587-
orig_burn_hash: self.burn_block.burn_header_hash,
2588-
anchored_block,
2589-
attempt,
2590-
tenure_begin,
2591-
},
2631+
assembled_block,
25922632
microblock_private_key,
25932633
bitcoin_controller.get_ongoing_commit(),
25942634
))
@@ -3721,11 +3761,9 @@ impl RelayerThread {
37213761
parent_consensus_hash, parent_block_hash
37223762
);
37233763

3724-
let mut microblock_thread_state = match MicroblockMinerThread::from_relayer_thread(self) {
3725-
Some(ts) => ts,
3726-
None => {
3727-
return false;
3728-
}
3764+
let Some(mut microblock_thread_state) = MicroblockMinerThread::from_relayer_thread(self)
3765+
else {
3766+
return false;
37293767
};
37303768

37313769
if let Ok(miner_handle) = thread::Builder::new()
@@ -3737,10 +3775,7 @@ impl RelayerThread {
37373775
miner_tip,
37383776
))
37393777
})
3740-
.map_err(|e| {
3741-
error!("Relayer: Failed to start tenure thread: {:?}", &e);
3742-
e
3743-
})
3778+
.inspect_err(|e| error!("Relayer: Failed to start tenure thread: {e:?}"))
37443779
{
37453780
// thread started!
37463781
self.miner_thread = Some(miner_handle);

0 commit comments

Comments
 (0)