Skip to content

Commit

Permalink
Add locks to forkchoice spec tests (#12165)
Browse files Browse the repository at this point in the history
* Add locks to forkchoice spec testS

* Fix

* Rm forkchoice store getter

* Rm useless lock
  • Loading branch information
terencechain committed Mar 21, 2023
1 parent 5bb1491 commit 79da486
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
5 changes: 0 additions & 5 deletions beacon-chain/blockchain/chain_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 1 addition & 7 deletions beacon-chain/blockchain/chain_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 14 additions & 3 deletions testing/spectest/shared/common/forkchoice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}

Expand Down

0 comments on commit 79da486

Please sign in to comment.