p2p

package
v0.0.0-...-5d85a61 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2023 License: GPL-3.0 Imports: 35 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	BroadcastNewSideChainMsg = 0x04
	ConfirmNewSideChainMsg   = 0x05

	RefreshValidatorNodeInfoMsg = 0x06
	RemoveValidatorNodeInfoMsg  = 0x07
)

Variables

View Source
var ErrPipeClosed = errors.New("p2p: read or write on closed message pipe")

Functions

func ExpectMsg

func ExpectMsg(r MsgReader, code uint64, content interface{}) error

func MsgPipe

func MsgPipe() (*MsgPipeRW, *MsgPipeRW)
Example
rw1, rw2 := MsgPipe()
go func() {
	Send(rw1, 8, [][]byte{{0, 0}})
	Send(rw1, 5, [][]byte{{1, 1}})
	rw1.Close()
}()

for {
	msg, err := rw2.ReadMsg()
	if err != nil {
		break
	}
	var data [][]byte
	msg.Decode(&data)
	fmt.Printf("msg: %d, %x\n", msg.Code, data[0])
}
Output:

func Send

func Send(w MsgWriter, msgcode uint64, data interface{}) error

func SendItems

func SendItems(w MsgWriter, msgcode uint64, elems ...interface{}) error

Types

type Cap

type Cap struct {
	Name    string
	Version uint
}

func (Cap) RlpData

func (cap Cap) RlpData() interface{}

func (Cap) String

func (cap Cap) String() string

type Config

type Config struct {
	PrivateKey *ecdsa.PrivateKey `toml:"-"`

	MaxPeers int

	MaxPendingPeers int `toml:",omitempty"`

	DialRatio int `toml:",omitempty"`

	NoDiscovery bool

	DiscoveryV5 bool `toml:",omitempty"`

	Name string `toml:"-"`

	BootstrapNodes []*discover.Node

	BootstrapNodesV5 []*discv5.Node `toml:",omitempty"`

	StaticNodes []*discover.Node

	TrustedNodes []*discover.Node

	LocalValidators []P2PValidator

	Validators map[P2PValidator]*P2PValidatorNodeInfo

	NetRestrict *netutil.Netlist `toml:",omitempty"`

	NodeDatabase string `toml:",omitempty"`

	Protocols []Protocol `toml:"-"`

	ListenAddr string

	NAT nat.Interface `toml:",omitempty"`

	Dialer NodeDialer `toml:"-"`

	NoDial bool `toml:",omitempty"`

	EnableMsgEvents bool

	Logger log.Logger `toml:",omitempty"`
}

type DiscReason

type DiscReason uint
const (
	DiscRequested DiscReason = iota
	DiscNetworkError
	DiscProtocolError
	DiscUselessPeer
	DiscTooManyPeers
	DiscAlreadyConnected
	DiscIncompatibleVersion
	DiscInvalidIdentity
	DiscQuitting
	DiscUnexpectedIdentity
	DiscSelf
	DiscReadTimeout
	DiscSubprotocolError = 0x10
)

func (DiscReason) Error

func (d DiscReason) Error() string

func (DiscReason) String

func (d DiscReason) String() string

type Msg

type Msg struct {
	Code       uint64
	Size       uint32
	Payload    io.Reader
	ReceivedAt time.Time
}

func (Msg) Decode

func (msg Msg) Decode(val interface{}) error

func (Msg) Discard

func (msg Msg) Discard() error

func (Msg) String

func (msg Msg) String() string

type MsgPipeRW

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

func (*MsgPipeRW) Close

func (p *MsgPipeRW) Close() error

func (*MsgPipeRW) ReadMsg

func (p *MsgPipeRW) ReadMsg() (Msg, error)

func (*MsgPipeRW) WriteMsg

func (p *MsgPipeRW) WriteMsg(msg Msg) error

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

type MsgReader

type MsgReader interface {
	ReadMsg() (Msg, error)
}

type MsgWriter

type MsgWriter interface {
	WriteMsg(Msg) error
}

type NodeDialer

type NodeDialer interface {
	Dial(*discover.Node) (net.Conn, error)
}

type NodeInfo

type NodeInfo struct {
	ID    string `json:"id"`
	Name  string `json:"name"`
	Enode string `json:"enode"`
	IP    string `json:"ip"`
	Ports struct {
		Discovery int `json:"discovery"`
		Listener  int `json:"listener"`
	} `json:"ports"`
	ListenAddr string                 `json:"listenAddr"`
	Protocols  map[string]interface{} `json:"protocols"`
}

type NodeInfoToSend

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

type P2PValidator

type P2PValidator struct {
	ChainId string
	Address common.Address
}

type P2PValidatorNodeInfo

type P2PValidatorNodeInfo struct {
	Node      discover.Node
	TimeStamp time.Time
	Validator P2PValidator
	Original  bool
}

func (*P2PValidatorNodeInfo) Hash

func (vni *P2PValidatorNodeInfo) Hash() common.Hash

type Peer

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

func NewPeer

func NewPeer(id discover.NodeID, name string, caps []Cap) *Peer

func (*Peer) Caps

func (p *Peer) Caps() []Cap

func (*Peer) Disconnect

func (p *Peer) Disconnect(reason DiscReason)

func (*Peer) ID

func (p *Peer) ID() discover.NodeID

func (*Peer) Inbound

func (p *Peer) Inbound() bool

func (*Peer) Info

func (p *Peer) Info() *PeerInfo

func (*Peer) LocalAddr

func (p *Peer) LocalAddr() net.Addr

func (*Peer) Log

func (p *Peer) Log() log.Logger

func (*Peer) Name

func (p *Peer) Name() string

func (*Peer) RemoteAddr

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

func (*Peer) String

func (p *Peer) String() string

type PeerEvent

type PeerEvent struct {
	Type     PeerEventType   `json:"type"`
	Peer     discover.NodeID `json:"peer"`
	Error    string          `json:"error,omitempty"`
	Protocol string          `json:"protocol,omitempty"`
	MsgCode  *uint64         `json:"msg_code,omitempty"`
	MsgSize  *uint32         `json:"msg_size,omitempty"`
}

type PeerEventType

type PeerEventType string
const (
	PeerEventTypeAdd PeerEventType = "add"

	PeerEventTypeDrop PeerEventType = "drop"

	PeerEventTypeMsgSend PeerEventType = "msgsend"

	PeerEventTypeMsgRecv PeerEventType = "msgrecv"

	PeerEventTypeRefreshValidator PeerEventType = "refreshvalidator"

	PeerEventTypeRemoveValidator PeerEventType = "removevalidator"
)

type PeerInfo

type PeerInfo struct {
	ID      string   `json:"id"`
	Name    string   `json:"name"`
	Caps    []string `json:"caps"`
	Network struct {
		LocalAddress  string `json:"localAddress"`
		RemoteAddress string `json:"remoteAddress"`
		Inbound       bool   `json:"inbound"`
		Trusted       bool   `json:"trusted"`
		Static        bool   `json:"static"`
	} `json:"network"`
	Protocols map[string]interface{} `json:"protocols"`
}

type Protocol

type Protocol struct {
	Name string

	Version uint

	Length uint64

	Run func(peer *Peer, rw MsgReadWriter) error

	NodeInfo func() interface{}

	PeerInfo func(id discover.NodeID) interface{}
}

type Server

type Server struct {
	Config

	DiscV5 *discv5.Network
	// contains filtered or unexported fields
}

func (*Server) AddChildProtocolCaps

func (srv *Server) AddChildProtocolCaps(sideProtocols []Protocol)

func (*Server) AddLocalValidator

func (srv *Server) AddLocalValidator(chainId string, address common.Address)

func (*Server) AddPeer

func (srv *Server) AddPeer(node *discover.Node)

func (*Server) BroadcastMsg

func (srv *Server) BroadcastMsg(msgCode uint64, data interface{})

func (*Server) NodeInfo

func (srv *Server) NodeInfo() *NodeInfo

func (*Server) PeerCount

func (srv *Server) PeerCount() int

func (*Server) Peers

func (srv *Server) Peers() []*Peer

func (*Server) PeersInfo

func (srv *Server) PeersInfo() []*PeerInfo

func (*Server) RemoveLocalValidator

func (srv *Server) RemoveLocalValidator(chainId string, address common.Address)

func (*Server) RemovePeer

func (srv *Server) RemovePeer(node *discover.Node)

func (*Server) Self

func (srv *Server) Self() *discover.Node

func (*Server) SetupConn

func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) error

func (*Server) Start

func (srv *Server) Start() (err error)

func (*Server) Stop

func (srv *Server) Stop()

func (*Server) SubscribeEvents

func (srv *Server) SubscribeEvents(ch chan *PeerEvent) event.Subscription

type TCPDialer

type TCPDialer struct {
	*net.Dialer
}

func (TCPDialer) Dial

func (t TCPDialer) Dial(dest *discover.Node) (net.Conn, error)

Directories

Path Synopsis
Package discv5 implements the RLPx v5 Topic Discovery Protocol.
Package discv5 implements the RLPx v5 Topic Discovery Protocol.
Package enr implements Ligo Chain Node Records as defined in EIP-778.
Package enr implements Ligo Chain Node Records as defined in EIP-778.

Jump to

Keyboard shortcuts

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