-
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
Open
thinktanktom
wants to merge
6
commits into
lambdaclass:main
Choose a base branch
from
thinktanktom:236-skip-duties-while-syncing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+47
−13
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
edafae3
Skip validator duties while syncing (#236)
thinktanktom f4b9cea
Fix lean_is_syncing metric initialisation and docs formatting
thinktanktom 7fd0b53
Update docs/metrics.md
pablodeymo 6e2305b
Merge branch 'main' into 236-skip-duties-while-syncing
pablodeymo 48faa02
remove duplicate line in metrics.md
thinktanktom cb031f1
Merge branch 'main' into 236-skip-duties-while-syncing
pablodeymo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
||
| 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. | ||
| let proposer_validator_id = (interval == 0 && slot > 0) | ||
| // Skip entirely while syncing — no complete chain view. | ||
| let proposer_validator_id = (!self.is_syncing && 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()); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.