Files
prysm/beacon-chain/sync/rpc_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

128 lines
3.9 KiB
Go

package sync
import (
"bytes"
"context"
"sync"
"testing"
"time"
"github.com/OffchainLabs/prysm/v7/beacon-chain/core/transition"
prysmP2P "github.com/OffchainLabs/prysm/v7/beacon-chain/p2p"
"github.com/OffchainLabs/prysm/v7/beacon-chain/p2p/encoder"
p2ptest "github.com/OffchainLabs/prysm/v7/beacon-chain/p2p/testing"
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
"github.com/OffchainLabs/prysm/v7/testing/assert"
"github.com/OffchainLabs/prysm/v7/testing/require"
"github.com/OffchainLabs/prysm/v7/testing/util"
libp2pcore "github.com/libp2p/go-libp2p/core"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/protocol"
)
func init() {
transition.SkipSlotCache.Disable()
}
// expectSuccess status code from a stream in regular sync.
func expectSuccess(t *testing.T, stream network.Stream) {
code, errMsg, err := ReadStatusCode(stream, &encoder.SszNetworkEncoder{})
require.NoError(t, err)
require.Equal(t, uint8(0), code, "Received non-zero response code")
require.Equal(t, "", errMsg, "Received error message from stream")
}
// expectSuccess status code from a stream in regular sync.
func expectFailure(t *testing.T, expectedCode uint8, expectedErrorMsg string, stream network.Stream) {
code, errMsg, err := ReadStatusCode(stream, &encoder.SszNetworkEncoder{})
require.NoError(t, err)
require.NotEqual(t, uint8(0), code, "Expected request to fail but got a 0 response code")
require.Equal(t, expectedCode, code, "Received incorrect response code")
require.Equal(t, expectedErrorMsg, errMsg)
}
// expectResetStream status code from a stream in regular sync.
func expectResetStream(t *testing.T, stream network.Stream) {
expectedErr := "stream reset"
_, _, err := ReadStatusCode(stream, &encoder.SszNetworkEncoder{})
require.ErrorContains(t, expectedErr, err)
}
func TestRegisterRPC_ReceivesValidMessage(t *testing.T) {
p2p := p2ptest.NewTestP2P(t)
r := &Service{
ctx: t.Context(),
cfg: &config{p2p: p2p},
rateLimiter: newRateLimiter(p2p),
}
var wg sync.WaitGroup
wg.Add(1)
topic := "/testing/foobar/1"
handler := func(ctx context.Context, msg any, stream libp2pcore.Stream) error {
m, ok := msg.(*ethpb.Fork)
if !ok {
t.Error("Object is not of type *pb.TestSimpleMessage")
}
assert.DeepEqual(t, []byte("fooo"), m.CurrentVersion, "Unexpected incoming message")
wg.Done()
return nil
}
prysmP2P.RPCTopicMappings[topic] = new(ethpb.Fork)
// Cleanup Topic mappings
defer func() {
delete(prysmP2P.RPCTopicMappings, topic)
}()
r.registerRPC(topic, handler)
p2p.ReceiveRPC(topic, &ethpb.Fork{CurrentVersion: []byte("fooo"), PreviousVersion: []byte("barr")})
if util.WaitTimeout(&wg, time.Second) {
t.Fatal("Did not receive RPC in 1 second")
}
}
func TestRPC_ReceivesInvalidMessage(t *testing.T) {
p2p := p2ptest.NewTestP2P(t)
remotePeer := p2ptest.NewTestP2P(t)
remotePeer.Connect(p2p)
r := &Service{
ctx: t.Context(),
cfg: &config{p2p: p2p},
rateLimiter: newRateLimiter(p2p),
}
topic := "/testing/foobar/1"
handler := func(ctx context.Context, msg any, stream libp2pcore.Stream) error {
m, ok := msg.(*ethpb.Fork)
if !ok {
t.Error("Object is not of type *pb.Fork")
}
if !bytes.Equal(m.CurrentVersion, []byte("fooo")) {
t.Errorf("Unexpected incoming message: %+v", m)
}
return nil
}
prysmP2P.RPCTopicMappings[topic] = new(ethpb.Fork)
// Cleanup Topic mappings
defer func() {
delete(prysmP2P.RPCTopicMappings, topic)
}()
r.registerRPC(topic, handler)
stream, err := remotePeer.Host().NewStream(t.Context(), p2p.BHost.ID(), protocol.ID(topic+p2p.Encoding().ProtocolSuffix()))
require.NoError(t, err)
// Write invalid SSZ object to peer.
_, err = stream.Write([]byte("JUNK MESSAGE"))
require.NoError(t, err)
time.Sleep(1 * time.Second)
faultCount, err := p2p.Peers().Scorers().BadResponsesScorer().Count(remotePeer.BHost.ID())
require.NoError(t, err)
assert.Equal(t, 1, faultCount, "peer was not penalised for sending bad message")
}