network

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package network provides P2P networking abstractions for the session layer. Supports post-quantum secure transport using hybrid key encapsulation.

Index

Constants

This section is empty.

Variables

View Source
var ErrMaxPeersReached = Error("maximum peers reached")

ErrMaxPeersReached is returned when the maximum number of peers is reached.

Functions

This section is empty.

Types

type Connection

type Connection interface {
	// Read reads data from the connection.
	Read(p []byte) (n int, err error)

	// Write writes data to the connection.
	Write(p []byte) (n int, err error)

	// Close closes the connection.
	Close() error

	// RemoteID returns the remote node ID.
	RemoteID() core.ID

	// RemoteAddr returns the remote address.
	RemoteAddr() string

	// LocalAddr returns the local address.
	LocalAddr() string
}

Connection represents a connection to a peer.

type Dialer

type Dialer interface {
	// Dial dials a connection to the address.
	Dial(ctx context.Context, addr string) (Connection, error)
}

Dialer dials outgoing connections.

type Error

type Error string

Error is a network error type.

func (Error) Error

func (e Error) Error() string

type Handler

type Handler interface {
	// Handle processes a message and optionally returns a response.
	Handle(ctx context.Context, msg *Message) (*Message, error)
}

Handler processes incoming messages.

type HandlerFunc

type HandlerFunc func(ctx context.Context, msg *Message) (*Message, error)

HandlerFunc is a function that implements Handler.

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx context.Context, msg *Message) (*Message, error)

Handle implements Handler.

type Listener

type Listener interface {
	// Accept accepts a new connection.
	Accept() (Connection, error)

	// Close closes the listener.
	Close() error

	// Addr returns the listener address.
	Addr() string
}

Listener listens for incoming connections.

type Message

type Message struct {
	// Type identifies the message type
	Type MessageType `json:"type"`

	// From is the sender's node ID
	From core.ID `json:"from"`

	// To is the recipient's node ID (empty for broadcast)
	To core.ID `json:"to,omitempty"`

	// Payload is the message data
	Payload []byte `json:"payload"`

	// Timestamp when the message was created
	Timestamp time.Time `json:"timestamp"`

	// Signature from the sender
	Signature []byte `json:"signature,omitempty"`
}

Message represents a network message.

type MessageType

type MessageType uint8

MessageType identifies the type of network message.

const (
	MessageTypeUnknown MessageType = iota
	MessageTypeHandshake
	MessageTypeSessionCreate
	MessageTypeSessionStart
	MessageTypeOracleRequest
	MessageTypeOracleRecord
	MessageTypeOracleCommit
	MessageTypeAttestation
	MessageTypeReceipt
	MessageTypeHeartbeat
	MessageTypePing
	MessageTypePong
)

func (MessageType) String

func (t MessageType) String() string

type Peer

type Peer struct {
	// ID is the peer's node ID
	ID core.ID `json:"id"`

	// Addr is the peer's network address
	Addr string `json:"addr"`

	// State is the connection state
	State PeerState `json:"state"`

	// PublicKey is the peer's public key (PQ-safe)
	PublicKey []byte `json:"publicKey,omitempty"`

	// LastSeen is when the peer was last seen
	LastSeen time.Time `json:"lastSeen"`

	// ConnectedAt is when the connection was established
	ConnectedAt time.Time `json:"connectedAt,omitempty"`

	// MessagesSent is the count of messages sent to this peer
	MessagesSent uint64 `json:"messagesSent"`

	// MessagesReceived is the count of messages received from this peer
	MessagesReceived uint64 `json:"messagesReceived"`

	// Latency is the measured round-trip latency
	Latency time.Duration `json:"latency"`
	// contains filtered or unexported fields
}

Peer represents a network peer.

func NewPeer

func NewPeer(id core.ID, addr string) *Peer

NewPeer creates a new peer.

func (*Peer) Connection

func (p *Peer) Connection() Connection

Connection returns the peer's connection.

func (*Peer) Disconnect

func (p *Peer) Disconnect() error

Disconnect disconnects the peer.

func (*Peer) IncrementReceived

func (p *Peer) IncrementReceived()

IncrementReceived increments the received message counter.

func (*Peer) IncrementSent

func (p *Peer) IncrementSent()

IncrementSent increments the sent message counter.

func (*Peer) SetConnection

func (p *Peer) SetConnection(conn Connection)

SetConnection sets the peer's connection.

func (*Peer) UpdateLatency

func (p *Peer) UpdateLatency(latency time.Duration)

UpdateLatency updates the peer's latency measurement.

type PeerManager

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

PeerManager manages the set of peers.

func NewPeerManager

func NewPeerManager(maxPeers int) *PeerManager

NewPeerManager creates a new peer manager.

func (*PeerManager) Add

func (pm *PeerManager) Add(peer *Peer) error

Add adds a peer.

func (*PeerManager) All

func (pm *PeerManager) All() []*Peer

All returns all peers.

func (*PeerManager) Connected

func (pm *PeerManager) Connected() []*Peer

Connected returns all connected peers.

func (*PeerManager) ConnectedIDs

func (pm *PeerManager) ConnectedIDs() []core.ID

ConnectedIDs returns IDs of all connected peers.

func (*PeerManager) Count

func (pm *PeerManager) Count() int

Count returns the total number of peers.

func (*PeerManager) CountConnected

func (pm *PeerManager) CountConnected() int

CountConnected returns the number of connected peers.

func (*PeerManager) Get

func (pm *PeerManager) Get(id core.ID) (*Peer, bool)

Get gets a peer by ID.

func (*PeerManager) GetByAddr

func (pm *PeerManager) GetByAddr(addr string) (*Peer, bool)

GetByAddr gets a peer by address.

func (*PeerManager) Remove

func (pm *PeerManager) Remove(id core.ID)

Remove removes a peer.

type PeerState

type PeerState uint8

PeerState represents the state of a peer connection.

const (
	PeerStateDisconnected PeerState = iota
	PeerStateConnecting
	PeerStateConnected
	PeerStateDisconnecting
)

func (PeerState) String

func (s PeerState) String() string

type Router

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

Router routes messages to handlers.

func NewRouter

func NewRouter() *Router

NewRouter creates a new message router.

func (*Router) Register

func (r *Router) Register(msgType MessageType, handler Handler)

Register registers a handler for a message type.

func (*Router) RegisterFunc

func (r *Router) RegisterFunc(msgType MessageType, f HandlerFunc)

RegisterFunc registers a handler function for a message type.

func (*Router) Route

func (r *Router) Route(ctx context.Context, msg *Message) (*Message, error)

Route routes a message to its handler.

type StreamConn

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

StreamConn wraps a connection with framed message reading/writing.

func NewStreamConn

func NewStreamConn(conn Connection) *StreamConn

NewStreamConn creates a new stream connection wrapper.

func (*StreamConn) Close

func (s *StreamConn) Close() error

Close closes the underlying connection.

func (*StreamConn) ReadMessage

func (s *StreamConn) ReadMessage() (*Message, error)

ReadMessage reads a framed message.

func (*StreamConn) WriteMessage

func (s *StreamConn) WriteMessage(msg *Message) error

WriteMessage writes a framed message.

type Transport

type Transport interface {
	// Start starts the transport.
	Start(ctx context.Context) error

	// Stop stops the transport.
	Stop() error

	// Send sends a message to a specific node.
	Send(ctx context.Context, to core.ID, msg *Message) error

	// Broadcast sends a message to all known peers.
	Broadcast(ctx context.Context, msg *Message) error

	// Receive returns a channel for receiving messages.
	Receive() <-chan *Message

	// Connect connects to a peer.
	Connect(ctx context.Context, addr string) error

	// Disconnect disconnects from a peer.
	Disconnect(nodeID core.ID) error

	// Peers returns the list of connected peer IDs.
	Peers() []core.ID

	// LocalID returns the local node ID.
	LocalID() core.ID
}

Transport is the core networking interface.

Jump to

Keyboard shortcuts

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