2020-12-14 12:27:15 +03:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
2021-12-13 06:16:03 +03:00
|
|
|
"time"
|
2020-12-14 12:27:15 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-07-04 04:49:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2021-09-28 11:03:38 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
2023-04-10 13:36:45 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/bsc"
|
2021-09-28 11:03:38 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/diff"
|
2020-12-14 12:27:15 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/snap"
|
2022-07-04 04:49:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/trust"
|
2020-12-14 12:27:15 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// errPeerSetClosed is returned if a peer is attempted to be added or removed
|
|
|
|
// from the peer set after it has been terminated.
|
|
|
|
errPeerSetClosed = errors.New("peerset closed")
|
|
|
|
|
|
|
|
// errPeerAlreadyRegistered is returned if a peer is attempted to be added
|
|
|
|
// to the peer set, but one with the same id already exists.
|
|
|
|
errPeerAlreadyRegistered = errors.New("peer already registered")
|
|
|
|
|
2021-12-13 06:16:03 +03:00
|
|
|
// errPeerWaitTimeout is returned if a peer waits extension for too long
|
|
|
|
errPeerWaitTimeout = errors.New("peer wait timeout")
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// errPeerNotRegistered is returned if a peer is attempted to be removed from
|
|
|
|
// a peer set, but no peer with the given id exists.
|
|
|
|
errPeerNotRegistered = errors.New("peer not registered")
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// errSnapWithoutEth is returned if a peer attempts to connect only on the
|
2021-12-13 06:16:03 +03:00
|
|
|
// snap protocol without advertising the eth main protocol.
|
2021-02-02 11:44:36 +03:00
|
|
|
errSnapWithoutEth = errors.New("peer connected on snap without compatible eth support")
|
2021-09-28 11:03:38 +03:00
|
|
|
|
|
|
|
// errDiffWithoutEth is returned if a peer attempts to connect only on the
|
2021-12-13 06:16:03 +03:00
|
|
|
// diff protocol without advertising the eth main protocol.
|
2021-09-28 11:03:38 +03:00
|
|
|
errDiffWithoutEth = errors.New("peer connected on diff without compatible eth support")
|
2022-07-04 04:49:07 +03:00
|
|
|
|
|
|
|
// errTrustWithoutEth is returned if a peer attempts to connect only on the
|
|
|
|
// trust protocol without advertising the eth main protocol.
|
|
|
|
errTrustWithoutEth = errors.New("peer connected on trust without compatible eth support")
|
2023-04-10 13:36:45 +03:00
|
|
|
|
|
|
|
// errBscWithoutEth is returned if a peer attempts to connect only on the
|
|
|
|
// bsc protocol without advertising the eth main protocol.
|
|
|
|
errBscWithoutEth = errors.New("peer connected on bsc without compatible eth support")
|
2020-12-14 12:27:15 +03:00
|
|
|
)
|
|
|
|
|
2021-12-13 06:16:03 +03:00
|
|
|
const (
|
|
|
|
// extensionWaitTimeout is the maximum allowed time for the extension wait to
|
|
|
|
// complete before dropping the connection as malicious.
|
|
|
|
extensionWaitTimeout = 10 * time.Second
|
2023-04-19 11:19:14 +03:00
|
|
|
tryWaitTimeout = 100 * time.Millisecond
|
2021-12-13 06:16:03 +03:00
|
|
|
)
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// peerSet represents the collection of active peers currently participating in
|
2021-02-02 11:44:36 +03:00
|
|
|
// the `eth` protocol, with or without the `snap` extension.
|
2020-12-14 12:27:15 +03:00
|
|
|
type peerSet struct {
|
2021-02-02 11:44:36 +03:00
|
|
|
peers map[string]*ethPeer // Peers connected on the `eth` protocol
|
|
|
|
snapPeers int // Number of `snap` compatible peers for connection prioritization
|
2020-12-14 12:27:15 +03:00
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
snapWait map[string]chan *snap.Peer // Peers connected on `eth` waiting for their snap extension
|
|
|
|
snapPend map[string]*snap.Peer // Peers connected on the `snap` protocol, but not yet on `eth`
|
2020-12-14 12:27:15 +03:00
|
|
|
|
2021-09-28 11:03:38 +03:00
|
|
|
diffWait map[string]chan *diff.Peer // Peers connected on `eth` waiting for their diff extension
|
|
|
|
diffPend map[string]*diff.Peer // Peers connected on the `diff` protocol, but not yet on `eth`
|
|
|
|
|
2022-07-04 04:49:07 +03:00
|
|
|
trustWait map[string]chan *trust.Peer // Peers connected on `eth` waiting for their trust extension
|
|
|
|
trustPend map[string]*trust.Peer // Peers connected on the `trust` protocol, but not yet on `eth`
|
|
|
|
|
2023-04-10 13:36:45 +03:00
|
|
|
bscWait map[string]chan *bsc.Peer // Peers connected on `eth` waiting for their bsc extension
|
|
|
|
bscPend map[string]*bsc.Peer // Peers connected on the `bsc` protocol, but not yet on `eth`
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
lock sync.RWMutex
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// newPeerSet creates a new peer set to track the active participants.
|
|
|
|
func newPeerSet() *peerSet {
|
|
|
|
return &peerSet{
|
2022-07-04 04:49:07 +03:00
|
|
|
peers: make(map[string]*ethPeer),
|
|
|
|
snapWait: make(map[string]chan *snap.Peer),
|
|
|
|
snapPend: make(map[string]*snap.Peer),
|
|
|
|
diffWait: make(map[string]chan *diff.Peer),
|
|
|
|
diffPend: make(map[string]*diff.Peer),
|
|
|
|
trustWait: make(map[string]chan *trust.Peer),
|
|
|
|
trustPend: make(map[string]*trust.Peer),
|
2023-04-10 13:36:45 +03:00
|
|
|
bscWait: make(map[string]chan *bsc.Peer),
|
|
|
|
bscPend: make(map[string]*bsc.Peer),
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// registerSnapExtension unblocks an already connected `eth` peer waiting for its
|
|
|
|
// `snap` extension, or if no such peer exists, tracks the extension for the time
|
|
|
|
// being until the `eth` main protocol starts looking for it.
|
|
|
|
func (ps *peerSet) registerSnapExtension(peer *snap.Peer) error {
|
|
|
|
// Reject the peer if it advertises `snap` without `eth` as `snap` is only a
|
|
|
|
// satellite protocol meaningful with the chain selection of `eth`
|
2021-02-05 16:15:22 +03:00
|
|
|
if !peer.RunningCap(eth.ProtocolName, eth.ProtocolVersions) {
|
2021-02-02 11:44:36 +03:00
|
|
|
return errSnapWithoutEth
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
// Ensure nobody can double connect
|
|
|
|
ps.lock.Lock()
|
|
|
|
defer ps.lock.Unlock()
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
id := peer.ID()
|
2021-02-02 11:44:36 +03:00
|
|
|
if _, ok := ps.peers[id]; ok {
|
|
|
|
return errPeerAlreadyRegistered // avoid connections with the same id as existing ones
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
if _, ok := ps.snapPend[id]; ok {
|
|
|
|
return errPeerAlreadyRegistered // avoid connections with the same id as pending ones
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
// Inject the peer into an `eth` counterpart is available, otherwise save for later
|
|
|
|
if wait, ok := ps.snapWait[id]; ok {
|
|
|
|
delete(ps.snapWait, id)
|
|
|
|
wait <- peer
|
|
|
|
return nil
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
ps.snapPend[id] = peer
|
2020-12-14 12:27:15 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:03:38 +03:00
|
|
|
// registerDiffExtension unblocks an already connected `eth` peer waiting for its
|
|
|
|
// `diff` extension, or if no such peer exists, tracks the extension for the time
|
|
|
|
// being until the `eth` main protocol starts looking for it.
|
|
|
|
func (ps *peerSet) registerDiffExtension(peer *diff.Peer) error {
|
|
|
|
// Reject the peer if it advertises `diff` without `eth` as `diff` is only a
|
|
|
|
// satellite protocol meaningful with the chain selection of `eth`
|
|
|
|
if !peer.RunningCap(eth.ProtocolName, eth.ProtocolVersions) {
|
|
|
|
return errDiffWithoutEth
|
|
|
|
}
|
|
|
|
// Ensure nobody can double connect
|
|
|
|
ps.lock.Lock()
|
|
|
|
defer ps.lock.Unlock()
|
|
|
|
|
|
|
|
id := peer.ID()
|
|
|
|
if _, ok := ps.peers[id]; ok {
|
|
|
|
return errPeerAlreadyRegistered // avoid connections with the same id as existing ones
|
|
|
|
}
|
|
|
|
if _, ok := ps.diffPend[id]; ok {
|
|
|
|
return errPeerAlreadyRegistered // avoid connections with the same id as pending ones
|
|
|
|
}
|
|
|
|
// Inject the peer into an `eth` counterpart is available, otherwise save for later
|
|
|
|
if wait, ok := ps.diffWait[id]; ok {
|
|
|
|
delete(ps.diffWait, id)
|
|
|
|
wait <- peer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ps.diffPend[id] = peer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-04 04:49:07 +03:00
|
|
|
// registerTrustExtension unblocks an already connected `eth` peer waiting for its
|
|
|
|
// `trust` extension, or if no such peer exists, tracks the extension for the time
|
|
|
|
// being until the `eth` main protocol starts looking for it.
|
|
|
|
func (ps *peerSet) registerTrustExtension(peer *trust.Peer) error {
|
|
|
|
// Reject the peer if it advertises `trust` without `eth` as `trust` is only a
|
|
|
|
// satellite protocol meaningful with the chain selection of `eth`
|
|
|
|
if !peer.RunningCap(eth.ProtocolName, eth.ProtocolVersions) {
|
|
|
|
return errTrustWithoutEth
|
|
|
|
}
|
|
|
|
// If the peer isn't verify node, don't register trust extension into eth protocol.
|
|
|
|
if !peer.VerifyNode() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Ensure nobody can double connect
|
|
|
|
ps.lock.Lock()
|
|
|
|
defer ps.lock.Unlock()
|
|
|
|
|
|
|
|
id := peer.ID()
|
|
|
|
if _, ok := ps.peers[id]; ok {
|
|
|
|
return errPeerAlreadyRegistered // avoid connections with the same id as existing ones
|
|
|
|
}
|
|
|
|
if _, ok := ps.trustPend[id]; ok {
|
|
|
|
return errPeerAlreadyRegistered // avoid connections with the same id as pending ones
|
|
|
|
}
|
|
|
|
// Inject the peer into an `eth` counterpart is available, otherwise save for later
|
|
|
|
if wait, ok := ps.trustWait[id]; ok {
|
|
|
|
delete(ps.trustWait, id)
|
|
|
|
wait <- peer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ps.trustPend[id] = peer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:36:45 +03:00
|
|
|
// registerBscExtension unblocks an already connected `eth` peer waiting for its
|
|
|
|
// `bsc` extension, or if no such peer exists, tracks the extension for the time
|
|
|
|
// being until the `eth` main protocol starts looking for it.
|
|
|
|
func (ps *peerSet) registerBscExtension(peer *bsc.Peer) error {
|
|
|
|
// Reject the peer if it advertises `bsc` without `eth` as `bsc` is only a
|
|
|
|
// satellite protocol meaningful with the chain selection of `eth`
|
|
|
|
if !peer.RunningCap(eth.ProtocolName, eth.ProtocolVersions) {
|
|
|
|
return errBscWithoutEth
|
|
|
|
}
|
|
|
|
// Ensure nobody can double connect
|
|
|
|
ps.lock.Lock()
|
|
|
|
defer ps.lock.Unlock()
|
|
|
|
|
|
|
|
id := peer.ID()
|
|
|
|
if _, ok := ps.peers[id]; ok {
|
|
|
|
return errPeerAlreadyRegistered // avoid connections with the same id as existing ones
|
|
|
|
}
|
|
|
|
if _, ok := ps.bscPend[id]; ok {
|
|
|
|
return errPeerAlreadyRegistered // avoid connections with the same id as pending ones
|
|
|
|
}
|
|
|
|
// Inject the peer into an `eth` counterpart is available, otherwise save for later
|
|
|
|
if wait, ok := ps.bscWait[id]; ok {
|
|
|
|
delete(ps.bscWait, id)
|
|
|
|
wait <- peer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ps.bscPend[id] = peer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// waitExtensions blocks until all satellite protocols are connected and tracked
|
|
|
|
// by the peerset.
|
|
|
|
func (ps *peerSet) waitSnapExtension(peer *eth.Peer) (*snap.Peer, error) {
|
|
|
|
// If the peer does not support a compatible `snap`, don't wait
|
2021-02-05 16:15:22 +03:00
|
|
|
if !peer.RunningCap(snap.ProtocolName, snap.ProtocolVersions) {
|
2021-02-02 11:44:36 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// Ensure nobody can double connect
|
2020-12-14 12:27:15 +03:00
|
|
|
ps.lock.Lock()
|
2021-02-02 11:44:36 +03:00
|
|
|
|
|
|
|
id := peer.ID()
|
|
|
|
if _, ok := ps.peers[id]; ok {
|
2020-12-14 12:27:15 +03:00
|
|
|
ps.lock.Unlock()
|
2021-02-02 11:44:36 +03:00
|
|
|
return nil, errPeerAlreadyRegistered // avoid connections with the same id as existing ones
|
|
|
|
}
|
|
|
|
if _, ok := ps.snapWait[id]; ok {
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerAlreadyRegistered // avoid connections with the same id as pending ones
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
// If `snap` already connected, retrieve the peer from the pending set
|
|
|
|
if snap, ok := ps.snapPend[id]; ok {
|
|
|
|
delete(ps.snapPend, id)
|
2020-12-14 12:27:15 +03:00
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
ps.lock.Unlock()
|
|
|
|
return snap, nil
|
|
|
|
}
|
|
|
|
// Otherwise wait for `snap` to connect concurrently
|
|
|
|
wait := make(chan *snap.Peer)
|
|
|
|
ps.snapWait[id] = wait
|
2020-12-14 12:27:15 +03:00
|
|
|
ps.lock.Unlock()
|
|
|
|
|
2021-12-13 06:16:03 +03:00
|
|
|
select {
|
|
|
|
case peer := <-wait:
|
|
|
|
return peer, nil
|
|
|
|
|
|
|
|
case <-time.After(extensionWaitTimeout):
|
|
|
|
ps.lock.Lock()
|
|
|
|
delete(ps.snapWait, id)
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerWaitTimeout
|
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:03:38 +03:00
|
|
|
// waitDiffExtension blocks until all satellite protocols are connected and tracked
|
|
|
|
// by the peerset.
|
|
|
|
func (ps *peerSet) waitDiffExtension(peer *eth.Peer) (*diff.Peer, error) {
|
|
|
|
// If the peer does not support a compatible `diff`, don't wait
|
|
|
|
if !peer.RunningCap(diff.ProtocolName, diff.ProtocolVersions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// Ensure nobody can double connect
|
|
|
|
ps.lock.Lock()
|
|
|
|
|
|
|
|
id := peer.ID()
|
|
|
|
if _, ok := ps.peers[id]; ok {
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerAlreadyRegistered // avoid connections with the same id as existing ones
|
|
|
|
}
|
|
|
|
if _, ok := ps.diffWait[id]; ok {
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerAlreadyRegistered // avoid connections with the same id as pending ones
|
|
|
|
}
|
|
|
|
// If `diff` already connected, retrieve the peer from the pending set
|
|
|
|
if diff, ok := ps.diffPend[id]; ok {
|
|
|
|
delete(ps.diffPend, id)
|
|
|
|
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return diff, nil
|
|
|
|
}
|
|
|
|
// Otherwise wait for `diff` to connect concurrently
|
|
|
|
wait := make(chan *diff.Peer)
|
|
|
|
ps.diffWait[id] = wait
|
|
|
|
ps.lock.Unlock()
|
|
|
|
|
2021-12-13 06:16:03 +03:00
|
|
|
select {
|
|
|
|
case peer := <-wait:
|
|
|
|
return peer, nil
|
|
|
|
|
|
|
|
case <-time.After(extensionWaitTimeout):
|
|
|
|
ps.lock.Lock()
|
|
|
|
delete(ps.diffWait, id)
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerWaitTimeout
|
|
|
|
}
|
2021-09-28 11:03:38 +03:00
|
|
|
}
|
|
|
|
|
2022-07-04 04:49:07 +03:00
|
|
|
// waitTrustExtension blocks until all satellite protocols are connected and tracked
|
|
|
|
// by the peerset.
|
|
|
|
func (ps *peerSet) waitTrustExtension(peer *eth.Peer) (*trust.Peer, error) {
|
|
|
|
// If the peer does not support a compatible `trust`, don't wait
|
|
|
|
if !peer.RunningCap(trust.ProtocolName, trust.ProtocolVersions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// If the peer isn't verify node, don't register trust extension into eth protocol.
|
|
|
|
if !peer.VerifyNode() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// Ensure nobody can double connect
|
|
|
|
ps.lock.Lock()
|
|
|
|
|
|
|
|
id := peer.ID()
|
|
|
|
if _, ok := ps.peers[id]; ok {
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerAlreadyRegistered // avoid connections with the same id as existing ones
|
|
|
|
}
|
|
|
|
if _, ok := ps.trustWait[id]; ok {
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerAlreadyRegistered // avoid connections with the same id as pending ones
|
|
|
|
}
|
|
|
|
// If `trust` already connected, retrieve the peer from the pending set
|
|
|
|
if trust, ok := ps.trustPend[id]; ok {
|
|
|
|
delete(ps.trustPend, id)
|
|
|
|
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return trust, nil
|
|
|
|
}
|
|
|
|
// Otherwise wait for `trust` to connect concurrently
|
|
|
|
wait := make(chan *trust.Peer)
|
|
|
|
ps.trustWait[id] = wait
|
|
|
|
ps.lock.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case peer := <-wait:
|
|
|
|
return peer, nil
|
|
|
|
|
|
|
|
case <-time.After(extensionWaitTimeout):
|
|
|
|
ps.lock.Lock()
|
|
|
|
delete(ps.trustWait, id)
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerWaitTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:36:45 +03:00
|
|
|
// waitBscExtension blocks until all satellite protocols are connected and tracked
|
|
|
|
// by the peerset.
|
|
|
|
func (ps *peerSet) waitBscExtension(peer *eth.Peer) (*bsc.Peer, error) {
|
|
|
|
// If the peer does not support a compatible `bsc`, don't wait
|
|
|
|
if !peer.RunningCap(bsc.ProtocolName, bsc.ProtocolVersions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// Ensure nobody can double connect
|
|
|
|
ps.lock.Lock()
|
|
|
|
|
|
|
|
id := peer.ID()
|
|
|
|
if _, ok := ps.peers[id]; ok {
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerAlreadyRegistered // avoid connections with the same id as existing ones
|
|
|
|
}
|
|
|
|
if _, ok := ps.bscWait[id]; ok {
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerAlreadyRegistered // avoid connections with the same id as pending ones
|
|
|
|
}
|
|
|
|
// If `bsc` already connected, retrieve the peer from the pending set
|
|
|
|
if bsc, ok := ps.bscPend[id]; ok {
|
|
|
|
delete(ps.bscPend, id)
|
|
|
|
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return bsc, nil
|
|
|
|
}
|
|
|
|
// Otherwise wait for `bsc` to connect concurrently
|
|
|
|
wait := make(chan *bsc.Peer)
|
|
|
|
ps.bscWait[id] = wait
|
|
|
|
ps.lock.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case peer := <-wait:
|
|
|
|
return peer, nil
|
|
|
|
|
|
|
|
case <-time.After(extensionWaitTimeout):
|
2023-04-19 11:19:14 +03:00
|
|
|
// could be deadlock, so we use TryLock to avoid it.
|
|
|
|
if ps.lock.TryLock() {
|
|
|
|
delete(ps.bscWait, id)
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerWaitTimeout
|
|
|
|
}
|
|
|
|
// if TryLock failed, we wait for a while and try again.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-wait:
|
|
|
|
// discard the peer, even though the peer arrived.
|
|
|
|
return nil, errPeerWaitTimeout
|
|
|
|
case <-time.After(tryWaitTimeout):
|
|
|
|
if ps.lock.TryLock() {
|
|
|
|
delete(ps.bscWait, id)
|
|
|
|
ps.lock.Unlock()
|
|
|
|
return nil, errPeerWaitTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 13:36:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:03:38 +03:00
|
|
|
func (ps *peerSet) GetDiffPeer(pid string) downloader.IDiffPeer {
|
|
|
|
if p := ps.peer(pid); p != nil && p.diffExt != nil {
|
|
|
|
return p.diffExt
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-04 04:49:07 +03:00
|
|
|
// GetVerifyPeers returns an array of verify nodes.
|
|
|
|
func (ps *peerSet) GetVerifyPeers() []core.VerifyPeer {
|
|
|
|
ps.lock.RLock()
|
|
|
|
defer ps.lock.RUnlock()
|
|
|
|
|
|
|
|
res := make([]core.VerifyPeer, 0)
|
|
|
|
for _, p := range ps.peers {
|
|
|
|
if p.trustExt != nil && p.trustExt.Peer != nil {
|
|
|
|
res = append(res, p.trustExt.Peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// registerPeer injects a new `eth` peer into the working set, or returns an error
|
|
|
|
// if the peer is already known.
|
2023-04-10 13:36:45 +03:00
|
|
|
func (ps *peerSet) registerPeer(peer *eth.Peer, ext *snap.Peer, diffExt *diff.Peer, trustExt *trust.Peer, bscExt *bsc.Peer) error {
|
2021-02-02 11:44:36 +03:00
|
|
|
// Start tracking the new peer
|
2020-12-14 12:27:15 +03:00
|
|
|
ps.lock.Lock()
|
2021-02-02 11:44:36 +03:00
|
|
|
defer ps.lock.Unlock()
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
if ps.closed {
|
|
|
|
return errPeerSetClosed
|
|
|
|
}
|
|
|
|
id := peer.ID()
|
2021-02-02 11:44:36 +03:00
|
|
|
if _, ok := ps.peers[id]; ok {
|
2020-12-14 12:27:15 +03:00
|
|
|
return errPeerAlreadyRegistered
|
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
eth := ðPeer{
|
|
|
|
Peer: peer,
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
if ext != nil {
|
|
|
|
eth.snapExt = &snapPeer{ext}
|
|
|
|
ps.snapPeers++
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2021-09-28 11:03:38 +03:00
|
|
|
if diffExt != nil {
|
|
|
|
eth.diffExt = &diffPeer{diffExt}
|
|
|
|
}
|
2022-07-04 04:49:07 +03:00
|
|
|
if trustExt != nil {
|
|
|
|
eth.trustExt = &trustPeer{trustExt}
|
|
|
|
}
|
2023-04-10 13:36:45 +03:00
|
|
|
if bscExt != nil {
|
|
|
|
eth.bscExt = &bscPeer{bscExt}
|
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
ps.peers[id] = eth
|
2020-12-14 12:27:15 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// unregisterPeer removes a remote peer from the active set, disabling any further
|
|
|
|
// actions to/from that particular entity.
|
|
|
|
func (ps *peerSet) unregisterPeer(id string) error {
|
2020-12-14 12:27:15 +03:00
|
|
|
ps.lock.Lock()
|
2021-02-02 11:44:36 +03:00
|
|
|
defer ps.lock.Unlock()
|
|
|
|
|
|
|
|
peer, ok := ps.peers[id]
|
2020-12-14 12:27:15 +03:00
|
|
|
if !ok {
|
|
|
|
return errPeerNotRegistered
|
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
delete(ps.peers, id)
|
|
|
|
if peer.snapExt != nil {
|
|
|
|
ps.snapPeers--
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// peer retrieves the registered peer with the given id.
|
|
|
|
func (ps *peerSet) peer(id string) *ethPeer {
|
2020-12-14 12:27:15 +03:00
|
|
|
ps.lock.RLock()
|
|
|
|
defer ps.lock.RUnlock()
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
return ps.peers[id]
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-25 06:00:14 +03:00
|
|
|
// headPeers retrieves a specified number list of peers.
|
|
|
|
func (ps *peerSet) headPeers(num uint) []*ethPeer {
|
|
|
|
ps.lock.RLock()
|
|
|
|
defer ps.lock.RUnlock()
|
|
|
|
|
|
|
|
if num > uint(len(ps.peers)) {
|
|
|
|
num = uint(len(ps.peers))
|
|
|
|
}
|
|
|
|
|
|
|
|
list := make([]*ethPeer, 0, num)
|
|
|
|
for _, p := range ps.peers {
|
|
|
|
list = append(list, p)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// peersWithoutBlock retrieves a list of peers that do not have a given block in
|
2023-04-10 13:36:45 +03:00
|
|
|
// their set of known hashes, so it might be propagated to them.
|
2021-02-02 11:44:36 +03:00
|
|
|
func (ps *peerSet) peersWithoutBlock(hash common.Hash) []*ethPeer {
|
2020-12-14 12:27:15 +03:00
|
|
|
ps.lock.RLock()
|
|
|
|
defer ps.lock.RUnlock()
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
list := make([]*ethPeer, 0, len(ps.peers))
|
|
|
|
for _, p := range ps.peers {
|
2020-12-14 12:27:15 +03:00
|
|
|
if !p.KnownBlock(hash) {
|
|
|
|
list = append(list, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// peersWithoutTransaction retrieves a list of peers that do not have a given
|
|
|
|
// transaction in their set of known hashes.
|
|
|
|
func (ps *peerSet) peersWithoutTransaction(hash common.Hash) []*ethPeer {
|
2020-12-14 12:27:15 +03:00
|
|
|
ps.lock.RLock()
|
|
|
|
defer ps.lock.RUnlock()
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
list := make([]*ethPeer, 0, len(ps.peers))
|
|
|
|
for _, p := range ps.peers {
|
2020-12-14 12:27:15 +03:00
|
|
|
if !p.KnownTransaction(hash) {
|
|
|
|
list = append(list, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:36:45 +03:00
|
|
|
// peersWithoutVote retrieves a list of peers that do not have a given
|
|
|
|
// vote in their set of known hashes.
|
|
|
|
func (ps *peerSet) peersWithoutVote(hash common.Hash) []*ethPeer {
|
|
|
|
ps.lock.RLock()
|
|
|
|
defer ps.lock.RUnlock()
|
|
|
|
|
|
|
|
list := make([]*ethPeer, 0, len(ps.peers))
|
|
|
|
for _, p := range ps.peers {
|
|
|
|
if p.bscExt != nil && !p.bscExt.KnownVote(hash) {
|
|
|
|
list = append(list, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// len returns if the current number of `eth` peers in the set. Since the `snap`
|
2021-01-25 21:06:52 +03:00
|
|
|
// peers are tied to the existence of an `eth` connection, that will always be a
|
2020-12-14 12:27:15 +03:00
|
|
|
// subset of `eth`.
|
2021-02-02 11:44:36 +03:00
|
|
|
func (ps *peerSet) len() int {
|
2020-12-14 12:27:15 +03:00
|
|
|
ps.lock.RLock()
|
|
|
|
defer ps.lock.RUnlock()
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
return len(ps.peers)
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// snapLen returns if the current number of `snap` peers in the set.
|
|
|
|
func (ps *peerSet) snapLen() int {
|
2021-01-25 21:06:52 +03:00
|
|
|
ps.lock.RLock()
|
|
|
|
defer ps.lock.RUnlock()
|
2021-02-02 11:44:36 +03:00
|
|
|
|
|
|
|
return ps.snapPeers
|
2021-01-25 21:06:52 +03:00
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// peerWithHighestTD retrieves the known peer with the currently highest total
|
2020-12-14 12:27:15 +03:00
|
|
|
// difficulty.
|
2021-02-02 11:44:36 +03:00
|
|
|
func (ps *peerSet) peerWithHighestTD() *eth.Peer {
|
2020-12-14 12:27:15 +03:00
|
|
|
ps.lock.RLock()
|
|
|
|
defer ps.lock.RUnlock()
|
|
|
|
|
|
|
|
var (
|
|
|
|
bestPeer *eth.Peer
|
|
|
|
bestTd *big.Int
|
|
|
|
)
|
2021-02-02 11:44:36 +03:00
|
|
|
for _, p := range ps.peers {
|
2023-02-09 17:01:44 +03:00
|
|
|
if p.Lagging() {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
if _, td := p.Head(); bestPeer == nil || td.Cmp(bestTd) > 0 {
|
|
|
|
bestPeer, bestTd = p.Peer, td
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestPeer
|
|
|
|
}
|
|
|
|
|
|
|
|
// close disconnects all peers.
|
|
|
|
func (ps *peerSet) close() {
|
|
|
|
ps.lock.Lock()
|
|
|
|
defer ps.lock.Unlock()
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
for _, p := range ps.peers {
|
2020-12-14 12:27:15 +03:00
|
|
|
p.Disconnect(p2p.DiscQuitting)
|
|
|
|
}
|
|
|
|
ps.closed = true
|
|
|
|
}
|