From 0a4e42545e22187311b7137edef0d45e00a5bd09 Mon Sep 17 00:00:00 2001 From: terencechain Date: Sun, 11 Jun 2023 21:30:06 -0700 Subject: [PATCH] Use next slot cache for sync committee (#12287) * Use next slot cache for sync committee * RWMutex * change mutex for last cached state * feat: change mutex * test: add db --------- Co-authored-by: Nishant Das Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- beacon-chain/blockchain/head_sync_committee_info.go | 6 +++++- beacon-chain/blockchain/head_sync_committee_info_test.go | 9 +++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/beacon-chain/blockchain/head_sync_committee_info.go b/beacon-chain/blockchain/head_sync_committee_info.go index 281861e4c6..8262bfe01d 100644 --- a/beacon-chain/blockchain/head_sync_committee_info.go +++ b/beacon-chain/blockchain/head_sync_committee_info.go @@ -157,7 +157,11 @@ func (s *Service) getSyncCommitteeHeadState(ctx context.Context, slot primitives if headState == nil || headState.IsNil() { return nil, errors.New("nil state") } - headState, err = transition.ProcessSlotsIfPossible(ctx, headState, slot) + headRoot, err := s.HeadRoot(ctx) + if err != nil { + return nil, err + } + headState, err = transition.ProcessSlotsUsingNextSlotCache(ctx, headState, headRoot, slot) if err != nil { return nil, err } diff --git a/beacon-chain/blockchain/head_sync_committee_info_test.go b/beacon-chain/blockchain/head_sync_committee_info_test.go index 3ec8331386..7e4c4bbf74 100644 --- a/beacon-chain/blockchain/head_sync_committee_info_test.go +++ b/beacon-chain/blockchain/head_sync_committee_info_test.go @@ -6,6 +6,7 @@ import ( "github.com/prysmaticlabs/prysm/v4/beacon-chain/cache" "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/signing" + dbTest "github.com/prysmaticlabs/prysm/v4/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/v4/config/params" "github.com/prysmaticlabs/prysm/v4/consensus-types/primitives" "github.com/prysmaticlabs/prysm/v4/testing/require" @@ -15,7 +16,7 @@ import ( func TestService_HeadSyncCommitteeIndices(t *testing.T) { s, _ := util.DeterministicGenesisStateAltair(t, params.BeaconConfig().TargetCommitteeSize) - c := &Service{} + c := &Service{cfg: &config{BeaconDB: dbTest.SetupDB(t)}} c.head = &head{state: s} // Current period @@ -38,7 +39,7 @@ func TestService_HeadSyncCommitteeIndices(t *testing.T) { func TestService_headCurrentSyncCommitteeIndices(t *testing.T) { s, _ := util.DeterministicGenesisStateAltair(t, params.BeaconConfig().TargetCommitteeSize) - c := &Service{} + c := &Service{cfg: &config{BeaconDB: dbTest.SetupDB(t)}} c.head = &head{state: s} // Process slot up to `EpochsPerSyncCommitteePeriod` so it can `ProcessSyncCommitteeUpdates`. @@ -66,7 +67,7 @@ func TestService_headNextSyncCommitteeIndices(t *testing.T) { func TestService_HeadSyncCommitteePubKeys(t *testing.T) { s, _ := util.DeterministicGenesisStateAltair(t, params.BeaconConfig().TargetCommitteeSize) - c := &Service{} + c := &Service{cfg: &config{BeaconDB: dbTest.SetupDB(t)}} c.head = &head{state: s} // Process slot up to 2 * `EpochsPerSyncCommitteePeriod` so it can run `ProcessSyncCommitteeUpdates` twice. @@ -81,7 +82,7 @@ func TestService_HeadSyncCommitteePubKeys(t *testing.T) { func TestService_HeadSyncCommitteeDomain(t *testing.T) { s, _ := util.DeterministicGenesisStateAltair(t, params.BeaconConfig().TargetCommitteeSize) - c := &Service{} + c := &Service{cfg: &config{BeaconDB: dbTest.SetupDB(t)}} c.head = &head{state: s} wanted, err := signing.Domain(s.Fork(), slots.ToEpoch(s.Slot()), params.BeaconConfig().DomainSyncCommittee, s.GenesisValidatorsRoot())