This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent duplicate ibft messages being processed by state machine (#811)
- Loading branch information
Showing
10 changed files
with
204 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
consensus/ibft/src/main/java/tech/pegasys/pantheon/consensus/ibft/MessageTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/MessageTrackerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.