-
Notifications
You must be signed in to change notification settings - Fork 15
Skip validator duties while syncing #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
edafae3
f4b9cea
7fd0b53
6e2305b
48faa02
cb031f1
a9d1025
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,13 +35,18 @@ pub const MILLISECONDS_PER_INTERVAL: u64 = 800; | |
| pub const INTERVALS_PER_SLOT: u64 = 5; | ||
| /// Milliseconds in a slot (derived from interval duration and count). | ||
| pub const MILLISECONDS_PER_SLOT: u64 = MILLISECONDS_PER_INTERVAL * INTERVALS_PER_SLOT; | ||
| /// Number of slots our head can lag behind the current slot before | ||
| /// validator duties are suppressed. During sync we lack a complete view | ||
| /// of the chain, so proposing or attesting would cast uninformed votes. | ||
| pub const SYNC_TOLERANCE_SLOTS: u64 = 2; | ||
| impl BlockChain { | ||
| pub fn spawn( | ||
| store: Store, | ||
| validator_keys: HashMap<u64, ValidatorSecretKey>, | ||
| is_aggregator: bool, | ||
| ) -> BlockChain { | ||
| metrics::set_is_aggregator(is_aggregator); | ||
| metrics::set_is_syncing(true); | ||
| let genesis_time = store.config().genesis_time; | ||
| let key_manager = key_manager::KeyManager::new(validator_keys); | ||
| let handle = BlockChainServer { | ||
|
|
@@ -51,6 +56,7 @@ impl BlockChain { | |
| pending_blocks: HashMap::new(), | ||
| is_aggregator, | ||
| pending_block_parents: HashMap::new(), | ||
| is_syncing: true, // assume syncing until on_tick proves otherwise | ||
| } | ||
| .start(); | ||
| let time_until_genesis = (SystemTime::UNIX_EPOCH + Duration::from_secs(genesis_time)) | ||
|
|
@@ -92,35 +98,49 @@ pub struct BlockChainServer { | |
|
|
||
| /// Whether this node acts as a committee aggregator. | ||
| is_aggregator: bool, | ||
| /// Whether this node is still catching up to the chain head. | ||
| /// When true, block proposal and attestation duties are skipped. | ||
| is_syncing: bool, | ||
| } | ||
|
|
||
| impl BlockChainServer { | ||
| fn on_tick(&mut self, timestamp_ms: u64) { | ||
| let genesis_time_ms = self.store.config().genesis_time * 1000; | ||
|
|
||
| // Calculate current slot and interval from milliseconds | ||
| let time_since_genesis_ms = timestamp_ms.saturating_sub(genesis_time_ms); | ||
| let slot = time_since_genesis_ms / MILLISECONDS_PER_SLOT; | ||
| let interval = (time_since_genesis_ms % MILLISECONDS_PER_SLOT) / MILLISECONDS_PER_INTERVAL; | ||
|
|
||
| // Fail fast: a state with zero validators is invalid and would cause | ||
| // panics in proposer selection and attestation processing. | ||
| if self.store.head_state().validators.is_empty() { | ||
| error!("Head state has no validators, skipping tick"); | ||
| return; | ||
| } | ||
|
|
||
| // Update current slot metric | ||
| metrics::update_current_slot(slot); | ||
|
|
||
| // Determine sync status: suppress validator duties while our head is | ||
| // more than SYNC_TOLERANCE_SLOTS behind the current slot. | ||
| // Log once per transition to avoid spam. | ||
| let head_slot = self.store.head_slot(); | ||
| let behind_by = slot.saturating_sub(head_slot); | ||
| let now_syncing = behind_by > SYNC_TOLERANCE_SLOTS; | ||
|
Comment on lines
+124
to
+126
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is too naive and can cause a devnet to stop block production entirely. We have to come up with a better heuristic.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, thanks for the detailed explanation. I've removed the sync guard from block proposal — the I agree the slot-lag heuristic is too blunt for proposal. A more reliable indicator would probably involve the P2P layer — e.g. checking whether we're actively fetching missing blocks from peers, or comparing our head against peer STATUS messages. Happy to take that on as a follow-up if that direction makes sense to you. |
||
| if now_syncing != self.is_syncing { | ||
| if now_syncing { | ||
| info!(%slot, %head_slot, %behind_by, "Node is syncing, pausing validator duties"); | ||
| } else { | ||
| info!(%slot, %head_slot, "Sync complete, resuming validator duties"); | ||
| } | ||
| self.is_syncing = now_syncing; | ||
| metrics::set_is_syncing(self.is_syncing); | ||
| } | ||
|
|
||
| // At interval 0, check if we will propose (but don't build the block yet). | ||
| // Tick forkchoice first to accept attestations, then build the block | ||
| // using the freshly-accepted attestations. | ||
| // Skip entirely while syncing — no complete chain view. | ||
| let proposer_validator_id = (interval == 0 && slot > 0) | ||
| .then(|| self.get_our_proposer(slot)) | ||
| .flatten(); | ||
|
|
||
| // Tick the store first - this accepts attestations at interval 0 if we have a proposal | ||
| // Tick the store first — accepts attestations at interval 0 if we have a proposal | ||
| let new_aggregates = store::on_tick( | ||
| &mut self.store, | ||
| timestamp_ms, | ||
|
|
@@ -136,19 +156,18 @@ impl BlockChainServer { | |
| } | ||
| } | ||
|
|
||
| // Now build and publish the block (after attestations have been accepted) | ||
| // Propose block at interval 0 (after attestations have been accepted) | ||
| if let Some(validator_id) = proposer_validator_id { | ||
| self.propose_block(slot, validator_id); | ||
| } | ||
|
|
||
| // Produce attestations at interval 1 (proposer already attested in block) | ||
| if interval == 1 { | ||
| // Produce attestations at interval 1 (proposer already attested in block). | ||
| // Skip while syncing. | ||
| if !self.is_syncing && interval == 1 { | ||
| self.produce_attestations(slot); | ||
| } | ||
|
|
||
| // Update safe target slot metric (updated by store.on_tick at interval 3) | ||
| metrics::update_safe_target_slot(self.store.safe_target_slot()); | ||
| // Update head slot metric (head may change when attestations are promoted at intervals 0/4) | ||
| metrics::update_head_slot(self.store.head_slot()); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.