diff --git a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/UniqueMessageMulticaster.java b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/UniqueMessageMulticaster.java index dc6dc3bbae..4e76648df3 100644 --- a/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/UniqueMessageMulticaster.java +++ b/consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/UniqueMessageMulticaster.java @@ -14,6 +14,7 @@ package tech.pegasys.pantheon.consensus.ibft; import tech.pegasys.pantheon.consensus.ibft.network.ValidatorMulticaster; import tech.pegasys.pantheon.ethereum.core.Address; +import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; import java.util.Collection; @@ -42,11 +43,11 @@ public class UniqueMessageMulticaster implements ValidatorMulticaster { } // Set that starts evicting members when it hits capacity - private final Set seenMessages = + private final Set seenMessages = Collections.newSetFromMap( - new LinkedHashMap() { + new LinkedHashMap() { @Override - protected boolean removeEldestEntry(final Map.Entry eldest) { + protected boolean removeEldestEntry(final Map.Entry eldest) { return size() > maxSeenMessages; } }); @@ -58,7 +59,7 @@ public class UniqueMessageMulticaster implements ValidatorMulticaster { @Override public void send(final MessageData message, final Collection
blackList) { - final int uniqueID = message.hashCode(); + final Hash uniqueID = Hash.hash(message.getData()); if (seenMessages.contains(uniqueID)) { return; } diff --git a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/UniqueMessageMulticasterTest.java b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/UniqueMessageMulticasterTest.java index a832780423..51931ef46b 100644 --- a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/UniqueMessageMulticasterTest.java +++ b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/UniqueMessageMulticasterTest.java @@ -85,9 +85,46 @@ public class UniqueMessageMulticasterTest { @Test public void passedInBlackListIsPassedToUnderlyingValidator() { - List
blackList = + final List
blackList = Lists.newArrayList(AddressHelpers.ofValue(0), AddressHelpers.ofValue(1)); messageTracker.send(messageSent, blackList); verify(multicaster, times(1)).send(messageSent, blackList); } + + @Test + public void anonymousMessageDataClassesContainingTheSameDataAreConsideredIdentical() { + + final MessageData arbitraryMessage_1 = + createAnonymousMessageData(BytesValue.wrap(new byte[4]), 1); + + final MessageData arbitraryMessage_2 = + createAnonymousMessageData(BytesValue.wrap(new byte[4]), 1); + + messageTracker.send(arbitraryMessage_1); + verify(multicaster, times(1)).send(arbitraryMessage_1, emptyList()); + reset(multicaster); + + messageTracker.send(arbitraryMessage_2); + verifyZeroInteractions(multicaster); + } + + private MessageData createAnonymousMessageData(final BytesValue content, final int code) { + return new MessageData() { + + @Override + public int getSize() { + return content.size(); + } + + @Override + public int getCode() { + return code; + } + + @Override + public BytesValue getData() { + return content; + } + }; + } }