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

Electra: BeaconState implementation #13919

Merged
merged 21 commits into from
May 6, 2024
Merged

Conversation

prestonvanloon
Copy link
Member

@prestonvanloon prestonvanloon commented Apr 26, 2024

What type of PR is this?

Feature

What does this PR do? Why is it needed?

Required for Electra.

Which issues(s) does this PR fix?

The beacon state is blocking progress on most electra PRs. #13903 #13898 #13893 #13911 #13888

Other notes for review

We might want to split this into smaller PRs. I split up the changes into logical commits that could be their on PRs, in order.

Merge the following PRs before this can be made ready for review:

@prestonvanloon prestonvanloon force-pushed the electra-beacon-state branch 2 times, most recently from 063f077 to e980f86 Compare April 26, 2024 22:12
@james-prysm james-prysm mentioned this pull request Apr 29, 2024
1 task
@prestonvanloon prestonvanloon force-pushed the electra-beacon-state branch 5 times, most recently from 00b695f to 6fd2121 Compare April 30, 2024 01:47
@prestonvanloon prestonvanloon force-pushed the electra-beacon-state branch 4 times, most recently from b597662 to 477c9cb Compare May 2, 2024 20:29
@prestonvanloon prestonvanloon marked this pull request as ready for review May 2, 2024 20:29
@prestonvanloon prestonvanloon requested a review from a team as a code owner May 2, 2024 20:29
@prestonvanloon prestonvanloon force-pushed the electra-beacon-state branch from 3f4df33 to 4950d24 Compare May 2, 2024 20:47
Copy link
Member Author

@prestonvanloon prestonvanloon left a comment

Choose a reason for hiding this comment

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

Left some commentary from self review. This PR is ready to review and is very large so it will need more than 1 approval.

Comment on lines -138 to -139
undo := util.HackDenebMaxuint(t)
defer undo()
Copy link
Member Author

Choose a reason for hiding this comment

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

This was renamed to HackElectraMaxuint and serves the same purpose. It's no longer needed in this particular test.

beacon-chain/state/interfaces.go Outdated Show resolved Hide resolved

// hasETH1WithdrawalCredential returns whether the validator has an ETH1
// Withdrawal prefix. It assumes that the caller has a lock on the state
func hasETH1WithdrawalCredential(val *ethpb.Validator) bool {
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved these to helpers library

require.Equal(t, false, hasETH1WithdrawalCredential(v))
}
func TestExpectedWithdrawals(t *testing.T) {
for _, stateVersion := range []int{version.Capella, version.Deneb, version.Electra} {
Copy link
Member Author

Choose a reason for hiding this comment

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

I refactored TestExpectedWIthdrawals_$FORK to be a more table driven style instead of a copy and paste with the version changed.

})
}

t.Run("electra all pending partial withdrawals", func(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This test load a ssz test from the spec tests repo as a fully hydrated example rather than hand crafting test data.

// This case is copied from spectest mainnet/electra/operations/voluntary_exit/pyspec_tests/exit_existing_churn_and_churn_limit_balance/pre.ssz_snappy
// and that case was retrieved from the consensus-spec-tests repository at tag v1.5.0-alpha.1.
// The spec tests shows that the exit epoch is 262 for validator 0 performing a voluntary exit.
serializedBytes, err := util.BazelFileBytes("testdata/electra_exit_0.ssz_snappy")
Copy link
Member Author

Choose a reason for hiding this comment

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

Another spec test example test data. Helped me find an off by one bug 😅

@@ -21,6 +23,9 @@ func (b *BeaconState) SetLatestExecutionPayloadHeader(val interfaces.ExecutionDa

switch header := val.Proto().(type) {
case *enginev1.ExecutionPayload:
if b.version != version.Bellatrix {
Copy link
Member Author

Choose a reason for hiding this comment

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

I found it odd and bad that we could have a beacon state that accepted the wrong type of execution payload, so I added that verification for each case and the appropriate test.

)

// SliceRoot computes the root of a slice of hashable objects.
func SliceRoot[T ssz.Hashable](slice []T, limit uint64) ([32]byte, error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the same logic exactly from beacon-chain/state/stateutil/historical_summaries_root.go, but parameterized / generic version

Copy link
Contributor

Choose a reason for hiding this comment

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

nice

func Merkleize(leaves [][]byte) [][][]byte {
hashFunc := hash.CustomSHA256Hasher()
layers := make([][][]byte, ssz.Depth(uint64(len(leaves)))+1)
for len(leaves) != 32 {
for len(leaves)%32 != 0 {
Copy link
Member Author

Choose a reason for hiding this comment

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

This was a bug where we would allocate infinite memory if the beacon state had more than 32 fields. Wow!

Copy link
Contributor

Choose a reason for hiding this comment

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

bug of the week

testing/util/electra.go Show resolved Hide resolved
@prestonvanloon prestonvanloon force-pushed the electra-beacon-state branch from 00d1f54 to 67dd466 Compare May 3, 2024 19:39
@prestonvanloon
Copy link
Member Author

All of @rkapka feedback is addressed. This PR is ready for another round of reviews

@prestonvanloon
Copy link
Member Author

Rebased onto develop and broken by #13948. Trying to figure how how to fix most effectively.

@prestonvanloon
Copy link
Member Author

Build is fixed as of 4ff0357

Copy link
Contributor

@rkapka rkapka left a comment

Choose a reason for hiding this comment

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

Fantastic work!

s.validators[i] = val
}
expected, err := s.ExpectedWithdrawals()
serializedSSZ, err := snappy.Decode(nil /* dst */, serializedBytes)
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a nice general test helper (get example state for fork).

Copy link
Member Author

Choose a reason for hiding this comment

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

If someone finds that useful, I would be happy to help move this to some shared method. I thought this was a rather specific example that I crawled the spectests repo to find.

Copy link
Contributor

Choose a reason for hiding this comment

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

Didn't mean that as feedback to move it btw, fine where it is.

)

// SliceRoot computes the root of a slice of hashable objects.
func SliceRoot[T ssz.Hashable](slice []T, limit uint64) ([32]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nice

@prestonvanloon prestonvanloon added this pull request to the merge queue May 6, 2024
Merged via the queue into develop with commit 9b2934f May 6, 2024
16 of 17 checks passed
@prestonvanloon prestonvanloon deleted the electra-beacon-state branch May 6, 2024 18:11
nisdas pushed a commit that referenced this pull request Jul 4, 2024
* Electra: Beacon State

* Electra: Beacon state fixes from PR 13919

* Add missing tests - part 1

* Split eip_7251_root.go into different files and reuse/share code with historical state summaries root. It's identical!

* Add missing tests - part 2

* deposit receipts start index getters and setters (#13947)

* adding in getters and setters for deposit receipts start index

* adding tests

* gaz

* Add missing tests - part 3 of 3

Update the electra withdrawal example with a ssz state containing pending partial withdrawals

* add tests for beacon-chain/state/state-native/getters_balance_deposits.go

* Add electra field to testing/util/block.go execution payload

* godoc commentary on public methods

* Fix failing test

* Add balances index out of bounds check and relevant tests.

* Revert switch case electra

* Instead of copying spectest data into testdata, use the spectest dependency

* Deepsource fixes

* Address @rkapka PR feedback

* s/MaxPendingPartialsPerWithdrawalSweep/MaxPendingPartialsPerWithdrawalsSweep/

* Use multivalue slice compatible accessors for validator and balance in ActiveBalanceAtIndex

* More @rkapka feedback. What a great reviewer!

* More tests for branching logic in ExitEpochAndUpdateChurn

* fix build

---------

Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants