Documentation ¶
Overview ¶
Package p2p implements the Hpb p2p network protocols.
Index ¶
- Constants
- Variables
- func ErrResp(code errCode, format string, v ...interface{}) error
- func ExpectMsg(r MsgReader, code uint64, content interface{}) error
- func HandleReqNodesMsg(p *Peer, msg Msg) error
- func HandleReqRemoteStateMsg(p *Peer, msg Msg) error
- func HandleResNodesMsg(p *Peer, msg Msg) error
- func HandleResRemoteStateMsg(p *Peer, msg Msg) error
- func SendData(p *Peer, msgCode uint64, data interface{}) error
- type Cap
- type ChanStatusCB
- type Config
- type DiscReason
- type HpbInfo
- type HpbProto
- type HwPair
- type ModuleType
- type Msg
- type MsgProcessCB
- type MsgReadWriter
- type MsgReader
- type MsgWriter
- type NodeDialer
- type NodeInfo
- type OnAddPeerCB
- type OnDropPeerCB
- type Peer
- func (p *Peer) Bandwidth() float64
- func (p *Peer) Exchange(our *exchangeData) (*exchangeData, error)
- func (p *Peer) GetID() string
- func (p *Peer) GetVersion() uint
- func (p *Peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis common.Hash) error
- func (p *Peer) Head() (hash common.Hash, td *big.Int)
- func (p *Peer) KnownBlockAdd(hash common.Hash)
- func (p *Peer) KnownBlockHas(hash common.Hash) bool
- func (p *Peer) KnownBlockSize() int
- func (p *Peer) KnownTxsAdd(hash common.Hash)
- func (p *Peer) KnownTxsHas(hash common.Hash) bool
- func (p *Peer) KnownTxsSize() int
- func (p *Peer) SetHead(hash common.Hash, td *big.Int)
- func (p *Peer) SetTxsRate(txs float64)
- func (p *Peer) String() string
- func (p *Peer) TxsRate() float64
- type PeerBase
- func (p *PeerBase) Address() common.Address
- func (p *PeerBase) Caps() []Cap
- func (p *PeerBase) Disconnect(reason DiscReason)
- func (p *PeerBase) ID() discover.NodeID
- func (p *PeerBase) LocalAddr() net.Addr
- func (p *PeerBase) LocalType() discover.NodeType
- func (p *PeerBase) Name() string
- func (p *PeerBase) RemoteAddr() net.Addr
- func (p *PeerBase) RemoteIP() string
- func (p *PeerBase) RemoteIperfPort() int
- func (p *PeerBase) RemoteListenPort() int
- func (p *PeerBase) RemoteType() discover.NodeType
- func (p *PeerBase) SetRemoteType(nt discover.NodeType) bool
- func (p *PeerBase) String() string
- func (p *PeerBase) Version() string
- type PeerEvent
- type PeerEventType
- type PeerInfo
- type PeerManager
- func (prm *PeerManager) BestPeer() *Peer
- func (prm *PeerManager) DefaultAddr() common.Address
- func (prm *PeerManager) GetHwInfo() []HwPair
- func (prm *PeerManager) GetLocalType() discover.NodeType
- func (prm *PeerManager) HandleReqBWTestMsg(p *Peer, msg Msg) error
- func (prm *PeerManager) HandleResBWTestMsg(p *Peer, msg Msg) error
- func (prm *PeerManager) Len() int
- func (prm *PeerManager) NodeInfo() *NodeInfo
- func (prm *PeerManager) P2pSvr() *Server
- func (prm *PeerManager) Peer(id string) *Peer
- func (prm *PeerManager) PeersAll() []*Peer
- func (prm *PeerManager) PeersInfo() []*PeerInfo
- func (prm *PeerManager) PeersWithoutBlock(hash common.Hash) []*Peer
- func (prm *PeerManager) PeersWithoutTx(hash common.Hash) []*Peer
- func (prm *PeerManager) Protocol() []Protocol
- func (prm *PeerManager) RegChanStatus(cb ChanStatusCB)
- func (prm *PeerManager) RegMsgProcess(msg uint64, cb MsgProcessCB)
- func (prm *PeerManager) RegOnAddPeer(cb OnAddPeerCB)
- func (prm *PeerManager) RegOnDropPeer(cb OnDropPeerCB)
- func (prm *PeerManager) RegStatMining(cb StatMining)
- func (prm *PeerManager) Register(p *Peer) error
- func (prm *PeerManager) SetHwInfo(pairs []HwPair) error
- func (prm *PeerManager) SetLocalType(nt discover.NodeType) bool
- func (prm *PeerManager) Start(coinbase common.Address) error
- func (prm *PeerManager) Stop()
- type Protocol
- type Server
- func (srv *Server) AddPeer(node *discover.Node)
- func (srv *Server) PeerCount() int
- func (srv *Server) Peers() []*PeerBase
- func (srv *Server) RemovePeer(node *discover.Node)
- func (srv *Server) Self() *discover.Node
- func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node)
- func (srv *Server) Start() (err error)
- func (srv *Server) Stop()
- func (srv *Server) SubscribeEvents(et event.EventType) event.Subscriber
- type StatDetail
- type StatMining
- type SynnodePid
- type TCPDialer
Constants ¶
const ( StatusMsg uint64 = 0x1010 ExchangeMsg uint64 = 0x1011 ReqNodesMsg uint64 = 0x1020 ResNodesMsg uint64 = 0x1021 ReqBWTestMsg uint64 = 0x1030 ResBWTestMsg uint64 = 0x1031 ReqRemoteStateMsg uint64 = 0x1040 ResRemoteStateMsg uint64 = 0x1041 NewBlockHashesMsg uint64 = 0x2012 TxMsg uint64 = 0x2013 GetBlockHeadersMsg uint64 = 0x2014 BlockHeadersMsg uint64 = 0x2015 GetBlockBodiesMsg uint64 = 0x2016 BlockBodiesMsg uint64 = 0x2017 NewBlockMsg uint64 = 0x2018 GetNodeDataMsg uint64 = 0x2019 NodeDataMsg uint64 = 0x201a GetReceiptsMsg uint64 = 0x201b ReceiptsMsg uint64 = 0x201c NewHashBlockMsg uint64 = 0x2020 )
message of hpb protocol
const ( PeerEventAdd event.EventType = 0x01 PeerEventDrop event.EventType = 0x02 PeerEventMsgSend event.EventType = 0x03 PeerEventMsgRecv event.EventType = 0x04 )
const ( ErrMsgTooLarge = iota ErrDecode ErrProtocolVersionMismatch ErrNetworkIdMismatch ErrGenesisBlockMismatch ErrNoStatusMsg ErrNoExchangeMsg )
const (
MaxMsgSize = 50 * 1024 * 1024
)
const ProtoName = "hpb"
HPB 支持的协议消息
const ProtoVersion100 uint = 100
const RandNonceSize = 32
Variables ¶
var INSTANCE = atomic.Value{}
var ProtocolVersions = []uint{ProtoVersion100}
Functions ¶
func ExpectMsg ¶
ExpectMsg reads a message from r and verifies that its code and encoded RLP content match the provided values. If content is nil, the payload is discarded and not verified.
func HandleReqNodesMsg ¶
func HandleReqRemoteStateMsg ¶
func HandleResNodesMsg ¶
func HandleResRemoteStateMsg ¶
Types ¶
type ChanStatusCB ¶
type Config ¶
type Config struct { PrivateKey *ecdsa.PrivateKey `toml:"-"` Name string `toml:"-"` BootstrapNodes []*discover.Node StaticNodes []*discover.Node NetRestrict *netutil.Netlist `toml:",omitempty"` NodeDatabase string `toml:",omitempty"` Protocols []Protocol `toml:"-"` ListenAddr string NAT nat.Interface `toml:",omitempty"` EnableMsgEvents bool NetworkId uint64 CoinBase common.Address TestMode bool }
Config holds Server options.
type DiscReason ¶
type DiscReason uint
const ( DiscRequested DiscReason = iota DiscNetworkError DiscProtocolError DiscUselessPeer DiscPeerBySyn DiscAlreadyConnected DiscIncompatibleVersion DiscInvalidIdentity DiscQuitting DiscUnexpectedIdentity DiscSelf DiscReadTimeout DiscUnknownNode DiscUnexpectedConnected DiscHwSignError DiscSubprotocolError = 0x10 )
func (DiscReason) Error ¶
func (d DiscReason) Error() string
func (DiscReason) String ¶
func (d DiscReason) String() string
type HpbProto ¶
type HpbProto struct {
// contains filtered or unexported fields
}
//////////////////////////////////////////////////////
type ModuleType ¶
type ModuleType uint
type Msg ¶
type Msg struct { Code uint64 Size uint32 // size of the paylod Payload io.Reader ReceivedAt time.Time }
Msg defines the structure of a p2p message.
Note that a Msg can only be sent once since the Payload reader is consumed during sending. It is not possible to create a Msg and send it any number of times. If you want to reuse an encoded structure, encode the payload into a byte array and create a separate Msg with a bytes.Reader as Payload for each send.
func (Msg) Decode ¶
Decode parses the RLP content of a message into the given value, which must be a pointer.
For the decoding rules, please see package rlp.
type MsgProcessCB ¶
type MsgReadWriter ¶
MsgReadWriter provides reading and writing of encoded messages. Implementations should ensure that ReadMsg and WriteMsg can be called simultaneously from multiple goroutines.
type NodeDialer ¶
NodeDialer is used to connect to nodes in the network, typically by using an underlying net.Dialer but also using net.Pipe in tests
type NodeInfo ¶
type NodeInfo struct { ID string `json:"id"` // Unique node identifier (also the encryption key) Name string `json:"name"` // Name of the node, including client type, version, OS, custom data Local string `json:"local"` // Local node type IP string `json:"ip"` // IP address of the node Ports struct { UDP int `json:"udp"` // UDP listening port for discovery protocol TCP int `json:"tcp"` // TCP listening port for RLPx } `json:"ports"` ListenAddr string `json:"listenAddr"` }
type OnAddPeerCB ¶
type OnDropPeerCB ¶
type Peer ¶
type Peer struct { *PeerBase // contains filtered or unexported fields }
func (*Peer) GetVersion ¶
func (*Peer) Handshake ¶
Handshake executes the eth protocol handshake, negotiating version number, network IDs, difficulties, head and genesis blocks.
func (*Peer) Head ¶
Head retrieves a copy of the current head hash and total difficulty of the peer.
func (*Peer) KnownBlockAdd ¶
func (*Peer) KnownBlockSize ¶
func (*Peer) KnownTxsAdd ¶
func (*Peer) KnownTxsSize ¶
type PeerBase ¶
type PeerBase struct {
// contains filtered or unexported fields
}
Peer represents a connected remote node.
func (*PeerBase) Disconnect ¶
func (p *PeerBase) Disconnect(reason DiscReason)
Disconnect terminates the peer connection with the given reason. It returns immediately and does not wait until the connection is closed.
func (*PeerBase) RemoteAddr ¶
RemoteAddr returns the remote address of the network connection.
func (*PeerBase) RemoteIperfPort ¶
func (*PeerBase) RemoteListenPort ¶
func (*PeerBase) RemoteType ¶
RemoteType returns the remote type of the node.
type PeerEventType ¶
type PeerEventType string
PeerEventType is the type of peer events emitted by a p2p.Server
type PeerInfo ¶
type PeerInfo struct { ID string `json:"id"` // Unique node identifier (also the encryption key) Name string `json:"name"` // Name of the node, including client type, version, OS, custom data Version string `json:"version"` // Ghpb version Remote string `json:"remote"` // Remote node type Cap string `json:"cap"` // Sum-protocols advertised by this particular peer Network struct { Local string `json:"local"` // Local endpoint of the TCP data connection Remote string `json:"remote"` // Remote endpoint of the TCP data connection } `json:"network"` Start string `json:"start"` // Beat string `json:"beat"` // Mining string `json:"mining"` // HPB interface{} `json:"hpb"` // Sub-protocol specific metadata fields }
type PeerManager ¶
type PeerManager struct {
// contains filtered or unexported fields
}
func PeerMgrInst ¶
func PeerMgrInst() *PeerManager
func (*PeerManager) BestPeer ¶
func (prm *PeerManager) BestPeer() *Peer
BestPeer retrieves the known peer with the currently highest total difficulty.
func (*PeerManager) DefaultAddr ¶
func (prm *PeerManager) DefaultAddr() common.Address
func (*PeerManager) GetHwInfo ¶
func (prm *PeerManager) GetHwInfo() []HwPair
func (*PeerManager) GetLocalType ¶
func (prm *PeerManager) GetLocalType() discover.NodeType
func (*PeerManager) HandleReqBWTestMsg ¶
func (prm *PeerManager) HandleReqBWTestMsg(p *Peer, msg Msg) error
func (*PeerManager) HandleResBWTestMsg ¶
func (prm *PeerManager) HandleResBWTestMsg(p *Peer, msg Msg) error
func (*PeerManager) Len ¶
func (prm *PeerManager) Len() int
func (prm *PeerManager) SetHpRemoteFlag(flag bool) { //log.Info("Change hp remote flag","from",prm.server.hpflag,"to",flag) if prm.server.hpflag != flag { prm.server.hpflag = flag } }
Len returns if the current number of peers in the set.
func (*PeerManager) NodeInfo ¶
func (prm *PeerManager) NodeInfo() *NodeInfo
func (*PeerManager) P2pSvr ¶
func (prm *PeerManager) P2pSvr() *Server
func (*PeerManager) Peer ¶
func (prm *PeerManager) Peer(id string) *Peer
Peer retrieves the registered peer with the given id.
func (*PeerManager) PeersAll ¶
func (prm *PeerManager) PeersAll() []*Peer
func (*PeerManager) PeersInfo ¶
func (prm *PeerManager) PeersInfo() []*PeerInfo
func (*PeerManager) PeersWithoutBlock ¶
func (prm *PeerManager) PeersWithoutBlock(hash common.Hash) []*Peer
PeersWithoutBlock retrieves a list of peers that do not have a given block in their set of known hashes.
func (*PeerManager) PeersWithoutTx ¶
func (prm *PeerManager) PeersWithoutTx(hash common.Hash) []*Peer
PeersWithoutTx retrieves a list of peers that do not have a given transaction in their set of known hashes.
func (*PeerManager) Protocol ¶
func (prm *PeerManager) Protocol() []Protocol
func (*PeerManager) RegChanStatus ¶
func (prm *PeerManager) RegChanStatus(cb ChanStatusCB)
func (*PeerManager) RegMsgProcess ¶
func (prm *PeerManager) RegMsgProcess(msg uint64, cb MsgProcessCB)
//////////////////////////////////////////////////////////////////
func (*PeerManager) RegOnAddPeer ¶
func (prm *PeerManager) RegOnAddPeer(cb OnAddPeerCB)
func (*PeerManager) RegOnDropPeer ¶
func (prm *PeerManager) RegOnDropPeer(cb OnDropPeerCB)
func (*PeerManager) RegStatMining ¶
func (prm *PeerManager) RegStatMining(cb StatMining)
func (*PeerManager) Register ¶
func (prm *PeerManager) Register(p *Peer) error
Register injects a new peer into the working set, or returns an error if the peer is already known.
func (*PeerManager) SetHwInfo ¶
func (prm *PeerManager) SetHwInfo(pairs []HwPair) error
func (*PeerManager) SetLocalType ¶
func (prm *PeerManager) SetLocalType(nt discover.NodeType) bool
func (*PeerManager) Stop ¶
func (prm *PeerManager) Stop()
type Protocol ¶
type Protocol struct { Name string Version uint Run func(p *PeerBase, rw MsgReadWriter) error }
Protocol represents a P2P subprotocol implementation.
type Server ¶
type Server struct { // Config fields may not be modified while the server is running. Config // contains filtered or unexported fields }
Server manages all peer connections.
func (*Server) AddPeer ¶
AddPeer connects to the given node and maintains the connection until the server is shut down. If the connection fails for any reason, the server will attempt to reconnect the peer.
func (*Server) RemovePeer ¶
RemovePeer disconnects from the given node
func (*Server) SetupConn ¶
SetupConn runs the handshakes and attempts to add the connection as a peer. It returns when the connection has been added as a peer or the handshakes have failed.
func (*Server) Stop ¶
func (srv *Server) Stop()
Stop terminates the server and all active peer connections. It blocks until all active connections have been closed.
func (*Server) SubscribeEvents ¶
func (srv *Server) SubscribeEvents(et event.EventType) event.Subscriber
SubscribePeers subscribes the given channel to peer events
type StatDetail ¶
//////////////////////////////////////////////////////
type StatMining ¶
type StatMining func() bool
type SynnodePid ¶
type SynnodePid struct {
PID string `json:"pid"`
}
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
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. |