diff --git a/ethereumj-core/src/main/java/org/ethereum/net/client/Capability.java b/ethereumj-core/src/main/java/org/ethereum/net/client/Capability.java index bd6b77e5..b6091414 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/client/Capability.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/client/Capability.java @@ -3,7 +3,7 @@ package org.ethereum.net.client; /** * The protocols and versions of those protocols that this peer support */ -public class Capability { +public class Capability implements Comparable { public final static String ETH = "eth"; public final static String SHH = "shh"; @@ -24,15 +24,21 @@ public class Capability { return version; } - public boolean equals(Object o) { - if(o instanceof Capability) { - Capability cap = (Capability) o; - if (cap.getName() == null) - return this.name == null; - else - return cap.getName().equals(this.name) && cap.getVersion() == this.version; - } - return false; + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof Capability)) return false; + + Capability other = (Capability)obj; + if (this.name == null) + return other.name == null; + else + return this.name.equals(other.name) && this.version == other.version; + } + + @Override + public int compareTo(Capability o) { + return this.name.compareTo(o.name); } public String toString() { diff --git a/ethereumj-core/src/main/java/org/ethereum/net/p2p/P2pHandler.java b/ethereumj-core/src/main/java/org/ethereum/net/p2p/P2pHandler.java index d122dc11..f860ad9a 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/p2p/P2pHandler.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/p2p/P2pHandler.java @@ -7,6 +7,7 @@ import static org.ethereum.net.message.StaticMessages.HELLO_MESSAGE; import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Set; import java.util.Timer; @@ -183,7 +184,9 @@ public class P2pHandler extends SimpleChannelInboundHandler { public void adaptMessageIds(List capabilities) { + Collections.sort(capabilities); byte offset = (byte) (P2pMessageCodes.USER.asByte() + 1); + for (Capability capability : capabilities) { if (capability.getName().equals(Capability.ETH)) { diff --git a/ethereumj-core/src/test/java/org/ethereum/net/wire/AdaptiveMessageIdsTest.java b/ethereumj-core/src/test/java/org/ethereum/net/wire/AdaptiveMessageIdsTest.java index 9984a2c7..0b723a6c 100644 --- a/ethereumj-core/src/test/java/org/ethereum/net/wire/AdaptiveMessageIdsTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/net/wire/AdaptiveMessageIdsTest.java @@ -40,7 +40,7 @@ public class AdaptiveMessageIdsTest { Assert.assertEquals(3, P2pMessageCodes.PONG.asByte()); Assert.assertEquals(4, P2pMessageCodes.GET_PEERS.asByte()); Assert.assertEquals(5, P2pMessageCodes.PEERS.asByte()); - Assert.assertEquals(16, P2pMessageCodes.USER.asByte()); + Assert.assertEquals(15, P2pMessageCodes.USER.asByte()); } @@ -102,48 +102,48 @@ public class AdaptiveMessageIdsTest { new Capability(Capability.SHH, ShhHandler.VERSION)); p2pHandler.adaptMessageIds(capabilities); - Assert.assertEquals(0x10 + 1, EthMessageCodes.STATUS.asByte()); - Assert.assertEquals(0x10 + 2, EthMessageCodes.GET_TRANSACTIONS.asByte()); - Assert.assertEquals(0x10 + 3, EthMessageCodes.TRANSACTIONS.asByte()); - Assert.assertEquals(0x10 + 4, EthMessageCodes.GET_BLOCK_HASHES.asByte()); - Assert.assertEquals(0x10 + 5, EthMessageCodes.BLOCK_HASHES.asByte()); - Assert.assertEquals(0x10 + 6, EthMessageCodes.GET_BLOCKS.asByte()); - Assert.assertEquals(0x10 + 7, EthMessageCodes.BLOCKS.asByte()); - Assert.assertEquals(0x10 + 8, EthMessageCodes.NEW_BLOCK.asByte()); - Assert.assertEquals(0x10 + 9, EthMessageCodes.PACKET_COUNT.asByte()); + Assert.assertEquals(0x10 + 0, EthMessageCodes.STATUS.asByte()); + Assert.assertEquals(0x10 + 1, EthMessageCodes.GET_TRANSACTIONS.asByte()); + Assert.assertEquals(0x10 + 2, EthMessageCodes.TRANSACTIONS.asByte()); + Assert.assertEquals(0x10 + 3, EthMessageCodes.GET_BLOCK_HASHES.asByte()); + Assert.assertEquals(0x10 + 4, EthMessageCodes.BLOCK_HASHES.asByte()); + Assert.assertEquals(0x10 + 5, EthMessageCodes.GET_BLOCKS.asByte()); + Assert.assertEquals(0x10 + 6, EthMessageCodes.BLOCKS.asByte()); + Assert.assertEquals(0x10 + 7, EthMessageCodes.NEW_BLOCK.asByte()); + Assert.assertEquals(0x10 + 8, EthMessageCodes.PACKET_COUNT.asByte()); - Assert.assertEquals(0x19 + 1, ShhMessageCodes.STATUS.asByte()); - Assert.assertEquals(0x19 + 2, ShhMessageCodes.MESSAGE.asByte()); - Assert.assertEquals(0x19 + 3, ShhMessageCodes.ADD_FILTER.asByte()); - Assert.assertEquals(0x19 + 4, ShhMessageCodes.REMOVE_FILTER.asByte()); - Assert.assertEquals(0x19 + 5, ShhMessageCodes.PACKET_COUNT.asByte()); + Assert.assertEquals(0x19 + 0, ShhMessageCodes.STATUS.asByte()); + Assert.assertEquals(0x19 + 1, ShhMessageCodes.MESSAGE.asByte()); + Assert.assertEquals(0x19 + 2, ShhMessageCodes.ADD_FILTER.asByte()); + Assert.assertEquals(0x19 + 3, ShhMessageCodes.REMOVE_FILTER.asByte()); + Assert.assertEquals(0x19 + 4, ShhMessageCodes.PACKET_COUNT.asByte()); } - @Test + @Test // Capabilities should be read in alphabetical order public void test5(){ P2pHandler p2pHandler = new P2pHandler(); List capabilities = Arrays.asList( - new Capability(Capability.ETH, EthHandler.VERSION), - new Capability(Capability.SHH, ShhHandler.VERSION)); + new Capability(Capability.SHH, ShhHandler.VERSION), + new Capability(Capability.ETH, EthHandler.VERSION)); p2pHandler.adaptMessageIds(capabilities); - Assert.assertEquals(0x10 + 1, ShhMessageCodes.STATUS.asByte()); - Assert.assertEquals(0x10 + 2, ShhMessageCodes.MESSAGE.asByte()); - Assert.assertEquals(0x10 + 3, ShhMessageCodes.ADD_FILTER.asByte()); - Assert.assertEquals(0x10 + 4, ShhMessageCodes.REMOVE_FILTER.asByte()); - Assert.assertEquals(0x10 + 5, ShhMessageCodes.PACKET_COUNT.asByte()); + Assert.assertEquals(0x10 + 0, EthMessageCodes.STATUS.asByte()); + Assert.assertEquals(0x10 + 1, EthMessageCodes.GET_TRANSACTIONS.asByte()); + Assert.assertEquals(0x10 + 2, EthMessageCodes.TRANSACTIONS.asByte()); + Assert.assertEquals(0x10 + 3, EthMessageCodes.GET_BLOCK_HASHES.asByte()); + Assert.assertEquals(0x10 + 4, EthMessageCodes.BLOCK_HASHES.asByte()); + Assert.assertEquals(0x10 + 5, EthMessageCodes.GET_BLOCKS.asByte()); + Assert.assertEquals(0x10 + 6, EthMessageCodes.BLOCKS.asByte()); + Assert.assertEquals(0x10 + 7, EthMessageCodes.NEW_BLOCK.asByte()); + Assert.assertEquals(0x10 + 8, EthMessageCodes.PACKET_COUNT.asByte()); - Assert.assertEquals(0x15 + 1, EthMessageCodes.STATUS.asByte()); - Assert.assertEquals(0x15 + 2, EthMessageCodes.GET_TRANSACTIONS.asByte()); - Assert.assertEquals(0x15 + 3, EthMessageCodes.TRANSACTIONS.asByte()); - Assert.assertEquals(0x15 + 4, EthMessageCodes.GET_BLOCK_HASHES.asByte()); - Assert.assertEquals(0x15 + 5, EthMessageCodes.BLOCK_HASHES.asByte()); - Assert.assertEquals(0x15 + 6, EthMessageCodes.GET_BLOCKS.asByte()); - Assert.assertEquals(0x15 + 7, EthMessageCodes.BLOCKS.asByte()); - Assert.assertEquals(0x15 + 8, EthMessageCodes.NEW_BLOCK.asByte()); - Assert.assertEquals(0x15 + 9, EthMessageCodes.PACKET_COUNT.asByte()); + Assert.assertEquals(0x19 + 0, ShhMessageCodes.STATUS.asByte()); + Assert.assertEquals(0x19 + 1, ShhMessageCodes.MESSAGE.asByte()); + Assert.assertEquals(0x19 + 2, ShhMessageCodes.ADD_FILTER.asByte()); + Assert.assertEquals(0x19 + 3, ShhMessageCodes.REMOVE_FILTER.asByte()); + Assert.assertEquals(0x19 + 4, ShhMessageCodes.PACKET_COUNT.asByte()); }