Skip to content

Commit

Permalink
Make BFT validator peers sub-protocol configurable (hyperledger#1806)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Frame <jasonwframe@gmail.com>
  • Loading branch information
jframe authored and eum602 committed Nov 3, 2023
1 parent 00dcaab commit 10f5624
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected MiningCoordinator createMiningCoordinator(
final ProposerSelector proposerSelector =
new ProposerSelector(blockchain, blockInterface, true, voteTallyCache);

peers = new ValidatorPeers(voteTallyCache);
peers = new ValidatorPeers(voteTallyCache, IbftSubProtocol.NAME);

final UniqueMessageMulticaster uniqueMessageMulticaster =
new UniqueMessageMulticaster(peers, bftConfig.getGossipedHistoryLimit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public class ValidatorPeers implements ValidatorMulticaster, PeerConnectionTrack

private static final Logger LOG = LogManager.getLogger();

private static final String PROTOCOL_NAME = "IBF";

// It's possible for multiple connections between peers to exist for brief periods, so map each
// address to a set of connections
private final Map<Address, Set<PeerConnection>> connectionsByAddress = new ConcurrentHashMap<>();
private final VoteTallyCache voteTallyCache;
private final String protocolName;

public ValidatorPeers(final VoteTallyCache voteTallyCache) {
public ValidatorPeers(final VoteTallyCache voteTallyCache, final String protocolName) {
this.voteTallyCache = voteTallyCache;
this.protocolName = protocolName;
}

@Override
Expand Down Expand Up @@ -94,7 +94,7 @@ private void sendMessageToSpecificAddresses(
.forEach(
connection -> {
try {
connection.sendForProtocol(PROTOCOL_NAME, message);
connection.sendForProtocol(protocolName, message);
} catch (final PeerNotConnected peerNotConnected) {
LOG.trace(
"Lost connection to a validator. remoteAddress={} peerInfo={}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
@RunWith(MockitoJUnitRunner.class)
public class ValidatorPeersTest {

public static final String PROTOCOL_NAME = "BFT";
private final List<Address> validators = newArrayList();
private final List<PublicKey> publicKeys = newArrayList();

Expand Down Expand Up @@ -81,15 +82,15 @@ public void onlyValidatorsAreSentAMessage() throws PeerNotConnected {
// Only add the first Peer's address to the validators.
validators.add(Util.publicKeyToAddress(publicKeys.get(0)));

final ValidatorPeers peers = new ValidatorPeers(voteTallyCache);
final ValidatorPeers peers = new ValidatorPeers(voteTallyCache, PROTOCOL_NAME);
for (final PeerConnection peer : peerConnections) {
peers.add(peer);
}

final MessageData messageToSend = new RawMessage(1, Bytes.EMPTY);
peers.send(messageToSend);

verify(peerConnections.get(0), times(1)).sendForProtocol("IBF", messageToSend);
verify(peerConnections.get(0), times(1)).sendForProtocol(PROTOCOL_NAME, messageToSend);
verify(peerConnections.get(1), never()).sendForProtocol(any(), any());
verify(peerConnections.get(2), never()).sendForProtocol(any(), any());
verify(peerConnections.get(3), never()).sendForProtocol(any(), any());
Expand All @@ -101,7 +102,7 @@ public void handlesDuplicateConnection() throws PeerNotConnected {
validators.add(peer0Address);
final PeerConnection duplicatePeer = mockPeerConnection(peer0Address);

final ValidatorPeers peers = new ValidatorPeers(voteTallyCache);
final ValidatorPeers peers = new ValidatorPeers(voteTallyCache, PROTOCOL_NAME);
for (final PeerConnection peer : peerConnections) {
peers.add(peer);
}
Expand All @@ -110,8 +111,8 @@ public void handlesDuplicateConnection() throws PeerNotConnected {
final MessageData messageToSend = new RawMessage(1, Bytes.EMPTY);
peers.send(messageToSend);

verify(peerConnections.get(0), times(1)).sendForProtocol("IBF", messageToSend);
verify(duplicatePeer, times(1)).sendForProtocol("IBF", messageToSend);
verify(peerConnections.get(0), times(1)).sendForProtocol(PROTOCOL_NAME, messageToSend);
verify(duplicatePeer, times(1)).sendForProtocol(PROTOCOL_NAME, messageToSend);
verify(peerConnections.get(1), never()).sendForProtocol(any(), any());
verify(peerConnections.get(2), never()).sendForProtocol(any(), any());
verify(peerConnections.get(3), never()).sendForProtocol(any(), any());
Expand All @@ -123,7 +124,7 @@ public void handlesTransientDuplicateConnection() throws PeerNotConnected {
validators.add(peer0Address);
final PeerConnection duplicatePeer = mockPeerConnection(peer0Address);

final ValidatorPeers peers = new ValidatorPeers(voteTallyCache);
final ValidatorPeers peers = new ValidatorPeers(voteTallyCache, PROTOCOL_NAME);
for (final PeerConnection peer : peerConnections) {
peers.add(peer);
}
Expand All @@ -133,7 +134,7 @@ public void handlesTransientDuplicateConnection() throws PeerNotConnected {
final MessageData messageToSend = new RawMessage(1, Bytes.EMPTY);
peers.send(messageToSend);

verify(peerConnections.get(0), times(1)).sendForProtocol("IBF", messageToSend);
verify(peerConnections.get(0), times(1)).sendForProtocol(PROTOCOL_NAME, messageToSend);
verify(duplicatePeer, never()).sendForProtocol("IBF", messageToSend);
verify(peerConnections.get(1), never()).sendForProtocol(any(), any());
verify(peerConnections.get(2), never()).sendForProtocol(any(), any());
Expand All @@ -144,7 +145,7 @@ public void handlesTransientDuplicateConnection() throws PeerNotConnected {
public void doesntSendToValidatorsWhichAreNotDirectlyConnected() throws PeerNotConnected {
validators.add(Util.publicKeyToAddress(publicKeys.get(0)));

final ValidatorPeers peers = new ValidatorPeers(voteTallyCache);
final ValidatorPeers peers = new ValidatorPeers(voteTallyCache, PROTOCOL_NAME);

// only add peer connections 1, 2 & 3, none of which should be invoked.
newArrayList(1, 2, 3).forEach(i -> peers.add(peerConnections.get(i)));
Expand All @@ -165,7 +166,7 @@ public void onlyValidatorsAreSentAMessageNotInExcludes() throws PeerNotConnected
validators.add(validatorAddress);
validators.add(Util.publicKeyToAddress(publicKeys.get(1)));

final ValidatorPeers peers = new ValidatorPeers(voteTallyCache);
final ValidatorPeers peers = new ValidatorPeers(voteTallyCache, PROTOCOL_NAME);
for (final PeerConnection peer : peerConnections) {
peers.add(peer);
}
Expand Down

0 comments on commit 10f5624

Please sign in to comment.