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

Capella beacon state #11141

Merged
merged 51 commits into from
Oct 12, 2022
Merged

Capella beacon state #11141

merged 51 commits into from
Oct 12, 2022

Conversation

rkapka
Copy link
Contributor

@rkapka rkapka commented Aug 1, 2022

What type of PR is this?

Feature

What does this PR do? Why is it needed?

Adds in Capella state, including new:

  • versions
  • types
  • getters
  • setters
  • hashing
  • spec params
  • SSZ generated code
  • proto conversions

This PR introduces the ExecutionData interface, which is a common abstraction over executionPayload, executionPayloadCapella, executionPayloadHeader and executionPayloadHeaderCapella. The reason for it is to avoid duplication in functions such as this:

func (s *Service) notifyNewPayload(ctx context.Context, postStateVersion int,
	postStateHeader interfaces.ExecutionData, blk interfaces.SignedBeaconBlock) (bool, error)

Here postStateHeader is of type ExecutionData, which means a single function can handle both possible underlying header types.

Which issues(s) does this PR fix?

Part of #11077

@rkapka rkapka requested review from nisdas, rauljordan and a team as code owners August 1, 2022 14:12
@rkapka rkapka requested a review from symbolpunk August 1, 2022 14:12
beacon-chain/db/kv/state.go Outdated Show resolved Hide resolved
fieldRoots[nativetypes.LatestExecutionPayloadHeaderCapella.RealPosition()] = executionPayloadRoot[:]

// Withdrawal queue root.
withdrawalQueueRoot := []byte("stub")
Copy link
Contributor Author

@rkapka rkapka Aug 1, 2022

Choose a reason for hiding this comment

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

I left an exercise for you @potuz - complete the stub

Copy link
Contributor

Choose a reason for hiding this comment

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

These are hashed as normal SSZ lists of Withdrawal objects, doesn't fastssz already cover this?

Copy link
Contributor

Choose a reason for hiding this comment

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

discussed offline, we can reuse Nishant's vectorized merkleizer

Copy link
Member

Choose a reason for hiding this comment

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

Do we intend to fix this ? Or is this meant to be in a follow up PR. i rather not have our hashing incomplete like this if we are adding in the whole state.

BaseFeePerGas: bytesutil.SafeCopyBytes(payload.BaseFeePerGas()),
BlockHash: bytesutil.SafeCopyBytes(payload.BlockHash()),
TransactionsRoot: txRoot[:],
WithdrawalsRoot: []byte("stub"),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another one for @potuz

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 this is the usual list.HTR from the withdrawal list in the execution payload.

@rkapka rkapka added Ready For Review A pull request ready for code review and removed Capella labels Sep 30, 2022
@@ -294,6 +294,28 @@ func (s *Store) saveStatesEfficientInternal(ctx context.Context, tx *bolt.Tx, bl
if err := valIdxBkt.Put(rt[:], validatorKeys[i]); err != nil {
return err
}
case *ethpb.BeaconStateCapella:
Copy link
Contributor

Choose a reason for hiding this comment

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

This case switch is a good candidate for simplification using generics since all cases are the same function based on pbState.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this was my impression too. But I didn't want to refactor existing code in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

There isn't even a need for generics if we can just pass in fastssz.Marshaler as an interface (beacon state satisfies this)

@@ -19,11 +21,20 @@ func (b *BeaconState) LatestExecutionPayloadHeader() (*enginev1.ExecutionPayload
b.lock.RLock()
defer b.lock.RUnlock()

return b.latestExecutionPayloadHeaderVal(), nil
if b.version == version.Bellatrix {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think in general and considering future forks, the best approach for all these switches is the opposite order

if version >= Capella { do something  }
else if version >= Bellatrix { do something }
else { not supported }

The first one is hit more often.

case nativetypes.LatestExecutionPayloadHeaderCapella:
return b.latestExecutionPayloadHeaderCapella.HashTreeRoot()
case nativetypes.WithdrawalQueue:
return [32]byte{}, errors.New("not implemented")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

and one more @potuz

Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, why doesn't fastssz already cover this give it's a list?

Copy link
Member

Choose a reason for hiding this comment

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

its not hard to do custom hashing for this too.

beacon-chain/state/state-native/hasher.go Outdated Show resolved Hide resolved
beacon-chain/state/state-native/hasher.go Outdated Show resolved Hide resolved
@@ -85,6 +85,10 @@ type WriteOnlyBeaconState interface {
UpdateSlashingsAtIndex(idx, val uint64) error
AppendHistoricalRoots(root [32]byte) error
SetLatestExecutionPayloadHeader(payload interfaces.ExecutionData) error
SetWithdrawalQueue(val []*enginev1.Withdrawal) error
Copy link
Member

Choose a reason for hiding this comment

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

Not really related to your PR, but why is the withdrawal object in our engine namespace ? Seems like the wrong place for it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fieldRoots[nativetypes.LatestExecutionPayloadHeaderCapella.RealPosition()] = executionPayloadRoot[:]

// Withdrawal queue root.
withdrawalQueueRoot := []byte("stub")
Copy link
Member

Choose a reason for hiding this comment

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

Do we intend to fix this ? Or is this meant to be in a follow up PR. i rather not have our hashing incomplete like this if we are adding in the whole state.

@@ -14,14 +14,20 @@ func (b *BeaconState) SetLatestExecutionPayloadHeader(val interfaces.ExecutionDa
b.lock.Lock()
defer b.lock.Unlock()

if b.version == version.Phase0 || b.version == version.Altair {
if b.version < version.Bellatrix {
return errNotSupported("SetLatestExecutionPayloadHeader", b.version)
}
header, ok := val.Proto().(*enginev1.ExecutionPayloadHeader)
Copy link
Member

Choose a reason for hiding this comment

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

can we use a switch statement here rather than multiple type assertions. Would make the code cleaner to read

case version.Altair:
dst.sharedFieldReferences = make(map[nativetypes.FieldIndex]*stateutil.Reference, 11)
case version.Bellatrix:
case version.Altair, version.Bellatrix, version.Capella:
Copy link
Member

Choose a reason for hiding this comment

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

this isnt correct, altair, bellatrix and capella have different sizes.

case nativetypes.LatestExecutionPayloadHeaderCapella:
return b.latestExecutionPayloadHeaderCapella.HashTreeRoot()
case nativetypes.WithdrawalQueue:
return [32]byte{}, errors.New("not implemented")
Copy link
Member

Choose a reason for hiding this comment

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

its not hard to do custom hashing for this too.

@@ -183,7 +183,7 @@ func (s *Service) getPayloadHash(ctx context.Context, root []byte) ([32]byte, er
// notifyForkchoiceUpdate signals execution engine on a new payload.
// It returns true if the EL has returned VALID for the block
func (s *Service) notifyNewPayload(ctx context.Context, postStateVersion int,
postStateHeader *enginev1.ExecutionPayloadHeader, blk interfaces.SignedBeaconBlock) (bool, error) {
postStateHeader interfaces.ExecutionData, blk interfaces.SignedBeaconBlock) (bool, error) {
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for changing *enginev1.ExecutionPayloadHeader to interfaces.ExecutionData. First glance, it seems unrelated to the PR title. Can you please explain it or even better cover that in the description?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the description

Comment on lines +67 to +68
b.lock.RLock()
defer b.lock.RUnlock()
Copy link
Member

Choose a reason for hiding this comment

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

Should we move these read locks before the version check? Currently, it's not consistent with SetWithdrawalQueue and the setter method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made everything consistent

terencechain
terencechain previously approved these changes Oct 4, 2022
@terencechain
Copy link
Member

Let's get one more approve before merge plz

@rkapka rkapka merged commit cafe0bd into develop Oct 12, 2022
@delete-merged-branch delete-merged-branch bot deleted the capella-state branch October 12, 2022 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready For Review A pull request ready for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants