discover

package
v0.0.0-...-a968a3f Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2022 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const MaxPeersPerChain = 10
View Source
const (

	// sort tab size
	SortTableSize = 64
)

Variables

This section is empty.

Functions

func CheckRelayIP

func CheckRelayIP(sender, addr net.IP) error

CheckRelayIP reports whether an IP relayed from the given sender IP is a valid connection target.

There are four rules:

  • Special network addresses are never valid.
  • Loopback addresses are OK if relayed by a loopback host.
  • LAN addresses are OK if relayed by a LAN host.
  • All other addresses are always acceptable.

func GetConnectedChains

func GetConnectedChains(boots []*ChainDataNodes, centre common.ChainID) common.ChainIDs

get all the connected chains

func GetTargetChain

func GetTargetChain(boots []*ChainDataNodes, selfChain common.ChainID, targetChain common.ChainID) common.ChainID

find a chain who was closet to the target chain

func GetVisitChainIds

func GetVisitChainIds(boots []*ChainDataNodes, centre common.ChainID) common.ChainIDs

Get the chainId list which needs to dial out

func IsIn

func IsIn(cids common.ChainIDs, cid common.ChainID) bool

func IsLAN

func IsLAN(ip net.IP) bool

IsLAN reports whether an IP is a local network address.

func IsSpecialNetwork

func IsSpecialNetwork(ip net.IP) bool

IsSpecialNetwork reports whether an IP is located in a special-use network range This includes broadcast, multicast and documentation addresses.

func IsTemporaryError

func IsTemporaryError(err error) bool

func SameNet

func SameNet(bits uint, ip, other net.IP) bool

SameNet reports whether two IP addresses have an equal prefix of the given bit length.

Types

type AbsTime

type AbsTime time.Duration

AbsTime represents absolute monotonic time.

func Now

func Now() AbsTime

Now returns the current absolute monotonic time.

func (AbsTime) Add

func (t AbsTime) Add(d time.Duration) AbsTime

Add returns t + d as absolute time.

func (AbsTime) Sub

func (t AbsTime) Sub(t2 AbsTime) time.Duration

Sub returns t - t2 as a duration.

type ChainDataNodes

type ChainDataNodes struct {
	ChainId   common.ChainID
	DataNodes []*Node
}

func ToChainDataNodes

func ToChainDataNodes(net common.NetType, bootId common.ChainID, infos []*common.ChainInfos) []*ChainDataNodes

type ChanTimer

type ChanTimer interface {
	Timer

	// The channel returned by C receives a value when the timer expires.
	C() <-chan AbsTime
	// Reset reschedules the timer with a new timeout.
	// It should be invoked only on stopped or expired timers with drained channels.
	Reset(time.Duration)
}

ChanTimer is a cancellable event created by NewTimer.

type Clock

type Clock interface {
	Now() AbsTime
	Sleep(time.Duration)
	NewTimer(time.Duration) ChanTimer
	After(time.Duration) <-chan AbsTime
	AfterFunc(d time.Duration, f func()) Timer
}

The Clock interface makes it possible to replace the monotonic system clock with a simulated clock.

type DiscoverTable

type DiscoverTable interface {
	Self() *Node
	Close()
	// modify by gy
	Len() int
	Resolve(target common.NodeID) *Node
	Lookup(target interface{}) []*Node
	ReadRandomNodes([]*Node) int

	// FOR SORT TABLE
	GetDataNodes() []*ChainDataNodes
	GetAccessChains() common.ChainIDs
	SetTmpNodes(dataNodes []*ChainDataNodes)
	SwitchToTmpNodes()
}

type Discovery

type Discovery interface {
	// discovery type
	Type() DiscoveryType
	// version
	Version() uint32
	// read msg from udp connection
	NodeTable() DiscoverTable
	//Get chainid from tab
	GetChainID(id common.NodeID) (common.ChainID, error)
	// ping
	Ping(common.NodeID, *net.UDPAddr) error
	// find node
	FindNode(toid common.NodeID, addr *net.UDPAddr, target interface{}) (map[common.ChainID][]*Node, error)
	// close
	Close() error
}

func SetupDiscoveryKAD

func SetupDiscoveryKAD(self *Node, c *net.UDPConn, cfg UDPConfig) (Discovery, error)

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

func SetupDiscoverySRT

func SetupDiscoverySRT(self *Node, c *net.UDPConn, cfg UDPConfig) (Discovery, error)

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

type DiscoveryType

type DiscoveryType string
const (
	KAD DiscoveryType = "KAD"
	SRT DiscoveryType = "SORT"
)

func (DiscoveryType) IsKAD

func (d DiscoveryType) IsKAD() bool

func (DiscoveryType) IsSRT

func (d DiscoveryType) IsSRT() bool

type DistinctNetSet

type DistinctNetSet struct {
	Subnet uint // number of common prefix bits
	Limit  uint // maximum number of IPs in each subnet
	// contains filtered or unexported fields
}

DistinctNetSet tracks IPs, ensuring that at most N of them fall into the same network range.

func (*DistinctNetSet) Add

func (s *DistinctNetSet) Add(ip net.IP) bool

Add adds an IP address to the set. It returns false (and doesn't add the IP) if the number of existing IPs in the defined range exceeds the limit.

func (DistinctNetSet) Contains

func (s DistinctNetSet) Contains(ip net.IP) bool

Contains whether the given IP is contained in the set.

func (DistinctNetSet) Len

func (s DistinctNetSet) Len() int

Len returns the number of tracked IPs.

func (*DistinctNetSet) Remove

func (s *DistinctNetSet) Remove(ip net.IP)

Remove removes an IP from the set.

func (DistinctNetSet) String

func (s DistinctNetSet) String() string

String implements fmt.Stringer

type Netlist

type Netlist []net.IPNet

Netlist is a list of IP networks.

func (*Netlist) Add

func (l *Netlist) Add(cidr string)

Add parses a CIDR mask and appends it to the list. It panics for invalid masks and is intended to be used for setting up static lists.

func (*Netlist) Contains

func (l *Netlist) Contains(ip net.IP) bool

Contains reports whether the given IP is contained in the list.

func (Netlist) MarshalTOML

func (l Netlist) MarshalTOML() interface{}

MarshalTOML implements toml.MarshalerRec.

func (*Netlist) UnmarshalTOML

func (l *Netlist) UnmarshalTOML(fn func(interface{}) error) error

UnmarshalTOML implements toml.UnmarshalerRec.

type Node

type Node struct {
	ID   common.NodeID
	IP   net.IP
	TCP  uint16
	UDP  uint16
	RPC  uint16
	PUB  []byte
	Hash common.Hash
	// contains filtered or unexported fields
}

p2p node struct

func NewNode

func NewNode(nid common.NodeID, ip net.IP, tcp uint16, udp uint16, rpc uint16) *Node

func (*Node) GetRpcAddress

func (n *Node) GetRpcAddress() string

func (*Node) GetTcpAddress

func (n *Node) GetTcpAddress() string

func (*Node) GetUdpAddress

func (n *Node) GetUdpAddress() string

func (*Node) Incomplete

func (n *Node) Incomplete() bool

func (*Node) String

func (n *Node) String() string

func (*Node) UdpAddr

func (n *Node) UdpAddr() *net.UDPAddr

type P2PConfig

type P2PConfig struct {
	DatabasePath string

	BootstrapNodes []*Node

	StaticNodes []*Node

	TrustedNodes []*Node

	NetRestrict *Netlist

	ListenAddr string

	MaxPeersCount int

	MaxPendCount int

	DialRatio int

	Nat nat.Nat

	AnnounceAddr *net.UDPAddr

	DiscoveryType DiscoveryType

	ChainDataNodes []*ChainDataNodes

	Clock Clock
}

type ReadPacket

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

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

type STable

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

func (*STable) Close

func (tab *STable) Close()

Close terminates the network listener and flushes the node database.

func (*STable) Find

func (tab *STable) Find(target common.NodeID) *Node

func (*STable) GetAccessChains

func (tab *STable) GetAccessChains() common.ChainIDs

Server uses this to filter the keys of ChainToPeers,close the peers in value if the key is not in return list

func (*STable) GetDataNodes

func (tab *STable) GetDataNodes() []*ChainDataNodes

Get data nodes

func (*STable) Len

func (tab *STable) Len() (n int)

modify by gy

func (*STable) Lookup

func (tab *STable) Lookup(target interface{}) []*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 (*STable) ReadRandomNodes

func (tab *STable) 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 (*STable) Resolve

func (tab *STable) Resolve(targetID common.NodeID) *Node

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

func (*STable) Self

func (tab *STable) Self() *Node

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

func (*STable) SetTmpNodes

func (tab *STable) SetTmpNodes(dataNodes []*ChainDataNodes)

Update the tmp nodes when chain structure was changed

func (*STable) SwitchToTmpNodes

func (tab *STable) SwitchToTmpNodes()

switch to changed chains

type System

type System struct{}

System implements Clock using the system clock.

func (System) After

func (c System) After(d time.Duration) <-chan AbsTime

After returns a channel which receives the current time after d has elapsed.

func (System) AfterFunc

func (c System) AfterFunc(d time.Duration, f func()) Timer

AfterFunc runs f on a new goroutine after the duration has elapsed.

func (System) NewTimer

func (c System) NewTimer(d time.Duration) ChanTimer

NewTimer creates a timer which can be rescheduled.

func (System) Now

func (c System) Now() AbsTime

Now returns the current monotonic time.

func (System) Sleep

func (c System) Sleep(d time.Duration)

Sleep blocks for the given duration.

type Table

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

func (*Table) Close

func (tab *Table) Close()

Close terminates the network listener and flushes the node database.

func (*Table) GetAccessChains

func (tab *Table) GetAccessChains() common.ChainIDs

func (*Table) GetDataNodes

func (tab *Table) GetDataNodes() []*ChainDataNodes

获取数据节点--暂时不支持

func (*Table) Len

func (tab *Table) Len() (n int)

modify by gy

func (*Table) Lookup

func (tab *Table) Lookup(targetID interface{}) []*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 common.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.

func (*Table) SetTmpNodes

func (tab *Table) SetTmpNodes(dataNodes []*ChainDataNodes)

func (*Table) SwitchToTmpNodes

func (tab *Table) SwitchToTmpNodes()

type Timer

type Timer interface {
	// Stop cancels the timer. It returns false if the timer has already
	// expired or been stopped.
	Stop() bool
}

Timer is a cancellable event created by AfterFunc.

type UDPConfig

type UDPConfig struct {
	ChainID common.ChainID
	BootId  common.ChainID
	NetType common.NetType
	// 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
	Bootnodes      []*Node      // list of bootstrap nodes
	ChainDataNodes []*ChainDataNodes
}

Config holds Table-related settings.

Jump to

Keyboard shortcuts

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