Documentation ¶
Index ¶
- Constants
- func ConstructDHT(ctx context.Context, listenstring string, nodekey *ecdsa.PrivateKey, ...) (*dht.IpfsDHT, p2phost.Host, error)
- func ExpectMsg(r MsgReader, code MessageType, content interface{}) error
- func GetAddr(target string) (ma.Multiaddr, peer.ID, error)
- func GetIPfromAddr(a ma.Multiaddr) string
- func MakePort(port string) string
- func NATParse(spec string) (libp2p.Option, error)
- func ParseNetlist(s string) ([]*net.IPNet, error)
- func PubkeyID(pub *ecdsa.PublicKey) libp2p.ID
- func Send(w MsgWriter, protocolID string, msgType MessageType, data interface{}) error
- func SendItems(w MsgWriter, protocolID string, msgType MessageType, elems ...interface{}) error
- func WritePackage(rw *bufio.ReadWriter, code uint64, data []byte) ([]byte, error)
- type Config
- type DhtTable
- type DiscReason
- type GoodMorningMsg
- type LevelDB
- func (d *LevelDB) Batch() (ds.Batch, error)
- func (d *LevelDB) Close() error
- func (d *LevelDB) Delete(key ds.Key) (err error)
- func (d *LevelDB) Get(key ds.Key) (value interface{}, err error)
- func (d *LevelDB) Has(key ds.Key) (exists bool, err error)
- func (d *LevelDB) Put(key ds.Key, value interface{}) (err error)
- func (d *LevelDB) Query(q query.Query) (query.Results, error)
- type MessageType
- type Msg
- type MsgBody
- type MsgHeader
- type MsgReadWriter
- type MsgReader
- type MsgWriter
- type Node
- type NodeID
- func (n NodeID) Bytes() []byte
- func (n NodeID) GoString() string
- func (n NodeID) MarshalText() ([]byte, error)
- func (n NodeID) PeerID() peer.ID
- func (n NodeID) Pubkey() (*ecdsa.PublicKey, error)
- func (n NodeID) String() string
- func (n NodeID) TerminalString() string
- func (n *NodeID) UnmarshalText(text []byte) error
- type NodeInfo
- type Peer
- type PeerEvent
- type PeerEventType
- type PeerInfo
- type Protocol
- type Server
- func (server *Server) AddPeer(ctx context.Context, node *Node)
- func (server *Server) Close()
- func (server *Server) GetPeerByRemoteID(s inet.Stream) *Peer
- func (server *Server) HandleStream(s inet.Stream)
- func (server *Server) LoadConfig(ctx context.Context) []peer.ID
- func (server *Server) NodeInfo() *NodeInfo
- func (server *Server) PeerCount() int
- func (server *Server) Peers() []*Peer
- func (server *Server) PeersInfo() []*PeerInfo
- func (server *Server) RemovePeer(node *Node)
- func (server *Server) Self() *Node
- func (server *Server) SetupStream(ctx context.Context, target peer.ID, pid string) error
- func (server *Server) Start() error
- func (server *Server) Stop()
- func (server *Server) SubscribeEvents(ch chan *PeerEvent) event.Subscription
- type Stream
- type VNTDht
- type VNTMsger
Constants ¶
const BootnodeCon = 1
const MessageHeaderLength = 5
MessageHeaderLength define message header length
const NodeIDBits = 512
包内都用peerID,对外方法使用NodeID
const (
// PID vnt protocol basic id
PID = "/p2p/1.0.0"
)
Variables ¶
This section is empty.
Functions ¶
func ConstructDHT ¶
func ConstructDHT(ctx context.Context, listenstring string, nodekey *ecdsa.PrivateKey, datadir string, restrictList []*net.IPNet, natm libp2p.Option) (*dht.IpfsDHT, p2phost.Host, error)
ConstructDHT create Kademlia DHT
func ExpectMsg ¶
func ExpectMsg(r MsgReader, code MessageType, content interface{}) error
ExpectMsg COMMENT: this function is just for _test.go files 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 GetIPfromAddr ¶
func Send ¶
func Send(w MsgWriter, protocolID string, msgType MessageType, data interface{}) error
Send is used to send message payload with specific messge type
func SendItems ¶
func SendItems(w MsgWriter, protocolID string, msgType MessageType, elems ...interface{}) error
SendItems can send many payload in one function call
func WritePackage ¶
临时测试使用
Types ¶
type Config ¶
type Config struct { PrivateKey *ecdsa.PrivateKey `toml:"-"` MaxPeers int MaxPendingPeers int `toml:",omitempty"` DialRatio int `toml:",omitempty"` NoDiscovery bool Name string `toml:"-"` BootstrapNodes []*Node StaticNodes []*Node TrustedNodes []*Node NetRestrict []*net.IPNet `toml:",omitempty"` NodeDatabase string `toml:",omitempty"` Protocols []Protocol `toml:"-"` ListenAddr string NAT libp2p.Option `toml:"-"` 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 GoodMorningMsg ¶
GoodMorningMsg message for goodmorning protocol
func (*GoodMorningMsg) HandleMessage ¶
func (gmm *GoodMorningMsg) HandleMessage() error
HandleMessage implement VNTMessage interface
type LevelDB ¶
type LevelDB struct {
// contains filtered or unexported fields
}
LevelDB vntdb object
func GetDatastore ¶
GetDatastore singleton design pattern
type MessageType ¶
type MessageType uint64
MessageType define vnt p2p protocol message type
const ( // GoodMorning say good morning protocol GoodMorning MessageType = iota // GoodAfternoon say good afternoon protocol GoodAfternoon // GoodNight say good night protocol GoodNight )
type Msg ¶
Msg message struct
func (*Msg) GetBodySize ¶
GetBodySize get message body size in uint32
type MsgBody ¶
type MsgBody struct { ProtocolID string //Protocol name Type MessageType ReceivedAt time.Time PayloadSize uint32 Payload io.Reader }
MsgBody message body
type MsgReadWriter ¶
type Node ¶
func MustParseNode ¶
type NodeID ¶
type NodeID [NodeIDBits / 8]byte
func PeerIDtoNodeID ¶
func (NodeID) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (NodeID) TerminalString ¶
TerminalString returns a shortened hex string for terminal logging.
func (*NodeID) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.
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 VNTNode string `json:"vnode"` // Vnode URL for adding this peer from remote peers IP string `json:"ip"` // IP address of the node Ports struct { Discovery int `json:"discovery"` // UDP listening port for discovery protocol Listener int `json:"listener"` // TCP listening port for RLPx } `json:"ports"` ListenAddr string `json:"listenAddr"` Protocols map[string]interface{} `json:"protocols"` }
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
func (*Peer) Disconnect ¶
func (p *Peer) Disconnect(reason DiscReason)
func (*Peer) Drop ¶ added in v0.6.2
func (p *Peer) Drop()
Drop this peer forever because of protocol mismatch
func (*Peer) RemoteAddr ¶
type PeerEvent ¶
type PeerEvent struct { Type PeerEventType `json:"type"` Peer libp2p.ID `json:"peer"` Error string `json:"error,omitempty"` Protocol string `json:"protocol,omitempty"` MsgCode *uint64 `json:"msg_code,omitempty"` MsgSize *uint32 `json:"msg_size,omitempty"` }
PeerEvent is an event emitted when peers are either added or dropped from a p2p.Server or when a message is sent or received on a peer connection
type PeerEventType ¶
type PeerEventType string
const ( // PeerEventTypeAdd is the type of event emitted when a peer is added // to a p2p.Server PeerEventTypeAdd PeerEventType = "add" // PeerEventTypeDrop is the type of event emitted when a peer is // dropped from a p2p.Server PeerEventTypeDrop PeerEventType = "drop" // PeerEventTypeMsgSend is the type of event emitted when a // message is successfully sent to a peer PeerEventTypeMsgSend PeerEventType = "msgsend" // PeerEventTypeMsgRecv is the type of event emitted when a // message is received from a peer PeerEventTypeMsgRecv PeerEventType = "msgrecv" )
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 Caps []string `json:"caps"` // Sum-protocols advertised by this particular peer Network struct { LocalAddress string `json:"localAddress"` // Local endpoint of the TCP data connection RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection Inbound bool `json:"inbound"` Trusted bool `json:"trusted"` Static bool `json:"static"` } `json:"network"` Protocols map[string]interface{} `json:"protocols"` // Sub-protocol specific metadata fields }
type Protocol ¶
type Protocol struct { Name string Version uint Length uint64 Run func(peer *Peer, rw MsgReadWriter) error NodeInfo func() interface{} PeerInfo func(id libp2p.ID) interface{} }
目前依然沿用原有的子协议结构,减少上层的改动
type Server ¶
type Server struct { Config // contains filtered or unexported fields }
func (*Server) Close ¶ added in v0.6.2
func (server *Server) Close()
close server.quit to broadcast the server shutdown
func (*Server) GetPeerByRemoteID ¶
GetPeerByRemoteID get specific peer by remoteID if it doesn't exist, new it this function guarantee get the wanted peer
func (*Server) HandleStream ¶
HandleStream handle all message which is from anywhere 主、被动连接都走的流程
func (*Server) RemovePeer ¶
func (*Server) SetupStream ¶
SetupStream 主动发起连接
func (*Server) SubscribeEvents ¶
func (server *Server) SubscribeEvents(ch chan *PeerEvent) event.Subscription
type VNTDht ¶
type VNTDht struct {
// contains filtered or unexported fields
}