Skip to content
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

feat: implement is_better_update for light client #14186

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

rupam-04
Copy link

@rupam-04 rupam-04 commented Jul 4, 2024

What type of PR is this?

Feature

What does this PR do? Why is it needed?

Implements the is_better_update function for light clients from CL specs

Which issues(s) does this PR fix?

Fixes #14185

Other notes for review
Facing an error in 2 other helper functions(isSyncCommitteeUpdate and isFinalityUpdate) required for the isBetterUpdate function. It says:

invalid operation: update.NextSyncCommitteeBranch != nextSyncCommitteeBranch (slice can only be compared to nil)

and

invalid operation: update.FinalityBranch != finalityBranch (slice can only be compared to nil)

respectively. Would appreciate some pointers

@rupam-04 rupam-04 requested a review from a team as a code owner July 4, 2024 11:32
Comment on lines 27 to 28
// createLightClientBootstrap - implements https://github.com/ethereum/consensus-specs/blob/3d235740e5f1e641d3b160c8688f26e7dc5a1894/specs/altair/light-client/full-node.md#create_light_client_bootstrap
// def create_light_client_bootstrap(state: BeaconState) -> LightClientBootstrap:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These specs are needed for all the four functions.

Comment on lines 356 to 357
newHasRelevantSyncCommittee := isSyncCommitteeUpdate(newUpdate) && (slots.ToEpoch(newUpdate.AttestedHeader.Slot) == slots.ToEpoch(newUpdate.SignatureSlot))
oldHasRelevantSyncCommittee := isSyncCommitteeUpdate(oldUpdate) && (slots.ToEpoch(oldUpdate.AttestedHeader.Slot) == slots.ToEpoch(oldUpdate.SignatureSlot))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slots.ToEpoch probably needs to be implemented in a different function compute_sync_committee_period_at_slot as per specs

Copy link
Author

@rupam-04 rupam-04 Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I saw the compute_sync_committee_period_at_slot is used in some other places of the codebase too and instead of implementing the function, slots.ToEpoch() has been used

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing epochs like this won't work because the two compared epochs don't have to be equal. It is enough that they are from the same sync committee period. You should use slots.SyncCommitteePeriod().

Comment on lines +372 to +373
newHasSyncCommitteeFinality := slots.ToEpoch(newUpdate.FinalizedHeader.Slot) == slots.ToEpoch(newUpdate.AttestedHeader.Slot)
oldHasSyncCommitteeFinality := slots.ToEpoch(oldUpdate.FinalizedHeader.Slot) == slots.ToEpoch(oldUpdate.AttestedHeader.Slot)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above slots.ToEpoch probably needs to be implemented in compute_sync_committee_period_at_slotas per specs

v2 "github.com/prysmaticlabs/prysm/v5/proto/eth/v2"
"github.com/prysmaticlabs/prysm/v5/proto/migration"
"github.com/prysmaticlabs/prysm/v5/time/slots"
)

const (
finalityBranchNumOfLeaves = 6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same constant is defined in beacon-chain/blockchain/lightclient.go. We should not duplicate constants because then we have to maintain the correct value in more than one place. The correct way is to export the existing one by making it upper-case, and using it in this package.

Comment on lines 333 to 338
newNumActiveParticipants := uint64(0)
for i := uint64(0); i < maxActiveParticipants; i++ {
if newUpdate.SyncAggregate.SyncCommitteeBits.BitAt(i) {
newNumActiveParticipants += 1
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SyncCommitteeBits.Count() will give you the number of bits set

Comment on lines 339 to 344
oldNumActiveParticipants := uint64(0)
for i := uint64(0); i < oldUpdate.SyncAggregate.SyncCommitteeBits.Len(); i++ {
if oldUpdate.SyncAggregate.SyncCommitteeBits.BitAt(i) {
oldNumActiveParticipants += 1
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Comment on lines 356 to 357
newHasRelevantSyncCommittee := isSyncCommitteeUpdate(newUpdate) && (slots.ToEpoch(newUpdate.AttestedHeader.Slot) == slots.ToEpoch(newUpdate.SignatureSlot))
oldHasRelevantSyncCommittee := isSyncCommitteeUpdate(oldUpdate) && (slots.ToEpoch(oldUpdate.AttestedHeader.Slot) == slots.ToEpoch(oldUpdate.SignatureSlot))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing epochs like this won't work because the two compared epochs don't have to be equal. It is enough that they are from the same sync committee period. You should use slots.SyncCommitteePeriod().

@rupam-04 rupam-04 force-pushed the is_better_update-implementation branch from 1eaf8ad to 1829feb Compare July 6, 2024 08:32
@rupam-04 rupam-04 requested a review from rkapka July 6, 2024 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add is_better_update helper function from CL specs
3 participants