Files
prysm/beacon-chain/p2p/pubsub_test.go
Preston Van Loon 2fd6bd8150 Add golang.org/x/tools modernize static analyzer and fix violations (#15946)
* Ran gopls modernize to fix everything

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

* Override rules_go provided dependency for golang.org/x/tools to v0.38.0.

To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches.

* Fix buildtag violations and ignore buildtag violations in external

* Introduce modernize analyzer package.

* Add modernize "any" analyzer.

* Fix violations of any analyzer

* Add modernize "appendclipped" analyzer.

* Fix violations of appendclipped

* Add modernize "bloop" analyzer.

* Add modernize "fmtappendf" analyzer.

* Add modernize "forvar" analyzer.

* Add modernize "mapsloop" analyzer.

* Add modernize "minmax" analyzer.

* Fix violations of minmax analyzer

* Add modernize "omitzero" analyzer.

* Add modernize "rangeint" analyzer.

* Fix violations of rangeint.

* Add modernize "reflecttypefor" analyzer.

* Fix violations of reflecttypefor analyzer.

* Add modernize "slicescontains" analyzer.

* Add modernize "slicessort" analyzer.

* Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686.

* Add modernize "stringscutprefix" analyzer.

* Add modernize "stringsbuilder" analyzer.

* Fix violations of stringsbuilder analyzer.

* Add modernize "stringsseq" analyzer.

* Add modernize "testingcontext" analyzer.

* Add modernize "waitgroup" analyzer.

* Changelog fragment

* gofmt

* gazelle

* Add modernize "newexpr" analyzer.

* Disable newexpr until go1.26

* Add more details in WORKSPACE on how to update the override

* @nalepae feedback on min()

* gofmt

* Fix violations of forvar
2025-11-14 01:27:22 +00:00

142 lines
3.6 KiB
Go

package p2p
import (
"context"
"fmt"
"sync"
"testing"
"time"
mock "github.com/OffchainLabs/prysm/v7/beacon-chain/blockchain/testing"
testDB "github.com/OffchainLabs/prysm/v7/beacon-chain/db/testing"
"github.com/OffchainLabs/prysm/v7/beacon-chain/p2p/encoder"
testp2p "github.com/OffchainLabs/prysm/v7/beacon-chain/p2p/testing"
"github.com/OffchainLabs/prysm/v7/beacon-chain/startup"
"github.com/OffchainLabs/prysm/v7/testing/assert"
"github.com/OffchainLabs/prysm/v7/testing/require"
"github.com/pkg/errors"
)
func TestService_PublishToTopicConcurrentMapWrite(t *testing.T) {
cs := startup.NewClockSynchronizer()
s, err := NewService(t.Context(), &Config{
StateNotifier: &mock.MockStateNotifier{},
ClockWaiter: cs,
DB: testDB.SetupDB(t),
})
require.NoError(t, err)
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
defer cancel()
go s.awaitStateInitialized()
fd := initializeStateWithForkDigest(ctx, t, cs)
if !s.isInitialized() {
t.Fatal("service was not initialized")
}
// Set up two connected test hosts.
p0 := testp2p.NewTestP2P(t)
p1 := testp2p.NewTestP2P(t)
p0.Connect(p1)
s.host = p0.BHost
s.pubsub = p0.PubSub()
topic := fmt.Sprintf(BlockSubnetTopicFormat, fd) + "/" + encoder.ProtocolSuffixSSZSnappy
// Establish the remote peer to be subscribed to the outgoing topic.
_, err = p1.SubscribeToTopic(topic)
require.NoError(t, err)
wg := sync.WaitGroup{}
wg.Add(10)
for i := range 10 {
go func(i int) {
assert.NoError(t, s.PublishToTopic(ctx, topic, []byte{}))
wg.Done()
}(i)
}
wg.Wait()
}
func TestExtractGossipDigest(t *testing.T) {
tests := []struct {
name string
topic string
want [4]byte
wantErr bool
error error
}{
{
name: "empty topic",
topic: "",
want: [4]byte{},
wantErr: true,
error: errors.New("invalid topic format"),
},
{
name: "too short topic",
topic: "/eth2/",
want: [4]byte{},
wantErr: true,
error: errors.New("invalid topic format"),
},
{
name: "bogus topic prefix",
topic: "/eth3/b5303f2a/beacon_coin",
want: [4]byte{},
wantErr: true,
error: errors.New("invalid topic format"),
},
{
name: "invalid digest in topic",
topic: "/eth2/zzxxyyaa/beacon_block" + "/" + encoder.ProtocolSuffixSSZSnappy,
want: [4]byte{},
wantErr: true,
error: errors.New("encoding/hex: invalid byte"),
},
{
name: "short digest",
topic: fmt.Sprintf(BlockSubnetTopicFormat, []byte{0xb5, 0x30, 0x3f}) + "/" + encoder.ProtocolSuffixSSZSnappy,
want: [4]byte{},
wantErr: true,
error: errors.New("invalid digest length wanted"),
},
{
name: "too short topic, missing suffixes",
topic: "/eth2/b5303f2a",
want: [4]byte{},
wantErr: true,
error: errors.New("invalid topic format"),
},
{
name: "valid topic",
topic: fmt.Sprintf(BlockSubnetTopicFormat, []byte{0xb5, 0x30, 0x3f, 0x2a}) + "/" + encoder.ProtocolSuffixSSZSnappy,
want: [4]byte{0xb5, 0x30, 0x3f, 0x2a},
wantErr: false,
error: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ExtractGossipDigest(tt.topic)
assert.Equal(t, err != nil, tt.wantErr)
if tt.wantErr {
assert.ErrorContains(t, tt.error.Error(), err)
}
assert.DeepEqual(t, tt.want, got)
})
}
}
func BenchmarkExtractGossipDigest(b *testing.B) {
topic := fmt.Sprintf(BlockSubnetTopicFormat, []byte{0xb5, 0x30, 0x3f, 0x2a}) + "/" + encoder.ProtocolSuffixSSZSnappy
for b.Loop() {
_, err := ExtractGossipDigest(topic)
if err != nil {
b.Fatal(err)
}
}
}