Reorder handshake to increase compat with v1 nodes + request remote peers

This commit is contained in:
catbref 2019-03-06 10:40:39 +00:00
parent bf3f78e718
commit 43eec116b5
4 changed files with 54 additions and 13 deletions

View File

@ -28,6 +28,7 @@ import org.qora.network.Network;
import org.qora.network.Peer;
import org.qora.network.message.BlockMessage;
import org.qora.network.message.GetBlockMessage;
import org.qora.network.message.GetPeersMessage;
import org.qora.network.message.GetSignaturesMessage;
import org.qora.network.message.HeightMessage;
import org.qora.network.message.Message;
@ -317,6 +318,9 @@ public class Controller extends Thread {
// Send our current height
network.broadcast(peer -> new HeightMessage(this.getChainHeight()));
// Request peers lists
network.broadcast(peer -> new GetPeersMessage());
}
public void onGeneratedBlock(BlockData newBlockData) {

View File

@ -15,23 +15,11 @@ public enum Handshake {
STARTED(null) {
@Override
public Handshake onMessage(Peer peer, Message message) {
return VERSION;
}
@Override
public void action(Peer peer) {
}
},
VERSION(MessageType.VERSION) {
@Override
public Handshake onMessage(Peer peer, Message message) {
peer.setVersionMessage((VersionMessage) message);
return SELF_CHECK;
}
@Override
public void action(Peer peer) {
sendVersion(peer);
}
},
SELF_CHECK(MessageType.PEER_ID) {
@ -63,6 +51,19 @@ public enum Handshake {
return null;
}
return VERSION;
}
@Override
public void action(Peer peer) {
sendMyId(peer);
}
},
VERSION(MessageType.VERSION) {
@Override
public Handshake onMessage(Peer peer, Message message) {
peer.setVersionMessage((VersionMessage) message);
// If we're both version 2 peers then next stage is proof
if (peer.getVersion() >= 2)
return PROOF;
@ -73,7 +74,7 @@ public enum Handshake {
@Override
public void action(Peer peer) {
sendMyId(peer);
sendVersion(peer);
}
},
PROOF(MessageType.PROOF) {

View File

@ -27,8 +27,10 @@ import org.apache.logging.log4j.Logger;
import org.qora.controller.Controller;
import org.qora.data.network.PeerData;
import org.qora.data.transaction.TransactionData;
import org.qora.network.message.GetPeersMessage;
import org.qora.network.message.HeightMessage;
import org.qora.network.message.Message;
import org.qora.network.message.Message.MessageType;
import org.qora.network.message.PeersMessage;
import org.qora.network.message.PeersV2Message;
import org.qora.network.message.PingMessage;
@ -306,6 +308,10 @@ public class Network extends Thread {
// Check message type is as expected
if (handshakeStatus.expectedMessageType != null && message.getType() != handshakeStatus.expectedMessageType) {
// v1 nodes are keen on sending PINGs early. Discard as we'll send a PING right after handshake
if (message.getType() == MessageType.PING)
return;
LOGGER.debug(String.format("Unexpected %s message from %s, expected %s", message.getType().name(), peer, handshakeStatus.expectedMessageType));
peer.disconnect();
return;
@ -429,6 +435,11 @@ public class Network extends Thread {
} catch (DataException e) {
LOGGER.error("Repository issue while sending unconfirmed transactions", e);
}
// Request their peers list
Message getPeersMessage = new GetPeersMessage();
if (!peer.sendMessage(getPeersMessage))
peer.disconnect();
}
/** Returns PEERS message made from peers we've connected to recently, and this node's details */

View File

@ -0,0 +1,25 @@
package org.qora.network.message;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
public class GetPeersMessage extends Message {
public GetPeersMessage() {
this(-1);
}
private GetPeersMessage(int id) {
super(id, MessageType.GET_PEERS);
}
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws UnsupportedEncodingException {
return new GetPeersMessage(id);
}
@Override
protected byte[] toData() {
return new byte[0];
}
}