140
140
use std:: cmp;
141
141
use std:: cmp:: Ordering as CmpOrdering ;
142
142
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 } ;
144
145
use std:: net:: SocketAddr ;
146
+ use std:: path:: Path ;
145
147
use std:: sync:: mpsc:: { Receiver , TrySendError } ;
146
148
use std:: thread:: JoinHandle ;
147
149
use std:: time:: Duration ;
148
- use std:: { fs , mem, thread} ;
150
+ use std:: { mem, thread} ;
149
151
150
152
use clarity:: vm:: ast:: ASTRules ;
151
153
use clarity:: vm:: costs:: ExecutionCost ;
@@ -237,7 +239,7 @@ pub(crate) enum MinerThreadResult {
237
239
/// Fully-assembled Stacks anchored, block as well as some extra metadata pertaining to how it was
238
240
/// linked to the burnchain and what view(s) the miner had of the burnchain before and after
239
241
/// completing the block.
240
- #[ derive( Clone ) ]
242
+ #[ derive( Clone , Serialize ) ]
241
243
pub struct AssembledAnchorBlock {
242
244
/// Consensus hash of the parent Stacks block
243
245
parent_consensus_hash : ConsensusHash ,
@@ -255,6 +257,28 @@ pub struct AssembledAnchorBlock {
255
257
tenure_begin : u128 ,
256
258
}
257
259
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
+
258
282
/// Miner chain tip, on top of which to build microblocks
259
283
#[ derive( Debug , Clone , PartialEq ) ]
260
284
pub struct MinerTip {
@@ -2567,28 +2591,44 @@ impl BlockMinerThread {
2567
2591
"attempt" => attempt
2568
2592
) ;
2569
2593
2594
+ let NodeConfig {
2595
+ mock_mining,
2596
+ mock_mining_output_dir,
2597
+ ..
2598
+ } = self . config . get_node_config ( false ) ;
2599
+
2570
2600
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
+ } ;
2571
2610
if res. is_none ( ) {
2572
2611
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 {
2574
2623
warn ! ( "Relayer: Failed to submit Bitcoin transaction" ) ;
2575
2624
return None ;
2576
2625
}
2577
- debug ! ( "Relayer: Mock-mining enabled; not sending Bitcoin transaction" ) ;
2578
2626
} else {
2579
2627
self . failed_to_submit_last_attempt = false ;
2580
2628
}
2581
2629
2582
2630
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,
2592
2632
microblock_private_key,
2593
2633
bitcoin_controller. get_ongoing_commit ( ) ,
2594
2634
) )
@@ -3721,11 +3761,9 @@ impl RelayerThread {
3721
3761
parent_consensus_hash, parent_block_hash
3722
3762
) ;
3723
3763
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 ;
3729
3767
} ;
3730
3768
3731
3769
if let Ok ( miner_handle) = thread:: Builder :: new ( )
@@ -3737,10 +3775,7 @@ impl RelayerThread {
3737
3775
miner_tip,
3738
3776
) )
3739
3777
} )
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:?}" ) )
3744
3779
{
3745
3780
// thread started!
3746
3781
self . miner_thread = Some ( miner_handle) ;
0 commit comments