-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add historical summaries to state and processing #11842
Conversation
func ProcessHistoricalRootsUpdate(state state.BeaconState) (state.BeaconState, error) { | ||
// ProcessHistoricalDataUpdate processes the updates to historical data during epoch processing. | ||
// For Capella state, per spec, historical summaries is updated instead of historical roots. | ||
func ProcessHistoricalDataUpdate(state state.BeaconState) (state.BeaconState, error) { |
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.
I decided to embed two different processing logic for historical data so that callers don't have to worry about what fork uses what.
Another approach is to implement two different functions ProcessHistoricalRootsUpdate
and ProcessHistoricalSummariesUpdate
with some duplicated conditions and let the callers decide which to call
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.
+1 to encapsulating that here, the way you're doing it
|
||
// HistoricalSummaries of the beacon state. | ||
func (b *BeaconState) HistoricalSummaries() []*ethpb.HistoricalSummary { | ||
if b.historicalSummaries == nil { |
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.
I skipped the versioning checks here and HistoricalRoots()
because the list would just be emptied and error out upon return. I don't think it's a big deal, and I didn't feel like changing the return argument to include an error. Happy to change it otherwise
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.
Don't most of the other getters return an error when a property is accessed on a version of the state which does not have that property?
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.
Very nice and clean!
require.NoError(t, err) | ||
return st | ||
}, | ||
verifier: func(st state.BeaconState) { |
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.
Verify historical summaries were not updates?
require.NoError(t, err) | ||
return st | ||
}, | ||
verifier: func(st state.BeaconState) { |
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.
Verify historical roots were not updated?
beacon-chain/state/interfaces.go
Outdated
@@ -85,6 +86,7 @@ type WriteOnlyBeaconState interface { | |||
SetSlashings(val []uint64) error | |||
UpdateSlashingsAtIndex(idx, val uint64) error | |||
AppendHistoricalRoots(root [32]byte) error | |||
AppendHistoricalSummariesUpdate(*ethpb.HistoricalSummary) error |
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.
This Update
part sounds weird. How about AppendHistoricalSummaries
?
func (b *BeaconState) AppendHistoricalRoots(root [32]byte) error { | ||
b.lock.Lock() | ||
defer b.lock.Unlock() | ||
|
||
if b.version > version.Bellatrix { | ||
return fmt.Errorf("AppendHistoricalRoots is not supported for version %d", b.version) |
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.
Please use the errNotSupported
function
@@ -124,6 +131,29 @@ func (b *BeaconState) AppendHistoricalRoots(root [32]byte) error { | |||
return nil | |||
} | |||
|
|||
// AppendHistoricalSummariesUpdate AppendHistoricalSummary for the beacon state. Appends the new value |
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.
Something is not correct
defer b.lock.Unlock() | ||
|
||
if b.version < version.Capella { | ||
return fmt.Errorf("AppendHistoricalSummariesUpdate is not supported for version %d", b.version) |
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.
Please use the errNotSupported
function
Co-authored-by: Radosław Kapka <[email protected]>
Co-authored-by: Radosław Kapka <[email protected]>
Co-authored-by: Radosław Kapka <[email protected]>
Co-authored-by: Radosław Kapka <[email protected]>
… into historical-summaries
tools/exploredb/main.go
Outdated
roots, err := st.HistoricalRoots() | ||
if err != nil { | ||
log.WithError(err).Error("could not get historical roots") | ||
return |
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.
return or continue logging?
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.
cont is probably better here
tools/exploredb/main.go
Outdated
} | ||
size, count = sizeAndCountOfByteList(roots) | ||
log.Infof("historical_roots : size = %s, count = %d", humanize.Bytes(size), count) | ||
|
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.
nitpick: not a fan of this whitespace formatting. I've been leaning more towards using WithField
to make structured logs easier.
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's a little unrelated to this PR
require.NoError(t, err) | ||
require.Equal(t, 0, len(summaries)) | ||
_, err = st.HistoricalSummaries() | ||
require.ErrorContains(t, "HistoricalSummaries is not supported for phase0", err) |
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.
can we use require.ErrorIs
instead of string matching the error text?
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.
Error is not exported. I can export the error though. Was trying to minimize the changes since the diff is getting big
@@ -67,15 +68,19 @@ func (b *BeaconState) forkVal() *ethpb.Fork { | |||
} | |||
|
|||
// HistoricalRoots based on epochs stored in the beacon state. | |||
func (b *BeaconState) HistoricalRoots() [][]byte { | |||
func (b *BeaconState) HistoricalRoots() ([][]byte, error) { | |||
if b.version > version.Bellatrix { |
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.
This is not accurate, the historical roots field still exists in the state. Just that it is now 'frozen'
@@ -530,6 +533,7 @@ func (b *BeaconState) Copy() state.BeaconState { | |||
nextSyncCommittee: b.nextSyncCommitteeVal(), | |||
latestExecutionPayloadHeader: b.latestExecutionPayloadHeaderVal(), | |||
latestExecutionPayloadHeaderCapella: b.latestExecutionPayloadHeaderCapellaVal(), | |||
historicalSummaries: b.historicalSummariesVal(), |
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.
if we are sharing fields for historical summaries with other state instances, then this is incorrect. This performs a whole copy of the field, when we should just be sharing the reference like how it is done for historical roots:
historicalRoots: b.historicalRoots,
} | ||
|
||
return fieldRoots, nil | ||
} | ||
|
||
func historicalSummaryRoot(summaries []*ethpb.HistoricalSummary) ([32]byte, error) { |
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.
This should be in the stateutil
package, since there is where we keep all the state hashing functions at
This PR implements ethereum/consensus-specs#2649, which adds historical summaries to the beacon state and updates them for epoch processing. This is part of the Capella update