mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 21:38:05 -05:00
* Subnets subscription: Avoid dynamic subscribing blocking in case not enough peers per subnets are found. * `subscribeWithParameters`: Use struct to avoid too many function parameters (no functional changes). * Optimise subnets search. Currently, when we are looking for peers in let's say data column sidecars subnets 3, 6 and 7, we first look for peers in subnet 3. If, during the crawling, we meet some peers with subnet 6, we discard them (because we are exclusively looking for peers with subnet 3). When we are happy, we start again with peers with subnet 6. This commit optimizes that by looking for peers with satisfy our constraints in one look. * Fix James' comment. * Fix James' comment. * Fix James' comment. * Fix James' commnet. * Fix James' comment. * Fix James' comment. * Fix James's comment. * Simplify following James' comment. * Fix James' comment. * Update beacon-chain/sync/rpc_goodbye.go Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com> * Update config/params/config.go Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com> * Update beacon-chain/sync/subscriber.go Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com> * Fix Preston's comment. * Fix Preston's comment. * `TestService_BroadcastDataColumn`: Re-add sleep 50 ms. * Fix Preston's comment. * Update beacon-chain/p2p/subnets.go Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com> --------- Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com>
166 lines
5.5 KiB
Go
166 lines
5.5 KiB
Go
package blockchain
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/OffchainLabs/prysm/v6/async/event"
|
|
mock "github.com/OffchainLabs/prysm/v6/beacon-chain/blockchain/testing"
|
|
"github.com/OffchainLabs/prysm/v6/beacon-chain/cache"
|
|
"github.com/OffchainLabs/prysm/v6/beacon-chain/cache/depositsnapshot"
|
|
statefeed "github.com/OffchainLabs/prysm/v6/beacon-chain/core/feed/state"
|
|
lightclient "github.com/OffchainLabs/prysm/v6/beacon-chain/core/light-client"
|
|
"github.com/OffchainLabs/prysm/v6/beacon-chain/db"
|
|
"github.com/OffchainLabs/prysm/v6/beacon-chain/db/filesystem"
|
|
testDB "github.com/OffchainLabs/prysm/v6/beacon-chain/db/testing"
|
|
mockExecution "github.com/OffchainLabs/prysm/v6/beacon-chain/execution/testing"
|
|
"github.com/OffchainLabs/prysm/v6/beacon-chain/forkchoice"
|
|
doublylinkedtree "github.com/OffchainLabs/prysm/v6/beacon-chain/forkchoice/doubly-linked-tree"
|
|
"github.com/OffchainLabs/prysm/v6/beacon-chain/operations/attestations"
|
|
"github.com/OffchainLabs/prysm/v6/beacon-chain/operations/blstoexec"
|
|
"github.com/OffchainLabs/prysm/v6/beacon-chain/p2p"
|
|
p2pTesting "github.com/OffchainLabs/prysm/v6/beacon-chain/p2p/testing"
|
|
"github.com/OffchainLabs/prysm/v6/beacon-chain/startup"
|
|
"github.com/OffchainLabs/prysm/v6/beacon-chain/state/stategen"
|
|
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
|
|
"github.com/OffchainLabs/prysm/v6/config/params"
|
|
"github.com/OffchainLabs/prysm/v6/consensus-types/interfaces"
|
|
"github.com/OffchainLabs/prysm/v6/consensus-types/primitives"
|
|
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
|
"github.com/OffchainLabs/prysm/v6/testing/require"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type mockBeaconNode struct {
|
|
stateFeed *event.Feed
|
|
mu sync.Mutex
|
|
}
|
|
|
|
// StateFeed mocks the same method in the beacon node.
|
|
func (mbn *mockBeaconNode) StateFeed() event.SubscriberSender {
|
|
mbn.mu.Lock()
|
|
defer mbn.mu.Unlock()
|
|
if mbn.stateFeed == nil {
|
|
mbn.stateFeed = new(event.Feed)
|
|
}
|
|
return mbn.stateFeed
|
|
}
|
|
|
|
type mockBroadcaster struct {
|
|
broadcastCalled bool
|
|
}
|
|
|
|
type mockAccessor struct {
|
|
mockBroadcaster
|
|
p2pTesting.MockPeerManager
|
|
}
|
|
|
|
func (mb *mockBroadcaster) Broadcast(_ context.Context, _ proto.Message) error {
|
|
mb.broadcastCalled = true
|
|
return nil
|
|
}
|
|
|
|
func (mb *mockBroadcaster) BroadcastAttestation(_ context.Context, _ uint64, _ ethpb.Att) error {
|
|
mb.broadcastCalled = true
|
|
return nil
|
|
}
|
|
|
|
func (mb *mockBroadcaster) BroadcastSyncCommitteeMessage(_ context.Context, _ uint64, _ *ethpb.SyncCommitteeMessage) error {
|
|
mb.broadcastCalled = true
|
|
return nil
|
|
}
|
|
|
|
func (mb *mockBroadcaster) BroadcastBlob(_ context.Context, _ uint64, _ *ethpb.BlobSidecar) error {
|
|
mb.broadcastCalled = true
|
|
return nil
|
|
}
|
|
|
|
func (mb *mockBroadcaster) BroadcastLightClientOptimisticUpdate(_ context.Context, _ interfaces.LightClientOptimisticUpdate) error {
|
|
mb.broadcastCalled = true
|
|
return nil
|
|
}
|
|
|
|
func (mb *mockBroadcaster) BroadcastLightClientFinalityUpdate(_ context.Context, _ interfaces.LightClientFinalityUpdate) error {
|
|
mb.broadcastCalled = true
|
|
return nil
|
|
}
|
|
|
|
func (mb *mockBroadcaster) BroadcastDataColumn(_ [fieldparams.RootLength]byte, _ uint64, _ *ethpb.DataColumnSidecar) error {
|
|
mb.broadcastCalled = true
|
|
return nil
|
|
}
|
|
|
|
func (mb *mockBroadcaster) BroadcastBLSChanges(_ context.Context, _ []*ethpb.SignedBLSToExecutionChange) {
|
|
}
|
|
|
|
var _ p2p.Broadcaster = (*mockBroadcaster)(nil)
|
|
|
|
type testServiceRequirements struct {
|
|
ctx context.Context
|
|
db db.Database
|
|
fcs forkchoice.ForkChoicer
|
|
sg *stategen.State
|
|
notif statefeed.Notifier
|
|
cs *startup.ClockSynchronizer
|
|
attPool attestations.Pool
|
|
attSrv *attestations.Service
|
|
blsPool *blstoexec.Pool
|
|
dc *depositsnapshot.Cache
|
|
}
|
|
|
|
func minimalTestService(t *testing.T, opts ...Option) (*Service, *testServiceRequirements) {
|
|
ctx := t.Context()
|
|
genesis := time.Now().Add(-1 * 4 * time.Duration(params.BeaconConfig().SlotsPerEpoch*primitives.Slot(params.BeaconConfig().SecondsPerSlot)) * time.Second) // Genesis was 4 epochs ago.
|
|
beaconDB := testDB.SetupDB(t)
|
|
fcs := doublylinkedtree.New()
|
|
fcs.SetGenesisTime(genesis)
|
|
sg := stategen.New(beaconDB, fcs)
|
|
notif := &mockBeaconNode{}
|
|
fcs.SetBalancesByRooter(sg.ActiveNonSlashedBalancesByRoot)
|
|
cs := startup.NewClockSynchronizer()
|
|
attPool := attestations.NewPool()
|
|
attSrv, err := attestations.NewService(ctx, &attestations.Config{Pool: attPool})
|
|
require.NoError(t, err)
|
|
blsPool := blstoexec.NewPool()
|
|
dc, err := depositsnapshot.New()
|
|
require.NoError(t, err)
|
|
req := &testServiceRequirements{
|
|
ctx: ctx,
|
|
db: beaconDB,
|
|
fcs: fcs,
|
|
sg: sg,
|
|
notif: notif,
|
|
cs: cs,
|
|
attPool: attPool,
|
|
attSrv: attSrv,
|
|
blsPool: blsPool,
|
|
dc: dc,
|
|
}
|
|
defOpts := []Option{WithDatabase(req.db),
|
|
WithStateNotifier(req.notif),
|
|
WithStateGen(req.sg),
|
|
WithForkChoiceStore(req.fcs),
|
|
WithClockSynchronizer(req.cs),
|
|
WithAttestationPool(req.attPool),
|
|
WithAttestationService(req.attSrv),
|
|
WithBLSToExecPool(req.blsPool),
|
|
WithDepositCache(dc),
|
|
WithTrackedValidatorsCache(cache.NewTrackedValidatorsCache()),
|
|
WithBlobStorage(filesystem.NewEphemeralBlobStorage(t)),
|
|
WithDataColumnStorage(filesystem.NewEphemeralDataColumnStorage(t)),
|
|
WithSyncChecker(mock.MockChecker{}),
|
|
WithExecutionEngineCaller(&mockExecution.EngineClient{}),
|
|
WithP2PBroadcaster(&mockAccessor{}),
|
|
WithLightClientStore(&lightclient.Store{}),
|
|
WithGenesisTime(genesis),
|
|
}
|
|
// append the variadic opts so they override the defaults by being processed afterwards
|
|
opts = append(defOpts, opts...)
|
|
s, err := NewService(req.ctx, opts...)
|
|
|
|
require.NoError(t, err)
|
|
return s, req
|
|
}
|