@@ -26,6 +26,8 @@ use clarity::types::chainstate::StacksPrivateKey;
26
26
use clarity:: types:: { PrivateKey , StacksEpochId } ;
27
27
use clarity:: util:: hash:: MerkleHashFunc ;
28
28
use clarity:: util:: secp256k1:: Secp256k1PublicKey ;
29
+ #[ cfg( any( test, feature = "testing" ) ) ]
30
+ use lazy_static:: lazy_static;
29
31
use libsigner:: v0:: messages:: {
30
32
BlockAccepted , BlockRejection , BlockResponse , MessageSlotID , MockProposal , MockSignature ,
31
33
RejectCode , SignerMessage ,
@@ -35,6 +37,8 @@ use slog::{slog_debug, slog_error, slog_info, slog_warn};
35
37
use stacks_common:: types:: chainstate:: StacksAddress ;
36
38
use stacks_common:: util:: get_epoch_time_secs;
37
39
use stacks_common:: util:: secp256k1:: MessageSignature ;
40
+ #[ cfg( any( test, feature = "testing" ) ) ]
41
+ use stacks_common:: util:: TestFlag ;
38
42
use stacks_common:: { debug, error, info, warn} ;
39
43
40
44
use crate :: chainstate:: { ProposalEvalConfig , SortitionsView } ;
@@ -45,29 +49,28 @@ use crate::signerdb::{BlockInfo, BlockState, SignerDb};
45
49
use crate :: Signer as SignerTrait ;
46
50
47
51
#[ cfg( any( test, feature = "testing" ) ) ]
48
- /// A global variable that can be used to reject all block proposals if the signer's public key is in the provided list
49
- pub static TEST_REJECT_ALL_BLOCK_PROPOSAL : std:: sync:: Mutex <
50
- Option < Vec < stacks_common:: types:: chainstate:: StacksPublicKey > > ,
51
- > = std:: sync:: Mutex :: new ( None ) ;
52
-
53
- #[ cfg( any( test, feature = "testing" ) ) ]
54
- /// A global variable that can be used to ignore block proposals if the signer's public key is in the provided list
55
- pub static TEST_IGNORE_ALL_BLOCK_PROPOSALS : std:: sync:: Mutex <
56
- Option < Vec < stacks_common:: types:: chainstate:: StacksPublicKey > > ,
57
- > = std:: sync:: Mutex :: new ( None ) ;
52
+ lazy_static ! {
53
+ /// A global variable that can be used to reject all block proposals if the signer's public key is in the provided list
54
+ pub static ref TEST_REJECT_ALL_BLOCK_PROPOSAL : TestFlag <Vec <stacks_common:: types:: chainstate:: StacksPublicKey >> = TestFlag :: default ( ) ;
55
+ }
58
56
59
57
#[ cfg( any( test, feature = "testing" ) ) ]
60
- /// Pause the block broadcast
61
- pub static TEST_PAUSE_BLOCK_BROADCAST : std:: sync:: Mutex < Option < bool > > = std:: sync:: Mutex :: new ( None ) ;
58
+ lazy_static ! {
59
+ /// A global variable that can be used to ignore block proposals if the signer's public key is in the provided list
60
+ pub static ref TEST_IGNORE_ALL_BLOCK_PROPOSALS : TestFlag <Vec <stacks_common:: types:: chainstate:: StacksPublicKey >> = TestFlag :: default ( ) ;
61
+ }
62
62
63
63
#[ cfg( any( test, feature = "testing" ) ) ]
64
- /// Skip broadcasting the block to the network
65
- pub static TEST_SKIP_BLOCK_BROADCAST : std:: sync:: Mutex < Option < bool > > = std:: sync:: Mutex :: new ( None ) ;
64
+ lazy_static ! {
65
+ /// Pause the block broadcast
66
+ pub static ref TEST_PAUSE_BLOCK_BROADCAST : TestFlag <bool > = TestFlag :: default ( ) ;
67
+ }
66
68
67
69
#[ cfg( any( test, feature = "testing" ) ) ]
68
- /// Skip any block responses from other signers
69
- pub static TEST_IGNORE_BLOCK_RESPONSES : std:: sync:: Mutex < Option < bool > > =
70
- std:: sync:: Mutex :: new ( None ) ;
70
+ lazy_static ! {
71
+ /// Skip broadcasting the block to the network
72
+ pub static ref TEST_SKIP_BLOCK_BROADCAST : TestFlag <bool > = TestFlag :: default ( ) ;
73
+ }
71
74
72
75
/// The stacks signer registered for the reward cycle
73
76
#[ derive( Debug ) ]
@@ -174,9 +177,8 @@ impl SignerTrait<SignerMessage> for Signer {
174
177
match message {
175
178
SignerMessage :: BlockProposal ( block_proposal) => {
176
179
#[ cfg( any( test, feature = "testing" ) ) ]
177
- if let Some ( public_keys) =
178
- & * TEST_IGNORE_ALL_BLOCK_PROPOSALS . lock ( ) . unwrap ( )
179
180
{
181
+ let public_keys = TEST_IGNORE_ALL_BLOCK_PROPOSALS . get ( ) ;
180
182
if public_keys. contains (
181
183
& stacks_common:: types:: chainstate:: StacksPublicKey :: from_private (
182
184
& self . private_key ,
@@ -405,8 +407,10 @@ impl Signer {
405
407
"burn_height" => block_proposal. burn_height,
406
408
) ;
407
409
crate :: monitoring:: increment_block_proposals_received ( ) ;
408
- #[ allow ( unused_mut ) ]
410
+ #[ cfg ( any ( test , feature = "testing" ) ) ]
409
411
let mut block_info = BlockInfo :: from ( block_proposal. clone ( ) ) ;
412
+ #[ cfg( not( any( test, feature = "testing" ) ) ) ]
413
+ let block_info = BlockInfo :: from ( block_proposal. clone ( ) ) ;
410
414
411
415
// Get sortition view if we don't have it
412
416
if sortition_state. is_none ( ) {
@@ -538,10 +542,6 @@ impl Signer {
538
542
stacks_client : & StacksClient ,
539
543
block_response : & BlockResponse ,
540
544
) {
541
- #[ cfg( any( test, feature = "testing" ) ) ]
542
- if self . test_ignore_block_responses ( block_response) {
543
- return ;
544
- }
545
545
match block_response {
546
546
BlockResponse :: Accepted ( accepted) => {
547
547
self . handle_block_signature ( stacks_client, accepted) ;
@@ -1071,7 +1071,7 @@ impl Signer {
1071
1071
1072
1072
#[ cfg( any( test, feature = "testing" ) ) ]
1073
1073
fn test_skip_block_broadcast ( & self , block : & NakamotoBlock ) -> bool {
1074
- if * TEST_SKIP_BLOCK_BROADCAST . lock ( ) . unwrap ( ) == Some ( true ) {
1074
+ if TEST_SKIP_BLOCK_BROADCAST . get ( ) {
1075
1075
let block_hash = block. header . signer_signature_hash ( ) ;
1076
1076
warn ! (
1077
1077
"{self}: Skipping block broadcast due to testing directive" ;
@@ -1099,9 +1099,7 @@ impl Signer {
1099
1099
block_info : & mut BlockInfo ,
1100
1100
block_response : Option < BlockResponse > ,
1101
1101
) -> Option < BlockResponse > {
1102
- let Some ( public_keys) = & * TEST_REJECT_ALL_BLOCK_PROPOSAL . lock ( ) . unwrap ( ) else {
1103
- return block_response;
1104
- } ;
1102
+ let public_keys = TEST_REJECT_ALL_BLOCK_PROPOSAL . get ( ) ;
1105
1103
if public_keys. contains (
1106
1104
& stacks_common:: types:: chainstate:: StacksPublicKey :: from_private ( & self . private_key ) ,
1107
1105
) {
@@ -1126,31 +1124,19 @@ impl Signer {
1126
1124
self . mainnet ,
1127
1125
) )
1128
1126
} else {
1129
- None
1130
- }
1131
- }
1132
-
1133
- #[ cfg( any( test, feature = "testing" ) ) ]
1134
- fn test_ignore_block_responses ( & self , block_response : & BlockResponse ) -> bool {
1135
- if * TEST_IGNORE_BLOCK_RESPONSES . lock ( ) . unwrap ( ) == Some ( true ) {
1136
- warn ! (
1137
- "{self}: Ignoring block response due to testing directive" ;
1138
- "block_response" => %block_response
1139
- ) ;
1140
- return true ;
1127
+ block_response
1141
1128
}
1142
- false
1143
1129
}
1144
1130
1145
1131
#[ cfg( any( test, feature = "testing" ) ) ]
1146
1132
fn test_pause_block_broadcast ( & self , block_info : & BlockInfo ) {
1147
- if * TEST_PAUSE_BLOCK_BROADCAST . lock ( ) . unwrap ( ) == Some ( true ) {
1133
+ if TEST_PAUSE_BLOCK_BROADCAST . get ( ) {
1148
1134
// Do an extra check just so we don't log EVERY time.
1149
1135
warn ! ( "{self}: Block broadcast is stalled due to testing directive." ;
1150
1136
"block_id" => %block_info. block. block_id( ) ,
1151
1137
"height" => block_info. block. header. chain_length,
1152
1138
) ;
1153
- while * TEST_PAUSE_BLOCK_BROADCAST . lock ( ) . unwrap ( ) == Some ( true ) {
1139
+ while TEST_PAUSE_BLOCK_BROADCAST . get ( ) {
1154
1140
std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 10 ) ) ;
1155
1141
}
1156
1142
info ! ( "{self}: Block validation is no longer stalled due to testing directive." ;
0 commit comments