Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 4 additions & 1 deletion execution/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ type EngineClient struct {
// clientMetrics is the metrics for the engine client.
metrics *clientMetrics
// capabilities is a map of capabilities that the execution client has.
capabilities map[string]struct{}
capabilitiesMu sync.RWMutex
capabilities map[string]struct{}
// connected will be set to true when we have successfully connected
// to the execution client.
connectedMu sync.RWMutex
Expand Down Expand Up @@ -166,6 +167,8 @@ func (s *EngineClient) IsConnected() bool {
}

func (s *EngineClient) HasCapability(capability string) bool {
s.capabilitiesMu.RLock()
defer s.capabilitiesMu.RUnlock()
_, ok := s.capabilities[capability]
return ok
}
Expand Down
62 changes: 62 additions & 0 deletions execution/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package client

import (
"sync"
"testing"
)

// TestCapabilitiesRace verifies that concurrent writes (ExchangeCapabilities)
// and reads (HasCapability) on the capabilities map do not race.
func TestCapabilitiesRace(t *testing.T) {
t.Parallel()

c := &EngineClient{
capabilities: make(map[string]struct{}),
}

const cap = "engine_newPayloadV3"
const iterations = 1000

var wg sync.WaitGroup
wg.Add(2)

// Goroutine 1: repeatedly write a capability (simulates ExchangeCapabilities).
go func() {
defer wg.Done()
for range iterations {
c.capabilitiesMu.Lock()
c.capabilities[cap] = struct{}{}
c.capabilitiesMu.Unlock()
}
}()

// Goroutine 2: repeatedly read a capability (simulates HasCapability).
go func() {
defer wg.Done()
for range iterations {
c.HasCapability(cap)
}
Comment on lines +46 to +58
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

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

for range iterations does not compile because iterations is an untyped integer constant, not an iterable. Use a counted loop (e.g., incrementing index) or range over a slice of length iterations so the test builds and can be exercised under go test -race.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

for range iterations is valid Go 1.22+ syntax for ranging over an integer constant, and this repo targets Go 1.26, so this compiles and runs correctly

}()

wg.Wait()
}
10 changes: 8 additions & 2 deletions execution/client/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,17 @@ func (s *EngineClient) ExchangeCapabilities(
return nil, err
}

// Capture and log the capabilities that the execution client has.
// Write all capabilities under a single lock acquisition.
s.capabilitiesMu.Lock()
for _, capability := range result {
s.logger.Info("Exchanged capability", "capability", capability)
s.capabilities[capability] = struct{}{}
}
s.capabilitiesMu.Unlock()

// Log after the lock is released to avoid blocking readers during I/O.
for _, capability := range result {
s.logger.Info("Exchanged capability", "capability", capability)
}

// Log the capabilities that the execution client does not have.
for _, capability := range ethclient.BeaconKitSupportedCapabilities() {
Expand Down