mirror of https://github.com/hyperledger/besu
Ibft transmitted packets are logged by gossiper (#652)
Messages which originate with the current node are logged in the gossiper such that if a remote peer sends a packet which originated from the local back to the local node, it should not go back out again. Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>pull/2/head
parent
73f4299b8c
commit
598fd59f21
@ -0,0 +1,20 @@ |
|||||||
|
/* |
||||||
|
* 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 tech.pegasys.pantheon.ethereum.p2p.api.Message; |
||||||
|
|
||||||
|
public interface Gossiper { |
||||||
|
|
||||||
|
void send(Message message); |
||||||
|
} |
@ -0,0 +1,68 @@ |
|||||||
|
/* |
||||||
|
* 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 tech.pegasys.pantheon.consensus.ibft.network.ValidatorMulticaster; |
||||||
|
import tech.pegasys.pantheon.ethereum.core.Address; |
||||||
|
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; |
||||||
|
|
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Collections; |
||||||
|
import java.util.LinkedHashMap; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
public class UniqueMessageMulticaster implements ValidatorMulticaster { |
||||||
|
|
||||||
|
private final int maxSeenMessages; |
||||||
|
private final ValidatorMulticaster multicaster; |
||||||
|
|
||||||
|
UniqueMessageMulticaster(final ValidatorMulticaster multicaster, final int maxSeenMessages) { |
||||||
|
this.maxSeenMessages = maxSeenMessages; |
||||||
|
this.multicaster = multicaster; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructor that attaches gossip logic to a set of multicaster |
||||||
|
* |
||||||
|
* @param multicaster Network connections to the remote validators |
||||||
|
*/ |
||||||
|
public UniqueMessageMulticaster(final ValidatorMulticaster multicaster) { |
||||||
|
this(multicaster, 10_000); |
||||||
|
} |
||||||
|
|
||||||
|
// Set that starts evicting members when it hits capacity
|
||||||
|
private final Set<Integer> seenMessages = |
||||||
|
Collections.newSetFromMap( |
||||||
|
new LinkedHashMap<Integer, Boolean>() { |
||||||
|
@Override |
||||||
|
protected boolean removeEldestEntry(final Map.Entry<Integer, Boolean> eldest) { |
||||||
|
return size() > maxSeenMessages; |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
@Override |
||||||
|
public void send(final MessageData message) { |
||||||
|
send(message, Collections.emptyList()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void send(final MessageData message, final Collection<Address> blackList) { |
||||||
|
final int uniqueID = message.hashCode(); |
||||||
|
if (seenMessages.contains(uniqueID)) { |
||||||
|
return; |
||||||
|
} |
||||||
|
multicaster.send(message, blackList); |
||||||
|
seenMessages.add(uniqueID); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,93 @@ |
|||||||
|
/* |
||||||
|
* 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.emptyList; |
||||||
|
import static org.mockito.Mockito.mock; |
||||||
|
import static org.mockito.Mockito.reset; |
||||||
|
import static org.mockito.Mockito.times; |
||||||
|
import static org.mockito.Mockito.verify; |
||||||
|
import static org.mockito.Mockito.verifyZeroInteractions; |
||||||
|
|
||||||
|
import tech.pegasys.pantheon.consensus.ibft.network.ValidatorMulticaster; |
||||||
|
import tech.pegasys.pantheon.ethereum.core.Address; |
||||||
|
import tech.pegasys.pantheon.ethereum.core.AddressHelpers; |
||||||
|
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; |
||||||
|
import tech.pegasys.pantheon.ethereum.p2p.wire.RawMessage; |
||||||
|
import tech.pegasys.pantheon.util.bytes.BytesValue; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import com.google.common.collect.Lists; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.junit.MockitoJUnitRunner; |
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class) |
||||||
|
public class UniqueMessageMulticasterTest { |
||||||
|
|
||||||
|
private final ValidatorMulticaster multicaster = mock(ValidatorMulticaster.class); |
||||||
|
private final UniqueMessageMulticaster messageTracker = |
||||||
|
new UniqueMessageMulticaster(multicaster, 5); |
||||||
|
private final RawMessage messageSent = new RawMessage(5, BytesValue.wrap(new byte[5])); |
||||||
|
|
||||||
|
@Test |
||||||
|
public void previouslySentMessageIsNotSentAgain() { |
||||||
|
|
||||||
|
messageTracker.send(messageSent); |
||||||
|
verify(multicaster, times(1)).send(messageSent, emptyList()); |
||||||
|
reset(multicaster); |
||||||
|
|
||||||
|
messageTracker.send(messageSent); |
||||||
|
messageTracker.send(messageSent, emptyList()); |
||||||
|
verifyZeroInteractions(multicaster); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void messagesSentWithABlackListAreNotRetransmitted() { |
||||||
|
messageTracker.send(messageSent, emptyList()); |
||||||
|
verify(multicaster, times(1)).send(messageSent, emptyList()); |
||||||
|
reset(multicaster); |
||||||
|
|
||||||
|
messageTracker.send(messageSent, emptyList()); |
||||||
|
messageTracker.send(messageSent); |
||||||
|
verifyZeroInteractions(multicaster); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void oldMessagesAreEvictedWhenFullAndCanThenBeRetransmitted() { |
||||||
|
final List<MessageData> messagesSent = Lists.newArrayList(); |
||||||
|
|
||||||
|
for (int i = 0; i < 6; i++) { |
||||||
|
final RawMessage msg = new RawMessage(i, BytesValue.wrap(new byte[i])); |
||||||
|
messagesSent.add(msg); |
||||||
|
messageTracker.send(msg); |
||||||
|
verify(multicaster, times(1)).send(msg, emptyList()); |
||||||
|
} |
||||||
|
reset(multicaster); |
||||||
|
|
||||||
|
messageTracker.send(messagesSent.get(5)); |
||||||
|
verifyZeroInteractions(multicaster); |
||||||
|
|
||||||
|
messageTracker.send(messagesSent.get(0)); |
||||||
|
verify(multicaster, times(1)).send(messagesSent.get(0), emptyList()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void passedInBlackListIsPassedToUnderlyingValidator() { |
||||||
|
List<Address> blackList = |
||||||
|
Lists.newArrayList(AddressHelpers.ofValue(0), AddressHelpers.ofValue(1)); |
||||||
|
messageTracker.send(messageSent, blackList); |
||||||
|
verify(multicaster, times(1)).send(messageSent, blackList); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue