Skip to content

Commit 615f8b5

Browse files
committed
make signer runloop run the "after-event" code on all invocations
1 parent ac370c0 commit 615f8b5

File tree

2 files changed

+86
-68
lines changed

2 files changed

+86
-68
lines changed

libsigner/src/events.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ struct BlockEvent {
589589
#[serde(with = "prefix_hex")]
590590
index_block_hash: StacksBlockId,
591591
#[serde(with = "prefix_opt_hex")]
592+
#[serde(default)]
592593
signer_signature_hash: Option<Sha512Trunc256Sum>,
593594
#[serde(with = "prefix_hex")]
594595
consensus_hash: ConsensusHash,

stacks-signer/src/v0/signer.rs

Lines changed: 85 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,91 @@ impl SignerTrait<SignerMessage> for Signer {
266266
.unwrap_or_else(|e| error!("{self}: failed to update local state machine for pending update"; "err" => ?e));
267267
}
268268

269+
self.handle_event_match(stacks_client, sortition_state, event, current_reward_cycle);
270+
271+
self.check_submitted_block_proposal();
272+
self.check_pending_block_validations(stacks_client);
273+
274+
if prior_state != self.local_state_machine {
275+
self.local_state_machine
276+
.send_signer_update_message(&mut self.stackerdb);
277+
}
278+
}
279+
280+
fn has_unprocessed_blocks(&self) -> bool {
281+
self.signer_db
282+
.has_unprocessed_blocks(self.reward_cycle)
283+
.unwrap_or_else(|e| {
284+
error!("{self}: Failed to check for pending blocks: {e:?}",);
285+
// Assume we have pending blocks to prevent premature cleanup
286+
true
287+
})
288+
}
289+
290+
fn get_local_state_machine(&self) -> &LocalStateMachine {
291+
&self.local_state_machine
292+
}
293+
294+
#[cfg(not(any(test, feature = "testing")))]
295+
fn get_pending_proposals_count(&self) -> u64 {
296+
0
297+
}
298+
299+
#[cfg(any(test, feature = "testing"))]
300+
fn get_pending_proposals_count(&self) -> u64 {
301+
self.signer_db
302+
.get_all_pending_block_validations()
303+
.map(|results| u64::try_from(results.len()).unwrap())
304+
.unwrap_or(0)
305+
}
306+
}
307+
308+
impl Signer {
309+
/// Determine this signers response to a proposed block
310+
/// Returns a BlockResponse if we have already validated the block
311+
/// Returns None otherwise
312+
fn determine_response(&mut self, block_info: &BlockInfo) -> Option<BlockResponse> {
313+
let valid = block_info.valid?;
314+
let response = if valid {
315+
debug!("{self}: Accepting block {}", block_info.block.block_id());
316+
self.create_block_acceptance(&block_info.block)
317+
} else {
318+
debug!("{self}: Rejecting block {}", block_info.block.block_id());
319+
self.create_block_rejection(RejectReason::RejectedInPriorRound, &block_info.block)
320+
};
321+
Some(response)
322+
}
323+
324+
/// Create a block acceptance response for a block
325+
pub fn create_block_acceptance(&self, block: &NakamotoBlock) -> BlockResponse {
326+
let signature = self
327+
.private_key
328+
.sign(block.header.signer_signature_hash().bits())
329+
.expect("Failed to sign block");
330+
BlockResponse::accepted(
331+
block.header.signer_signature_hash(),
332+
signature,
333+
self.signer_db.calculate_tenure_extend_timestamp(
334+
self.proposal_config
335+
.tenure_idle_timeout
336+
.saturating_add(self.proposal_config.tenure_idle_timeout_buffer),
337+
block,
338+
true,
339+
),
340+
)
341+
}
342+
343+
/// The actual switch-on-event processing of an event.
344+
/// This is separated from the Signer trait implementation of process_event
345+
/// so that the "do on every event" functionality can run after every event processing
346+
/// (i.e. even if the event_match does an early return).
347+
fn handle_event_match(
348+
&mut self,
349+
stacks_client: &StacksClient,
350+
sortition_state: &mut Option<SortitionsView>,
351+
event: &SignerEvent<SignerMessage>,
352+
current_reward_cycle: u64,
353+
) {
269354
match event {
270355
SignerEvent::BlockValidationResponse(block_validate_response) => {
271356
debug!("{self}: Received a block proposal result from the stacks node...");
@@ -431,74 +516,6 @@ impl SignerTrait<SignerMessage> for Signer {
431516
}
432517
}
433518
}
434-
435-
if prior_state != self.local_state_machine {
436-
self.local_state_machine
437-
.send_signer_update_message(&mut self.stackerdb);
438-
}
439-
}
440-
441-
fn has_unprocessed_blocks(&self) -> bool {
442-
self.signer_db
443-
.has_unprocessed_blocks(self.reward_cycle)
444-
.unwrap_or_else(|e| {
445-
error!("{self}: Failed to check for pending blocks: {e:?}",);
446-
// Assume we have pending blocks to prevent premature cleanup
447-
true
448-
})
449-
}
450-
451-
fn get_local_state_machine(&self) -> &LocalStateMachine {
452-
&self.local_state_machine
453-
}
454-
455-
#[cfg(not(any(test, feature = "testing")))]
456-
fn get_pending_proposals_count(&self) -> u64 {
457-
0
458-
}
459-
460-
#[cfg(any(test, feature = "testing"))]
461-
fn get_pending_proposals_count(&self) -> u64 {
462-
self.signer_db
463-
.get_all_pending_block_validations()
464-
.map(|results| u64::try_from(results.len()).unwrap())
465-
.unwrap_or(0)
466-
}
467-
}
468-
469-
impl Signer {
470-
/// Determine this signers response to a proposed block
471-
/// Returns a BlockResponse if we have already validated the block
472-
/// Returns None otherwise
473-
fn determine_response(&mut self, block_info: &BlockInfo) -> Option<BlockResponse> {
474-
let valid = block_info.valid?;
475-
let response = if valid {
476-
debug!("{self}: Accepting block {}", block_info.block.block_id());
477-
self.create_block_acceptance(&block_info.block)
478-
} else {
479-
debug!("{self}: Rejecting block {}", block_info.block.block_id());
480-
self.create_block_rejection(RejectReason::RejectedInPriorRound, &block_info.block)
481-
};
482-
Some(response)
483-
}
484-
485-
/// Create a block acceptance response for a block
486-
pub fn create_block_acceptance(&self, block: &NakamotoBlock) -> BlockResponse {
487-
let signature = self
488-
.private_key
489-
.sign(block.header.signer_signature_hash().bits())
490-
.expect("Failed to sign block");
491-
BlockResponse::accepted(
492-
block.header.signer_signature_hash(),
493-
signature,
494-
self.signer_db.calculate_tenure_extend_timestamp(
495-
self.proposal_config
496-
.tenure_idle_timeout
497-
.saturating_add(self.proposal_config.tenure_idle_timeout_buffer),
498-
block,
499-
true,
500-
),
501-
)
502519
}
503520

504521
/// Create a block rejection response for a block with the given reject code

0 commit comments

Comments
 (0)