Skip to content

Commit

Permalink
12984 use peers for Network component
Browse files Browse the repository at this point in the history
Signed-off-by: Kore Aguda <kore@swirldslabs.com>
  • Loading branch information
kfa-aguda committed Apr 30, 2024
1 parent 74d668d commit dd516d0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ protected SyncGossip(
final BasicConfig basicConfig = platformContext.getConfiguration().getConfigData(BasicConfig.class);
final List<PeerInfo> peers = Utilities.createPeerInfoList(addressBook, selfId);

topology =
new StaticTopology(random, peers, addressBook.getIndexOfNodeId(selfId), basicConfig.numConnections());
topology = new StaticTopology(random, peers, selfId, basicConfig.numConnections());
final NetworkPeerIdentifier peerIdentifier = new NetworkPeerIdentifier(platformContext, peers);
final SocketFactory socketFactory =
NetworkUtils.createSocketFactory(selfId, peers, keysAndCerts, platformContext.getConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,31 @@ public class StaticTopology implements NetworkTopology {
private static final long SEED = 0;

/** nodes are mapped so lookups are efficient. **/
private Map<NodeId, Integer> peerNodeToIdMap = new HashMap<>();
private Map<NodeId, Long> peerNodeToIdMap = new HashMap<>();

/**
* Two nodes are neighbors if their node indexes are neighbors in the connection graph.
*/
private final RandomGraph connectionGraph;

private final int selfIndex;
private final NodeId selfId;

/**
* Constructor.
*
* @param random a source of randomness, used to chose random neighbors, does not need to be
* cryptographically secure
* @param peers the set of peers in the network
* @param selfIndex the index of this node in the address book
* @param selfId the ID of this node
* @param numberOfNeighbors the number of neighbors each node should have
*/
public StaticTopology(
@NonNull final Random random,
@NonNull final List<PeerInfo> peers,
final int selfIndex,
@NonNull final NodeId selfId,
final int numberOfNeighbors) {
this.peerNodeToIdMap = map(peers);
this.selfIndex = selfIndex;
this.selfId = selfId;
this.connectionGraph = new RandomGraph(random, peers.size() + 1, numberOfNeighbors, SEED);
}

Expand All @@ -74,8 +74,7 @@ public Set<NodeId> getNeighbors() {
*/
@Override
public boolean shouldConnectToMe(final NodeId nodeId) {
final int nodeIndex = getIndexOfNodeId(nodeId);
return isNeighbor(nodeId) && nodeIndex < selfIndex;
return isNeighbor(nodeId) && nodeId.id() < selfId.id();
}

/**
Expand All @@ -93,8 +92,7 @@ private boolean isNeighbor(final NodeId nodeId) {
*/
@Override
public boolean shouldConnectTo(final NodeId nodeId) {
final int nodeIndex = getIndexOfNodeId(nodeId);
return isNeighbor(nodeId) && nodeIndex > selfIndex;
return isNeighbor(nodeId) && nodeId.id() > selfId.id();
}

/**
Expand All @@ -105,33 +103,17 @@ public RandomGraph getConnectionGraph() {
return connectionGraph;
}

/**
* Returns the index of the given node, which must not be the self index
* or -1 if the node is not in the peer list
*
* @param nodeId the node ID
* @return the index of the node in the peer list
*/
private int getIndexOfNodeId(@NonNull final NodeId nodeId) {
final Integer index = peerNodeToIdMap.get(nodeId);
if (index == null) {
return -1;
}
return selfIndex == index ? index + 1 : index;
}

/**
* Maps the list of peers to a map of node IDs to their index in the peer list
* and populates the peerNodesList with the node IDs
*
* @param peers the list of peers
* @return the map of node IDs to their index in the peer list
* @return the map of node IDs to their peer Id
*/
@NonNull
private Map<NodeId, Integer> map(@NonNull final List<PeerInfo> peers) {
for (int i = 0; i < peers.size(); i++) {
final PeerInfo peer = peers.get(i);
peerNodeToIdMap.put(peer.nodeId(), i);
private Map<NodeId, Long> map(@NonNull final List<PeerInfo> peers) {
for (final PeerInfo peer : peers) {
peerNodeToIdMap.put(peer.nodeId(), peer.nodeId().id());
}
return peerNodeToIdMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ void testShouldConnectToMe(final int numNodes, final int numNeighbors) throws Ex
final NodeId selfId = addressBook.getNodeId(r.nextInt(numNodes));

final List<PeerInfo> peers = Utilities.createPeerInfoList(addressBook, selfId);
final NetworkTopology topology =
new StaticTopology(r, peers, addressBook.getIndexOfNodeId(selfId), numNeighbors);
final NetworkTopology topology = new StaticTopology(r, peers, selfId, numNeighbors);

final StaticConnectionManagers managers = new StaticConnectionManagers(topology, connectionCreator);
final List<NodeId> neighbors = topology.getNeighbors().stream().toList();
Expand Down Expand Up @@ -98,8 +97,7 @@ void testShouldConnectTo(final int numNodes, final int numNeighbors) throws Exce
new RandomAddressBookGenerator(r).setSize(numNodes).build();
final NodeId selfId = addressBook.getNodeId(r.nextInt(numNodes));
final List<PeerInfo> peers = Utilities.createPeerInfoList(addressBook, selfId);
final NetworkTopology topology =
new StaticTopology(r, peers, addressBook.getIndexOfNodeId(selfId), numNeighbors);
final NetworkTopology topology = new StaticTopology(r, peers, selfId, numNeighbors);

final StaticConnectionManagers managers = new StaticConnectionManagers(topology, connectionCreator);
final List<NodeId> neighbors = topology.getNeighbors().stream().toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ void testFullyConnectedTopology(final int numNodes, final int numNeighbors, fina

final List<PeerInfo> peers = Utilities.createPeerInfoList(addressBook, thisNodeId);

final NetworkTopology topology =
new StaticTopology(random, peers, addressBook.getIndexOfNodeId(thisNodeId), numNeighbors);
final NetworkTopology topology = new StaticTopology(random, peers, thisNodeId, numNeighbors);
final Set<NodeId> neighbors = topology.getNeighbors();
final Set<NodeId> expected = IntStream.range(0, numNodes)
.mapToObj(addressBook::getNodeId)
Expand Down

0 comments on commit dd516d0

Please sign in to comment.