Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add locks to forkchoice spec tests #12165

Merged
merged 5 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
potuz marked this conversation as resolved.
Show resolved Hide resolved
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()
potuz marked this conversation as resolved.
Show resolved Hide resolved
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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a ForkChoiceStore and ForkChoicer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove one, they are exactly the same method defined in the same file. I do not know from where they are. We should in fact try to remove them both and only export locked wrappers to forkchoice

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