15
15
use std:: collections:: HashMap ;
16
16
use std:: fmt:: Debug ;
17
17
use std:: sync:: mpsc:: Sender ;
18
- #[ cfg( any( test, feature = "testing" ) ) ]
19
- use std:: sync:: LazyLock ;
20
18
use std:: time:: { Duration , Instant } ;
21
19
22
20
use blockstack_lib:: chainstate:: nakamoto:: { NakamotoBlock , NakamotoBlockHeader } ;
@@ -35,12 +33,8 @@ use libsigner::v0::messages::{
35
33
use libsigner:: { BlockProposal , SignerEvent } ;
36
34
use slog:: { slog_debug, slog_error, slog_info, slog_warn} ;
37
35
use stacks_common:: types:: chainstate:: StacksAddress ;
38
- #[ cfg( any( test, feature = "testing" ) ) ]
39
- use stacks_common:: types:: chainstate:: StacksPublicKey ;
40
36
use stacks_common:: util:: get_epoch_time_secs;
41
37
use stacks_common:: util:: secp256k1:: MessageSignature ;
42
- #[ cfg( any( test, feature = "testing" ) ) ]
43
- use stacks_common:: util:: TestFlag ;
44
38
use stacks_common:: { debug, error, info, warn} ;
45
39
46
40
use crate :: chainstate:: { ProposalEvalConfig , SortitionsView } ;
@@ -50,27 +44,13 @@ use crate::runloop::SignerResult;
50
44
use crate :: signerdb:: { BlockInfo , BlockState , SignerDb } ;
51
45
use crate :: Signer as SignerTrait ;
52
46
53
- #[ cfg( any( test, feature = "testing" ) ) ]
54
- /// A global variable that can be used to reject all block proposals if the signer's public key is in the provided list
55
- pub static TEST_REJECT_ALL_BLOCK_PROPOSAL : LazyLock < TestFlag < Vec < StacksPublicKey > > > =
56
- LazyLock :: new ( TestFlag :: default) ;
57
-
58
- #[ cfg( any( test, feature = "testing" ) ) ]
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 TEST_IGNORE_ALL_BLOCK_PROPOSALS : LazyLock < TestFlag < Vec < StacksPublicKey > > > =
61
- LazyLock :: new ( TestFlag :: default) ;
62
-
63
- #[ cfg( any( test, feature = "testing" ) ) ]
64
- /// Pause the block broadcast
65
- pub static TEST_PAUSE_BLOCK_BROADCAST : LazyLock < TestFlag < bool > > = LazyLock :: new ( TestFlag :: default) ;
66
-
67
- #[ cfg( any( test, feature = "testing" ) ) ]
68
- /// Skip broadcasting the block to the network
69
- pub static TEST_SKIP_BLOCK_BROADCAST : LazyLock < TestFlag < bool > > = LazyLock :: new ( TestFlag :: default) ;
70
-
71
47
/// The stacks signer registered for the reward cycle
72
48
#[ derive( Debug ) ]
73
49
pub struct Signer {
50
+ /// The private key of the signer
51
+ #[ cfg( any( test, feature = "testing" ) ) ]
52
+ pub private_key : StacksPrivateKey ,
53
+ #[ cfg( not( any( test, feature = "testing" ) ) ) ]
74
54
/// The private key of the signer
75
55
private_key : StacksPrivateKey ,
76
56
/// The stackerdb client
@@ -175,20 +155,8 @@ impl SignerTrait<SignerMessage> for Signer {
175
155
match message {
176
156
SignerMessage :: BlockProposal ( block_proposal) => {
177
157
#[ cfg( any( test, feature = "testing" ) ) ]
178
- {
179
- let public_keys = TEST_IGNORE_ALL_BLOCK_PROPOSALS . get ( ) ;
180
- if public_keys. contains (
181
- & stacks_common:: types:: chainstate:: StacksPublicKey :: from_private (
182
- & self . private_key ,
183
- ) ,
184
- ) {
185
- warn ! ( "{self}: Ignoring block proposal due to testing directive" ;
186
- "block_id" => %block_proposal. block. block_id( ) ,
187
- "height" => block_proposal. block. header. chain_length,
188
- "consensus_hash" => %block_proposal. block. header. consensus_hash
189
- ) ;
190
- continue ;
191
- }
158
+ if self . test_ignore_all_block_proposals ( block_proposal) {
159
+ continue ;
192
160
}
193
161
self . handle_block_proposal (
194
162
stacks_client,
@@ -1121,87 +1089,6 @@ impl Signer {
1121
1089
}
1122
1090
}
1123
1091
1124
- #[ cfg( any( test, feature = "testing" ) ) ]
1125
- fn test_skip_block_broadcast ( & self , block : & NakamotoBlock ) -> bool {
1126
- if TEST_SKIP_BLOCK_BROADCAST . get ( ) {
1127
- let block_hash = block. header . signer_signature_hash ( ) ;
1128
- warn ! (
1129
- "{self}: Skipping block broadcast due to testing directive" ;
1130
- "block_id" => %block. block_id( ) ,
1131
- "height" => block. header. chain_length,
1132
- "consensus_hash" => %block. header. consensus_hash
1133
- ) ;
1134
-
1135
- if let Err ( e) = self
1136
- . signer_db
1137
- . set_block_broadcasted ( & block_hash, get_epoch_time_secs ( ) )
1138
- {
1139
- warn ! ( "{self}: Failed to set block broadcasted for {block_hash}: {e:?}" ) ;
1140
- }
1141
- return true ;
1142
- }
1143
- false
1144
- }
1145
-
1146
- #[ cfg( any( test, feature = "testing" ) ) ]
1147
- fn test_reject_block_proposal (
1148
- & mut self ,
1149
- block_proposal : & BlockProposal ,
1150
- block_info : & mut BlockInfo ,
1151
- block_response : Option < BlockResponse > ,
1152
- ) -> Option < BlockResponse > {
1153
- let public_keys = TEST_REJECT_ALL_BLOCK_PROPOSAL . get ( ) ;
1154
- if public_keys. contains (
1155
- & stacks_common:: types:: chainstate:: StacksPublicKey :: from_private ( & self . private_key ) ,
1156
- ) {
1157
- warn ! ( "{self}: Rejecting block proposal automatically due to testing directive" ;
1158
- "block_id" => %block_proposal. block. block_id( ) ,
1159
- "height" => block_proposal. block. header. chain_length,
1160
- "consensus_hash" => %block_proposal. block. header. consensus_hash
1161
- ) ;
1162
- if let Err ( e) = block_info. mark_locally_rejected ( ) {
1163
- warn ! ( "{self}: Failed to mark block as locally rejected: {e:?}" , ) ;
1164
- } ;
1165
- // We must insert the block into the DB to prevent subsequent repeat proposals being accepted (should reject
1166
- // as invalid since we rejected in a prior round if this crops up again)
1167
- // in case this is the first time we saw this block. Safe to do since this is testing case only.
1168
- self . signer_db
1169
- . insert_block ( block_info)
1170
- . unwrap_or_else ( |e| self . handle_insert_block_error ( e) ) ;
1171
- Some ( BlockResponse :: rejected (
1172
- block_proposal. block . header . signer_signature_hash ( ) ,
1173
- RejectCode :: TestingDirective ,
1174
- & self . private_key ,
1175
- self . mainnet ,
1176
- self . signer_db . calculate_tenure_extend_timestamp (
1177
- self . proposal_config . tenure_idle_timeout ,
1178
- & block_proposal. block ,
1179
- false ,
1180
- ) ,
1181
- ) )
1182
- } else {
1183
- block_response
1184
- }
1185
- }
1186
-
1187
- #[ cfg( any( test, feature = "testing" ) ) ]
1188
- fn test_pause_block_broadcast ( & self , block_info : & BlockInfo ) {
1189
- if TEST_PAUSE_BLOCK_BROADCAST . get ( ) {
1190
- // Do an extra check just so we don't log EVERY time.
1191
- warn ! ( "{self}: Block broadcast is stalled due to testing directive." ;
1192
- "block_id" => %block_info. block. block_id( ) ,
1193
- "height" => block_info. block. header. chain_length,
1194
- ) ;
1195
- while TEST_PAUSE_BLOCK_BROADCAST . get ( ) {
1196
- std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 10 ) ) ;
1197
- }
1198
- info ! ( "{self}: Block validation is no longer stalled due to testing directive." ;
1199
- "block_id" => %block_info. block. block_id( ) ,
1200
- "height" => block_info. block. header. chain_length,
1201
- ) ;
1202
- }
1203
- }
1204
-
1205
1092
/// Send a mock signature to stackerdb to prove we are still alive
1206
1093
fn mock_sign ( & mut self , mock_proposal : MockProposal ) {
1207
1094
info ! ( "{self}: Mock signing mock proposal: {mock_proposal:?}" ) ;
@@ -1216,7 +1103,7 @@ impl Signer {
1216
1103
}
1217
1104
1218
1105
/// Helper for logging insert_block error
1219
- fn handle_insert_block_error ( & self , e : DBError ) {
1106
+ pub fn handle_insert_block_error ( & self , e : DBError ) {
1220
1107
error ! ( "{self}: Failed to insert block into signer-db: {e:?}" ) ;
1221
1108
panic ! ( "{self} Failed to write block to signerdb: {e}" ) ;
1222
1109
}
0 commit comments