p2ptest

package
v1.1.0-dev.3 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeChannelDesc

func MakeChannelDesc(chID p2p.ChannelID) *p2p.ChannelDescriptor

func NodeInSlice

func NodeInSlice(id types.NodeID, ids []types.NodeID) bool

func RequireEmpty

func RequireEmpty(ctx context.Context, t *testing.T, channels ...p2p.Channel)

RequireEmpty requires that the given channel is empty.

func RequireError

func RequireError(ctx context.Context, t *testing.T, channel p2p.Channel, peerError p2p.PeerError)

RequireError requires that the given peer error is submitted for a peer.

func RequireNoUpdates

func RequireNoUpdates(ctx context.Context, t *testing.T, peerUpdates *p2p.PeerUpdates)

RequireNoUpdates requires that a PeerUpdates subscription is empty.

func RequireReceive

func RequireReceive(ctx context.Context, t *testing.T, channel p2p.Channel, expect p2p.Envelope)

RequireReceive requires that the given envelope is received on the channel.

func RequireReceiveUnordered

func RequireReceiveUnordered(ctx context.Context, t *testing.T, channel p2p.Channel, expect []*p2p.Envelope)

RequireReceiveUnordered requires that the given envelopes are all received on the channel, ignoring order.

func RequireSend

func RequireSend(ctx context.Context, t *testing.T, channel p2p.Channel, envelope p2p.Envelope)

RequireSend requires that the given envelope is sent on the channel.

func RequireSendReceive

func RequireSendReceive(
	ctx context.Context,
	t *testing.T,
	channel p2p.Channel,
	peerID types.NodeID,
	send proto.Message,
)

RequireSendReceive requires that a given Protobuf message is sent to the given peer, and then that the given response is received back.

func RequireUpdate

func RequireUpdate(t *testing.T, peerUpdates *p2p.PeerUpdates, expect p2p.PeerUpdate)

RequireUpdate requires that a PeerUpdates subscription yields the given update.

func RequireUpdates

func RequireUpdates(t *testing.T, peerUpdates *p2p.PeerUpdates, expect []p2p.PeerUpdate)

RequireUpdates requires that a PeerUpdates subscription yields the given updates in the given order.

func WithLoggerAttrs

func WithLoggerAttrs(ctx context.Context, attrs ...interface{}) context.Context

Types

type Message

type Message = p2p.Echo

Message is a simple message containing a string-typed Value field.

type Network

type Network struct {
	Nodes map[types.NodeID]*Node
	// contains filtered or unexported fields
}

Network sets up an in-memory network that can be used for high-level P2P testing. It creates an arbitrary number of nodes that are connected to each other, and can open channels across all nodes with custom reactors.

func MakeNetwork

func MakeNetwork(ctx context.Context, t *testing.T, opts NetworkOptions, logger log.Logger) *Network

MakeNetwork creates a test network with the given number of nodes and connects them to each other.

func (*Network) AnyNode

func (n *Network) AnyNode() *Node

AnyNode returns a any node.

func (*Network) MakeChannels

func (n *Network) MakeChannels(
	ctx context.Context,
	t *testing.T,
	chDesc *p2p.ChannelDescriptor,
) map[types.NodeID]p2p.Channel

MakeChannels makes a channel on all nodes and returns them, automatically doing error checks and cleanups.

func (*Network) MakeChannelsNoCleanup

func (n *Network) MakeChannelsNoCleanup(
	ctx context.Context,
	t *testing.T,
	chDesc *p2p.ChannelDescriptor,
) map[types.NodeID]p2p.Channel

MakeChannelsNoCleanup makes a channel on all nodes and returns them, automatically doing error checks. The caller must ensure proper cleanup of all the channels.

func (*Network) MakeNode

func (n *Network) MakeNode(ctx context.Context, t *testing.T, proTxHash crypto.ProTxHash, opts NodeOptions) *Node

MakeNode creates a new Node configured for the network with a running peer manager, but does not add it to the existing network. Callers are responsible for updating peering relationships.

func (*Network) NodeByProTxHash

func (n *Network) NodeByProTxHash(proTxHash types.ProTxHash) *Node

NodeByProTxHash returns a node with the given proTxHash or `nil` if not found.

func (*Network) NodeIDs

func (n *Network) NodeIDs() []types.NodeID

NodeIDs returns the network's node IDs.

func (*Network) Peers

func (n *Network) Peers(id types.NodeID) []*Node

Peers returns a node's peers (i.e. everyone except itself).

func (*Network) Remove

func (n *Network) Remove(ctx context.Context, t *testing.T, id types.NodeID)

Remove removes a node from the network, stopping it and waiting for all other nodes to pick up the disconnection.

func (*Network) Start

func (n *Network) Start(ctx context.Context, t *testing.T)

Start starts the network by setting up a list of node addresses to dial in addition to creating a peer update subscription for each node. Finally, all nodes are connected to each other.

type NetworkOptions

type NetworkOptions struct {
	Config      *config.Config
	NumNodes    int
	BufferSize  int
	NodeOpts    NodeOptions
	ProTxHashes []crypto.ProTxHash
}

NetworkOptions is an argument structure to parameterize the MakeNetwork function.

type Node

type Node struct {
	NodeID      types.NodeID
	NodeInfo    types.NodeInfo
	NodeAddress p2p.NodeAddress
	PrivKey     crypto.PrivKey
	Router      *p2p.Router
	Client      *p2pclient.Client
	PeerManager *p2p.PeerManager
	Transport   *p2p.MemoryTransport
	// contains filtered or unexported fields
}

Node is a node in a Network, with a Router and a PeerManager.

func (*Node) MakeChannel

func (n *Node) MakeChannel(
	ctx context.Context,
	t *testing.T,
	chDesc *p2p.ChannelDescriptor,
) p2p.Channel

MakeChannel opens a channel, with automatic error handling and cleanup. On test cleanup, it also checks that the channel is empty, to make sure all expected messages have been asserted.

func (*Node) MakeChannelNoCleanup

func (n *Node) MakeChannelNoCleanup(
	ctx context.Context,
	t *testing.T,
	chDesc *p2p.ChannelDescriptor,
) p2p.Channel

MakeChannelNoCleanup opens a channel, with automatic error handling. The caller must ensure proper cleanup of the channel.

func (*Node) MakePeerUpdates

func (n *Node) MakePeerUpdates(ctx context.Context, t *testing.T) *p2p.PeerUpdates

MakePeerUpdates opens a peer update subscription, with automatic cleanup. It checks that all updates have been consumed during cleanup.

func (*Node) MakePeerUpdatesNoRequireEmpty

func (n *Node) MakePeerUpdatesNoRequireEmpty(ctx context.Context, _t *testing.T) *p2p.PeerUpdates

MakePeerUpdatesNoRequireEmpty opens a peer update subscription, with automatic cleanup. It does *not* check that all updates have been consumed, but will close the update channel.

type NodeOptions

type NodeOptions struct {
	MaxPeers     uint16
	MaxConnected uint16
	ChanDescr    map[p2p.ChannelID]*p2p.ChannelDescriptor
}

Jump to

Keyboard shortcuts

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