network

package
v0.0.0-...-8792f1c Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DecodedMessage

type DecodedMessage struct {
	From NetAddr
	Data any
}

func DefaultRPCDecodeFunc

func DefaultRPCDecodeFunc(rpc RPC) (*DecodedMessage, error)

type LocalTransport

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

func NewLocalTransport

func NewLocalTransport(addr NetAddr) *LocalTransport

func (*LocalTransport) Addr

func (t *LocalTransport) Addr() NetAddr

func (*LocalTransport) Broadcast

func (t *LocalTransport) Broadcast(payload []byte) error

func (*LocalTransport) Connect

func (t *LocalTransport) Connect(tr Transport) error

func (*LocalTransport) Consume

func (t *LocalTransport) Consume() <-chan RPC

func (*LocalTransport) SendMessage

func (t *LocalTransport) SendMessage(to NetAddr, payload []byte) error

type Message

type Message struct {
	Header MessageType
	Data   []byte
}

func NewMessage

func NewMessage(t MessageType, data []byte) *Message

func (*Message) Bytes

func (msg *Message) Bytes() []byte

type MessageType

type MessageType byte
const (
	MessageTypeTx   MessageType = 0x1
	MessageTypeBock MessageType = 0x2
)

type NetAddr

type NetAddr string

type RPC

type RPC struct {
	From    NetAddr
	Payload io.Reader
}

type RPCDecodeFunc

type RPCDecodeFunc func(RPC) (*DecodedMessage, error)

type RPCProcessor

type RPCProcessor interface {
	ProcessMessage(*DecodedMessage) error
}

type Server

type Server struct {
	ServerOpts
	// contains filtered or unexported fields
}

func NewServer

func NewServer(opts ServerOpts) *Server

func (*Server) ProcessMessage

func (s *Server) ProcessMessage(msg *DecodedMessage) error

func (*Server) Start

func (s *Server) Start()

type ServerOpts

type ServerOpts struct {
	RPCDecodeFunc RPCDecodeFunc
	RPCProcessor  RPCProcessor
	Transports    []Transport
	BlockTime     time.Duration
	PrivateKey    *crypto.PrivateKey
}

type TCPPeer

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

TCPPeer represents a remote node connected over TCP.

func (*TCPPeer) Addr

func (p *TCPPeer) Addr() NetAddr

func (*TCPPeer) Close

func (p *TCPPeer) Close() error

func (*TCPPeer) Send

func (p *TCPPeer) Send(data []byte) error

type TCPTransport

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

TCPTransport is a Transport that communicates over raw TCP.

Message framing: each message is prefixed with a 4-byte big-endian uint32 that holds the payload length, followed by the payload bytes.

func NewTCPTransport

func NewTCPTransport(addr NetAddr) *TCPTransport

func (*TCPTransport) Addr

func (t *TCPTransport) Addr() NetAddr

Addr returns the local listen address of this transport.

func (*TCPTransport) Broadcast

func (t *TCPTransport) Broadcast(payload []byte) error

Broadcast sends a framed message to all connected peers.

func (*TCPTransport) Close

func (t *TCPTransport) Close() error

Close shuts down the listener and all peer connections.

func (*TCPTransport) Connect

func (t *TCPTransport) Connect(tr Transport) error

Connect dials a remote TCP address and registers it as a peer.

func (*TCPTransport) Consume

func (t *TCPTransport) Consume() <-chan RPC

Consume returns the channel where inbound RPCs are delivered.

func (*TCPTransport) ListenAndAccept

func (t *TCPTransport) ListenAndAccept() error

ListenAndAccept starts the TCP listener and accepts incoming connections in a background goroutine. Call this once before using the transport.

func (*TCPTransport) SendMessage

func (t *TCPTransport) SendMessage(to NetAddr, payload []byte) error

SendMessage sends a framed message to a specific peer.

type Transport

type Transport interface {
	Consume() <-chan RPC
	Connect(Transport) error
	SendMessage(NetAddr, []byte) error
	Broadcast([]byte) error
	Addr() NetAddr
}

type TxMapSorter

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

TxMapSorter sorts transactions by FirstSeen (oldest first).

func NewTxMapSorter

func NewTxMapSorter(txMap map[types.Hash]*core.Transaction) *TxMapSorter

func (*TxMapSorter) Len

func (s *TxMapSorter) Len() int

func (*TxMapSorter) Less

func (s *TxMapSorter) Less(i, j int) bool

func (*TxMapSorter) Swap

func (s *TxMapSorter) Swap(i, j int)

type TxPool

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

TxPool holds pending transactions up to a maximum capacity. When the pool is full, the oldest transaction (lowest FirstSeen) is evicted to make room for the new one.

func NewTxPool

func NewTxPool(maxLen ...int) *TxPool

func (*TxPool) Add

func (p *TxPool) Add(tx *core.Transaction) error

Add adds a transaction to the pool. If the pool is at capacity, the oldest transaction is evicted first. The caller is responsible for checking duplicates via Has() before calling Add.

func (*TxPool) Flush

func (p *TxPool) Flush()

func (*TxPool) Has

func (p *TxPool) Has(hash types.Hash) bool

func (*TxPool) Len

func (p *TxPool) Len() int

func (*TxPool) MaxLen

func (p *TxPool) MaxLen() int

func (*TxPool) Transactions

func (p *TxPool) Transactions() []*core.Transaction

Transactions returns all transactions sorted by FirstSeen (oldest first).

Jump to

Keyboard shortcuts

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