p2ptest

package
v0.33.33-pebble-chunk-... Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: AGPL-3.0 Imports: 42 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureNoPubsubExchangeBetweenGroups added in v0.31.0

func EnsureNoPubsubExchangeBetweenGroups(t *testing.T,
	ctx context.Context,
	groupANodes []p2p.LibP2PNode,
	groupAIdentifiers flow.IdentifierList,
	groupBNodes []p2p.LibP2PNode,
	groupBIdentifiers flow.IdentifierList,
	topic channels.Topic,
	count int,
	messageFactory func() interface{})

EnsureNoPubsubExchangeBetweenGroups ensures that no pubsub message is exchanged between the given groups of nodes. Args: - t: *testing.T instance - ctx: context.Context instance - groupANodes: first group of nodes- no message should be exchanged from any node of this group to the other group. - groupAIdentifiers: identifiers of the nodes in the first group. - groupBNodes: second group of nodes- no message should be exchanged from any node of this group to the other group. - groupBIdentifiers: identifiers of the nodes in the second group. - topic: pubsub topic- no message should be exchanged on this topic. - count: number of messages to be exchanged- no message should be exchanged. - messageFactory: function to create a unique message to be published by the node.

func EnsureNoPubsubMessageExchange added in v0.31.0

func EnsureNoPubsubMessageExchange(t *testing.T,
	ctx context.Context,
	from []p2p.LibP2PNode,
	to []p2p.LibP2PNode,
	toIdentifiers flow.IdentifierList,
	topic channels.Topic,
	count int,
	messageFactory func() interface{})

EnsureNoPubsubMessageExchange ensures that the no pubsub message is exchanged "from" the given nodes "to" the given nodes. Args:

  • from: the nodes that send messages to the other group but their message must not be received by the other group.

- to: the nodes that are the target of the messages sent by the other group ("from") but must not receive any message from them. - topic: the topic to exchange messages on. - count: the number of messages to exchange from each node. - messageFactory: a function that creates a unique message to be published by the node.

func EnsureNotConnectedBetweenGroups added in v0.31.0

func EnsureNotConnectedBetweenGroups(t *testing.T, ctx context.Context, groupA []p2p.LibP2PNode, groupB []p2p.LibP2PNode)

EnsureNotConnectedBetweenGroups ensures no connection exists between the given groups of nodes.

func EnsurePubsubMessageExchange added in v0.30.0

func EnsurePubsubMessageExchange(t *testing.T, ctx context.Context, nodes []p2p.LibP2PNode, topic channels.Topic, count int, messageFactory func() interface{})

EnsurePubsubMessageExchange ensures that the given connected nodes exchange the given message on the given channel through pubsub. Args:

  • nodes: the nodes to exchange messages
  • ctx: the context- the test will fail if the context expires.
  • topic: the topic to exchange messages on
  • count: the number of messages to exchange from each node.
  • messageFactory: a function that creates a unique message to be published by the node. The function should return a different message each time it is called.

Note-1: this function assumes a timeout of 5 seconds for each message to be received. Note-2: TryConnectionAndEnsureConnected() must be called to connect all nodes before calling this function.

func EnsurePubsubMessageExchangeFromNode added in v0.32.0

func EnsurePubsubMessageExchangeFromNode(t *testing.T,
	ctx context.Context,
	sender p2p.LibP2PNode,
	receiverNode p2p.LibP2PNode,
	receiverIdentifier flow.Identifier,
	topic channels.Topic,
	count int,
	messageFactory func() interface{})

EnsurePubsubMessageExchangeFromNode ensures that the given node exchanges the given message on the given channel through pubsub with the other nodes. Args:

  • node: the node to exchange messages

- ctx: the context- the test will fail if the context expires. - sender: the node that sends the message to the other node. - receiverNode: the node that receives the message from the other node. - receiverIdentifier: the identifier of the receiver node. - topic: the topic to exchange messages on. - count: the number of messages to exchange from `sender` to `receiver`. - messageFactory: a function that creates a unique message to be published by the node.

func EnsureStreamCreationInBothDirections added in v0.30.0

func EnsureStreamCreationInBothDirections(t *testing.T, ctx context.Context, nodes []p2p.LibP2PNode)

EnsureStreamCreationInBothDirections ensure that between each pair of nodes in the given list, a stream is created in both directions.

func GossipSubCtrlFixture added in v0.33.1

func GossipSubCtrlFixture(opts ...GossipSubCtrlOption) *pb.ControlMessage

GossipSubCtrlFixture returns a ControlMessage with the given options.

func GossipSubMessageFixture added in v0.33.1

func GossipSubMessageFixture(t *testing.T) *pb.Message

GossipSubMessageFixture returns a random gossipSub message; this contains a single pubsub message that is exchanged between nodes. The message is generated randomly. Args: - t: *testing.T instance Returns: - *pb.Message: a random gossipSub message Note: the message is not signed.

func GossipSubMessageIdsFixture added in v0.33.1

func GossipSubMessageIdsFixture(count int) []string

GossipSubMessageIdsFixture returns a slice of random gossipSub message IDs of the given size.

func GossipSubRpcFixture added in v0.33.1

func GossipSubRpcFixture(t *testing.T, msgCnt int, opts ...GossipSubCtrlOption) *pb.RPC

GossipSubRpcFixture returns a random GossipSub RPC message. An RPC message is the GossipSub-level message that is exchanged between nodes. It contains individual messages, subscriptions, and control messages. Args: - t: *testing.T instance - msgCnt: number of messages to generate - opts: options to customize control messages (not having an option means no control message). Returns: - *pb.RPC: a random GossipSub RPC message Note: the message is not signed.

func GossipSubRpcFixtures added in v0.33.1

func GossipSubRpcFixtures(t *testing.T, count int) []*pb.RPC

GossipSubRpcFixtures returns a slice of random message IDs for testing. Args: - t: *testing.T instance - count: number of message IDs to generate Returns: - []string: slice of message IDs. Note: evey other parameters that are not explicitly set are set to 10. This function suites applications that need to generate a large number of RPC messages with filled random data. For a better control over the generated data, use GossipSubRpcFixture.

func GossipSubTopicIdFixture added in v0.33.1

func GossipSubTopicIdFixture() string

GossipSubTopicIdFixture returns a random gossipSub topic ID.

func LetNodesDiscoverEachOther

func LetNodesDiscoverEachOther(t *testing.T, ctx context.Context, nodes []p2p.LibP2PNode, ids flow.IdentityList)

LetNodesDiscoverEachOther connects all nodes to each other on the pubsub mesh.

func MockInspectorNotificationDistributorReadyDoneAware added in v0.33.1

func MockInspectorNotificationDistributorReadyDoneAware(d *mockp2p.GossipSubInspectorNotificationDistributor)

MockInspectorNotificationDistributorReadyDoneAware mocks the Ready and Done methods of the distributor to return a channel that is already closed, so that the distributor is considered ready and done when the test needs.

func MockScoringRegistrySubscriptionValidatorReadyDoneAware added in v0.33.1

func MockScoringRegistrySubscriptionValidatorReadyDoneAware(s *mockp2p.SubscriptionValidator)

MockScoringRegistrySubscriptionValidatorReadyDoneAware mocks the Ready and Done methods of the subscription validator to return a channel that is already closed, so that the distributor is considered ready and done when the test needs.

func NetworkingKeyFixtures

func NetworkingKeyFixtures(t *testing.T) crypto.PrivateKey

NetworkingKeyFixtures is a test helper that generates a ECDSA flow key pair.

func NewConnectionGater added in v0.32.0

func NewConnectionGater(idProvider module.IdentityProvider, allowListFilter p2p.PeerFilter) p2p.ConnectionGater

NewConnectionGater creates a new connection gater for testing with given allow listing filter.

func NodeFixture

func NodeFixture(t *testing.T,
	sporkID flow.Identifier,
	dhtPrefix string,
	idProvider module.IdentityProvider,
	opts ...NodeFixtureParameterOption) (p2p.LibP2PNode, flow.Identity)

NodeFixture is a test fixture that creates a single libp2p node with the given key, spork id, and options. It returns the node and its identity.

func NodesFixture

func NodesFixture(t *testing.T,
	sporkID flow.Identifier,
	dhtPrefix string,
	count int,
	idProvider module.IdentityProvider,
	opts ...NodeFixtureParameterOption) ([]p2p.LibP2PNode, flow.IdentityList)

NodesFixture is a test fixture that creates a number of libp2p nodes with the given callback function for stream handling. It returns the nodes and their identities.

func PeerIdSliceFixture added in v0.31.0

func PeerIdSliceFixture(t *testing.T, n int) peer.IDSlice

PeerIdSliceFixture returns a slice of random peer IDs for testing. peer ID is the identifier of a node on the libp2p network. Args: - t: *testing.T instance - n: number of peer IDs to generate Returns: - peer.IDSlice: slice of peer IDs

func PeerManagerConfigFixture added in v0.32.0

func PeerManagerConfigFixture(opts ...func(*p2pbuilderconfig.PeerManagerConfig)) *p2pbuilderconfig.PeerManagerConfig

PeerManagerConfigFixture is a test fixture that sets the default config for the peer manager.

func PubsubMessageFixture added in v0.32.0

func PubsubMessageFixture(t *testing.T, opts ...func(*pb.Message)) *pb.Message

PubsubMessageFixture is a test helper that returns a random pubsub message with the given options applied. If no options are provided, the message will be random. Args:

t: testing.T

opt: variadic list of options to apply to the message Returns: *pb.Message: pubsub message

func RegisterPeerProviders added in v0.33.1

func RegisterPeerProviders(_ *testing.T, nodes []p2p.LibP2PNode)

RegisterPeerProviders registers the peer provider for all the nodes in the input slice. All node ids are registered as the peers provider for all the nodes. This means that every node will be connected to every other node by the peer manager. This is useful for suppressing the "peer provider not set" verbose warning logs in tests scenarios where it is desirable to have all nodes connected to each other. Args: - t: testing.T- the test object; not used, but included in the signature to defensively prevent misuse of the test utility in production. - nodes: nodes to register the peer provider for, each node will be connected to all other nodes.

func RequireConnectedEventually added in v0.32.0

func RequireConnectedEventually(t *testing.T, nodes []p2p.LibP2PNode, tick time.Duration, timeout time.Duration)

RequireConnectedEventually ensures eventually that the given nodes are already connected to each other. It fails the test if any of the nodes is not connected to any other node. Args: - nodes: the nodes to check - tick: the tick duration - timeout: the timeout duration

func RequireEventuallyNotConnected added in v0.32.0

func RequireEventuallyNotConnected(t *testing.T, groupA []p2p.LibP2PNode, groupB []p2p.LibP2PNode, tick time.Duration, timeout time.Duration)

RequireEventuallyNotConnected ensures eventually that the given groups of nodes are not connected to each other. It fails the test if any of the nodes from groupA is connected to any of the nodes from groupB. Args: - groupA: the first group of nodes - groupB: the second group of nodes - tick: the tick duration - timeout: the timeout duration

func StartNode

func StartNode(t *testing.T, ctx irrecoverable.SignalerContext, node p2p.LibP2PNode)

StartNode start a single node using the provided context, timing out if nodes are not all Ready() before duration expires, (i.e., 2 seconds). Args: - t: testing.T- the test object. - ctx: context to use. - node: node to start.

func StartNodes

func StartNodes(t *testing.T, ctx irrecoverable.SignalerContext, nodes []p2p.LibP2PNode)

StartNodes start all nodes in the input slice using the provided context, timing out if nodes are not all Ready() before duration expires

func StopNode

func StopNode(t *testing.T, node p2p.LibP2PNode, cancel context.CancelFunc)

StopNode stops a single node using the provided cancel func, timing out if nodes are not all Done() before duration expires, (i.e., 2 seconds). Args: - t: testing.T- the test object. - node: node to stop. - cancel: cancel func, the function first cancels the context and then waits for the nodes to be done.

func StopNodes

func StopNodes(t *testing.T, nodes []p2p.LibP2PNode, cancel context.CancelFunc)

StopNodes stops all nodes in the input slice using the provided cancel func, timing out if nodes are not all Done() before duration expires (i.e., 5 seconds). Args: - t: testing.T- the test object. - nodes: nodes to stop. - cancel: cancel func, the function first cancels the context and then waits for the nodes to be done.

func StreamHandlerFixture

func StreamHandlerFixture(t *testing.T) (func(s network.Stream), chan string)

StreamHandlerFixture returns a stream handler that writes the received message to the given channel.

func TryConnectionAndEnsureConnected added in v0.32.0

func TryConnectionAndEnsureConnected(t *testing.T, ctx context.Context, nodes []p2p.LibP2PNode)

TryConnectionAndEnsureConnected tries connecting nodes to each other and ensures that the given nodes are connected to each other. It fails the test if any of the nodes is not connected to any other node.

func WithFrom added in v0.32.0

func WithFrom(from peer.ID) func(*pb.Message)

WithFrom is a test helper that returns a function that sets the from field of a pubsub message to the given peer id.

func WithTopic added in v0.32.0

func WithTopic(topic string) func(*pb.Message)

WithTopic is a test helper that returns a function that sets the topic of a pubsub message to the given topic.

func WithZeroJitterAndZeroBackoff added in v0.32.0

func WithZeroJitterAndZeroBackoff(t *testing.T) func(*p2pbuilderconfig.PeerManagerConfig)

WithZeroJitterAndZeroBackoff is a test fixture that sets the default config for the peer manager. It uses a backoff connector with zero jitter and zero backoff.

func WithoutSignature added in v0.32.0

func WithoutSignature() func(*pb.Message)

WithoutSignature is a test helper that returns a function that sets the signature of a pubsub message to nil, effectively removing the signature.

func WithoutSignerId added in v0.32.0

func WithoutSignerId() func(*pb.Message)

WithoutSignerId is a test helper that returns a function that sets the from field of a pubsub message to nil, effectively removing the signer id.

Types

type GossipSubCtrlOption added in v0.33.1

type GossipSubCtrlOption func(*pb.ControlMessage)

func WithGraft added in v0.33.1

func WithGraft(msgCount int, topicId string) GossipSubCtrlOption

WithGraft adds GRAFT control messages with given topicID to the control message.

func WithIHave added in v0.33.1

func WithIHave(msgCount, msgIDCount int, topicId string) GossipSubCtrlOption

WithIHave adds iHave control messages of the given size and number to the control message.

func WithIHaveMessageIDs added in v0.33.7

func WithIHaveMessageIDs(msgIDs []string, topicId string) GossipSubCtrlOption

WithIHaveMessageIDs adds iHave control messages with the given message IDs to the control message.

func WithIWant added in v0.33.1

func WithIWant(iWantCount int, msgIdsPerIWant int) GossipSubCtrlOption

WithIWant adds iWant control messages of the given size and number to the control message. The message IDs are generated randomly. Args:

msgCount: number of iWant messages to add.
msgIdsPerIWant: number of message IDs to add to each iWant message.

Returns: A GossipSubCtrlOption that adds iWant messages to the control message. Example: WithIWant(2, 3) will add 2 iWant messages, each with 3 message IDs.

func WithPrune added in v0.33.1

func WithPrune(msgCount int, topicId string) GossipSubCtrlOption

WithPrune adds PRUNE control messages with given topicID to the control message.

type MockStream added in v0.32.2

type MockStream struct {
	// contains filtered or unexported fields
}

MockStream is a mocked libp2p stream that is implemented as a pipe with a reader and writer. Whatever is written on the stream is written by the writer on the pipe, which in turn makes it available for read by the reader.

func NewMockStream added in v0.32.2

func NewMockStream(pw *io.PipeWriter, pr *io.PipeReader) *MockStream

func (*MockStream) Close added in v0.32.2

func (m *MockStream) Close() error

func (*MockStream) CloseRead added in v0.32.2

func (m *MockStream) CloseRead() error

func (*MockStream) CloseWrite added in v0.32.2

func (m *MockStream) CloseWrite() error

func (*MockStream) Conn added in v0.32.2

func (m *MockStream) Conn() network.Conn

func (*MockStream) ID added in v0.32.2

func (m *MockStream) ID() string

func (*MockStream) Protocol added in v0.32.2

func (m *MockStream) Protocol() protocol.ID

func (*MockStream) Read added in v0.32.2

func (m *MockStream) Read(p []byte) (int, error)

func (*MockStream) Reset added in v0.32.2

func (m *MockStream) Reset() error

func (*MockStream) Scope added in v0.32.2

func (m *MockStream) Scope() network.StreamScope

func (*MockStream) SetDeadline added in v0.32.2

func (m *MockStream) SetDeadline(_ time.Time) error

func (*MockStream) SetProtocol added in v0.32.2

func (m *MockStream) SetProtocol(_ protocol.ID) error

func (*MockStream) SetReadDeadline added in v0.32.2

func (m *MockStream) SetReadDeadline(_ time.Time) error

func (*MockStream) SetWriteDeadline added in v0.32.2

func (m *MockStream) SetWriteDeadline(_ time.Time) error

func (*MockStream) Stat added in v0.32.2

func (m *MockStream) Stat() network.Stats

func (*MockStream) Write added in v0.32.2

func (m *MockStream) Write(p []byte) (int, error)

type NodeFixtureParameterOption

type NodeFixtureParameterOption func(*NodeFixtureParameters)

func EnablePeerScoringWithOverride added in v0.32.0

func EnablePeerScoringWithOverride(override *p2p.PeerScoringConfigOverride) NodeFixtureParameterOption

EnablePeerScoringWithOverride enables peer scoring for the GossipSub pubsub system with the given override. Any existing peer scoring config attribute that is set in the override will override the default peer scoring config. Anything that is left to nil or zero value in the override will be ignored and the default value will be used. Note: it is not recommended to override the default peer scoring config in production unless you know what you are doing. Default Use Tip: use p2p.PeerScoringConfigNoOverride as the argument to this function to enable peer scoring without any override. Args:

  • PeerScoringConfigOverride: override for the peer scoring config- Recommended to use p2p.PeerScoringConfigNoOverride for production or when you don't want to override the default peer scoring config.

Returns: - NodeFixtureParameterOption: a function that can be passed to the NodeFixture function to enable peer scoring.

func OverrideFlowConfig added in v0.32.0

func OverrideFlowConfig(cfg *config.FlowConfig) NodeFixtureParameterOption

func OverrideGossipSubRpcInspectorSuiteFactory added in v0.32.0

func OverrideGossipSubRpcInspectorSuiteFactory(factory p2p.GossipSubRpcInspectorSuiteFactoryFunc) NodeFixtureParameterOption

func WithConnectionGater

func WithConnectionGater(connGater p2p.ConnectionGater) NodeFixtureParameterOption

func WithConnectionManager added in v0.30.0

func WithConnectionManager(connManager connmgr.ConnManager) NodeFixtureParameterOption

func WithDHTOptions

func WithDHTOptions(opts ...dht.Option) NodeFixtureParameterOption

func WithDefaultResourceManager added in v0.30.0

func WithDefaultResourceManager() NodeFixtureParameterOption

WithDefaultResourceManager sets the resource manager to nil, which will cause the node to use the default resource manager. Otherwise, it uses the resource manager provided by the test (the infinite resource manager).

func WithDefaultStreamHandler

func WithDefaultStreamHandler(handler network.StreamHandler) NodeFixtureParameterOption

func WithLogger

func WithLogger(logger zerolog.Logger) NodeFixtureParameterOption

func WithMetricsCollector added in v0.30.0

func WithMetricsCollector(metrics module.NetworkMetrics) NodeFixtureParameterOption

func WithNetworkingAddress

func WithNetworkingAddress(address string) NodeFixtureParameterOption

func WithNetworkingPrivateKey

func WithNetworkingPrivateKey(key crypto.PrivateKey) NodeFixtureParameterOption

func WithPeerScoreParamsOption added in v0.30.0

func WithPeerScoreParamsOption(cfg *p2p.PeerScoringConfigOverride) NodeFixtureParameterOption

func WithPreferredUnicasts

func WithPreferredUnicasts(unicasts []protocols.ProtocolName) NodeFixtureParameterOption

func WithResourceManager added in v0.32.3

func WithResourceManager(resourceManager network.ResourceManager) NodeFixtureParameterOption

WithResourceManager sets the resource manager to the provided resource manager. Otherwise, it uses the resource manager provided by the test (the infinite resource manager).

func WithRole

func WithRole(role flow.Role) NodeFixtureParameterOption

func WithUnicastHandlerFunc added in v0.32.0

func WithUnicastHandlerFunc(handler network.StreamHandler) NodeFixtureParameterOption

func WithUnicastRateLimitDistributor added in v0.32.0

func WithUnicastRateLimitDistributor(distributor p2p.UnicastRateLimiterDistributor) NodeFixtureParameterOption

type NodeFixtureParameters

type NodeFixtureParameters struct {
	HandlerFunc                       network.StreamHandler
	NetworkingType                    flownet.NetworkingType
	Unicasts                          []protocols.ProtocolName
	Key                               crypto.PrivateKey
	Address                           string
	DhtOptions                        []dht.Option
	Role                              flow.Role
	Logger                            zerolog.Logger
	PeerScoringEnabled                bool
	IdProvider                        module.IdentityProvider
	PeerScoringConfigOverride         *p2p.PeerScoringConfigOverride
	PeerManagerConfig                 *p2pbuilderconfig.PeerManagerConfig
	PeerProvider                      p2p.PeersProvider // peer manager parameter
	ConnGater                         p2p.ConnectionGater
	ConnManager                       connmgr.ConnManager
	GossipSubFactory                  p2p.GossipSubFactoryFunc
	GossipSubConfig                   p2p.GossipSubAdapterConfigFunc
	MetricsCfg                        *p2pbuilderconfig.MetricsConfig
	ResourceManager                   network.ResourceManager
	GossipSubRpcInspectorSuiteFactory p2p.GossipSubRpcInspectorSuiteFactoryFunc
	FlowConfig                        *config.FlowConfig
	UnicastRateLimiterDistributor     p2p.UnicastRateLimiterDistributor
}

Jump to

Keyboard shortcuts

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