mirror of https://github.com/hyperledger/besu
Prevent duplicate ibft messages being processed by state machine (#811)
Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>pull/2/head
parent
8d64c44c40
commit
a00160783b
@ -0,0 +1,38 @@ |
||||
/* |
||||
* Copyright 2019 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
*/ |
||||
package tech.pegasys.pantheon.consensus.ibft; |
||||
|
||||
import static java.util.Collections.newSetFromMap; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.core.Hash; |
||||
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; |
||||
|
||||
import java.util.Set; |
||||
|
||||
public class MessageTracker { |
||||
private final Set<Hash> seenMessages; |
||||
|
||||
public MessageTracker(final int messageTrackingLimit) { |
||||
this.seenMessages = newSetFromMap(new SizeLimitedMap<>(messageTrackingLimit)); |
||||
} |
||||
|
||||
public void addSeenMessage(final MessageData message) { |
||||
final Hash uniqueID = Hash.hash(message.getData()); |
||||
seenMessages.add(uniqueID); |
||||
} |
||||
|
||||
public boolean hasSeenMessage(final MessageData message) { |
||||
final Hash uniqueID = Hash.hash(message.getData()); |
||||
return seenMessages.contains(uniqueID); |
||||
} |
||||
} |
@ -0,0 +1,59 @@ |
||||
/* |
||||
* Copyright 2019 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
*/ |
||||
package tech.pegasys.pantheon.consensus.ibft; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; |
||||
import tech.pegasys.pantheon.util.bytes.BytesValue; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
public class MessageTrackerTest { |
||||
private final MessageTracker messageTracker = new MessageTracker(5); |
||||
|
||||
@Test |
||||
public void duplicateMessagesAreConsideredSeen() { |
||||
final MessageData arbitraryMessage_1 = |
||||
createAnonymousMessageData(BytesValue.wrap(new byte[4]), 1); |
||||
|
||||
final MessageData arbitraryMessage_2 = |
||||
createAnonymousMessageData(BytesValue.wrap(new byte[4]), 1); |
||||
|
||||
assertThat(messageTracker.hasSeenMessage(arbitraryMessage_1)).isFalse(); |
||||
assertThat(messageTracker.hasSeenMessage(arbitraryMessage_2)).isFalse(); |
||||
|
||||
messageTracker.addSeenMessage(arbitraryMessage_1); |
||||
assertThat(messageTracker.hasSeenMessage(arbitraryMessage_2)).isTrue(); |
||||
} |
||||
|
||||
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…
Reference in new issue