p2p comment prettify
This commit is contained in:
parent
e4fae760af
commit
bdc2711f27
|
@ -615,8 +615,8 @@ func (a *AddrBook) moveToOld(ka *knownAddress) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// doublesha256(key + sourcegroup +
|
// doublesha256( key + sourcegroup +
|
||||||
// int64(doublesha256(key + group + sourcegroup))%bucket_per_source_group) % num_new_buckes
|
// int64(doublesha256(key + group + sourcegroup))%bucket_per_group ) % num_new_buckets
|
||||||
func (a *AddrBook) calcNewBucket(addr, src *NetAddress) int {
|
func (a *AddrBook) calcNewBucket(addr, src *NetAddress) int {
|
||||||
data1 := []byte{}
|
data1 := []byte{}
|
||||||
data1 = append(data1, []byte(a.key)...)
|
data1 = append(data1, []byte(a.key)...)
|
||||||
|
@ -636,7 +636,8 @@ func (a *AddrBook) calcNewBucket(addr, src *NetAddress) int {
|
||||||
return int(binary.BigEndian.Uint64(hash2) % newBucketCount)
|
return int(binary.BigEndian.Uint64(hash2) % newBucketCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
// doublesha256(key + group + truncate_to_64bits(doublesha256(key + addr))%buckets_per_group) % num_buckets
|
// doublesha256( key + group +
|
||||||
|
// int64(doublesha256(key + addr))%buckets_per_group ) % num_old_buckets
|
||||||
func (a *AddrBook) calcOldBucket(addr *NetAddress) int {
|
func (a *AddrBook) calcOldBucket(addr *NetAddress) int {
|
||||||
data1 := []byte{}
|
data1 := []byte{}
|
||||||
data1 = append(data1, []byte(a.key)...)
|
data1 = append(data1, []byte(a.key)...)
|
||||||
|
|
|
@ -99,8 +99,6 @@ SKIP_UPNP:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accept connections and pass on the channel
|
// Accept connections and pass on the channel
|
||||||
// Reading from the channel blocks on the peerHandshake for each connection
|
|
||||||
// Connection is ignored if we have too many connections to that ip range
|
|
||||||
func (l *DefaultListener) listenRoutine() {
|
func (l *DefaultListener) listenRoutine() {
|
||||||
for {
|
for {
|
||||||
conn, err := l.listener.Accept()
|
conn, err := l.listener.Accept()
|
||||||
|
|
|
@ -22,6 +22,7 @@ type Peer struct {
|
||||||
Data *CMap // User data.
|
Data *CMap // User data.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: blocking
|
||||||
func peerHandshake(conn net.Conn, ourNodeInfo *types.NodeInfo) (*types.NodeInfo, error) {
|
func peerHandshake(conn net.Conn, ourNodeInfo *types.NodeInfo) (*types.NodeInfo, error) {
|
||||||
var peerNodeInfo = new(types.NodeInfo)
|
var peerNodeInfo = new(types.NodeInfo)
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
|
@ -22,7 +22,7 @@ var (
|
||||||
|
|
||||||
// PeerSet is a special structure for keeping a table of peers.
|
// PeerSet is a special structure for keeping a table of peers.
|
||||||
// Iteration over the peers is super fast and thread-safe.
|
// Iteration over the peers is super fast and thread-safe.
|
||||||
// We also track how many peers per ip range and avoid too many
|
// We also track how many peers per IP range and avoid too many
|
||||||
type PeerSet struct {
|
type PeerSet struct {
|
||||||
mtx sync.Mutex
|
mtx sync.Mutex
|
||||||
lookup map[string]*peerSetItem
|
lookup map[string]*peerSetItem
|
||||||
|
@ -44,7 +44,7 @@ func NewPeerSet() *PeerSet {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns false if peer with key (uuid) is already in set
|
// Returns false if peer with key (uuid) is already in set
|
||||||
// or if we have too many peers from the peer's ip range
|
// or if we have too many peers from the peer's IP range
|
||||||
func (ps *PeerSet) Add(peer *Peer) error {
|
func (ps *PeerSet) Add(peer *Peer) error {
|
||||||
ps.mtx.Lock()
|
ps.mtx.Lock()
|
||||||
defer ps.mtx.Unlock()
|
defer ps.mtx.Unlock()
|
||||||
|
@ -52,8 +52,8 @@ func (ps *PeerSet) Add(peer *Peer) error {
|
||||||
return ErrSwitchDuplicatePeer
|
return ErrSwitchDuplicatePeer
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure we havent maxed out connections for the peer's ip range yet
|
// ensure we havent maxed out connections for the peer's IP range yet
|
||||||
// and update the ip range counters
|
// and update the IP range counters
|
||||||
if !ps.updateIPRangeCounts(peer.Host) {
|
if !ps.updateIPRangeCounts(peer.Host) {
|
||||||
return ErrSwitchMaxPeersPerIPRange
|
return ErrSwitchMaxPeersPerIPRange
|
||||||
}
|
}
|
||||||
|
@ -126,9 +126,9 @@ func (ps *PeerSet) List() []*Peer {
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// track the number of ips we're connected to for each ip address range
|
// track the number of IPs we're connected to for each IP address range
|
||||||
|
|
||||||
// forms an ip address hierarchy tree with counts
|
// forms an IP address hierarchy tree with counts
|
||||||
// the struct itself is not thread safe and should always only be accessed with the ps.mtx locked
|
// the struct itself is not thread safe and should always only be accessed with the ps.mtx locked
|
||||||
type nestedCounter struct {
|
type nestedCounter struct {
|
||||||
count int
|
count int
|
||||||
|
@ -141,7 +141,7 @@ func NewNestedCounter() *nestedCounter {
|
||||||
return nc
|
return nc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we have too many ips in the ip range of the incoming connection
|
// Check if we have too many IPs in the IP range of the incoming connection
|
||||||
// Thread safe
|
// Thread safe
|
||||||
func (ps *PeerSet) HasMaxForIPRange(conn net.Conn) (ok bool) {
|
func (ps *PeerSet) HasMaxForIPRange(conn net.Conn) (ok bool) {
|
||||||
ps.mtx.Lock()
|
ps.mtx.Lock()
|
||||||
|
@ -161,7 +161,7 @@ func (ps *PeerSet) HasMaxForIPRange(conn net.Conn) (ok bool) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update counts for this address' ip range
|
// Update counts for this address' IP range
|
||||||
// Returns false if we already have enough connections
|
// Returns false if we already have enough connections
|
||||||
// Not thread safe (only called by ps.Add())
|
// Not thread safe (only called by ps.Add())
|
||||||
func (ps *PeerSet) updateIPRangeCounts(address string) bool {
|
func (ps *PeerSet) updateIPRangeCounts(address string) bool {
|
||||||
|
@ -171,7 +171,7 @@ func (ps *PeerSet) updateIPRangeCounts(address string) bool {
|
||||||
return updateNestedCountRecursive(c, spl, 0)
|
return updateNestedCountRecursive(c, spl, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// recursively descend the ip hierarchy, checking if we have
|
// recursively descend the IP hierarchy, checking if we have
|
||||||
// max peers for each range and updating if not
|
// max peers for each range and updating if not
|
||||||
func updateNestedCountRecursive(c *nestedCounter, ipBytes []string, index int) bool {
|
func updateNestedCountRecursive(c *nestedCounter, ipBytes []string, index int) bool {
|
||||||
if index == len(ipBytes) {
|
if index == len(ipBytes) {
|
||||||
|
|
|
@ -194,7 +194,7 @@ func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) (*Peer, er
|
||||||
peer := newPeer(conn, peerNodeInfo, outbound, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError)
|
peer := newPeer(conn, peerNodeInfo, outbound, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError)
|
||||||
|
|
||||||
// Add the peer to .peers
|
// Add the peer to .peers
|
||||||
// ignore if duplicate or if we already have too many for that ip range
|
// ignore if duplicate or if we already have too many for that IP range
|
||||||
if err := sw.peers.Add(peer); err != nil {
|
if err := sw.peers.Add(peer); err != nil {
|
||||||
log.Info("Ignoring peer", "error", err, "peer", peer)
|
log.Info("Ignoring peer", "error", err, "peer", peer)
|
||||||
peer.stop() // will also close conn
|
peer.stop() // will also close conn
|
||||||
|
@ -315,7 +315,7 @@ func (sw *Switch) listenerRoutine(l Listener) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore connections from ip ranges for which we have too many
|
// Ignore connections from IP ranges for which we have too many
|
||||||
if sw.peers.HasMaxForIPRange(inConn) {
|
if sw.peers.HasMaxForIPRange(inConn) {
|
||||||
log.Debug("Ignoring inbound connection: already have enough peers for that IP range", "address", inConn.RemoteAddr().String())
|
log.Debug("Ignoring inbound connection: already have enough peers for that IP range", "address", inConn.RemoteAddr().String())
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue