prysm-pulse/beacon-chain/p2p/peers/peerdata/store_test.go
Manu NALEPA 65f71b3a48
P2P: Simplify code (#13719)
* `subscribeStaticWithSubnets`: Fix docstring.

* `buildOptions`: Avoid `options` mutations.

* `dv5Cfg`: Avoid mutation.

* `RefreshENR`: Use default for all but Phase0.

* `udp4`, `udp6`: Create enum.

* `p2p.Config`: `BootstrapNodeAddr`==> `BootstrapNodeAddrs`.

* `p2p.Config`: `Discv5BootStrapAddr` ==> `Discv5BootStrapAddrs`.

* `TestScorers_BadResponses_Score`: Improve.

* `BeaconNode`: Avoid mutation.

* `TestStore_TrustedPeers`: Remove blankline.

* Remove blank identifiers.

* `privKey`: Keep the majority of code with low indentation.

* `P2PPreregistration`: Return error instead of fatal log.

* `parseBootStrapAddrs` => `ParseBootStrapAddrs` (export)

* `p2p.Config`: Remove `BootstrapNodeAddrs`.

* `NewService`: Avoid mutation when possible.

* `Service`: Remove blank identifier.

* `buildOptions`: Avoid `log.Fatalf` (make deepsource happy).

* `registerGRPCGateway`: Use `net.JoinHostPort` (make deepsource happy).

* `registerBuilderService`: Make deepsource happy.

* `scorers`: Add `NoLock` suffix (make deepsource happy).

* `scorerr`: Add some `NoLock`suffixes (making deepsource happy).

* `discovery_test.go`. Remove init.

Rationale:
`rand.Seed` is deprecated: As of Go 1.20 there is no reason to call Seed with a random value. Programs that call Seed with a known value to get a specific sequence of results should use New(NewSource(seed)) to obtain a local random generator.

This makes deepsource happy as well.

* `createListener`: Reduce cyclomatic complexity (make deepsource happy).

* `startDB`: Reduce cyclomatic complexity (make deepsource happy).

* `main`: Log a FATAL on error.

This way, the error message is very readable.
Before this commit, the error message is the less readable
message in the logs.

* `New`: Reduce cyclomatic complexity (make deepsource happy).

* `main`: Avoid `App` mutation, and make deepsource happy.

* Update beacon-chain/node/node.go

Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com>

* `bootnodes` ==> `BootNodes` (Fix PR comment).

* Remove duplicate `configureFastSSZHashingAlgorithm` since already done in `configureBeacon`. (Fix PR comment)

* Add `TestCreateLocalNode`. (PR comment fix.)

* `startModules` ==> `startBaseServices (Fix PR comment).

* `buildOptions` return errors consistently.

* `New`: Change ordering.

---------

Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com>
2024-03-15 11:08:19 +00:00

111 lines
3.1 KiB
Go

package peerdata_test
import (
"context"
"testing"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p/peers/peerdata"
"github.com/prysmaticlabs/prysm/v5/testing/assert"
"github.com/prysmaticlabs/prysm/v5/testing/require"
)
func TestStore_GetSetDelete(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store := peerdata.NewStore(ctx, &peerdata.StoreConfig{
MaxPeers: 12,
})
assert.NotNil(t, store)
assert.Equal(t, 12, store.Config().MaxPeers)
// Check non-existent data.
pid := peer.ID("00001")
peerData, ok := store.PeerData(pid)
assert.Equal(t, false, ok)
assert.Equal(t, (*peerdata.PeerData)(nil), peerData)
assert.Equal(t, 0, len(store.Peers()))
// Associate data.
store.SetPeerData(pid, &peerdata.PeerData{
BadResponses: 3,
ProcessedBlocks: 42,
})
peerData, ok = store.PeerData(pid)
assert.Equal(t, true, ok)
assert.Equal(t, 3, peerData.BadResponses)
assert.Equal(t, uint64(42), peerData.ProcessedBlocks)
require.Equal(t, 1, len(store.Peers()))
peers := store.Peers()
_, ok = peers[pid]
require.Equal(t, true, ok)
assert.Equal(t, 3, peers[pid].BadResponses)
assert.Equal(t, uint64(42), peers[pid].ProcessedBlocks)
// Remove data from peer.
store.DeletePeerData(pid)
peerData, ok = store.PeerData(pid)
assert.Equal(t, false, ok)
assert.Equal(t, (*peerdata.PeerData)(nil), peerData)
assert.Equal(t, 0, len(store.Peers()))
}
func TestStore_PeerDataGetOrCreate(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store := peerdata.NewStore(ctx, &peerdata.StoreConfig{
MaxPeers: 12,
})
assert.NotNil(t, store)
assert.Equal(t, 12, store.Config().MaxPeers)
pid := peer.ID("00001")
peerData, ok := store.PeerData(pid)
assert.Equal(t, false, ok)
assert.Equal(t, (*peerdata.PeerData)(nil), peerData)
assert.Equal(t, 0, len(store.Peers()))
peerData = store.PeerDataGetOrCreate(pid)
assert.NotNil(t, peerData)
assert.Equal(t, 0, peerData.BadResponses)
assert.Equal(t, uint64(0), peerData.ProcessedBlocks)
require.Equal(t, 1, len(store.Peers()))
// Method must be idempotent, check.
peerData = store.PeerDataGetOrCreate(pid)
assert.NotNil(t, peerData)
assert.Equal(t, 0, peerData.BadResponses)
assert.Equal(t, uint64(0), peerData.ProcessedBlocks)
require.Equal(t, 1, len(store.Peers()))
}
func TestStore_TrustedPeers(t *testing.T) {
store := peerdata.NewStore(context.Background(), &peerdata.StoreConfig{
MaxPeers: 12,
})
pid1 := peer.ID("00001")
pid2 := peer.ID("00002")
pid3 := peer.ID("00003")
tPeers := []peer.ID{pid1, pid2, pid3}
store.SetTrustedPeers(tPeers)
assert.Equal(t, true, store.IsTrustedPeer(pid1))
assert.Equal(t, true, store.IsTrustedPeer(pid2))
assert.Equal(t, true, store.IsTrustedPeer(pid3))
tPeers = store.GetTrustedPeers()
assert.Equal(t, 3, len(tPeers))
store.DeleteTrustedPeers(tPeers)
tPeers = store.GetTrustedPeers()
assert.Equal(t, 0, len(tPeers))
assert.Equal(t, false, store.IsTrustedPeer(pid1))
assert.Equal(t, false, store.IsTrustedPeer(pid2))
assert.Equal(t, false, store.IsTrustedPeer(pid3))
}