hyperledger-labs / Scorex

Scorex 2.0 Core
Apache License 2.0
543 stars 115 forks source link

Introducing session id and network id during the handshake #382

Closed kushti closed 3 years ago

kushti commented 3 years ago

A handshake can be enhanced via adding a new PeerFeature, see LocalAddressPeerFeature. Do a new feature with id = 3 which contains network magic bytes (from settings) and a session ID. The session ID should be generated when the node started, 64 random bits (8 bytes) enough. If session ID is not received from other peer, then it equals to 0.

Then the following code needs to be rewritten, isSelf should be based on session Id, not address.

//drop connection to self if occurred or peer already connected val shouldDrop = isSelf(remoteAddress) || connectionForPeerAddress(peerAddress).exists(_.handlerRef != peerHandlerRef) if (shouldDrop) { connectedPeer.handlerRef ! CloseConnection peerManagerRef ! RemovePeer(peerAddress) connections -= connectedPeer.connectionId.remoteAddress } else { peerManagerRef ! AddOrUpdatePeer(peerInfo)

    val updatedConnectedPeer = connectedPeer.copy(peerInfo = Some(peerInfo))
    connections += remoteAddress -> updatedConnectedPeer
    context.system.eventStream.publish(HandshakedPeer(updatedConnectedPeer))
  }

Also, connection should be dropped if handshake is coming from another network (different magic)

Also for the code above, if peer already connected, " peerManagerRef ! RemovePeer(peerAddress) connections -= connectedPeer.connectionId.remoteAddress" is not needed (it is needed for self only)