Documentation ¶
Overview ¶
Package testing includes useful utilities for mocking a beacon node's p2p service for unit tests.
Index ¶
- Constants
- type FakeP2P
- func (_ *FakeP2P) AddConnectionHandler(_, _ func(ctx context.Context, id peer.ID) error)
- func (_ *FakeP2P) AddDisconnectionHandler(_ func(ctx context.Context, id peer.ID) error)
- func (_ *FakeP2P) AddPingMethod(_ func(ctx context.Context, id peer.ID) error)
- func (_ *FakeP2P) Broadcast(_ context.Context, _ proto.Message) error
- func (_ *FakeP2P) BroadcastAttestation(_ context.Context, _ uint64, _ ethpb.Att) error
- func (_ *FakeP2P) BroadcastBlob(_ context.Context, _ uint64, _ *ethpb.BlobSidecar) error
- func (_ *FakeP2P) BroadcastSyncCommitteeMessage(_ context.Context, _ uint64, _ *ethpb.SyncCommitteeMessage) error
- func (_ *FakeP2P) Disconnect(_ peer.ID) error
- func (_ *FakeP2P) DiscoveryAddresses() ([]multiaddr.Multiaddr, error)
- func (_ *FakeP2P) ENR() *enr.Record
- func (_ *FakeP2P) Encoding() encoder.NetworkEncoding
- func (_ *FakeP2P) FindPeersWithSubnet(_ context.Context, _ string, _ uint64, _ int) (bool, error)
- func (_ *FakeP2P) Host() host.Host
- func (_ *FakeP2P) InterceptAccept(_ network.ConnMultiaddrs) (allow bool)
- func (_ *FakeP2P) InterceptAddrDial(peer.ID, multiaddr.Multiaddr) (allow bool)
- func (_ *FakeP2P) InterceptPeerDial(peer.ID) (allow bool)
- func (_ *FakeP2P) InterceptSecured(network.Direction, peer.ID, network.ConnMultiaddrs) (allow bool)
- func (_ *FakeP2P) InterceptUpgraded(network.Conn) (allow bool, reason control.DisconnectReason)
- func (_ *FakeP2P) JoinTopic(_ string, _ ...pubsub.TopicOpt) (*pubsub.Topic, error)
- func (_ *FakeP2P) LeaveTopic(_ string) error
- func (_ *FakeP2P) Metadata() metadata.Metadata
- func (_ *FakeP2P) MetadataSeq() uint64
- func (_ *FakeP2P) PeerID() peer.ID
- func (_ *FakeP2P) Peers() *peers.Status
- func (_ *FakeP2P) PubSub() *pubsub.PubSub
- func (_ *FakeP2P) PublishToTopic(_ context.Context, _ string, _ []byte, _ ...pubsub.PubOpt) error
- func (_ *FakeP2P) RefreshENR()
- func (_ *FakeP2P) Send(_ context.Context, _ interface{}, _ string, _ peer.ID) (network.Stream, error)
- func (_ *FakeP2P) SetStreamHandler(_ string, _ network.StreamHandler)
- func (_ *FakeP2P) SubscribeToTopic(_ string, _ ...pubsub.SubOpt) (*pubsub.Subscription, error)
- type MockBroadcaster
- func (m *MockBroadcaster) Broadcast(_ context.Context, msg proto.Message) error
- func (m *MockBroadcaster) BroadcastAttestation(_ context.Context, _ uint64, a ethpb.Att) error
- func (m *MockBroadcaster) BroadcastBlob(context.Context, uint64, *ethpb.BlobSidecar) error
- func (m *MockBroadcaster) BroadcastSyncCommitteeMessage(_ context.Context, _ uint64, _ *ethpb.SyncCommitteeMessage) error
- func (m *MockBroadcaster) NumAttestations() int
- func (m *MockBroadcaster) NumMessages() int
- type MockHost
- func (m *MockHost) Addrs() []ma.Multiaddr
- func (_ *MockHost) Close() error
- func (_ *MockHost) ConnManager() connmgr.ConnManager
- func (_ *MockHost) Connect(_ context.Context, _ peer.AddrInfo) error
- func (_ *MockHost) EventBus() event.Bus
- func (_ *MockHost) ID() peer.ID
- func (_ *MockHost) Mux() protocol.Switch
- func (_ *MockHost) Network() network.Network
- func (_ *MockHost) NewStream(_ context.Context, _ peer.ID, _ ...protocol.ID) (network.Stream, error)
- func (_ *MockHost) Peerstore() peerstore.Peerstore
- func (_ *MockHost) RemoveStreamHandler(_ protocol.ID)
- func (_ *MockHost) SetStreamHandler(_ protocol.ID, _ network.StreamHandler)
- func (_ *MockHost) SetStreamHandlerMatch(protocol.ID, func(id protocol.ID) bool, network.StreamHandler)
- type MockMetadataProvider
- type MockPeerManager
- func (_ MockPeerManager) AddPingMethod(_ func(ctx context.Context, id peer.ID) error)
- func (_ *MockPeerManager) Disconnect(peer.ID) error
- func (m MockPeerManager) DiscoveryAddresses() ([]multiaddr.Multiaddr, error)
- func (m MockPeerManager) ENR() *enr.Record
- func (_ MockPeerManager) FindPeersWithSubnet(_ context.Context, _ string, _ uint64, _ int) (bool, error)
- func (m *MockPeerManager) Host() host.Host
- func (m *MockPeerManager) PeerID() peer.ID
- func (_ MockPeerManager) RefreshENR()
- type MockPeersProvider
- type TestP2P
- func (p *TestP2P) AddConnectionHandler(f, _ func(ctx context.Context, id peer.ID) error)
- func (p *TestP2P) AddDisconnectionHandler(f func(ctx context.Context, id peer.ID) error)
- func (_ *TestP2P) AddPingMethod(_ func(ctx context.Context, id peer.ID) error)
- func (p *TestP2P) Broadcast(_ context.Context, _ proto.Message) error
- func (p *TestP2P) BroadcastAttestation(_ context.Context, _ uint64, _ ethpb.Att) error
- func (p *TestP2P) BroadcastBlob(context.Context, uint64, *ethpb.BlobSidecar) error
- func (p *TestP2P) BroadcastSyncCommitteeMessage(_ context.Context, _ uint64, _ *ethpb.SyncCommitteeMessage) error
- func (p *TestP2P) Connect(b *TestP2P)
- func (p *TestP2P) Disconnect(pid peer.ID) error
- func (_ *TestP2P) DiscoveryAddresses() ([]multiaddr.Multiaddr, error)
- func (_ *TestP2P) ENR() *enr.Record
- func (_ *TestP2P) Encoding() encoder.NetworkEncoding
- func (_ *TestP2P) FindPeersWithSubnet(_ context.Context, _ string, _ uint64, _ int) (bool, error)
- func (p *TestP2P) ForkDigest() ([4]byte, error)
- func (p *TestP2P) Host() host.Host
- func (_ *TestP2P) InterceptAccept(_ network.ConnMultiaddrs) (allow bool)
- func (_ *TestP2P) InterceptAddrDial(peer.ID, multiaddr.Multiaddr) (allow bool)
- func (_ *TestP2P) InterceptPeerDial(peer.ID) (allow bool)
- func (_ *TestP2P) InterceptSecured(network.Direction, peer.ID, network.ConnMultiaddrs) (allow bool)
- func (_ *TestP2P) InterceptUpgraded(network.Conn) (allow bool, reason control.DisconnectReason)
- func (p *TestP2P) JoinTopic(topic string, opts ...pubsub.TopicOpt) (*pubsub.Topic, error)
- func (p *TestP2P) LeaveTopic(topic string) error
- func (p *TestP2P) Metadata() metadata.Metadata
- func (p *TestP2P) MetadataSeq() uint64
- func (p *TestP2P) PeerID() peer.ID
- func (p *TestP2P) Peers() *peers.Status
- func (p *TestP2P) PubSub() *pubsub.PubSub
- func (p *TestP2P) PublishToTopic(ctx context.Context, topic string, data []byte, opts ...pubsub.PubOpt) error
- func (p *TestP2P) ReceivePubSub(topic string, msg proto.Message)
- func (p *TestP2P) ReceiveRPC(topic string, msg proto.Message)
- func (_ *TestP2P) RefreshENR()
- func (p *TestP2P) Send(ctx context.Context, msg interface{}, topic string, pid peer.ID) (network.Stream, error)
- func (p *TestP2P) SetStreamHandler(topic string, handler network.StreamHandler)
- func (_ *TestP2P) Started() bool
- func (p *TestP2P) SubscribeToTopic(topic string, opts ...pubsub.SubOpt) (*pubsub.Subscription, error)
Constants ¶
const ( MockRawPeerId0 = "16Uiu2HAkyWZ4Ni1TpvDS8dPxsozmHY85KaiFjodQuV6Tz5tkHVeR" MockRawPeerId1 = "16Uiu2HAm4HgJ9N1o222xK61o7LSgToYWoAy1wNTJRkh9gLZapVAy" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FakeP2P ¶
type FakeP2P struct { }
FakeP2P stack
func (*FakeP2P) AddConnectionHandler ¶
AddConnectionHandler -- fake.
func (*FakeP2P) AddDisconnectionHandler ¶
AddDisconnectionHandler -- fake.
func (*FakeP2P) AddPingMethod ¶
AddPingMethod -- fake.
func (*FakeP2P) BroadcastAttestation ¶
BroadcastAttestation -- fake.
func (*FakeP2P) BroadcastBlob ¶
BroadcastBlob -- fake.
func (*FakeP2P) BroadcastSyncCommitteeMessage ¶
func (_ *FakeP2P) BroadcastSyncCommitteeMessage(_ context.Context, _ uint64, _ *ethpb.SyncCommitteeMessage) error
BroadcastSyncCommitteeMessage -- fake.
func (*FakeP2P) DiscoveryAddresses ¶
DiscoveryAddresses -- fake
func (*FakeP2P) FindPeersWithSubnet ¶
FindPeersWithSubnet mocks the p2p func.
func (*FakeP2P) InterceptAccept ¶
func (_ *FakeP2P) InterceptAccept(_ network.ConnMultiaddrs) (allow bool)
InterceptAccept -- fake.
func (*FakeP2P) InterceptAddrDial ¶
InterceptAddrDial -- fake.
func (*FakeP2P) InterceptPeerDial ¶
InterceptPeerDial -- fake.
func (*FakeP2P) InterceptSecured ¶
InterceptSecured -- fake.
func (*FakeP2P) InterceptUpgraded ¶
InterceptUpgraded -- fake.
func (*FakeP2P) PublishToTopic ¶
PublishToTopic -- fake.
func (*FakeP2P) Send ¶
func (_ *FakeP2P) Send(_ context.Context, _ interface{}, _ string, _ peer.ID) (network.Stream, error)
Send -- fake.
func (*FakeP2P) SetStreamHandler ¶
func (_ *FakeP2P) SetStreamHandler(_ string, _ network.StreamHandler)
SetStreamHandler -- fake.
func (*FakeP2P) SubscribeToTopic ¶
SubscribeToTopic -- fake.
type MockBroadcaster ¶
type MockBroadcaster struct { BroadcastCalled atomic.Bool BroadcastMessages []proto.Message BroadcastAttestations []ethpb.Att // contains filtered or unexported fields }
MockBroadcaster implements p2p.Broadcaster for testing.
func (*MockBroadcaster) BroadcastAttestation ¶
BroadcastAttestation records a broadcast occurred.
func (*MockBroadcaster) BroadcastBlob ¶
func (m *MockBroadcaster) BroadcastBlob(context.Context, uint64, *ethpb.BlobSidecar) error
BroadcastBlob broadcasts a blob for mock.
func (*MockBroadcaster) BroadcastSyncCommitteeMessage ¶
func (m *MockBroadcaster) BroadcastSyncCommitteeMessage(_ context.Context, _ uint64, _ *ethpb.SyncCommitteeMessage) error
BroadcastSyncCommitteeMessage records a broadcast occurred.
func (*MockBroadcaster) NumAttestations ¶
func (m *MockBroadcaster) NumAttestations() int
NumAttestations returns the number of attestations broadcasted.
func (*MockBroadcaster) NumMessages ¶
func (m *MockBroadcaster) NumMessages() int
NumMessages returns the number of messages broadcasted.
type MockHost ¶
MockHost is a fake implementation of libp2p2's Host interface.
func (*MockHost) NewStream ¶
func (_ *MockHost) NewStream(_ context.Context, _ peer.ID, _ ...protocol.ID) (network.Stream, error)
NewStream --
func (*MockHost) RemoveStreamHandler ¶
RemoveStreamHandler --
func (*MockHost) SetStreamHandler ¶
func (_ *MockHost) SetStreamHandler(_ protocol.ID, _ network.StreamHandler)
SetStreamHandler --
func (*MockHost) SetStreamHandlerMatch ¶
func (_ *MockHost) SetStreamHandlerMatch(protocol.ID, func(id protocol.ID) bool, network.StreamHandler)
SetStreamHandlerMatch --
type MockMetadataProvider ¶
MockMetadataProvider is a fake implementation of the MetadataProvider interface.
func (*MockMetadataProvider) Metadata ¶
func (m *MockMetadataProvider) Metadata() metadata.Metadata
Metadata --
func (*MockMetadataProvider) MetadataSeq ¶
func (m *MockMetadataProvider) MetadataSeq() uint64
MetadataSeq --
type MockPeerManager ¶
type MockPeerManager struct { Enr *enr.Record PID peer.ID BHost host.Host DiscoveryAddr []multiaddr.Multiaddr FailDiscoveryAddr bool }
MockPeerManager is mock of the PeerManager interface.
func (MockPeerManager) AddPingMethod ¶
AddPingMethod .
func (*MockPeerManager) Disconnect ¶
func (_ *MockPeerManager) Disconnect(peer.ID) error
Disconnect .
func (MockPeerManager) DiscoveryAddresses ¶
func (m MockPeerManager) DiscoveryAddresses() ([]multiaddr.Multiaddr, error)
DiscoveryAddresses .
func (MockPeerManager) FindPeersWithSubnet ¶
func (_ MockPeerManager) FindPeersWithSubnet(_ context.Context, _ string, _ uint64, _ int) (bool, error)
FindPeersWithSubnet .
type MockPeersProvider ¶
type MockPeersProvider struct {
// contains filtered or unexported fields
}
MockPeersProvider implements PeersProvider for testing.
func (*MockPeersProvider) ClearPeers ¶
func (m *MockPeersProvider) ClearPeers()
ClearPeers removes all known peers.
func (*MockPeersProvider) Peers ¶
func (m *MockPeersProvider) Peers() *peers.Status
Peers provides access the peer status.
type TestP2P ¶
type TestP2P struct { BHost host.Host BroadcastCalled atomic.Bool DelaySend bool Digest [4]byte LocalMetadata metadata.Metadata // contains filtered or unexported fields }
TestP2P represents a p2p implementation that can be used for testing.
func NewTestP2P ¶
NewTestP2P initializes a new p2p test service.
func (*TestP2P) AddConnectionHandler ¶
AddConnectionHandler handles the connection with a newly connected peer.
func (*TestP2P) AddDisconnectionHandler ¶
AddDisconnectionHandler --
func (*TestP2P) AddPingMethod ¶
AddPingMethod mocks the p2p func.
func (*TestP2P) BroadcastAttestation ¶
BroadcastAttestation broadcasts an attestation.
func (*TestP2P) BroadcastBlob ¶
BroadcastBlob broadcasts a blob for mock.
func (*TestP2P) BroadcastSyncCommitteeMessage ¶
func (p *TestP2P) BroadcastSyncCommitteeMessage(_ context.Context, _ uint64, _ *ethpb.SyncCommitteeMessage) error
BroadcastSyncCommitteeMessage broadcasts a sync committee message.
func (*TestP2P) Disconnect ¶
Disconnect from a peer.
func (*TestP2P) DiscoveryAddresses ¶
DiscoveryAddresses --
func (*TestP2P) Encoding ¶
func (_ *TestP2P) Encoding() encoder.NetworkEncoding
Encoding returns ssz encoding.
func (*TestP2P) FindPeersWithSubnet ¶
FindPeersWithSubnet mocks the p2p func.
func (*TestP2P) ForkDigest ¶
ForkDigest mocks the p2p func.
func (*TestP2P) InterceptAccept ¶
func (_ *TestP2P) InterceptAccept(_ network.ConnMultiaddrs) (allow bool)
InterceptAccept .
func (*TestP2P) InterceptAddrDial ¶
InterceptAddrDial .
func (*TestP2P) InterceptPeerDial ¶
InterceptPeerDial .
func (*TestP2P) InterceptSecured ¶
InterceptSecured .
func (*TestP2P) InterceptUpgraded ¶
InterceptUpgraded .
func (*TestP2P) LeaveTopic ¶
LeaveTopic closes topic and removes corresponding handler from list of joined topics. This method will return error if there are outstanding event handlers or subscriptions.
func (*TestP2P) MetadataSeq ¶
MetadataSeq mocks metadata sequence number.
func (*TestP2P) PubSub ¶
PubSub returns reference underlying floodsub. This test library uses floodsub to ensure all connected peers receive the message.
func (*TestP2P) PublishToTopic ¶
func (p *TestP2P) PublishToTopic(ctx context.Context, topic string, data []byte, opts ...pubsub.PubOpt) error
PublishToTopic publishes message to previously joined topic.
func (*TestP2P) ReceivePubSub ¶
ReceivePubSub simulates an incoming message over pubsub on a given topic.
func (*TestP2P) ReceiveRPC ¶
ReceiveRPC simulates an incoming RPC.
func (*TestP2P) Send ¶
func (p *TestP2P) Send(ctx context.Context, msg interface{}, topic string, pid peer.ID) (network.Stream, error)
Send a message to a specific peer.
func (*TestP2P) SetStreamHandler ¶
func (p *TestP2P) SetStreamHandler(topic string, handler network.StreamHandler)
SetStreamHandler for RPC.
func (*TestP2P) SubscribeToTopic ¶
func (p *TestP2P) SubscribeToTopic(topic string, opts ...pubsub.SubOpt) (*pubsub.Subscription, error)
SubscribeToTopic joins (if necessary) and subscribes to PubSub topic.