network

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2025 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 {
	Peers     map[NetAddr]*LocalTransport
	Lock      sync.RWMutex
	ConsumeCh chan RPC
	// contains filtered or unexported fields
}

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 = 0x0
	MessageTypeBlock
)

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
	MemPool     *TxPool
	IsValidator bool
	RpcCh       chan RPC

	QuitChan chan struct{}
	// contains filtered or unexported fields
}

func NewServer

func NewServer(opts ServerOpts) (*Server, error)

func (*Server) CreateNewBlock

func (s *Server) CreateNewBlock() error

func (*Server) ProcessMessage

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

func (*Server) Start

func (s *Server) Start()

func (*Server) ValidatorLoop

func (s *Server) ValidatorLoop()

type ServerOpts

type ServerOpts struct {
	ID     string
	Logger log.Logger
	// RPCHandler is responsible for handling remote procedure calls (RPCs)
	// within the network server. It defines the methods and logic required
	// to process incoming RPC requests and send appropriate responses.
	RPCDecodeFunc RPCDecodeFunc
	RPCProcessor  RPCProcessor
	Transports    []Transport
	PrivateKey    *crypto.PrivateKey
	BlockTime     time.Duration
}

type Transport

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

func NewLocalTransport

func NewLocalTransport(addr NetAddr) Transport

type TxMapSorter

type TxMapSorter struct {
	Transations []*core.Transaction
}

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

Less

func (*TxMapSorter) Swap

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

Swap

type TxPool

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

func NewTxPool

func NewTxPool() *TxPool

func (*TxPool) Add

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

Add adds a transaction to the pool. The caller is responsible for checking if the transaction already exists in the pool.

func (*TxPool) Contains

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

Has checks if a transaction with the given hash exists in the pool.

func (*TxPool) Flush

func (p *TxPool) Flush()

Flush removes all transactions from the pool.

func (*TxPool) Len

func (p *TxPool) Len() int

Len returns the number of transactions currently in the pool.

func (*TxPool) Transactions

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

Transactions returns a slice of all transactions in the pool.

Jump to

Keyboard shortcuts

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