Skip to content

Commit 527c0de

Browse files
committed
fix(network,core): adapt to avalanchego APIs, fix metrics clash, stabilize chain config I/O
- Alias libevm metrics import to evmmetrics and update usages to avoid symbol collision with local metrics in the network and network/stats packages. - Pass a copied extras object into customrawdb ReadChainConfig/WriteChainConfig to avoid data races and shared-mutation bugs under new avalanchego versions.
1 parent 0d5eb51 commit 527c0de

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

core/genesis.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,15 @@ func SetupGenesisBlock(
182182
return newcfg, common.Hash{}, err
183183
}
184184

185-
extra := params.GetExtra(newcfg)
185+
// Avoid mutating shared extras attached to newcfg (tests may reuse global configs).
186+
extra := *params.GetExtra(newcfg)
186187

187-
storedcfg := customrawdb.ReadChainConfig(db, stored, extra)
188+
storedcfg := customrawdb.ReadChainConfig(db, stored, &extra)
188189
// If there is no previously stored chain config, write the chain config to disk.
189190
if storedcfg == nil {
190191
// Note: this can happen since we did not previously write the genesis block and chain config in the same batch.
191192
log.Warn("Found genesis block without chain config")
192-
customrawdb.WriteChainConfig(db, stored, newcfg, *extra)
193+
customrawdb.WriteChainConfig(db, stored, newcfg, extra)
193194
return newcfg, stored, nil
194195
}
195196

@@ -228,7 +229,7 @@ func SetupGenesisBlock(
228229
}
229230
// Required to write the chain config to disk to ensure both the chain config and upgrade bytes are persisted to disk.
230231
// Note: this intentionally removes an extra check from upstream.
231-
customrawdb.WriteChainConfig(db, stored, newcfg, *extra)
232+
customrawdb.WriteChainConfig(db, stored, newcfg, extra)
232233
return newcfg, stored, nil
233234
}
234235

@@ -405,8 +406,9 @@ func (g *Genesis) Commit(db ethdb.Database, triedb *triedb.Database) (*types.Blo
405406
rawdb.WriteHeadBlockHash(batch, block.Hash())
406407
rawdb.WriteHeadHeaderHash(batch, block.Hash())
407408

408-
extra := params.GetExtra(config)
409-
customrawdb.WriteChainConfig(batch, block.Hash(), config, *extra)
409+
// Avoid mutating shared extras attached to config
410+
extraCommit := *params.GetExtra(config)
411+
customrawdb.WriteChainConfig(batch, block.Hash(), config, extraCommit)
410412
if err := batch.Write(); err != nil {
411413
return nil, fmt.Errorf("failed to write genesis block: %w", err)
412414
}

network/peer_tracker.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/ava-labs/avalanchego/utils/set"
1313
"github.com/ava-labs/avalanchego/version"
1414
"github.com/ava-labs/libevm/log"
15-
"github.com/ava-labs/libevm/metrics"
15+
evmmetrics "github.com/ava-labs/libevm/metrics" // alias to avoid name collision with local 'metrics' in this package
1616

1717
safemath "github.com/ava-labs/avalanchego/utils/math"
1818
)
@@ -42,24 +42,24 @@ type peerInfo struct {
4242
// Note: is not thread safe, caller must handle synchronization.
4343
type peerTracker struct {
4444
peers map[ids.NodeID]*peerInfo // all peers we are connected to
45-
numTrackedPeers metrics.Gauge
45+
numTrackedPeers evmmetrics.Gauge
4646
trackedPeers set.Set[ids.NodeID] // peers that we have sent a request to
47-
numResponsivePeers metrics.Gauge
47+
numResponsivePeers evmmetrics.Gauge
4848
responsivePeers set.Set[ids.NodeID] // peers that responded to the last request they were sent
4949
bandwidthHeap safemath.AveragerHeap // tracks bandwidth peers are responding with
50-
averageBandwidthMetric metrics.GaugeFloat64
50+
averageBandwidthMetric evmmetrics.GaugeFloat64
5151
averageBandwidth safemath.Averager
5252
}
5353

5454
func NewPeerTracker() *peerTracker {
5555
return &peerTracker{
5656
peers: make(map[ids.NodeID]*peerInfo),
57-
numTrackedPeers: metrics.GetOrRegisterGauge("net_tracked_peers", nil),
57+
numTrackedPeers: evmmetrics.GetOrRegisterGauge("net_tracked_peers", nil),
5858
trackedPeers: make(set.Set[ids.NodeID]),
59-
numResponsivePeers: metrics.GetOrRegisterGauge("net_responsive_peers", nil),
59+
numResponsivePeers: evmmetrics.GetOrRegisterGauge("net_responsive_peers", nil),
6060
responsivePeers: make(set.Set[ids.NodeID]),
6161
bandwidthHeap: safemath.NewMaxAveragerHeap(),
62-
averageBandwidthMetric: metrics.GetOrRegisterGaugeFloat64("net_average_bandwidth", nil),
62+
averageBandwidthMetric: evmmetrics.GetOrRegisterGaugeFloat64("net_average_bandwidth", nil),
6363
averageBandwidth: safemath.NewAverager(0, bandwidthHalflife, time.Now()),
6464
}
6565
}

network/stats/stats.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package stats
66
import (
77
"time"
88

9-
"github.com/ava-labs/libevm/metrics"
9+
evmmetrics "github.com/ava-labs/libevm/metrics" // alias to avoid name collision with local 'metrics' in this package
1010
)
1111

1212
// RequestHandlerStats provides the interface for metrics for app requests.
@@ -16,8 +16,8 @@ type RequestHandlerStats interface {
1616
}
1717

1818
type requestHandlerStats struct {
19-
timeUntilDeadline metrics.Timer
20-
droppedRequests metrics.Counter
19+
timeUntilDeadline evmmetrics.Timer
20+
droppedRequests evmmetrics.Counter
2121
}
2222

2323
func (h *requestHandlerStats) IncDeadlineDroppedRequest() {
@@ -30,7 +30,7 @@ func (h *requestHandlerStats) UpdateTimeUntilDeadline(duration time.Duration) {
3030

3131
func NewRequestHandlerStats() RequestHandlerStats {
3232
return &requestHandlerStats{
33-
timeUntilDeadline: metrics.GetOrRegisterTimer("net_req_time_until_deadline", nil),
34-
droppedRequests: metrics.GetOrRegisterCounter("net_req_deadline_dropped", nil),
33+
timeUntilDeadline: evmmetrics.GetOrRegisterTimer("net_req_time_until_deadline", nil),
34+
droppedRequests: evmmetrics.GetOrRegisterCounter("net_req_deadline_dropped", nil),
3535
}
3636
}

0 commit comments

Comments
 (0)