p2p

package
v0.0.0-...-91a82d4 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: LGPL-3.0 Imports: 27 Imported by: 5

README

p2p

  1. implementation of discovery node (kad)
  2. implementation of peer-to-peer network

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SendMessage

func SendMessage(w MsgWriter, code uint64, data interface{}) error

Types

type Config

type Config struct {
	PrivateKey     *ecdsa.PrivateKey
	MaxPeers       int `mapstructure:"p2p-maxpeers"`
	MaxAcceptConns int
	Name           string
	NetworkID      uint64
	BootNodeStrs   []string `mapstructure:"�p2p-bootnodes"`
	BootNodes      []*discover.Node
	ListenAddr     string `mapstructure:"p2p-listenaddr"`
	Protocols      []*Protocol
	NodeDatabase   string `mapstructure:"p2p-nodes"`
}

Config server options.

type Dialer

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

Dialer is used to connect to node in the network

type DialerManager

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

DialerManager schedules dials and discovery lookups.

func NewDialerManager

func NewDialerManager(maxdialers int, bootnodes []*discover.Node, ntab *discover.Table) *DialerManager

NewDialerManager

func (*DialerManager) AddStatic

func (dm *DialerManager) AddStatic(n *discover.Node)

func (*DialerManager) Done

func (dm *DialerManager) Done(t Task, now time.Time)

func (*DialerManager) NewTasks

func (dm *DialerManager) NewTasks(runningTasks int, peers map[discover.NodeID]*Peer) (tasks []Task)

func (*DialerManager) RemoveStatic

func (dm *DialerManager) RemoveStatic(n *discover.Node)

type DialerTask

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

DialerTask is generated for node to dial.

func (*DialerTask) Do

func (t *DialerTask) Do(srv *Server) error

type DiscoverTask

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

DiscoverTask runs discovery operations for lookup.

func (*DiscoverTask) Do

func (t *DiscoverTask) Do(srv *Server) error

type Message

type Message struct {
	Code    uint64
	Payload []byte

	ReceivedAt time.Time
}

Message defines a p2p message.

func (*Message) DecodePayload

func (msg *Message) DecodePayload(val interface{}) error

DecodePayload parses the RLP content of a message

func (*Message) EncodePayload

func (msg *Message) EncodePayload(val interface{}) error

EncodePayload set the RLP content of a message

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

MsgReadWriter provides reading and writing of messages.

type MsgReader

type MsgReader interface {
	ReadMsg() (*Message, error)
}

MsgReader provides reading of messages.

type MsgWriter

type MsgWriter interface {
	WriteMsg(*Message) error
}

MsgWriter provides writing of messages.

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 Peer

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

Peer represent a connected remote node

func NewPeer

func NewPeer(conn *conn, protocols []*Protocol) *Peer

NewPeer returns a peer

func (*Peer) Disconnect

func (p *Peer) Disconnect(reason string)

Disconnect terminates the peer connection with the given reason.

func (*Peer) ID

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

ID return the node id

func (*Peer) LocalAddr

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

LocalAddr returns the local address of connection.

func (*Peer) Name

func (p *Peer) Name() string

Name return the node name

func (*Peer) PeerInfo

func (p *Peer) PeerInfo() *PeerInfo

func (*Peer) Protocols

func (p *Peer) Protocols() []*ProtocolKey

Protocols return supported subprotocols of the remote peer.

func (*Peer) RemoteAddr

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

RemoteAddr returns the remote address of connection.

func (*Peer) String

func (p *Peer) String() string

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"`
	} `json:"network"`
	Protocols map[string]interface{} `json:"protocols"`
}

type ProtoHandshake

type ProtoHandshake struct {
	Version    uint64
	Name       string
	Protocols  []*ProtocolKey
	ListenPort uint64
	ID         discover.NodeID
}

ProtoHandshake defines the protocol handshake.

type Protocol

type Protocol struct {
	Name string

	Version uint

	Offset uint64

	Size uint64

	Run func(peer *Peer, rw MsgReadWriter) error

	NodeInfo func() interface{}

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

Protocol defined a P2P subprotocol.

func (*Protocol) Key

func (p *Protocol) Key() *ProtocolKey

type ProtocolKey

type ProtocolKey struct {
	Name    string
	Version uint
}

ProtocolKey defines the structure of a protocol id.

type ProtocolKeys

type ProtocolKeys []*ProtocolKey

ProtocolKeys

func (ProtocolKeys) Len

func (ps ProtocolKeys) Len() int

func (ProtocolKeys) Less

func (ps ProtocolKeys) Less(i, j int) bool

func (ProtocolKeys) Swap

func (ps ProtocolKeys) Swap(i, j int)

type QuitReason

type QuitReason uint
const (
	QuitRequested QuitReason = iota
	QuitNetworkError
	QuitProtocolError
	QuitUselessPeer
	QuitTooManyPeers
	QuitAlreadyConnected
	QuitIncompatibleVersion
	QuitInvalidIdentity
	QuitQuitting
	QuitUnexpectedIdentity
	QuitSelf
	QuitReadTimeout
	QuitSubprotocolError = 0x10
)

func (QuitReason) Error

func (d QuitReason) Error() string

type Server

type Server struct {
	Config
	sync.RWMutex
	// contains filtered or unexported fields
}

Server manages all peer connections.

func (*Server) AddPeer

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

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) 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, dest *discover.Node) error

func (*Server) Start

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

func (*Server) Stop

func (srv *Server) Stop()

type TCPDialer

type TCPDialer struct {
	*net.Dialer
}

TCPDialer implements the Dialer interface

func (*TCPDialer) Dial

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

Dial create TCP connections to node in the network

type Task

type Task interface {
	Do(*Server) error
}

Task is used to done for discover

Directories

Path Synopsis
Package discover implements the Node Discovery Protocol.
Package discover implements the Node Discovery Protocol.
Package nat provides access to common network port mapping protocols.
Package nat provides access to common network port mapping protocols.
Package netutil contains extensions to the net package.
Package netutil contains extensions to the net package.

Jump to

Keyboard shortcuts

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