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.
Limit size of Ibft future message buffer (#873)
- Loading branch information
Showing
8 changed files
with
416 additions
and
78 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
103 changes: 103 additions & 0 deletions
103
.../src/main/java/tech/pegasys/pantheon/consensus/ibft/statemachine/FutureMessageBuffer.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,103 @@ | ||
/* | ||
* 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.statemachine; | ||
|
||
import tech.pegasys.pantheon.ethereum.p2p.api.Message; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.NavigableMap; | ||
import java.util.TreeMap; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
/** | ||
* Buffer which holds future IBFT messages. | ||
* | ||
* <p>This buffer only allows messages to be added which have a chain height greater than current | ||
* height and up to chain futureMessagesMaxDistance from the current chain height. | ||
* | ||
* <p>When the total number of messages is greater futureMessagesLimit then messages are evicted. | ||
* | ||
* <p>If there is more than one height in the buffer then all messages for the highest chain height | ||
* are removed. Otherwise if there is only one height the oldest inserted message is removed. | ||
*/ | ||
public class FutureMessageBuffer { | ||
private final NavigableMap<Long, List<Message>> buffer = new TreeMap<>(); | ||
private final long futureMessagesMaxDistance; | ||
private final long futureMessagesLimit; | ||
private long chainHeight; | ||
|
||
public FutureMessageBuffer( | ||
final long futureMessagesMaxDistance, | ||
final long futureMessagesLimit, | ||
final long chainHeight) { | ||
this.futureMessagesMaxDistance = futureMessagesMaxDistance; | ||
this.futureMessagesLimit = futureMessagesLimit; | ||
this.chainHeight = chainHeight; | ||
} | ||
|
||
public void addMessage(final long msgChainHeight, final Message rawMsg) { | ||
if (futureMessagesLimit == 0 || !validMessageHeight(msgChainHeight, chainHeight)) { | ||
return; | ||
} | ||
|
||
addMessageToBuffer(msgChainHeight, rawMsg); | ||
|
||
if (totalMessagesSize() > futureMessagesLimit) { | ||
evictMessages(); | ||
} | ||
} | ||
|
||
private void addMessageToBuffer(final long msgChainHeight, final Message rawMsg) { | ||
buffer.putIfAbsent(msgChainHeight, new ArrayList<>()); | ||
buffer.get(msgChainHeight).add(rawMsg); | ||
} | ||
|
||
private boolean validMessageHeight(final long msgChainHeight, final long currentHeight) { | ||
final boolean isFutureMsg = msgChainHeight > currentHeight; | ||
final boolean withinMaxChainHeight = | ||
msgChainHeight <= currentHeight + futureMessagesMaxDistance; | ||
return isFutureMsg && withinMaxChainHeight; | ||
} | ||
|
||
private void evictMessages() { | ||
if (buffer.size() > 1) { | ||
buffer.remove(buffer.lastKey()); | ||
} else if (buffer.size() == 1) { | ||
List<Message> messages = buffer.firstEntry().getValue(); | ||
messages.remove(0); | ||
} | ||
} | ||
|
||
public List<Message> retrieveMessagesForHeight(final long height) { | ||
chainHeight = height; | ||
final List<Message> messages = buffer.getOrDefault(height, Collections.emptyList()); | ||
discardPreviousHeightMessages(); | ||
return messages; | ||
} | ||
|
||
private void discardPreviousHeightMessages() { | ||
if (!buffer.isEmpty()) { | ||
for (long h = buffer.firstKey(); h <= chainHeight; h++) { | ||
buffer.remove(h); | ||
} | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
long totalMessagesSize() { | ||
return buffer.values().stream().map(List::size).reduce(0, Integer::sum).longValue(); | ||
} | ||
} |
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
Oops, something went wrong.