p2p

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0, UNKNOWN not legal advice Imports: 0 Imported by: 0

Documentation

Overview

Package p2p contains testing code that is moved over, and adapted from p2p/test_utils.go. This isn't a good way to simulate the networking layer in TM2 modules. It actually isn't a good way to simulate the networking layer, in anything.

Code is carried over to keep the testing code of p2p-dependent modules happy and "working". We should delete this entire package the second TM2 module unit tests don't need to rely on a live p2p cluster to pass.

Index

Constants

View Source
const MaxReceiveLog = 1024

MaxReceiveLog bounds what one peer message may put in a reactor's log, and in the error the switch logs again when it stops the peer.

Variables

View Source
var Package = amino.RegisterPackage(amino.NewPackage(
	"github.com/gnolang/gno/tm2/pkg/internal/p2p",
	"p2ptesting",
	amino.GetCallersDirname(),
).WithTypes(
	&UnhandledMessage{},
	&RejectedMessage{},
	&malformedMessage{},
))

Functions

func BadTypeURLMessage

func BadTypeURLMessage(n int) []byte

BadTypeURLMessage returns an Any whose type URL is n bytes of 0xff, which is not ASCII text.

func DebugLogger

func DebugLogger(w io.Writer) *slog.Logger

DebugLogger logs every level to w as text, the way a node at debug does.

func Deliver

func Deliver(t *testing.T, desc conn.ChannelDescriptor, n int) error

Deliver sends a message of n bytes on the channel desc describes, between two connections built with desc, and returns nil once it arrives, or the error the receiving connection stops with.

func MakeConnectedPeers

func MakeConnectedPeers(
	t *testing.T,
	ctx context.Context,
	cfg TestingConfig,
) ([]*p2p.MultiplexSwitch, []*p2p.MultiplexTransport)

MakeConnectedPeers creates a cluster of peers, with the given options. Used to simulate the networking layer for a TM2 module

func MalformedMessage

func MalformedMessage(n int) []byte

MalformedMessage returns a message of n bytes of data that never decodes, with an error quoting all of them, as params.Param's does for a malformed entry.

func RequireBoundedReceive

func RequireBoundedReceive(t *testing.T, logged *bytes.Buffer, sw *StopRecorder, name string, stop bool, receive func())

RequireBoundedReceive runs receive and requires its log, and the error of the stop it causes when stop is set, to stay within MaxReceiveLog.

func UnknownTypeURLMessage

func UnknownTypeURLMessage(n int) []byte

UnknownTypeURLMessage returns an Any whose type URL is n printable bytes naming no registered type.

Types

type Peer

type Peer struct {
	*service.BaseService

	Outbound, Persistent, Private bool
	// contains filtered or unexported fields
}

Peer is a live peer, utilized for testing purposes. This Peer implementation is NOT thread safe

func NewPeer

func NewPeer(t *testing.T) *Peer

NewPeer creates and starts a new mock peer. It generates a new routable address for the peer

func (*Peer) CloseConn

func (mp *Peer) CloseConn() error

func (*Peer) FlushStop

func (mp *Peer) FlushStop()

func (*Peer) Get

func (mp *Peer) Get(key string) any

func (*Peer) ID

func (mp *Peer) ID() p2pTypes.ID

func (*Peer) IsOutbound

func (mp *Peer) IsOutbound() bool

func (*Peer) IsPersistent

func (mp *Peer) IsPersistent() bool

func (*Peer) IsPrivate

func (mp *Peer) IsPrivate() bool

func (*Peer) NodeInfo

func (mp *Peer) NodeInfo() p2pTypes.NodeInfo

func (*Peer) RemoteAddr

func (mp *Peer) RemoteAddr() net.Addr

func (*Peer) RemoteIP

func (mp *Peer) RemoteIP() net.IP

func (*Peer) Send

func (mp *Peer) Send(_ byte, _ []byte) bool

func (*Peer) Set

func (mp *Peer) Set(key string, value any)

func (*Peer) SocketAddr

func (mp *Peer) SocketAddr() *p2pTypes.NetAddress

func (*Peer) Status

func (mp *Peer) Status() conn.ConnectionStatus

func (*Peer) TrySend

func (mp *Peer) TrySend(_ byte, _ []byte) bool

type RejectedMessage

type RejectedMessage struct {
	Data []byte
}

RejectedMessage is a message whose ValidateBasic fails with an error carrying all of Data.

func (*RejectedMessage) ValidateBasic

func (m *RejectedMessage) ValidateBasic() error

type StopRecorder

type StopRecorder struct {
	p2p.Switch
	Errs []error
}

StopRecorder is a switch that records why each peer was stopped.

func (*StopRecorder) StopPeerForError

func (s *StopRecorder) StopPeerForError(_ p2p.PeerConn, err error)

type TestingConfig

type TestingConfig struct {
	P2PCfg        *p2pcfg.P2PConfig          // the common p2p configuration
	Count         int                        // the size of the cluster
	SwitchOptions map[int][]p2p.SwitchOption // multiplex switch options
	Channels      []byte                     // the common p2p peer multiplex channels
}

TestingConfig is the P2P cluster testing config

type UnhandledMessage

type UnhandledMessage struct {
	Data []byte
}

UnhandledMessage is a valid message that no reactor handles, being of another package than every reactor's.

func (*UnhandledMessage) ValidateBasic

func (*UnhandledMessage) ValidateBasic() error

Jump to

Keyboard shortcuts

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