Skip to content

Commit 26ddfd4

Browse files
committed
refactor(gw-listener): address review nits on drift detector
- Prefix DriftDetector fields with `drift_` for consistency with ConfigSettings - Move classify_handle from free function to &self method on DriftDetector
1 parent 996dd28 commit 26ddfd4

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

coprocessor/fhevm-engine/gw-listener/src/drift_detector.rs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ enum HandleOutcome {
8585
pub(crate) struct DriftDetector {
8686
current_expected_senders: Vec<Address>,
8787
/// Handles waiting for consensus or post-consensus grace. Bounded implicitly:
88-
/// `evict_stale` removes entries after `no_consensus_timeout` (no consensus)
89-
/// or `post_consensus_grace` (consensus reached). Steady-state size is
88+
/// `evict_stale` removes entries after `drift_no_consensus_timeout` (no consensus)
89+
/// or `drift_post_consensus_grace` (consensus reached). Steady-state size is
9090
/// proportional to handle throughput * timeout duration.
9191
open_handles: HashMap<CiphertextDigest, HandleState>,
9292
host_chain_id: ChainId,
9393
local_node_id: String,
94-
no_consensus_timeout: Duration,
95-
post_consensus_grace: Duration,
94+
drift_no_consensus_timeout: Duration,
95+
drift_post_consensus_grace: Duration,
9696
deferred_drift_detected: u64,
9797
deferred_consensus_timeout: u64,
9898
deferred_missing_submission: u64,
@@ -103,16 +103,16 @@ impl DriftDetector {
103103
pub(crate) fn new(
104104
expected_senders: Vec<Address>,
105105
host_chain_id: ChainId,
106-
no_consensus_timeout: Duration,
107-
post_consensus_grace: Duration,
106+
drift_no_consensus_timeout: Duration,
107+
drift_post_consensus_grace: Duration,
108108
) -> Self {
109109
Self {
110110
current_expected_senders: expected_senders,
111111
open_handles: HashMap::new(),
112112
host_chain_id,
113113
local_node_id: std::env::var("HOSTNAME").unwrap_or_else(|_| "unknown".to_owned()),
114-
no_consensus_timeout,
115-
post_consensus_grace,
114+
drift_no_consensus_timeout,
115+
drift_post_consensus_grace,
116116
deferred_drift_detected: 0,
117117
deferred_consensus_timeout: 0,
118118
deferred_missing_submission: 0,
@@ -356,12 +356,7 @@ impl DriftDetector {
356356
let mut finished = Vec::new();
357357

358358
for (handle, state) in &self.open_handles {
359-
match classify_handle(
360-
state,
361-
now,
362-
self.no_consensus_timeout,
363-
self.post_consensus_grace,
364-
) {
359+
match self.classify_handle(state, now) {
365360
HandleOutcome::Pending => {}
366361
HandleOutcome::LocalDigestNeverAppeared => {
367362
let Some(consensus) = state.consensus.as_ref() else {
@@ -575,38 +570,37 @@ impl DriftDetector {
575570
self.evaluate_open_handles(Instant::now());
576571
Ok(())
577572
}
578-
}
579573

580-
fn classify_handle(
581-
state: &HandleState,
582-
now: Instant,
583-
no_consensus_timeout: Duration,
584-
post_consensus_grace: Duration,
585-
) -> HandleOutcome {
586-
if let Some(consensus) = &state.consensus {
587-
if !state.local_consensus_checked {
588-
return if now.duration_since(consensus.received_at) >= no_consensus_timeout {
589-
HandleOutcome::LocalDigestNeverAppeared
590-
} else {
591-
HandleOutcome::Pending
592-
};
593-
}
574+
fn classify_handle(&self, state: &HandleState, now: Instant) -> HandleOutcome {
575+
if let Some(consensus) = &state.consensus {
576+
if !state.local_consensus_checked {
577+
return if now.duration_since(consensus.received_at)
578+
>= self.drift_no_consensus_timeout
579+
{
580+
HandleOutcome::LocalDigestNeverAppeared
581+
} else {
582+
HandleOutcome::Pending
583+
};
584+
}
594585

595-
if state.submissions.len() < state.expected_senders.len() {
596-
return if now.duration_since(consensus.received_at) >= post_consensus_grace {
597-
HandleOutcome::NotAllCoprocessorsSubmitted
598-
} else {
599-
HandleOutcome::Pending
600-
};
601-
}
586+
if state.submissions.len() < state.expected_senders.len() {
587+
return if now.duration_since(consensus.received_at)
588+
>= self.drift_post_consensus_grace
589+
{
590+
HandleOutcome::NotAllCoprocessorsSubmitted
591+
} else {
592+
HandleOutcome::Pending
593+
};
594+
}
602595

603-
unreachable!("handle should have been removed by finish_if_complete");
604-
}
596+
unreachable!("handle should have been removed by finish_if_complete");
597+
}
605598

606-
if now.duration_since(state.first_seen_at) >= no_consensus_timeout {
607-
HandleOutcome::GatewayNeverReachedConsensus
608-
} else {
609-
HandleOutcome::Pending
599+
if now.duration_since(state.first_seen_at) >= self.drift_no_consensus_timeout {
600+
HandleOutcome::GatewayNeverReachedConsensus
601+
} else {
602+
HandleOutcome::Pending
603+
}
610604
}
611605
}
612606

0 commit comments

Comments
 (0)