From 79da4865081f3a740f3db091280838519be3433a Mon Sep 17 00:00:00 2001 From: terencechain Date: Tue, 21 Mar 2023 11:39:40 -0700 Subject: [PATCH] Add locks to forkchoice spec tests (#12165) * Add locks to forkchoice spec testS * Fix * Rm forkchoice store getter * Rm useless lock --- beacon-chain/blockchain/chain_info.go | 5 ----- beacon-chain/blockchain/chain_info_test.go | 8 +------- .../shared/common/forkchoice/builder.go | 17 ++++++++++++++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/beacon-chain/blockchain/chain_info.go b/beacon-chain/blockchain/chain_info.go index 775e752ff85..2246b4b7b0c 100644 --- a/beacon-chain/blockchain/chain_info.go +++ b/beacon-chain/blockchain/chain_info.go @@ -484,8 +484,3 @@ func (s *Service) Ancestor(ctx context.Context, root []byte, slot primitives.Slo func (s *Service) SetGenesisTime(t time.Time) { s.genesisTime = t } - -// ForkChoiceStore returns the fork choice store in the service. -func (s *Service) ForkChoiceStore() forkchoice.ForkChoicer { - return s.cfg.ForkChoiceStore -} diff --git a/beacon-chain/blockchain/chain_info_test.go b/beacon-chain/blockchain/chain_info_test.go index 4c8359e8d42..c9ba86d8109 100644 --- a/beacon-chain/blockchain/chain_info_test.go +++ b/beacon-chain/blockchain/chain_info_test.go @@ -71,12 +71,6 @@ func TestHeadRoot_Nil(t *testing.T) { assert.DeepEqual(t, params.BeaconConfig().ZeroHash[:], headRoot, "Incorrect pre chain start value") } -func TestService_ForkChoiceStore(t *testing.T) { - c := &Service{cfg: &config{ForkChoiceStore: doublylinkedtree.New()}} - p := c.ForkChoiceStore() - require.Equal(t, primitives.Epoch(0), p.FinalizedCheckpoint().Epoch) -} - func TestFinalizedCheckpt_GenesisRootOk(t *testing.T) { ctx := context.Background() beaconDB := testDB.SetupDB(t) @@ -559,7 +553,7 @@ func TestService_IsFinalized(t *testing.T) { ctx := context.Background() c := &Service{cfg: &config{BeaconDB: beaconDB, ForkChoiceStore: doublylinkedtree.New()}} r1 := [32]byte{'a'} - require.NoError(t, c.ForkChoiceStore().UpdateFinalizedCheckpoint(&forkchoicetypes.Checkpoint{ + require.NoError(t, c.ForkChoicer().UpdateFinalizedCheckpoint(&forkchoicetypes.Checkpoint{ Root: r1, })) b := util.NewBeaconBlock() diff --git a/testing/spectest/shared/common/forkchoice/builder.go b/testing/spectest/shared/common/forkchoice/builder.go index 1e3fc413cd1..6399b96d371 100644 --- a/testing/spectest/shared/common/forkchoice/builder.go +++ b/testing/spectest/shared/common/forkchoice/builder.go @@ -38,6 +38,9 @@ func NewBuilder(t testing.TB, initialState state.BeaconState, initialBlock inter // Tick resets the genesis time to now()-tick and adjusts the slot to the appropriate value. func (bb *Builder) Tick(t testing.TB, tick int64) { + bb.service.ForkChoicer().Lock() + defer bb.service.ForkChoicer().Unlock() + bb.service.SetGenesisTime(time.Unix(time.Now().Unix()-tick, 0)) lastSlot := uint64(bb.lastTick) / params.BeaconConfig().SecondsPerSlot currentSlot := uint64(tick) / params.BeaconConfig().SecondsPerSlot @@ -104,11 +107,17 @@ func (bb *Builder) PoWBlock(pb *ethpb.PowBlock) { // Attestation receives the attestation and updates forkchoice. func (bb *Builder) Attestation(t testing.TB, a *ethpb.Attestation) { + bb.service.ForkChoicer().Lock() + defer bb.service.ForkChoicer().Unlock() + require.NoError(t, bb.service.OnAttestation(context.TODO(), a, params.BeaconNetworkConfig().MaximumGossipClockDisparity)) } // AttesterSlashing receives an attester slashing and feeds it to forkchoice. func (bb *Builder) AttesterSlashing(s *ethpb.AttesterSlashing) { + bb.service.ForkChoicer().Lock() + defer bb.service.ForkChoicer().Unlock() + slashings := []*ethpb.AttesterSlashing{s} bb.service.InsertSlashingsToForkChoiceStore(context.TODO(), slashings) } @@ -119,7 +128,9 @@ func (bb *Builder) Check(t testing.TB, c *Check) { return } ctx := context.TODO() + bb.service.ForkChoicer().Lock() require.NoError(t, bb.service.UpdateAndSaveHeadWithBalances(ctx)) + bb.service.ForkChoicer().Unlock() if c.Head != nil { r, err := bb.service.HeadRoot(ctx) require.NoError(t, err) @@ -144,9 +155,9 @@ func (bb *Builder) Check(t testing.TB, c *Check) { } if c.ProposerBoostRoot != nil { want := fmt.Sprintf("%#x", common.FromHex(*c.ProposerBoostRoot)) - bb.service.ForkChoiceStore().RLock() - got := fmt.Sprintf("%#x", bb.service.ForkChoiceStore().ProposerBoost()) - bb.service.ForkChoiceStore().RUnlock() + bb.service.ForkChoicer().RLock() + got := fmt.Sprintf("%#x", bb.service.ForkChoicer().ProposerBoost()) + bb.service.ForkChoicer().RUnlock() require.DeepEqual(t, want, got) }