mirror of https://github.com/status-im/nim-eth.git
Rework duplicate connections check and fix #36
This commit is contained in:
parent
ee27111e2b
commit
f8bdec88c9
|
@ -70,12 +70,10 @@ proc processIncoming(server: StreamServer,
|
||||||
yield peerfut
|
yield peerfut
|
||||||
if not peerfut.failed:
|
if not peerfut.failed:
|
||||||
let peer = peerfut.read()
|
let peer = peerfut.read()
|
||||||
|
trace "Connection established (incoming)", peer
|
||||||
if node.peerPool != nil:
|
if node.peerPool != nil:
|
||||||
if not node.peerPool.addPeer(peer):
|
node.peerPool.connectingNodes.excl(peer.remote)
|
||||||
# In case an outgoing connection was added in the meanwhile or a
|
node.peerPool.addPeer(peer)
|
||||||
# malicious peer opens multiple connections
|
|
||||||
debug "Disconnecting peer (incoming)", reason = AlreadyConnected
|
|
||||||
await peer.disconnect(AlreadyConnected)
|
|
||||||
|
|
||||||
proc listeningAddress*(node: EthereumNode): ENode =
|
proc listeningAddress*(node: EthereumNode): ENode =
|
||||||
return initENode(node.keys.pubKey, node.address)
|
return initENode(node.keys.pubKey, node.address)
|
||||||
|
|
|
@ -107,24 +107,19 @@ proc getRandomBootnode(p: PeerPool): Option[Node] =
|
||||||
if p.discovery.bootstrapNodes.len != 0:
|
if p.discovery.bootstrapNodes.len != 0:
|
||||||
result = option(p.discovery.bootstrapNodes.rand())
|
result = option(p.discovery.bootstrapNodes.rand())
|
||||||
|
|
||||||
proc addPeer*(pool: PeerPool, peer: Peer): bool =
|
proc addPeer*(pool: PeerPool, peer: Peer) =
|
||||||
if peer.remote notin pool.connectedNodes:
|
doAssert(peer.remote notin pool.connectedNodes)
|
||||||
pool.connectedNodes[peer.remote] = peer
|
pool.connectedNodes[peer.remote] = peer
|
||||||
for o in pool.observers.values:
|
for o in pool.observers.values:
|
||||||
if not o.onPeerConnected.isNil:
|
if not o.onPeerConnected.isNil:
|
||||||
if o.protocol.isNil or peer.supports(o.protocol):
|
if o.protocol.isNil or peer.supports(o.protocol):
|
||||||
o.onPeerConnected(peer)
|
o.onPeerConnected(peer)
|
||||||
return true
|
|
||||||
else: return false
|
|
||||||
|
|
||||||
proc connectToNode*(p: PeerPool, n: Node) {.async.} =
|
proc connectToNode*(p: PeerPool, n: Node) {.async.} =
|
||||||
let peer = await p.connect(n)
|
let peer = await p.connect(n)
|
||||||
if not peer.isNil:
|
if not peer.isNil:
|
||||||
trace "Connection established", peer
|
trace "Connection established (outgoing)", peer
|
||||||
if not p.addPeer(peer):
|
p.addPeer(peer)
|
||||||
# In case an incoming connection was added in the meanwhile
|
|
||||||
trace "Disconnecting peer (outgoing)", reason = AlreadyConnected
|
|
||||||
await peer.disconnect(AlreadyConnected)
|
|
||||||
|
|
||||||
proc connectToNodes(p: PeerPool, nodes: seq[Node]) {.async.} =
|
proc connectToNodes(p: PeerPool, nodes: seq[Node]) {.async.} =
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
|
|
|
@ -1480,6 +1480,16 @@ proc rlpxAccept*(node: EthereumNode,
|
||||||
udpPort: remote.port)
|
udpPort: remote.port)
|
||||||
result.remote = newNode(initEnode(handshake.remoteHPubkey, address))
|
result.remote = newNode(initEnode(handshake.remoteHPubkey, address))
|
||||||
|
|
||||||
|
# In case there is an outgoing connection started with this peer we give
|
||||||
|
# precedence to that one and we disconnect here with `AlreadyConnected`
|
||||||
|
if result.remote in node.peerPool.connectedNodes or
|
||||||
|
result.remote in node.peerPool.connectingNodes:
|
||||||
|
trace "Duplicate connection in rlpxAccept"
|
||||||
|
raisePeerDisconnected("Peer already connecting or connected",
|
||||||
|
AlreadyConnected)
|
||||||
|
|
||||||
|
node.peerPool.connectingNodes.incl(result.remote)
|
||||||
|
|
||||||
await postHelloSteps(result, response)
|
await postHelloSteps(result, response)
|
||||||
ok = true
|
ok = true
|
||||||
except PeerDisconnected as e:
|
except PeerDisconnected as e:
|
||||||
|
|
Loading…
Reference in New Issue