2018-03-14 09:36:59 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2022-10-12 19:57:49 +00:00
|
|
|
"math"
|
2019-10-28 04:25:52 +00:00
|
|
|
"sync"
|
2022-10-12 19:57:49 +00:00
|
|
|
"sync/atomic"
|
2018-03-14 09:36:59 +00:00
|
|
|
"time"
|
2020-05-22 09:59:18 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/capability"
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-10-13 15:26:05 +00:00
|
|
|
maxPoolSize = 10000
|
2019-09-12 13:29:50 +00:00
|
|
|
connRetries = 3
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Discoverer is an interface that is responsible for maintaining
|
2019-02-13 18:01:10 +00:00
|
|
|
// a healthy connection pool.
|
2018-03-14 09:36:59 +00:00
|
|
|
type Discoverer interface {
|
|
|
|
BackFill(...string)
|
2022-10-12 19:57:49 +00:00
|
|
|
GetFanOut() int
|
2022-10-13 19:53:20 +00:00
|
|
|
NetworkSize() int
|
2018-03-14 09:36:59 +00:00
|
|
|
PoolCount() int
|
|
|
|
RequestRemote(int)
|
2022-11-17 14:07:19 +00:00
|
|
|
RegisterSelf(AddressablePeer)
|
|
|
|
RegisterGood(AddressablePeer)
|
|
|
|
RegisterConnected(AddressablePeer)
|
|
|
|
UnregisterConnected(AddressablePeer, bool)
|
2018-03-23 20:36:59 +00:00
|
|
|
UnconnectedPeers() []string
|
|
|
|
BadPeers() []string
|
2020-05-22 09:59:18 +00:00
|
|
|
GoodPeers() []AddressWithCapabilities
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// AddressWithCapabilities represents a node address with its capabilities.
|
2020-05-22 09:59:18 +00:00
|
|
|
type AddressWithCapabilities struct {
|
|
|
|
Address string
|
|
|
|
Capabilities capability.Capabilities
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// DefaultDiscovery default implementation of the Discoverer interface.
|
2018-03-14 09:36:59 +00:00
|
|
|
type DefaultDiscovery struct {
|
2022-11-17 14:07:19 +00:00
|
|
|
seeds map[string]string
|
2018-03-23 20:36:59 +00:00
|
|
|
transport Transporter
|
2019-10-28 04:25:52 +00:00
|
|
|
lock sync.RWMutex
|
2018-03-23 20:36:59 +00:00
|
|
|
dialTimeout time.Duration
|
|
|
|
badAddrs map[string]bool
|
2019-09-13 16:51:58 +00:00
|
|
|
connectedAddrs map[string]bool
|
2022-11-17 14:07:19 +00:00
|
|
|
handshakedAddrs map[string]bool
|
2020-05-22 09:59:18 +00:00
|
|
|
goodAddrs map[string]capability.Capabilities
|
2019-09-12 13:29:50 +00:00
|
|
|
unconnectedAddrs map[string]int
|
2021-03-26 09:26:45 +00:00
|
|
|
attempted map[string]bool
|
2022-10-12 19:57:49 +00:00
|
|
|
optimalFanOut int32
|
2022-10-13 19:53:20 +00:00
|
|
|
networkSize int32
|
2018-03-23 20:36:59 +00:00
|
|
|
requestCh chan int
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultDiscovery returns a new DefaultDiscovery.
|
2020-10-13 13:30:10 +00:00
|
|
|
func NewDefaultDiscovery(addrs []string, dt time.Duration, ts Transporter) *DefaultDiscovery {
|
2022-11-17 14:07:19 +00:00
|
|
|
var seeds = make(map[string]string)
|
|
|
|
for i := range addrs {
|
|
|
|
seeds[addrs[i]] = ""
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
d := &DefaultDiscovery{
|
2022-11-17 14:07:19 +00:00
|
|
|
seeds: seeds,
|
2018-03-23 20:36:59 +00:00
|
|
|
transport: ts,
|
|
|
|
dialTimeout: dt,
|
|
|
|
badAddrs: make(map[string]bool),
|
2019-09-13 16:51:58 +00:00
|
|
|
connectedAddrs: make(map[string]bool),
|
2022-11-17 14:07:19 +00:00
|
|
|
handshakedAddrs: make(map[string]bool),
|
2020-05-22 09:59:18 +00:00
|
|
|
goodAddrs: make(map[string]capability.Capabilities),
|
2019-09-12 13:29:50 +00:00
|
|
|
unconnectedAddrs: make(map[string]int),
|
2021-03-26 09:26:45 +00:00
|
|
|
attempted: make(map[string]bool),
|
2018-03-23 20:36:59 +00:00
|
|
|
requestCh: make(chan int),
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:52:19 +00:00
|
|
|
func newDefaultDiscovery(addrs []string, dt time.Duration, ts Transporter) Discoverer {
|
|
|
|
return NewDefaultDiscovery(addrs, dt, ts)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// BackFill implements the Discoverer interface and will backfill
|
2018-03-14 09:36:59 +00:00
|
|
|
// the pool with the given addresses.
|
|
|
|
func (d *DefaultDiscovery) BackFill(addrs ...string) {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Lock()
|
2022-10-13 12:47:55 +00:00
|
|
|
d.backfill(addrs...)
|
|
|
|
d.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DefaultDiscovery) backfill(addrs ...string) {
|
2018-03-14 09:36:59 +00:00
|
|
|
for _, addr := range addrs {
|
2022-11-17 14:07:19 +00:00
|
|
|
if d.badAddrs[addr] || d.connectedAddrs[addr] || d.handshakedAddrs[addr] ||
|
2019-10-28 04:25:52 +00:00
|
|
|
d.unconnectedAddrs[addr] > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
d.pushToPoolOrDrop(addr)
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2022-10-12 19:29:55 +00:00
|
|
|
d.updateNetSize()
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// PoolCount returns the number of the available node addresses.
|
2018-03-14 09:36:59 +00:00
|
|
|
func (d *DefaultDiscovery) PoolCount() int {
|
2022-10-13 15:26:05 +00:00
|
|
|
d.lock.RLock()
|
|
|
|
defer d.lock.RUnlock()
|
|
|
|
return d.poolCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DefaultDiscovery) poolCount() int {
|
|
|
|
return len(d.unconnectedAddrs)
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// pushToPoolOrDrop tries to push the address given into the pool, but if the pool
|
2021-05-12 20:17:03 +00:00
|
|
|
// is already full, it just drops it.
|
2019-09-12 13:47:08 +00:00
|
|
|
func (d *DefaultDiscovery) pushToPoolOrDrop(addr string) {
|
2022-10-13 15:26:05 +00:00
|
|
|
if len(d.unconnectedAddrs) < maxPoolSize {
|
|
|
|
d.unconnectedAddrs[addr] = connRetries
|
2019-09-12 13:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// RequestRemote tries to establish a connection with n nodes.
|
2022-10-13 15:26:05 +00:00
|
|
|
func (d *DefaultDiscovery) RequestRemote(requested int) {
|
|
|
|
for ; requested > 0; requested-- {
|
|
|
|
var nextAddr string
|
|
|
|
d.lock.Lock()
|
|
|
|
for addr := range d.unconnectedAddrs {
|
2022-11-17 14:07:19 +00:00
|
|
|
if !d.connectedAddrs[addr] && !d.handshakedAddrs[addr] && !d.attempted[addr] {
|
2022-10-13 15:26:05 +00:00
|
|
|
nextAddr = addr
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nextAddr == "" {
|
|
|
|
// Empty pool, try seeds.
|
2022-11-17 14:07:19 +00:00
|
|
|
for addr, ip := range d.seeds {
|
|
|
|
if ip == "" && !d.attempted[addr] {
|
2022-10-13 15:26:05 +00:00
|
|
|
nextAddr = addr
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nextAddr == "" {
|
|
|
|
d.lock.Unlock()
|
|
|
|
// The pool is empty, but all seed nodes are already connected (or attempted),
|
|
|
|
// we can end up in an infinite loop here, so drop the request.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
d.attempted[nextAddr] = true
|
|
|
|
d.lock.Unlock()
|
|
|
|
go d.tryAddress(nextAddr)
|
2020-02-24 12:39:31 +00:00
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:07:19 +00:00
|
|
|
// RegisterSelf registers the given Peer as a bad one, because it's our own node.
|
|
|
|
func (d *DefaultDiscovery) RegisterSelf(p AddressablePeer) {
|
|
|
|
var connaddr = p.ConnectionAddr()
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Lock()
|
2022-11-17 14:07:19 +00:00
|
|
|
delete(d.connectedAddrs, connaddr)
|
|
|
|
d.registerBad(connaddr, true)
|
|
|
|
d.registerBad(p.PeerAddr().String(), true)
|
|
|
|
d.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DefaultDiscovery) registerBad(addr string, force bool) {
|
|
|
|
_, isSeed := d.seeds[addr]
|
|
|
|
if isSeed {
|
|
|
|
if !force {
|
|
|
|
d.seeds[addr] = ""
|
|
|
|
} else {
|
|
|
|
d.seeds[addr] = "forever" // That's our own address, so never try connecting to it.
|
2022-10-13 15:26:05 +00:00
|
|
|
}
|
2022-11-17 14:07:19 +00:00
|
|
|
} else {
|
2022-10-13 15:26:05 +00:00
|
|
|
d.unconnectedAddrs[addr]--
|
2022-11-17 14:07:19 +00:00
|
|
|
if d.unconnectedAddrs[addr] <= 0 || force {
|
2022-10-13 15:26:05 +00:00
|
|
|
d.badAddrs[addr] = true
|
|
|
|
delete(d.unconnectedAddrs, addr)
|
|
|
|
delete(d.goodAddrs, addr)
|
|
|
|
}
|
2019-10-28 04:25:52 +00:00
|
|
|
}
|
2022-10-12 19:29:55 +00:00
|
|
|
d.updateNetSize()
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnconnectedPeers returns all addresses of unconnected addrs.
|
|
|
|
func (d *DefaultDiscovery) UnconnectedPeers() []string {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RLock()
|
2019-02-09 15:53:58 +00:00
|
|
|
addrs := make([]string, 0, len(d.unconnectedAddrs))
|
2018-03-23 20:36:59 +00:00
|
|
|
for addr := range d.unconnectedAddrs {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RUnlock()
|
2018-03-23 20:36:59 +00:00
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// BadPeers returns all addresses of bad addrs.
|
|
|
|
func (d *DefaultDiscovery) BadPeers() []string {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RLock()
|
2019-02-09 15:53:58 +00:00
|
|
|
addrs := make([]string, 0, len(d.badAddrs))
|
2018-03-23 20:36:59 +00:00
|
|
|
for addr := range d.badAddrs {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RUnlock()
|
2018-03-23 20:36:59 +00:00
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2019-09-13 16:51:58 +00:00
|
|
|
// GoodPeers returns all addresses of known good peers (that at least once
|
2019-10-17 09:30:24 +00:00
|
|
|
// succeeded handshaking with us).
|
2020-05-22 09:59:18 +00:00
|
|
|
func (d *DefaultDiscovery) GoodPeers() []AddressWithCapabilities {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RLock()
|
2020-05-22 09:59:18 +00:00
|
|
|
addrs := make([]AddressWithCapabilities, 0, len(d.goodAddrs))
|
|
|
|
for addr, cap := range d.goodAddrs {
|
|
|
|
addrs = append(addrs, AddressWithCapabilities{
|
|
|
|
Address: addr,
|
|
|
|
Capabilities: cap,
|
|
|
|
})
|
2019-09-13 16:51:58 +00:00
|
|
|
}
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RUnlock()
|
2019-09-13 16:51:58 +00:00
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2022-11-17 14:07:19 +00:00
|
|
|
// RegisterGood registers a known good connected peer that has passed
|
2019-10-17 09:30:24 +00:00
|
|
|
// handshake successfully.
|
2022-11-17 14:07:19 +00:00
|
|
|
func (d *DefaultDiscovery) RegisterGood(p AddressablePeer) {
|
|
|
|
var s = p.PeerAddr().String()
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Lock()
|
2022-11-17 14:07:19 +00:00
|
|
|
d.handshakedAddrs[s] = true
|
|
|
|
d.goodAddrs[s] = p.Version().Capabilities
|
2020-10-13 11:16:06 +00:00
|
|
|
delete(d.badAddrs, s)
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Unlock()
|
2019-09-13 16:51:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:07:19 +00:00
|
|
|
// UnregisterConnected tells the discoverer that this peer is no longer
|
2022-04-20 18:30:09 +00:00
|
|
|
// connected, but it is still considered a good one.
|
2022-11-17 14:07:19 +00:00
|
|
|
func (d *DefaultDiscovery) UnregisterConnected(p AddressablePeer, duplicate bool) {
|
|
|
|
var (
|
|
|
|
peeraddr = p.PeerAddr().String()
|
|
|
|
connaddr = p.ConnectionAddr()
|
|
|
|
)
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Lock()
|
2022-11-17 14:07:19 +00:00
|
|
|
delete(d.connectedAddrs, connaddr)
|
|
|
|
if !duplicate {
|
|
|
|
for addr, ip := range d.seeds {
|
|
|
|
if ip == peeraddr {
|
|
|
|
d.seeds[addr] = ""
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(d.handshakedAddrs, peeraddr)
|
|
|
|
if _, ok := d.goodAddrs[peeraddr]; ok {
|
|
|
|
d.backfill(peeraddr)
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-11-17 14:07:19 +00:00
|
|
|
// RegisterConnected tells discoverer that the given peer is now connected.
|
|
|
|
func (d *DefaultDiscovery) RegisterConnected(p AddressablePeer) {
|
|
|
|
var addr = p.ConnectionAddr()
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Lock()
|
2022-11-17 14:07:19 +00:00
|
|
|
d.registerConnected(addr)
|
|
|
|
d.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DefaultDiscovery) registerConnected(addr string) {
|
2019-10-28 04:25:52 +00:00
|
|
|
delete(d.unconnectedAddrs, addr)
|
|
|
|
d.connectedAddrs[addr] = true
|
2022-10-12 19:29:55 +00:00
|
|
|
d.updateNetSize()
|
2019-09-13 16:51:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 19:57:49 +00:00
|
|
|
// GetFanOut returns the optimal number of nodes to broadcast packets to.
|
|
|
|
func (d *DefaultDiscovery) GetFanOut() int {
|
|
|
|
return int(atomic.LoadInt32(&d.optimalFanOut))
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:53:20 +00:00
|
|
|
// NetworkSize returns the estimated network size.
|
|
|
|
func (d *DefaultDiscovery) NetworkSize() int {
|
|
|
|
return int(atomic.LoadInt32(&d.networkSize))
|
|
|
|
}
|
|
|
|
|
2022-10-12 19:29:55 +00:00
|
|
|
// updateNetSize updates network size estimation metric. Must be called under read lock.
|
|
|
|
func (d *DefaultDiscovery) updateNetSize() {
|
2022-11-17 14:07:19 +00:00
|
|
|
var netsize = len(d.handshakedAddrs) + len(d.unconnectedAddrs) + 1 // 1 for the node itself.
|
|
|
|
var fanOut = 2.5 * math.Log(float64(netsize-1)) // -1 for the number of potential peers.
|
|
|
|
if netsize == 2 { // log(1) == 0.
|
2022-10-12 19:57:49 +00:00
|
|
|
fanOut = 1 // But we still want to push messages to the peer.
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.StoreInt32(&d.optimalFanOut, int32(fanOut+0.5)) // Truncating conversion, hence +0.5.
|
2022-10-13 19:53:20 +00:00
|
|
|
atomic.StoreInt32(&d.networkSize, int32(netsize))
|
2022-10-12 19:57:49 +00:00
|
|
|
updateNetworkSizeMetric(netsize)
|
2022-10-13 15:26:05 +00:00
|
|
|
updatePoolCountMetric(d.poolCount())
|
2022-10-12 19:29:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 08:59:16 +00:00
|
|
|
func (d *DefaultDiscovery) tryAddress(addr string) {
|
2022-11-17 14:07:19 +00:00
|
|
|
p, err := d.transport.Dial(addr, d.dialTimeout)
|
2021-03-26 09:26:45 +00:00
|
|
|
d.lock.Lock()
|
|
|
|
delete(d.attempted, addr)
|
2022-11-17 14:07:19 +00:00
|
|
|
if err == nil {
|
|
|
|
if _, ok := d.seeds[addr]; ok {
|
|
|
|
d.seeds[addr] = p.PeerAddr().String()
|
|
|
|
}
|
|
|
|
d.registerConnected(addr)
|
|
|
|
} else {
|
|
|
|
d.registerBad(addr, false)
|
|
|
|
}
|
2021-03-26 09:26:45 +00:00
|
|
|
d.lock.Unlock()
|
|
|
|
if err != nil {
|
2022-10-24 11:34:48 +00:00
|
|
|
time.Sleep(d.dialTimeout)
|
2019-10-28 04:25:52 +00:00
|
|
|
d.RequestRemote(1)
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
}
|