Fix AdaptiveMessageIDs for alphabetical order

This commit is contained in:
nicksavers 2014-10-18 22:30:53 +02:00
parent b41e47b7c6
commit b14472b931
3 changed files with 51 additions and 42 deletions

View File

@ -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<Capability> {
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() {

View File

@ -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<P2pMessage> {
public void adaptMessageIds(List<Capability> capabilities) {
Collections.sort(capabilities);
byte offset = (byte) (P2pMessageCodes.USER.asByte() + 1);
for (Capability capability : capabilities) {
if (capability.getName().equals(Capability.ETH)) {

View File

@ -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<Capability> 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());
}