p2p

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2020 License: LGPL-3.0 Imports: 30 Imported by: 2

Documentation

Overview

Package p2p implements the Lemochain p2p network protocols.

Index

Constants

View Source
const (
	MaxReconnectCount int8 = 5
	MaxNodeCount           = 100

	WhiteFile = "nodewhitelist"
	FoundFile = "foundlist"
	BlackFile = "nodeblacklist"
)
View Source
const (
	StatusNormal int32 = iota
	StatusHardFork
	StatusManualDisconnect
	StatusFailedHandshake
	StatusBadData
	StatusDifferentGenesis
)
View Source
const NodeIDBits = 512

Variables

View Source
var (
	ErrMaxReconnect  = errors.New("reconnect has reached max count")
	ErrNoSpecialNode = errors.New("doesn't have this special node")
	ErrHasStared     = errors.New("has been started")
	ErrNotStart      = errors.New("not start")
)
View Source
var (
	ErrConnectSelf        = errors.New("can't connect yourself")
	ErrBlackListNode      = errors.New("can't connect black list node")
	ErrGenesisNotMatch    = errors.New("can't match genesis block")
	ErrBadRemoteID        = errors.New("bad remoteID")
	ErrNilRemoteID        = errors.New("remoteID can't be nil")
	ErrUnavailablePackage = errors.New("unavailable net package")
	ErrBadPubKey          = errors.New("invalid public key")
	ErrRecoveryFailed     = errors.New("recovery public key failed")
	ErrAlreadyRunning     = errors.New("has already running")
	ErrNilPrvKey          = errors.New("privateKey can't be nil")
	ErrLengthOverflow     = errors.New("net stream package length too long")

	ErrRlpDecode = errors.New("rlp decode failed")

	ErrSrvHasStopped = errors.New("server has stopped")
)
View Source
var (
	PackagePrefix = []byte{0x5a, 0x48}     // package flag
	PackageLength = 4                      // package length bytes
	PackageMaxLen = 1 * 1024 * 1024 * 1024 // 1 Gb
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Name       string            // server's Name
	PrivateKey *ecdsa.PrivateKey // private key
	Port       int               // listen port
}

Config holds Server options.

type DialManager added in v1.0.1

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

func NewDialManager added in v1.0.1

func NewDialManager(handleConn HandleConnFunc, discover *DiscoverManager) *DialManager

func (*DialManager) Start added in v1.0.1

func (m *DialManager) Start() error

Start

func (*DialManager) Stop added in v1.0.1

func (m *DialManager) Stop() error

Stop

type DiscoverManager added in v1.0.1

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

func NewDiscoverManager added in v1.0.1

func NewDiscoverManager(dataDir string) *DiscoverManager

func (*DiscoverManager) AddNewList added in v1.0.1

func (m *DiscoverManager) AddNewList(nodes []string)

AddNewList for discovery

func (*DiscoverManager) GetNodesForDiscover added in v1.0.1

func (m *DiscoverManager) GetNodesForDiscover(sequence uint, rPeerNodeID string) []string

GetNodesForDiscover get available nodes for node discovery

func (*DiscoverManager) InWhiteList added in v1.2.0

func (m *DiscoverManager) InWhiteList(nodeID *NodeID) (ok bool)

InWhiteList node in white list

func (*DiscoverManager) IsBlackNode added in v1.3.0

func (m *DiscoverManager) IsBlackNode(nodeID *NodeID) bool

is black list node

func (*DiscoverManager) PutBlackNode added in v1.3.0

func (m *DiscoverManager) PutBlackNode(nodeID *NodeID, endpoint string)

PutBlackNode

func (*DiscoverManager) SetConnectResult added in v1.0.1

func (m *DiscoverManager) SetConnectResult(nodeID *NodeID, success bool) error

SetConnectResult set connect result

func (*DiscoverManager) SetDeputyNodes added in v1.0.1

func (m *DiscoverManager) SetDeputyNodes(nodes []string)

SetDeputyNodes add deputy nodes

func (*DiscoverManager) SetReconnect added in v1.0.1

func (m *DiscoverManager) SetReconnect(nodeID *NodeID) error

SetReconnect start reconnect

func (*DiscoverManager) Start added in v1.0.1

func (m *DiscoverManager) Start() error

Start

func (*DiscoverManager) Stop added in v1.0.1

func (m *DiscoverManager) Stop() error

Stop

type HandleConnFunc added in v1.0.1

type HandleConnFunc func(fd net.Conn, nodeID *NodeID) error

type IDialManager added in v1.0.2

type IDialManager interface {
	Start() error
	Stop() error
	// contains filtered or unexported methods
}

type IPeer added in v1.0.2

type IPeer interface {
	ReadMsg() (msg *Msg, err error)
	WriteMsg(code MsgCode, msg []byte) (err error)
	SetWriteDeadline(duration time.Duration)
	RNodeID() *NodeID
	RAddress() string
	LAddress() string
	DoHandshake(prv *ecdsa.PrivateKey, nodeID *NodeID) error
	Run() (err error)
	NeedReConnect() bool
	SetStatus(status int32)
	Close()
}

func NewPeer added in v1.2.0

func NewPeer(fd net.Conn) IPeer

NewPeer

type Msg

type Msg struct {
	Code       MsgCode
	Content    []byte
	ReceivedAt time.Time
}

func (*Msg) CheckCode

func (msg *Msg) CheckCode() bool

CheckCode is code invalid

func (*Msg) Decode

func (msg *Msg) Decode(data interface{}) error

Decode decode stream to object

func (*Msg) Empty

func (msg *Msg) Empty() bool

Empty is msg empty

type MsgCode added in v1.4.0

type MsgCode uint32
const (
	HeartbeatMsg    MsgCode = 0x01 // heartbeat message
	ProHandshakeMsg MsgCode = 0x02 // protocol handshake message
	LstStatusMsg    MsgCode = 0x03 // latest status message
	GetLstStatusMsg MsgCode = 0x04 // get latest status message
	BlockHashMsg    MsgCode = 0x05 // block's hash message
	TxsMsg          MsgCode = 0x06 // transactions message
	GetBlocksMsg    MsgCode = 0x07 // get blocks message
	BlocksMsg       MsgCode = 0x08 // blocks message
	ConfirmMsg      MsgCode = 0x09 // a confirm of one block message
	GetConfirmsMsg  MsgCode = 0x0a // get confirms of one block message
	ConfirmsMsg     MsgCode = 0x0b // confirms of one block message
	// for find node
	DiscoverReqMsg MsgCode = 0x0c // find node request message
	DiscoverResMsg MsgCode = 0x0d // find node response message

	// for lemochain-server and light node
	GetBlocksWithChangeLogMsg MsgCode = 0x0e
)

func (MsgCode) String added in v1.4.0

func (i MsgCode) String() string

type NodeID

type NodeID [NodeIDBits / 8]byte

NodeID

func BytesToNodeID added in v1.0.2

func BytesToNodeID(input []byte) *NodeID

BytesToNodeID convert bytes to NodeID

func ParseNodeString added in v1.3.0

func ParseNodeString(node string) (*NodeID, string)

ParseNodeString verify invalid

func PubKeyToNodeID added in v1.0.2

func PubKeyToNodeID(pub *ecdsa.PublicKey) NodeID

PubKeyToNodeID returns a marshaled representation of the given public key.

func (NodeID) Hash added in v1.3.0

func (id NodeID) Hash() common.Hash

Hash return nodeId hash

func (NodeID) PubKey

func (id NodeID) PubKey() (*ecdsa.PublicKey, error)

PubKey convert to public key

func (NodeID) String

func (id NodeID) String() string

String

type Peer

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

Peer represents a connected remote node.

func (*Peer) Close

func (p *Peer) Close()

Close close peer

func (*Peer) DoHandshake added in v1.0.2

func (p *Peer) DoHandshake(prv *ecdsa.PrivateKey, nodeID *NodeID) (err error)

DoHandshake do handshake when connection

func (*Peer) LAddress added in v1.0.2

func (p *Peer) LAddress() string

LAddress local address (ipv4:port)

func (*Peer) NeedReConnect

func (p *Peer) NeedReConnect() bool

NeedReConnect

func (*Peer) RAddress added in v1.0.2

func (p *Peer) RAddress() string

RAddress remote address (ipv4:port)

func (*Peer) RNodeID added in v1.0.2

func (p *Peer) RNodeID() *NodeID

RNodeID

func (*Peer) ReadMsg

func (p *Peer) ReadMsg() (msg *Msg, err error)

ReadMsg read message for call of outside

func (*Peer) Run added in v1.0.2

func (p *Peer) Run() (err error)

Run Run peer and block this

func (*Peer) SetStatus added in v1.0.2

func (p *Peer) SetStatus(status int32)

SetStatus set peer's status

func (*Peer) SetWriteDeadline added in v1.0.2

func (p *Peer) SetWriteDeadline(duration time.Duration)

SetWriteDeadline

func (*Peer) WriteMsg

func (p *Peer) WriteMsg(code MsgCode, msg []byte) (err error)

WriteMsg send message to net stream

type PeerConnInfo

type PeerConnInfo struct {
	LocalAddr  string `json:"localAddress"`
	RemoteAddr string `json:"remoteAddress"`
	NodeID     string `json:"remoteNodeID"`
}

func (PeerConnInfo) MarshalJSON

func (p PeerConnInfo) MarshalJSON() ([]byte, error)

MarshalJSON marshals as JSON.

func (*PeerConnInfo) UnmarshalJSON

func (p *PeerConnInfo) UnmarshalJSON(input []byte) error

UnmarshalJSON unmarshals from JSON.

type RawNode added in v1.0.1

type RawNode struct {
	NodeID      *NodeID
	Endpoint    string
	IsReconnect bool
	ConnCounter int8
	Sequence    int32 // fresh(已连接): >0; stale(连接过并失败): <0; connecting(可以连接): 0
}

RawNode wrap node connection info for discovery

func (*RawNode) String added in v1.0.2

func (n *RawNode) String() string

String string formatter

type Server

type Server struct {
	Config // configuration
	// contains filtered or unexported fields
}

Server manages all peer connections

func NewServer added in v1.0.1

func NewServer(config Config, discover *DiscoverManager) *Server

func (*Server) Connect

func (srv *Server) Connect(node string) string

Connect add new connection for api format must be: "NodeID@ip:port"

func (*Server) Connections

func (srv *Server) Connections() []PeerConnInfo

Connections get total connections for api

func (*Server) Disconnect

func (srv *Server) Disconnect(node string) bool

Disconnect disconnect a connection for api

func (*Server) HandleConn

func (srv *Server) HandleConn(fd net.Conn, nodeID *NodeID) error

HandleConn handle net connection

func (*Server) Start

func (srv *Server) Start() error

Start

func (*Server) Stop

func (srv *Server) Stop()

Stop

Jump to

Keyboard shortcuts

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