Ibft to uniquely ID messages by their hash (#730)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
tmohay 6 years ago committed by GitHub
parent c628a175c5
commit ad3bac6907
  1. 9
      consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/UniqueMessageMulticaster.java
  2. 39
      consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/UniqueMessageMulticasterTest.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<Integer> seenMessages =
private final Set<Hash> seenMessages =
Collections.newSetFromMap(
new LinkedHashMap<Integer, Boolean>() {
new LinkedHashMap<Hash, Boolean>() {
@Override
protected boolean removeEldestEntry(final Map.Entry<Integer, Boolean> eldest) {
protected boolean removeEldestEntry(final Map.Entry<Hash, Boolean> eldest) {
return size() > maxSeenMessages;
}
});
@ -58,7 +59,7 @@ public class UniqueMessageMulticaster implements ValidatorMulticaster {
@Override
public void send(final MessageData message, final Collection<Address> blackList) {
final int uniqueID = message.hashCode();
final Hash uniqueID = Hash.hash(message.getData());
if (seenMessages.contains(uniqueID)) {
return;
}

@ -85,9 +85,46 @@ public class UniqueMessageMulticasterTest {
@Test
public void passedInBlackListIsPassedToUnderlyingValidator() {
List<Address> blackList =
final List<Address> 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;
}
};
}
}

Loading…
Cancel
Save