testing

package
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2021 License: GPL-3.0 Imports: 31 Imported by: 0

Documentation

Overview

Package testing includes useful utilities for mocking a beacon node's p2p service for unit tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FakeP2P

type FakeP2P struct {
}

FakeP2P stack

func NewFuzzTestP2P

func NewFuzzTestP2P() *FakeP2P

NewFuzzTestP2P - Create a new fake p2p stack.

func (*FakeP2P) AddConnectionHandler

func (p *FakeP2P) AddConnectionHandler(_, _ func(ctx context.Context, id peer.ID) error)

AddConnectionHandler -- fake.

func (*FakeP2P) AddDisconnectionHandler

func (p *FakeP2P) AddDisconnectionHandler(_ func(ctx context.Context, id peer.ID) error)

AddDisconnectionHandler -- fake.

func (*FakeP2P) AddPingMethod

func (p *FakeP2P) AddPingMethod(_ func(ctx context.Context, id peer.ID) error)

AddPingMethod -- fake.

func (*FakeP2P) Broadcast

func (p *FakeP2P) Broadcast(_ context.Context, _ proto.Message) error

Broadcast -- fake.

func (*FakeP2P) BroadcastAttestation

func (p *FakeP2P) BroadcastAttestation(_ context.Context, _ uint64, _ *ethpb.Attestation) error

BroadcastAttestation -- fake.

func (*FakeP2P) BroadcastSyncCommitteeMessage

func (p *FakeP2P) BroadcastSyncCommitteeMessage(_ context.Context, _ uint64, _ *ethpb.SyncCommitteeMessage) error

BroadcastSyncCommitteeMessage -- fake.

func (*FakeP2P) Disconnect

func (p *FakeP2P) Disconnect(_ peer.ID) error

Disconnect -- fake.

func (*FakeP2P) DiscoveryAddresses

func (p *FakeP2P) DiscoveryAddresses() ([]multiaddr.Multiaddr, error)

DiscoveryAddresses -- fake

func (*FakeP2P) ENR

func (p *FakeP2P) ENR() *enr.Record

ENR returns the enr of the local peer.

func (*FakeP2P) Encoding

func (p *FakeP2P) Encoding() encoder.NetworkEncoding

Encoding -- fake.

func (*FakeP2P) FindPeersWithSubnet

func (p *FakeP2P) FindPeersWithSubnet(_ context.Context, _ string, _ uint64, _ int) (bool, error)

FindPeersWithSubnet mocks the p2p func.

func (*FakeP2P) Host

func (p *FakeP2P) Host() host.Host

Host -- fake.

func (*FakeP2P) InterceptAccept

func (p *FakeP2P) InterceptAccept(_ network.ConnMultiaddrs) (allow bool)

InterceptAccept -- fake.

func (*FakeP2P) InterceptAddrDial

func (p *FakeP2P) InterceptAddrDial(peer.ID, multiaddr.Multiaddr) (allow bool)

InterceptAddrDial -- fake.

func (*FakeP2P) InterceptPeerDial

func (p *FakeP2P) InterceptPeerDial(peer.ID) (allow bool)

InterceptPeerDial -- fake.

func (*FakeP2P) InterceptSecured

func (p *FakeP2P) InterceptSecured(network.Direction, peer.ID, network.ConnMultiaddrs) (allow bool)

InterceptSecured -- fake.

func (*FakeP2P) InterceptUpgraded

func (p *FakeP2P) InterceptUpgraded(network.Conn) (allow bool, reason control.DisconnectReason)

InterceptUpgraded -- fake.

func (*FakeP2P) JoinTopic

func (p *FakeP2P) JoinTopic(_ string, _ ...pubsub.TopicOpt) (*pubsub.Topic, error)

JoinTopic -- fake.

func (*FakeP2P) LeaveTopic

func (p *FakeP2P) LeaveTopic(_ string) error

LeaveTopic -- fake.

func (*FakeP2P) Metadata

func (p *FakeP2P) Metadata() metadata.Metadata

Metadata -- fake.

func (*FakeP2P) MetadataSeq

func (p *FakeP2P) MetadataSeq() uint64

MetadataSeq -- fake.

func (*FakeP2P) PeerID

func (p *FakeP2P) PeerID() peer.ID

PeerID -- fake.

func (*FakeP2P) Peers

func (p *FakeP2P) Peers() *peers.Status

Peers -- fake.

func (*FakeP2P) PubSub

func (p *FakeP2P) PubSub() *pubsub.PubSub

PubSub -- fake.

func (*FakeP2P) PublishToTopic

func (p *FakeP2P) PublishToTopic(_ context.Context, _ string, _ []byte, _ ...pubsub.PubOpt) error

PublishToTopic -- fake.

func (*FakeP2P) RefreshENR

func (p *FakeP2P) RefreshENR()

RefreshENR mocks the p2p func.

func (*FakeP2P) Send

func (p *FakeP2P) Send(_ context.Context, _ interface{}, _ string, _ peer.ID) (network.Stream, error)

Send -- fake.

func (*FakeP2P) SetStreamHandler

func (p *FakeP2P) SetStreamHandler(_ string, _ network.StreamHandler)

SetStreamHandler -- fake.

func (*FakeP2P) SubscribeToTopic

func (p *FakeP2P) SubscribeToTopic(_ string, _ ...pubsub.SubOpt) (*pubsub.Subscription, error)

SubscribeToTopic -- fake.

type MockBroadcaster

type MockBroadcaster struct {
	BroadcastCalled       bool
	BroadcastMessages     []proto.Message
	BroadcastAttestations []*ethpb.Attestation
}

MockBroadcaster implements p2p.Broadcaster for testing.

func (*MockBroadcaster) Broadcast

func (m *MockBroadcaster) Broadcast(_ context.Context, msg proto.Message) error

Broadcast records a broadcast occurred.

func (*MockBroadcaster) BroadcastAttestation

func (m *MockBroadcaster) BroadcastAttestation(_ context.Context, _ uint64, a *ethpb.Attestation) error

BroadcastAttestation records a broadcast occurred.

func (*MockBroadcaster) BroadcastSyncCommitteeMessage

func (m *MockBroadcaster) BroadcastSyncCommitteeMessage(_ context.Context, _ uint64, _ *ethpb.SyncCommitteeMessage) error

BroadcastSyncCommitteeMessage records a broadcast occurred.

type MockHost

type MockHost struct {
	Addresses []ma.Multiaddr
}

MockHost is a fake implementation of libp2p2's Host interface.

func (*MockHost) Addrs

func (m *MockHost) Addrs() []ma.Multiaddr

Addrs --

func (*MockHost) Close

func (m *MockHost) Close() error

Close --

func (*MockHost) ConnManager

func (m *MockHost) ConnManager() connmgr.ConnManager

ConnManager --

func (*MockHost) Connect

func (m *MockHost) Connect(ctx context.Context, pi peer.AddrInfo) error

Connect --

func (*MockHost) EventBus

func (m *MockHost) EventBus() event.Bus

EventBus --

func (*MockHost) ID

func (m *MockHost) ID() peer.ID

ID --

func (*MockHost) Mux

func (m *MockHost) Mux() protocol.Switch

Mux --

func (*MockHost) Network

func (m *MockHost) Network() network.Network

Network --

func (*MockHost) NewStream

func (m *MockHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (network.Stream, error)

NewStream --

func (*MockHost) Peerstore

func (m *MockHost) Peerstore() peerstore.Peerstore

Peerstore --

func (*MockHost) RemoveStreamHandler

func (m *MockHost) RemoveStreamHandler(pid protocol.ID)

RemoveStreamHandler --

func (*MockHost) SetStreamHandler

func (m *MockHost) SetStreamHandler(pid protocol.ID, handler network.StreamHandler)

SetStreamHandler --

func (*MockHost) SetStreamHandlerMatch

func (m *MockHost) SetStreamHandlerMatch(protocol.ID, func(string) bool, network.StreamHandler)

SetStreamHandlerMatch --

type MockMetadataProvider

type MockMetadataProvider struct {
	Data metadata.Metadata
}

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

func (m MockPeerManager) AddPingMethod(_ func(ctx context.Context, id peer.ID) error)

AddPingMethod .

func (*MockPeerManager) Disconnect

func (m *MockPeerManager) Disconnect(peer.ID) error

Disconnect .

func (MockPeerManager) DiscoveryAddresses

func (m MockPeerManager) DiscoveryAddresses() ([]multiaddr.Multiaddr, error)

DiscoveryAddresses .

func (MockPeerManager) ENR

func (m MockPeerManager) ENR() *enr.Record

ENR .

func (MockPeerManager) FindPeersWithSubnet

func (m MockPeerManager) FindPeersWithSubnet(_ context.Context, _ string, _ uint64, _ int) (bool, error)

FindPeersWithSubnet .

func (*MockPeerManager) Host

func (m *MockPeerManager) Host() host.Host

Host .

func (*MockPeerManager) PeerID

func (m *MockPeerManager) PeerID() peer.ID

PeerID .

func (MockPeerManager) RefreshENR

func (m MockPeerManager) RefreshENR()

RefreshENR .

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 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

func NewTestP2P(t *testing.T) *TestP2P

NewTestP2P initializes a new p2p test service.

func (*TestP2P) AddConnectionHandler

func (p *TestP2P) AddConnectionHandler(f, _ func(ctx context.Context, id peer.ID) error)

AddConnectionHandler handles the connection with a newly connected peer.

func (*TestP2P) AddDisconnectionHandler

func (p *TestP2P) AddDisconnectionHandler(f func(ctx context.Context, id peer.ID) error)

AddDisconnectionHandler --

func (*TestP2P) AddPingMethod

func (p *TestP2P) AddPingMethod(_ func(ctx context.Context, id peer.ID) error)

AddPingMethod mocks the p2p func.

func (*TestP2P) Broadcast

func (p *TestP2P) Broadcast(_ context.Context, _ proto.Message) error

Broadcast a message.

func (*TestP2P) BroadcastAttestation

func (p *TestP2P) BroadcastAttestation(_ context.Context, _ uint64, _ *ethpb.Attestation) error

BroadcastAttestation broadcasts an attestation.

func (*TestP2P) BroadcastSyncCommitteeMessage

func (p *TestP2P) BroadcastSyncCommitteeMessage(_ context.Context, _ uint64, _ *ethpb.SyncCommitteeMessage) error

BroadcastSyncCommitteeMessage broadcasts a sync committee message.

func (*TestP2P) Connect

func (p *TestP2P) Connect(b *TestP2P)

Connect two test peers together.

func (*TestP2P) Disconnect

func (p *TestP2P) Disconnect(pid peer.ID) error

Disconnect from a peer.

func (*TestP2P) DiscoveryAddresses

func (p *TestP2P) DiscoveryAddresses() ([]multiaddr.Multiaddr, error)

DiscoveryAddresses --

func (*TestP2P) ENR

func (p *TestP2P) ENR() *enr.Record

ENR returns the enr of the local peer.

func (*TestP2P) Encoding

func (p *TestP2P) Encoding() encoder.NetworkEncoding

Encoding returns ssz encoding.

func (*TestP2P) FindPeersWithSubnet

func (p *TestP2P) FindPeersWithSubnet(_ context.Context, _ string, _ uint64, _ int) (bool, error)

FindPeersWithSubnet mocks the p2p func.

func (*TestP2P) ForkDigest

func (p *TestP2P) ForkDigest() ([4]byte, error)

ForkDigest mocks the p2p func.

func (*TestP2P) Host

func (p *TestP2P) Host() host.Host

Host returns the libp2p host of the local peer.

func (*TestP2P) InterceptAccept

func (p *TestP2P) InterceptAccept(_ network.ConnMultiaddrs) (allow bool)

InterceptAccept .

func (*TestP2P) InterceptAddrDial

func (p *TestP2P) InterceptAddrDial(peer.ID, multiaddr.Multiaddr) (allow bool)

InterceptAddrDial .

func (*TestP2P) InterceptPeerDial

func (p *TestP2P) InterceptPeerDial(peer.ID) (allow bool)

InterceptPeerDial .

func (*TestP2P) InterceptSecured

func (p *TestP2P) InterceptSecured(network.Direction, peer.ID, network.ConnMultiaddrs) (allow bool)

InterceptSecured .

func (*TestP2P) InterceptUpgraded

func (p *TestP2P) InterceptUpgraded(network.Conn) (allow bool, reason control.DisconnectReason)

InterceptUpgraded .

func (*TestP2P) JoinTopic

func (p *TestP2P) JoinTopic(topic string, opts ...pubsub.TopicOpt) (*pubsub.Topic, error)

JoinTopic will join PubSub topic, if not already joined.

func (*TestP2P) LeaveTopic

func (p *TestP2P) LeaveTopic(topic string) error

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) Metadata

func (p *TestP2P) Metadata() metadata.Metadata

Metadata mocks the peer's metadata.

func (*TestP2P) MetadataSeq

func (p *TestP2P) MetadataSeq() uint64

MetadataSeq mocks metadata sequence number.

func (*TestP2P) PeerID

func (p *TestP2P) PeerID() peer.ID

PeerID returns the Peer ID of the local peer.

func (*TestP2P) Peers

func (p *TestP2P) Peers() *peers.Status

Peers returns the peer status.

func (*TestP2P) PubSub

func (p *TestP2P) PubSub() *pubsub.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

func (p *TestP2P) ReceivePubSub(topic string, msg proto.Message)

ReceivePubSub simulates an incoming message over pubsub on a given topic.

func (*TestP2P) ReceiveRPC

func (p *TestP2P) ReceiveRPC(topic string, msg proto.Message)

ReceiveRPC simulates an incoming RPC.

func (*TestP2P) RefreshENR

func (p *TestP2P) RefreshENR()

RefreshENR mocks the p2p func.

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) Started

func (p *TestP2P) Started() bool

Started always returns true.

func (*TestP2P) SubscribeToTopic

func (p *TestP2P) SubscribeToTopic(topic string, opts ...pubsub.SubOpt) (*pubsub.Subscription, error)

SubscribeToTopic joins (if necessary) and subscribes to PubSub topic.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL