-
Notifications
You must be signed in to change notification settings - Fork 250
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
more fork-choice fixes #1388
Merged
Merged
more fork-choice fixes #1388
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 |
---|---|---|
|
@@ -162,6 +162,14 @@ func atSlot*(blck: BlockRef, slot: Slot): BlockSlot = | |
## block proposal) | ||
BlockSlot(blck: blck.getAncestorAt(slot), slot: slot) | ||
|
||
func atEpochStart*(blck: BlockRef, epoch: Epoch): BlockSlot = | ||
## Return the BlockSlot corresponding to the first slot in the given epoch | ||
atSlot(blck, epoch.compute_start_slot_at_epoch) | ||
|
||
func atEpochEnd*(blck: BlockRef, epoch: Epoch): BlockSlot = | ||
## Return the BlockSlot corresponding to the last slot in the given epoch | ||
atSlot(blck, (epoch + 1).compute_start_slot_at_epoch - 1) | ||
|
||
func getEpochInfo*(blck: BlockRef, state: BeaconState): EpochRef = | ||
# This is the only intended mechanism by which to get an EpochRef | ||
let | ||
|
@@ -186,7 +194,20 @@ func getEpochInfo*(blck: BlockRef, state: BeaconState): EpochRef = | |
|
||
func getEpochCache*(blck: BlockRef, state: BeaconState): StateCache = | ||
let epochInfo = getEpochInfo(blck, state) | ||
result = StateCache() | ||
if epochInfo.epoch > 0: | ||
# When doing state transitioning, both the current and previous epochs are | ||
# useful from a cache perspective since attestations may come from either - | ||
# we'll use the last slot from the epoch because it is more likely to | ||
# be filled in already, compared to the first slot where the block might | ||
# be from the epoch before. | ||
let | ||
prevEpochBlck = blck.atEpochEnd(epochInfo.epoch - 1).blck | ||
|
||
for ei in prevEpochBlck.epochsInfo: | ||
if ei.epoch == epochInfo.epoch - 1: | ||
result.shuffled_active_validator_indices[ei.epoch] = | ||
ei.shuffled_active_validator_indices | ||
|
||
result.shuffled_active_validator_indices[state.get_current_epoch()] = | ||
epochInfo.shuffled_active_validator_indices | ||
|
||
|
@@ -279,9 +300,8 @@ proc init*(T: type CandidateChains, | |
# from the same epoch as the head, thus the finalized and justified slots are | ||
# the same - these only change on epoch boundaries. | ||
let | ||
finalizedSlot = | ||
tmpState.data.data.finalized_checkpoint.epoch.compute_start_slot_at_epoch() | ||
finalizedHead = headRef.atSlot(finalizedSlot) | ||
finalizedHead = headRef.atEpochStart( | ||
tmpState.data.data.finalized_checkpoint.epoch) | ||
|
||
let res = CandidateChains( | ||
blocks: blocks, | ||
|
@@ -316,12 +336,18 @@ proc init*(T: type CandidateChains, | |
res | ||
|
||
proc getEpochRef*(pool: CandidateChains, blck: BlockRef, epoch: Epoch): EpochRef = | ||
let bs = blck.atSlot(epoch.compute_start_slot_at_epoch) | ||
for e in bs.blck.epochsInfo: | ||
if e.epoch == epoch: | ||
return e | ||
var bs = blck.atEpochEnd(epoch) | ||
|
||
while true: | ||
# Any block from within the same epoch will carry the same epochinfo, so | ||
# we start at the most recent one | ||
for e in bs.blck.epochsInfo: | ||
if e.epoch == epoch: | ||
return e | ||
if bs.slot == epoch.compute_start_slot_at_epoch: | ||
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. Couldn't blocks skip slots here, including the first slot of an epoch? 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. that's why |
||
break | ||
bs = bs.parent | ||
|
||
# TODO use any state from epoch | ||
pool.withState(pool.tmpState, bs): | ||
getEpochInfo(blck, state) | ||
|
||
|
@@ -723,13 +749,19 @@ proc updateHead*(dag: CandidateChains, newHead: BlockRef) = | |
lastHead = dag.head | ||
dag.db.putHeadBlock(newHead.root) | ||
|
||
# Start off by making sure we have the right state | ||
updateStateData( | ||
dag, dag.headState, BlockSlot(blck: newHead, slot: newHead.slot)) | ||
# Start off by making sure we have the right state - as a special case, we'll | ||
# check the last block that was cleared by clearance - it might be just the | ||
# thing we're looking for | ||
|
||
if dag.clearanceState.blck == newHead and | ||
dag.clearanceState.data.data.slot == newHead.slot: | ||
assign(dag.headState, dag.clearanceState) | ||
else: | ||
updateStateData( | ||
dag, dag.headState, newHead.atSlot(newHead.slot)) | ||
|
||
dag.head = newHead | ||
|
||
# TODO isAncestorOf may be expensive - too expensive? | ||
if not lastHead.isAncestorOf(newHead): | ||
info "Updated head block with reorg", | ||
lastHead = shortLog(lastHead), | ||
|
@@ -750,10 +782,8 @@ proc updateHead*(dag: CandidateChains, newHead: BlockRef) = | |
justified = shortLog(dag.headState.data.data.current_justified_checkpoint), | ||
finalized = shortLog(dag.headState.data.data.finalized_checkpoint) | ||
let | ||
finalizedEpochStartSlot = | ||
dag.headState.data.data.finalized_checkpoint.epoch. | ||
compute_start_slot_at_epoch() | ||
finalizedHead = newHead.atSlot(finalizedEpochStartSlot) | ||
finalizedHead = newHead.atEpochStart( | ||
dag.headState.data.data.finalized_checkpoint.epoch) | ||
|
||
doAssert (not finalizedHead.blck.isNil), | ||
"Block graph should always lead to a finalized block" | ||
|
@@ -773,7 +803,6 @@ proc updateHead*(dag: CandidateChains, newHead: BlockRef) = | |
# cur = cur.parent | ||
# dag.delState(cur) | ||
|
||
|
||
block: # Clean up block refs, walking block by block | ||
# Finalization means that we choose a single chain as the canonical one - | ||
# it also means we're no longer interested in any branches from that chain | ||
|
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this might create race conditions with other parts of the code perhaps if the local validators aren't yet there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, the code is more synchronous now without the await because addlocalvalidators is no longer asynchronous (notably, because it no longer has to schedule anything on the async loop)