diff --git a/src/main/java/org/qora/controller/Controller.java b/src/main/java/org/qora/controller/Controller.java index 1410b8c0..ebdf13f4 100644 --- a/src/main/java/org/qora/controller/Controller.java +++ b/src/main/java/org/qora/controller/Controller.java @@ -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) { diff --git a/src/main/java/org/qora/network/Handshake.java b/src/main/java/org/qora/network/Handshake.java index a32acd11..2bf9da67 100644 --- a/src/main/java/org/qora/network/Handshake.java +++ b/src/main/java/org/qora/network/Handshake.java @@ -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) { diff --git a/src/main/java/org/qora/network/Network.java b/src/main/java/org/qora/network/Network.java index c11daf7d..506b0f13 100644 --- a/src/main/java/org/qora/network/Network.java +++ b/src/main/java/org/qora/network/Network.java @@ -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 */ diff --git a/src/main/java/org/qora/network/message/GetPeersMessage.java b/src/main/java/org/qora/network/message/GetPeersMessage.java new file mode 100644 index 00000000..1ddb1eb5 --- /dev/null +++ b/src/main/java/org/qora/network/message/GetPeersMessage.java @@ -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]; + } + +}