discover

package
v0.0.0-...-4f94b01 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2023 License: GPL-2.0, GPL-3.0 Imports: 40 Imported by: 0

Documentation

Overview

Package discover implements the Node Discovery Protocol.

The Node Discovery protocol provides a way to find RLPx nodes that can be connected to. It uses a Kademlia-like protocol to maintain a distributed database of the IDs and endpoints of all listening nodes.

Index

Constants

View Source
const (
	SendWaitTime = 1 * time.Minute

	Smpcprotocol_type = iota + 1
	Xprotocol_type
	Sdkprotocol_type

	Smpc_findGroupPacket = iota + 10 + neighborsPacket //14
	Xp_findGroupPacket
	Sdk_findGroupPacket
	Smpc_groupPacket
	Sdk_groupPacket
	Xp_groupPacket
	Smpc_groupInfoPacket
	Sdk_groupStatusPacket
	PeerMsgPacket

	Xp_getCCPacket

	Smpc_MsgSplitPacket
	Smpc_MsgSplitAckPacket
	Smpc_BroadcastMsgPacket

	Ack_Packet
)
View Source
const NodeIDBits = 512

Variables

View Source
var (
	Smpcdelimiter  = "smpcmsg"
	Smpc_groupList *Group
	Xp_groupList   *Group

	LocalIP      string
	RemoteIP     net.IP
	RemotePort   = uint16(0)
	RemoteUpdate = false
	SelfEnode    = ""
	SelfIPPort   = ""

	Xp_changed = 0

	SDK_groupList map[NodeID]*Group = make(map[NodeID]*Group)
	GroupSDK      sync.Mutex

	Sequence   = uint64(1)
	SelfID     = ""
	SelfNodeID NodeID

	NeighRelay bool = false
)
View Source
var (
	Smpc_groupMemNum = 0
	Xp_groupMemNum   = 0
	SDK_groupNum     = 0
)
View Source
var (
	SmpcCall  func(interface{}, string)
	Msg2Peer  = common.NewSafeMap(10)
	MsgAckMap = common.NewSafeMap(10)
)

Functions

func AddNodes

func AddNodes(nid NodeID)

func CheckNetwokConnect

func CheckNetwokConnect()

func Compress

func Compress(c []byte) (string, error)

func Decode2

func Decode2(s string, datatype string) (interface{}, error)

func Encode2

func Encode2(obj interface{}) (string, error)

func GetEnode

func GetEnode() string

func GetEnodeStatus

func GetEnodeStatus(enode string) (string, error)

func GetGroupDir

func GetGroupDir() string

func GetLocalIP

func GetLocalIP() string

func GetRemoteIP

func GetRemoteIP() net.IP

func GetRemotePort

func GetRemotePort() uint16

func GetSmpcGidDb

func GetSmpcGidDb(eid string) error

GetSmpcGidDb open database for group db

func InitGroup

func InitGroup()

func InitIP

func InitIP(ip string, port uint16)

func InitP2pDir

func InitP2pDir()

func MergeSplitMsg

func MergeSplitMsg(ms *MsgSend) string

func ParseNodes

func ParseNodes(n []*Node) (int, string)

func PingNode

func PingNode(id NodeID, ip net.IP, port int) error

func PrintBucketNodeInfo

func PrintBucketNodeInfo(id NodeID)

func RecoverGroupAll

func RecoverGroupAll(SdkGroup map[NodeID]*Group) error

func RecoverGroupSDKList

func RecoverGroupSDKList() error

func RegisterBroadcastNodeCallback

func RegisterBroadcastNodeCallback(callbackfunc func(interface{}, string))

func RegisterGroupCallback

func RegisterGroupCallback(callbackfunc func(NodeID, string, interface{}, int, string))

func RegisterPriKeyCallback

func RegisterPriKeyCallback(callbackfunc func(interface{}))

func RegisterSdkMsgCallback

func RegisterSdkMsgCallback(sdkbackfunc func(interface{}, string) <-chan string)

func RegisterSdkMsgRetCallback

func RegisterSdkMsgRetCallback(sdkbackfunc func(interface{}, string))

func RegisterSendCallback

func RegisterSendCallback(callbackfunc func(interface{}))

func RegisterSmpcMsgCallback

func RegisterSmpcMsgCallback(callbackfunc func(interface{}) <-chan string)

func RegisterSmpcMsgRetCallback

func RegisterSmpcMsgRetCallback(callbackfunc func(interface{}))

func RegisterXpMsgCallback

func RegisterXpMsgCallback(callbackfunc func(interface{}) <-chan string)

func RegisterXpMsgRetCallback

func RegisterXpMsgRetCallback(callbackfunc func(interface{}))

func Remove

func Remove(n *Node)

func RemoveSequenceDoneRecv

func RemoveSequenceDoneRecv(id string)

func SendMsgAck

func SendMsgAck(t *udp, msghash string, from *net.UDPAddr, fromid NodeID)

func SendMsgSplitToPeerWithUDP

func SendMsgSplitToPeerWithUDP(toid NodeID, toaddr *net.UDPAddr, req interface{}, p2pType int, ptype int) error

send any object with udp split msg

func SendMsgToBroadcastNode

func SendMsgToBroadcastNode(node *Node, msg string) error

func SendMsgToNode

func SendMsgToNode(toid NodeID, toaddr *net.UDPAddr, msg string) error

send group info

func SendToGroup

func SendToGroup(gid NodeID, msg string, allNodes bool, p2pType int, gg []*Node) (string, error)

func SendToGroupCC

func SendToGroupCC(toid NodeID, toaddr *net.UDPAddr, msg string, p2pType int) (string, error)

func SendToMyselfAndReturn

func SendToMyselfAndReturn(selfID, msg string, p2pType int)

func SendToPeer

func SendToPeer(gid, toid NodeID, toaddr *net.UDPAddr, msg string, p2pType int) error

func StartCreateSDKGroup

func StartCreateSDKGroup(gid NodeID, threshold string, enode []*Node, Type string, exist bool, subGroup bool) string

func StoreGroupSDKListToDb

func StoreGroupSDKListToDb() error

func StoreGroupToDb

func StoreGroupToDb(groupInfo *Group) error

func UnCompress

func UnCompress(s string) (string, error)

func UpdateGroupNodesNumber

func UpdateGroupNodesNumber(number, p2pType int)

func UpdateGroupSDKNode

func UpdateGroupSDKNode(nodeID NodeID, ipport net.Addr)

func UpdateMyselfIP

func UpdateMyselfIP()

func UpdateOnLine

func UpdateOnLine(nodeID NodeID, online bool)

Types

type Ack

type Ack struct {
	Sequence   uint64
	Expiration uint64
}

type Config

type Config struct {
	// These settings are required and configure the UDP listener:
	PrivateKey *ecdsa.PrivateKey

	// These settings are optional:
	AnnounceAddr *net.UDPAddr      // local address announced in the DHT
	NodeDBPath   string            // if set, the node database is stored at this filesystem location
	NetRestrict  *netutil.Netlist  // network whitelist
	Bootnodes    []*Node           // list of bootstrap nodes
	Unhandled    chan<- ReadPacket // unhandled packets are sent on this channel
}

Config holds Table-related settings.

type Group

type Group struct {
	sync.Mutex
	ID NodeID
	//Gname      string
	Mode string // 2/3

	P2pType byte
	Nodes   []RpcNode
	Type    string // group type: 1+2, 1+1+1
	//userID      []string
	Expiration uint64
	// Ignore additional fields (for forward compatibility).
	Rest []rlp.RawValue `rlp:"tail"`
	// contains filtered or unexported fields
}

func NewGroup

func NewGroup() *Group

func RecoverGroupByGID

func RecoverGroupByGID(gid NodeID) (*Group, error)

type GroupSDKList

type GroupSDKList struct {
	Nodes []*Node
}

type MsgAck

type MsgAck struct {
	MsgHash string
	Flag    string
}

type MsgSend

type MsgSend struct {
	MsgHash    string
	Pos        string
	Num        string
	PacketType string
	SplitMsg   string
}

type Node

type Node struct {
	IP       net.IP // len 4 for IPv4 or 16 for IPv6
	UDP, TCP uint16 // port numbers
	ID       NodeID // the node's public key
	// contains filtered or unexported fields
}

Node represents a host on the network. The fields of Node may not be modified.

func GetGroup

func GetGroup(gid, id NodeID, addr *net.UDPAddr, target NodeID, p2pType int) []*Node

func MustParseNode

func MustParseNode(rawurl string) *Node

MustParseNode parses a node URL. It panics if the URL is not valid.

func NewNode

func NewNode(id NodeID, ip net.IP, udpPort, tcpPort uint16) *Node

NewNode creates a new node. It is mostly meant to be used for testing purposes.

func ParseNode

func ParseNode(rawurl string) (*Node, error)

ParseNode parses a node designator.

There are two basic forms of node designators

  • incomplete nodes, which only have the public key (node ID)
  • complete nodes, which contain the public key and IP/Port information

For incomplete nodes, the designator must look like one of these

enode://<hex node id>
<hex node id>

For complete nodes, the node ID is encoded in the username portion of the URL, separated from the host by an @ sign. The hostname can only be given as an IP address, DNS domain names are not allowed. The port in the host name section is the TCP listening port. If the TCP and UDP (discovery) ports differ, the UDP port is specified as query parameter "discport".

In the following example, the node URL describes a node with IP address 10.3.58.6, TCP listening port 30303 and UDP discovery port 30301.

enode://<hex node id>@10.3.58.6:30303?discport=30301

func (*Node) Incomplete

func (n *Node) Incomplete() bool

Incomplete returns true for nodes with no IP address.

func (*Node) MarshalText

func (n *Node) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Node) String

func (n *Node) String() string

The string representation of a Node is a URL. Please see ParseNode for a description of the format.

func (*Node) UnmarshalText

func (n *Node) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type NodeID

type NodeID [NodeIDBits / 8]byte

NodeID is a unique identifier for each node. The node identifier is a marshaled elliptic curve public key.

func BytesID

func BytesID(b []byte) (NodeID, error)

BytesID converts a byte slice to a NodeID

func GetLocalID

func GetLocalID() NodeID

func HexID

func HexID(in string) (NodeID, error)

HexID converts a hex string to a NodeID. The string may be prefixed with 0x.

func MustBytesID

func MustBytesID(b []byte) NodeID

MustBytesID converts a byte slice to a NodeID. It panics if the byte slice is not a valid NodeID.

func MustHexID

func MustHexID(in string) NodeID

MustHexID converts a hex string to a NodeID. It panics if the string is not a valid NodeID.

func PubkeyID

func PubkeyID(pub *ecdsa.PublicKey) NodeID

PubkeyID returns a marshaled representation of the given public key.

func (NodeID) Bytes

func (n NodeID) Bytes() []byte

Bytes returns a byte slice representation of the NodeID

func (NodeID) GoString

func (n NodeID) GoString() string

The Go syntax representation of a NodeID is a call to HexID.

func (NodeID) MarshalText

func (n NodeID) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (NodeID) Pubkey

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

Pubkey returns the public key represented by the node ID. It returns an error if the ID is not a point on the curve.

func (NodeID) String

func (n NodeID) String() string

NodeID prints as a long hexadecimal number.

func (NodeID) TerminalString

func (n NodeID) TerminalString() string

TerminalString returns a shortened hex string for terminal logging.

func (*NodeID) UnmarshalText

func (n *NodeID) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type OnLineStatus

type OnLineStatus struct {
	Status bool
	Lock   sync.Mutex
}

type ReadPacket

type ReadPacket struct {
	Data []byte
	Addr *net.UDPAddr
}

ReadPacket is sent to the unhandled channel when it could not be processed

type RpcNode

type RpcNode struct {
	IP  net.IP // len 4 for IPv4 or 16 for IPv6
	UDP uint16 // for discovery protocol
	TCP uint16 // for RLPx protocol
	ID  NodeID
}

RPC request structures

type SmpcBroadcastMsg

type SmpcBroadcastMsg struct {
	Data string
}

type Table

type Table struct {
	// contains filtered or unexported fields
}
var Table4group *Table

func ListenUDP

func ListenUDP(c conn, cfg Config) (*Table, error)

ListenUDP returns a new table that listens for UDP packets on laddr.

func (*Table) Close

func (tab *Table) Close()

Close terminates the network listener and flushes the node database.

func (*Table) Lookup

func (tab *Table) Lookup(targetID NodeID) []*Node

Lookup performs a network search for nodes close to the given target. It approaches the target by querying nodes that are closer to it on each iteration. The given target does not need to be an actual node identifier.

func (*Table) ReadRandomNodes

func (tab *Table) ReadRandomNodes(buf []*Node) (n int)

ReadRandomNodes fills the given slice with random nodes from the table. It will not write the same node more than once. The nodes in the slice are copies and can be modified by the caller.

func (*Table) Resolve

func (tab *Table) Resolve(targetID NodeID) *Node

Resolve searches for a specific node with the given ID. It returns nil if the node could not be found.

func (*Table) Self

func (tab *Table) Self() *Node

Self returns the local node. The returned node should not be modified by the caller.

Jump to

Keyboard shortcuts

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