Documentation ¶
Overview ¶
Package p2p implements the Lemochain p2p network protocols.
Index ¶
- Constants
- Variables
- type Config
- type DialManager
- type DiscoverManager
- func (m *DiscoverManager) AddNewList(nodes []string)
- func (m *DiscoverManager) GetNodesForDiscover(sequence uint, rPeerNodeID string) []string
- func (m *DiscoverManager) InWhiteList(nodeID *NodeID) (ok bool)
- func (m *DiscoverManager) IsBlackNode(nodeID *NodeID) bool
- func (m *DiscoverManager) PutBlackNode(nodeID *NodeID, endpoint string)
- func (m *DiscoverManager) SetConnectResult(nodeID *NodeID, success bool) error
- func (m *DiscoverManager) SetDeputyNodes(nodes []string)
- func (m *DiscoverManager) SetReconnect(nodeID *NodeID) error
- func (m *DiscoverManager) Start() error
- func (m *DiscoverManager) Stop() error
- type HandleConnFunc
- type IDialManager
- type IPeer
- type Msg
- type MsgCode
- type NodeID
- type Peer
- func (p *Peer) Close()
- func (p *Peer) DoHandshake(prv *ecdsa.PrivateKey, nodeID *NodeID) (err error)
- func (p *Peer) LAddress() string
- func (p *Peer) NeedReConnect() bool
- func (p *Peer) RAddress() string
- func (p *Peer) RNodeID() *NodeID
- func (p *Peer) ReadMsg() (msg *Msg, err error)
- func (p *Peer) Run() (err error)
- func (p *Peer) SetStatus(status int32)
- func (p *Peer) SetWriteDeadline(duration time.Duration)
- func (p *Peer) WriteMsg(code MsgCode, msg []byte) (err error)
- type PeerConnInfo
- type RawNode
- type Server
Constants ¶
const ( MaxReconnectCount int8 = 5 MaxNodeCount = 100 WhiteFile = "nodewhitelist" FoundFile = "foundlist" BlackFile = "nodeblacklist" )
const ( StatusNormal int32 = iota StatusHardFork StatusManualDisconnect StatusFailedHandshake StatusBadData StatusDifferentGenesis )
const NodeIDBits = 512
Variables ¶
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") )
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") 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") )
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
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
type IDialManager ¶ added in v1.0.2
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() }
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 )
type NodeID ¶
type NodeID [NodeIDBits / 8]byte
NodeID
func BytesToNodeID ¶ added in v1.0.2
BytesToNodeID convert bytes to NodeID
func ParseNodeString ¶ added in v1.3.0
ParseNodeString verify invalid
func PubKeyToNodeID ¶ added in v1.0.2
PubKeyToNodeID returns a marshaled representation of the given public key.
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
Peer represents a connected remote node.
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) SetWriteDeadline ¶ added in v1.0.2
SetWriteDeadline
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
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) Connections ¶
func (srv *Server) Connections() []PeerConnInfo
Connections get total connections for api
func (*Server) Disconnect ¶
Disconnect disconnect a connection for api
func (*Server) HandleConn ¶
HandleConn handle net connection