mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 21:08:10 -05:00
Rebuild Trie After Reaching Limit (#8740)
* fix up indices * add comment * fix up again * add test Co-authored-by: Victor Farazdagi <simple.square@gmail.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
@@ -65,7 +65,7 @@ func (b *BeaconState) AppendCurrentEpochAttestations(val *pbp2p.PendingAttestati
|
||||
|
||||
b.state.CurrentEpochAttestations = append(atts, val)
|
||||
b.markFieldAsDirty(currentEpochAttestations)
|
||||
b.dirtyIndices[currentEpochAttestations] = append(b.dirtyIndices[currentEpochAttestations], uint64(len(b.state.CurrentEpochAttestations)-1))
|
||||
b.addDirtyIndices(currentEpochAttestations, []uint64{uint64(len(b.state.CurrentEpochAttestations) - 1)})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -94,6 +94,5 @@ func (b *BeaconState) AppendPreviousEpochAttestations(val *pbp2p.PendingAttestat
|
||||
b.state.PreviousEpochAttestations = append(atts, val)
|
||||
b.markFieldAsDirty(previousEpochAttestations)
|
||||
b.addDirtyIndices(previousEpochAttestations, []uint64{uint64(len(b.state.PreviousEpochAttestations) - 1)})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package stateV0
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
types "github.com/prysmaticlabs/eth2-types"
|
||||
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
||||
)
|
||||
|
||||
@@ -21,3 +24,49 @@ func TestBeaconState_RotateAttestations(t *testing.T) {
|
||||
require.Equal(t, 0, len(st.currentEpochAttestations()))
|
||||
require.Equal(t, types.Slot(456), st.previousEpochAttestations()[0].Data.Slot)
|
||||
}
|
||||
|
||||
func TestAppendBeyondIndicesLimit(t *testing.T) {
|
||||
zeroHash := params.BeaconConfig().ZeroHash
|
||||
mockblockRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
||||
for i := 0; i < len(mockblockRoots); i++ {
|
||||
mockblockRoots[i] = zeroHash[:]
|
||||
}
|
||||
|
||||
mockstateRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
||||
for i := 0; i < len(mockstateRoots); i++ {
|
||||
mockstateRoots[i] = zeroHash[:]
|
||||
}
|
||||
mockrandaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
|
||||
for i := 0; i < len(mockrandaoMixes); i++ {
|
||||
mockrandaoMixes[i] = zeroHash[:]
|
||||
}
|
||||
st, err := InitializeFromProto(&pb.BeaconState{
|
||||
Slot: 1,
|
||||
CurrentEpochAttestations: []*pb.PendingAttestation{{Data: ð.AttestationData{Slot: 456}}},
|
||||
PreviousEpochAttestations: []*pb.PendingAttestation{{Data: ð.AttestationData{Slot: 123}}},
|
||||
Validators: []*eth.Validator{},
|
||||
Eth1Data: ð.Eth1Data{},
|
||||
BlockRoots: mockblockRoots,
|
||||
StateRoots: mockstateRoots,
|
||||
RandaoMixes: mockrandaoMixes,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
_, err = st.HashTreeRoot(context.Background())
|
||||
require.NoError(t, err)
|
||||
for i := fieldIndex(0); i < fieldIndex(params.BeaconConfig().BeaconStateFieldCount); i++ {
|
||||
st.dirtyFields[i] = true
|
||||
}
|
||||
_, err = st.HashTreeRoot(context.Background())
|
||||
require.NoError(t, err)
|
||||
for i := 0; i < 10; i++ {
|
||||
assert.NoError(t, st.AppendValidator(ð.Validator{}))
|
||||
}
|
||||
assert.Equal(t, false, st.rebuildTrie[validators])
|
||||
assert.NotEqual(t, len(st.dirtyIndices[validators]), int(0))
|
||||
|
||||
for i := 0; i < indicesLimit; i++ {
|
||||
assert.NoError(t, st.AppendValidator(ð.Validator{}))
|
||||
}
|
||||
assert.Equal(t, true, st.rebuildTrie[validators])
|
||||
assert.Equal(t, len(st.dirtyIndices[validators]), int(0))
|
||||
}
|
||||
|
||||
@@ -9,6 +9,34 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
)
|
||||
|
||||
// For our setters, we have a field reference counter through
|
||||
// which we can track shared field references. This helps when
|
||||
// performing state copies, as we simply copy the reference to the
|
||||
// field. When we do need to do need to modify these fields, we
|
||||
// perform a full copy of the field. This is true of most of our
|
||||
// fields except for the following below.
|
||||
// 1) BlockRoots
|
||||
// 2) StateRoots
|
||||
// 3) Eth1DataVotes
|
||||
// 4) RandaoMixes
|
||||
// 5) HistoricalRoots
|
||||
// 6) CurrentEpochAttestations
|
||||
// 7) PreviousEpochAttestations
|
||||
// 8) Validators
|
||||
//
|
||||
// The fields referred to above are instead copied by reference, where
|
||||
// we simply copy the reference to the underlying object instead of the
|
||||
// whole object. This is possible due to how we have structured our state
|
||||
// as we copy the value on read, so as to ensure the underlying object is
|
||||
// not mutated while it is being accessed during a state read.
|
||||
|
||||
const (
|
||||
// This specifies the limit till which we process all dirty indices for a certain field.
|
||||
// If we have more dirty indices than the theshold, then we rebuild the whole trie. This
|
||||
// comes due to the fact that O(alogn) > O(n) beyong a certain value of a.
|
||||
indicesLimit = 8000
|
||||
)
|
||||
|
||||
// SetGenesisTime for the beacon state.
|
||||
func (b *BeaconState) SetGenesisTime(val uint64) error {
|
||||
b.lock.Lock()
|
||||
@@ -144,5 +172,12 @@ func (b *BeaconState) markFieldAsDirty(field fieldIndex) {
|
||||
// addDirtyIndices adds the relevant dirty field indices, so that they
|
||||
// can be recomputed.
|
||||
func (b *BeaconState) addDirtyIndices(index fieldIndex, indices []uint64) {
|
||||
if b.rebuildTrie[index] {
|
||||
return
|
||||
}
|
||||
b.dirtyIndices[index] = append(b.dirtyIndices[index], indices...)
|
||||
if len(b.dirtyIndices[index]) > indicesLimit {
|
||||
b.rebuildTrie[index] = true
|
||||
b.dirtyIndices[index] = []uint64{}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user