Skip to content

Commit

Permalink
Use StateRoot as Key for Cache (#7540)
Browse files Browse the repository at this point in the history
* checkpoint

* review

* comment

* fix bad bug

* fix up
  • Loading branch information
nisdas committed Oct 19, 2020
1 parent f31d40c commit 329fbff
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 18 deletions.
9 changes: 9 additions & 0 deletions beacon-chain/core/helpers/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ func BlockRootAtSlot(state *stateTrie.BeaconState, slot uint64) ([]byte, error)
return state.BlockRootAtIndex(slot % params.BeaconConfig().SlotsPerHistoricalRoot)
}

// StateRootAtSlot returns the cached state root at that particular slot. If no state
// root has been cached it will return a zero-hash.
func StateRootAtSlot(state *stateTrie.BeaconState, slot uint64) ([]byte, error) {
if slot >= state.Slot() || state.Slot() > slot+params.BeaconConfig().SlotsPerHistoricalRoot {
return []byte{}, errors.Errorf("slot %d out of bounds", slot)
}
return state.StateRootAtIndex(slot % params.BeaconConfig().SlotsPerHistoricalRoot)
}

// BlockRoot returns the block root stored in the BeaconState for epoch start slot.
//
// Spec pseudocode definition:
Expand Down
18 changes: 14 additions & 4 deletions beacon-chain/core/helpers/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package helpers

import (
"bytes"
"fmt"
"sort"

Expand Down Expand Up @@ -348,9 +349,9 @@ func UpdateCommitteeCache(state *stateTrie.BeaconState, epoch uint64) error {

// UpdateProposerIndicesInCache updates proposer indices entry of the committee cache.
func UpdateProposerIndicesInCache(state *stateTrie.BeaconState, epoch uint64) error {
// The cache uses the block root at the last epoch slot as key. (e.g. for epoch 1, the key is root at slot 31)
// The cache uses the state root at the (current epoch - 2)'s slot as key. (e.g. for epoch 2, the key is root at slot 31)
// Which is the reason why we skip genesis epoch.
if epoch <= params.BeaconConfig().GenesisEpoch {
if epoch <= params.BeaconConfig().GenesisEpoch+params.BeaconConfig().MinSeedLookahead {
return nil
}

Expand All @@ -362,14 +363,23 @@ func UpdateProposerIndicesInCache(state *stateTrie.BeaconState, epoch uint64) er
if err != nil {
return err
}
s, err := EndSlot(PrevEpoch(state))
// Use state root from (current_epoch - 1 - lookahead))
wantedEpoch := PrevEpoch(state)
if wantedEpoch >= params.BeaconConfig().MinSeedLookahead {
wantedEpoch -= params.BeaconConfig().MinSeedLookahead
}
s, err := EndSlot(wantedEpoch)
if err != nil {
return err
}
r, err := BlockRootAtSlot(state, s)
r, err := StateRootAtSlot(state, s)
if err != nil {
return err
}
// Skip Cache if we have an invalid key
if r == nil || bytes.Equal(r, params.BeaconConfig().ZeroHash[:]) {
return nil
}
return proposerIndicesCache.AddProposerIndices(&cache.ProposerIndices{
BlockRoot: bytesutil.ToBytes32(r),
ProposerIndices: proposerIndices,
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/helpers/committee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ func TestPrecomputeProposerIndices_Ok(t *testing.T) {
}

func TestUpdateProposerIndicesInCache_CouldNotGetActiveIndices(t *testing.T) {
err := UpdateProposerIndicesInCache(&beaconstate.BeaconState{}, 1)
err := UpdateProposerIndicesInCache(&beaconstate.BeaconState{}, 2)
want := "nil inner state"
require.ErrorContains(t, want, err)
}
37 changes: 24 additions & 13 deletions beacon-chain/core/helpers/validators.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package helpers

import (
"bytes"

"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
Expand Down Expand Up @@ -175,26 +177,35 @@ func ValidatorChurnLimit(activeValidatorCount uint64) (uint64, error) {
// return compute_proposer_index(state, indices, seed)
func BeaconProposerIndex(state *stateTrie.BeaconState) (uint64, error) {
e := CurrentEpoch(state)
// The cache uses the block root of the previous epoch's last slot as key. (e.g. Starting epoch 1, slot 32, the key would be block root at slot 31)
// The cache uses the state root of the previous epoch - minimum_seed_lookahead last slot as key. (e.g. Starting epoch 1, slot 32, the key would be block root at slot 31)
// For simplicity, the node will skip caching of genesis epoch.
if e > params.BeaconConfig().GenesisEpoch {
s, err := EndSlot(PrevEpoch(state))
if err != nil {
return 0, err
if e > params.BeaconConfig().GenesisEpoch+params.BeaconConfig().MinSeedLookahead {
wantedEpoch := PrevEpoch(state)
if wantedEpoch >= params.BeaconConfig().MinSeedLookahead {
wantedEpoch -= params.BeaconConfig().MinSeedLookahead
}
r, err := BlockRootAtSlot(state, s)
s, err := EndSlot(wantedEpoch)
if err != nil {
return 0, err
}
proposerIndices, err := proposerIndicesCache.ProposerIndices(bytesutil.ToBytes32(r))
r, err := StateRootAtSlot(state, s)
if err != nil {
return 0, errors.Wrap(err, "could not interface with committee cache")
}
if proposerIndices != nil {
return proposerIndices[state.Slot()%params.BeaconConfig().SlotsPerEpoch], nil
return 0, err
}
if err := UpdateProposerIndicesInCache(state, e); err != nil {
return 0, errors.Wrap(err, "could not update committee cache")
if r != nil && !bytes.Equal(r, params.BeaconConfig().ZeroHash[:]) {
proposerIndices, err := proposerIndicesCache.ProposerIndices(bytesutil.ToBytes32(r))
if err != nil {
return 0, errors.Wrap(err, "could not interface with committee cache")
}
if proposerIndices != nil {
if len(proposerIndices) != int(params.BeaconConfig().SlotsPerEpoch) {
return 0, errors.Errorf("length of proposer indices is not equal %d to slots per epoch", len(proposerIndices))
}
return proposerIndices[state.Slot()%params.BeaconConfig().SlotsPerEpoch], nil
}
if err := UpdateProposerIndicesInCache(state, e); err != nil {
return 0, errors.Wrap(err, "could not update committee cache")
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions beacon-chain/state/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,32 @@ func (b *BeaconState) stateRoots() [][]byte {
return b.safeCopy2DByteSlice(b.state.StateRoots)
}

// StateRootAtIndex retrieves a specific state root based on an
// input index value.
func (b *BeaconState) StateRootAtIndex(idx uint64) ([]byte, error) {
if !b.HasInnerState() {
return nil, ErrNilInnerState
}
if b.state.StateRoots == nil {
return nil, nil
}

b.lock.RLock()
defer b.lock.RUnlock()

return b.stateRootAtIndex(idx)
}

// stateRootAtIndex retrieves a specific state root based on an
// input index value.
// This assumes that a lock is already held on BeaconState.
func (b *BeaconState) stateRootAtIndex(idx uint64) ([]byte, error) {
if !b.HasInnerState() {
return nil, ErrNilInnerState
}
return b.safeCopyBytesAtIndex(b.state.StateRoots, idx)
}

// HistoricalRoots based on epochs stored in the beacon state.
func (b *BeaconState) HistoricalRoots() [][]byte {
if !b.HasInnerState() {
Expand Down

0 comments on commit 329fbff

Please sign in to comment.