Documentation
¶
Overview ¶
Package network provides P2P networking abstractions for the session layer. Supports post-quantum secure transport using hybrid key encapsulation.
Index ¶
- Variables
- type Connection
- type Dialer
- type Error
- type Handler
- type HandlerFunc
- type Listener
- type Message
- type MessageType
- type Peer
- type PeerManager
- func (pm *PeerManager) Add(peer *Peer) error
- func (pm *PeerManager) All() []*Peer
- func (pm *PeerManager) Connected() []*Peer
- func (pm *PeerManager) ConnectedIDs() []core.ID
- func (pm *PeerManager) Count() int
- func (pm *PeerManager) CountConnected() int
- func (pm *PeerManager) Get(id core.ID) (*Peer, bool)
- func (pm *PeerManager) GetByAddr(addr string) (*Peer, bool)
- func (pm *PeerManager) Remove(id core.ID)
- type PeerState
- type Router
- type StreamConn
- type Transport
Constants ¶
This section is empty.
Variables ¶
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 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 ¶
HandlerFunc is a function that 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 (*Peer) Connection ¶
func (p *Peer) Connection() Connection
Connection returns the peer's connection.
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 ¶
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) 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.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router routes messages to handlers.
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.
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.