les: add unknown peers to server pool instead of rejecting them

This commit is contained in:
Zsolt Felfoldi 2017-01-23 01:54:01 +01:00
parent 1886d03faa
commit f5348e17f8
2 changed files with 45 additions and 38 deletions

View File

@ -160,9 +160,6 @@ func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, network
if manager.serverPool != nil { if manager.serverPool != nil {
addr := p.RemoteAddr().(*net.TCPAddr) addr := p.RemoteAddr().(*net.TCPAddr)
entry = manager.serverPool.connect(peer, addr.IP, uint16(addr.Port)) entry = manager.serverPool.connect(peer, addr.IP, uint16(addr.Port))
if entry == nil {
return fmt.Errorf("unwanted connection")
}
} }
peer.poolEntry = entry peer.poolEntry = entry
select { select {

View File

@ -160,10 +160,10 @@ func (pool *serverPool) connect(p *peer, ip net.IP, port uint16) *poolEntry {
defer pool.lock.Unlock() defer pool.lock.Unlock()
entry := pool.entries[p.ID()] entry := pool.entries[p.ID()]
if entry == nil { if entry == nil {
return nil entry = pool.findOrNewNode(p.ID(), ip, port)
} }
glog.V(logger.Debug).Infof("connecting to %v, state: %v", p.id, entry.state) glog.V(logger.Debug).Infof("connecting to %v, state: %v", p.id, entry.state)
if entry.state != psDialed { if entry.state == psConnected || entry.state == psRegistered {
return nil return nil
} }
pool.connWg.Add(1) pool.connWg.Add(1)
@ -250,11 +250,17 @@ type poolStatAdjust struct {
// adjustBlockDelay adjusts the block announce delay statistics of a node // adjustBlockDelay adjusts the block announce delay statistics of a node
func (pool *serverPool) adjustBlockDelay(entry *poolEntry, time time.Duration) { func (pool *serverPool) adjustBlockDelay(entry *poolEntry, time time.Duration) {
if entry == nil {
return
}
pool.adjustStats <- poolStatAdjust{pseBlockDelay, entry, time} pool.adjustStats <- poolStatAdjust{pseBlockDelay, entry, time}
} }
// adjustResponseTime adjusts the request response time statistics of a node // adjustResponseTime adjusts the request response time statistics of a node
func (pool *serverPool) adjustResponseTime(entry *poolEntry, time time.Duration, timeout bool) { func (pool *serverPool) adjustResponseTime(entry *poolEntry, time time.Duration, timeout bool) {
if entry == nil {
return
}
if timeout { if timeout {
pool.adjustStats <- poolStatAdjust{pseResponseTimeout, entry, time} pool.adjustStats <- poolStatAdjust{pseResponseTimeout, entry, time}
} else { } else {
@ -375,39 +381,7 @@ func (pool *serverPool) eventLoop() {
case node := <-pool.discNodes: case node := <-pool.discNodes:
pool.lock.Lock() pool.lock.Lock()
now := mclock.Now() entry := pool.findOrNewNode(discover.NodeID(node.ID), node.IP, node.TCP)
id := discover.NodeID(node.ID)
entry := pool.entries[id]
if entry == nil {
glog.V(logger.Debug).Infof("discovered %v", node.String())
entry = &poolEntry{
id: id,
addr: make(map[string]*poolEntryAddress),
addrSelect: *newWeightedRandomSelect(),
shortRetry: shortRetryCnt,
}
pool.entries[id] = entry
// initialize previously unknown peers with good statistics to give a chance to prove themselves
entry.connectStats.add(1, initStatsWeight)
entry.delayStats.add(0, initStatsWeight)
entry.responseStats.add(0, initStatsWeight)
entry.timeoutStats.add(0, initStatsWeight)
}
entry.lastDiscovered = now
addr := &poolEntryAddress{
ip: node.IP,
port: node.TCP,
}
if a, ok := entry.addr[addr.strKey()]; ok {
addr = a
} else {
entry.addr[addr.strKey()] = addr
}
addr.lastSeen = now
entry.addrSelect.update(addr)
if !entry.known {
pool.newQueue.setLatest(entry)
}
pool.updateCheckDial(entry) pool.updateCheckDial(entry)
pool.lock.Unlock() pool.lock.Unlock()
@ -434,6 +408,42 @@ func (pool *serverPool) eventLoop() {
} }
} }
func (pool *serverPool) findOrNewNode(id discover.NodeID, ip net.IP, port uint16) *poolEntry {
now := mclock.Now()
entry := pool.entries[id]
if entry == nil {
glog.V(logger.Debug).Infof("discovered %v", id.String())
entry = &poolEntry{
id: id,
addr: make(map[string]*poolEntryAddress),
addrSelect: *newWeightedRandomSelect(),
shortRetry: shortRetryCnt,
}
pool.entries[id] = entry
// initialize previously unknown peers with good statistics to give a chance to prove themselves
entry.connectStats.add(1, initStatsWeight)
entry.delayStats.add(0, initStatsWeight)
entry.responseStats.add(0, initStatsWeight)
entry.timeoutStats.add(0, initStatsWeight)
}
entry.lastDiscovered = now
addr := &poolEntryAddress{
ip: ip,
port: port,
}
if a, ok := entry.addr[addr.strKey()]; ok {
addr = a
} else {
entry.addr[addr.strKey()] = addr
}
addr.lastSeen = now
entry.addrSelect.update(addr)
if !entry.known {
pool.newQueue.setLatest(entry)
}
return entry
}
// loadNodes loads known nodes and their statistics from the database // loadNodes loads known nodes and their statistics from the database
func (pool *serverPool) loadNodes() { func (pool *serverPool) loadNodes() {
enc, err := pool.db.Get(pool.dbKey) enc, err := pool.db.Get(pool.dbKey)