Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
add accurate interface parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
smatthewenglish committed Nov 30, 2018
1 parent 06748c1 commit b4f04fe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void kickstartBootstrapPeers(final List<Peer> bootstrapPeers) {
BytesValue peerId = bootstrapPeer.getId();
outstandingRequestList.add(new OutstandingRequest(bootstrapPeer));
contactedInCurrentExecution.add(peerId);
bondingAgent.performBonding(bootstrapPeer);
neighborFinder.issueFindNodeRequest(bootstrapPeer);
bondingAgent.performBonding(bootstrapPeer, true);
neighborFinder.issueFindNodeRequest(bootstrapPeer, target);
}
}

Expand Down Expand Up @@ -86,7 +86,7 @@ private void executeFindNodeRequest(final DiscoveryPeer peer) {
BytesValue peerId = peer.getId();
outstandingRequestList.add(new OutstandingRequest(peer));
contactedInCurrentExecution.add(peerId);
neighborFinder.issueFindNodeRequest(peer);
neighborFinder.issueFindNodeRequest(peer, target);
}

/**
Expand All @@ -106,7 +106,7 @@ void onNeighboursPacketReceived(final NeighborsPacketData neighboursPacket, fina
List<DiscoveryPeer> receivedPeerList = neighboursPacket.getNodes();
for (DiscoveryPeer receivedPeer : receivedPeerList) {
if (!peerBlacklist.contains(receivedPeer)) {
bondingAgent.performBonding(receivedPeer);
bondingAgent.performBonding(receivedPeer, false);
anteList.add(new PeerDistance(receivedPeer, distance(target, receivedPeer.getId())));
}
}
Expand Down Expand Up @@ -202,19 +202,21 @@ public String toString() {

public interface NeighborFinder {
/**
* Wait for the peer to complete bonding before issuance of FindNode request.
* Sends a FIND_NEIGHBORS message to a {@link DiscoveryPeer}, in search of a target value.
*
* @param peer
* @param peer the peer to interrogate
* @param target the target node ID to find
*/
void issueFindNodeRequest(final Peer peer);
void issueFindNodeRequest(final Peer peer, final BytesValue target);
}

public interface BondingAgent {
/**
* If peer is not previously known, initiate bonding process.
* Initiates a bonding PING-PONG cycle with a peer.
*
* @param peer
* @param peer The targeted peer.
* @param bootstrap Whether this is a bootstrap interaction.
*/
void performBonding(final Peer peer);
void performBonding(final Peer peer, final boolean bootstrap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class RecursivePeerRefreshStateTest {

private RecursivePeerRefreshState recursivePeerRefreshState;

private final BytesValue target =
BytesValue.fromHexString(
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");

private final RecursivePeerRefreshState.BondingAgent bondingAgent =
mock(RecursivePeerRefreshState.BondingAgent.class);
private final RecursivePeerRefreshState.NeighborFinder neighborFinder =
Expand Down Expand Up @@ -77,9 +81,6 @@ public class RecursivePeerRefreshStateTest {
public void setup() throws Exception {
JsonNode peers =
MAPPER.readTree(RecursivePeerRefreshStateTest.class.getResource("/peers.json"));
BytesValue target =
BytesValue.fromHexString(
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
recursivePeerRefreshState =
new RecursivePeerRefreshState(target, new PeerBlacklist(), bondingAgent, neighborFinder);

Expand Down Expand Up @@ -182,80 +183,80 @@ private boolean matchPeerToCorrespondingPacketData(
public void shouldIssueRequestToPeerWithLesserDistanceGreaterHops() {
recursivePeerRefreshState.kickstartBootstrapPeers(Collections.singletonList(peer_000));

verify(bondingAgent).performBonding(peer_000);
verify(neighborFinder).issueFindNodeRequest(peer_000);
verify(bondingAgent).performBonding(peer_000, true);
verify(neighborFinder).issueFindNodeRequest(peer_000, target);

recursivePeerRefreshState.onNeighboursPacketReceived(neighborsPacketData_000, peer_000);

assertThat(recursivePeerRefreshState.getOutstandingRequestList().size()).isLessThanOrEqualTo(3);

verify(bondingAgent).performBonding(peer_010);
verify(bondingAgent).performBonding(peer_011);
verify(bondingAgent).performBonding(peer_012);
verify(bondingAgent).performBonding(peer_013);
verify(bondingAgent).performBonding(peer_010, false);
verify(bondingAgent).performBonding(peer_011, false);
verify(bondingAgent).performBonding(peer_012, false);
verify(bondingAgent).performBonding(peer_013, false);

verify(neighborFinder, never()).issueFindNodeRequest(peer_010);
verify(neighborFinder).issueFindNodeRequest(peer_011);
verify(neighborFinder).issueFindNodeRequest(peer_012);
verify(neighborFinder).issueFindNodeRequest(peer_013);
verify(neighborFinder, never()).issueFindNodeRequest(peer_010, target);
verify(neighborFinder).issueFindNodeRequest(peer_011, target);
verify(neighborFinder).issueFindNodeRequest(peer_012, target);
verify(neighborFinder).issueFindNodeRequest(peer_013, target);

recursivePeerRefreshState.onNeighboursPacketReceived(neighborsPacketData_011, peer_011);

assertThat(recursivePeerRefreshState.getOutstandingRequestList().size()).isLessThanOrEqualTo(3);

verify(bondingAgent).performBonding(peer_120);
verify(bondingAgent).performBonding(peer_121);
verify(bondingAgent).performBonding(peer_122);
verify(bondingAgent).performBonding(peer_123);
verify(bondingAgent).performBonding(peer_120, false);
verify(bondingAgent).performBonding(peer_121, false);
verify(bondingAgent).performBonding(peer_122, false);
verify(bondingAgent).performBonding(peer_123, false);

recursivePeerRefreshState.onNeighboursPacketReceived(neighborsPacketData_012, peer_012);

assertThat(recursivePeerRefreshState.getOutstandingRequestList().size()).isLessThanOrEqualTo(3);

verify(bondingAgent).performBonding(peer_220);
verify(bondingAgent).performBonding(peer_221);
verify(bondingAgent).performBonding(peer_222);
verify(bondingAgent).performBonding(peer_223);
verify(bondingAgent).performBonding(peer_220, false);
verify(bondingAgent).performBonding(peer_221, false);
verify(bondingAgent).performBonding(peer_222, false);
verify(bondingAgent).performBonding(peer_223, false);

recursivePeerRefreshState.onNeighboursPacketReceived(neighborsPacketData_013, peer_013);

assertThat(recursivePeerRefreshState.getOutstandingRequestList().size()).isLessThanOrEqualTo(3);

verify(bondingAgent).performBonding(peer_320);
verify(bondingAgent).performBonding(peer_321);
verify(bondingAgent).performBonding(peer_322);
verify(bondingAgent).performBonding(peer_323);
verify(bondingAgent).performBonding(peer_320, false);
verify(bondingAgent).performBonding(peer_321, false);
verify(bondingAgent).performBonding(peer_322, false);
verify(bondingAgent).performBonding(peer_323, false);

verify(neighborFinder, never()).issueFindNodeRequest(peer_320);
verify(neighborFinder, never()).issueFindNodeRequest(peer_321);
verify(neighborFinder, never()).issueFindNodeRequest(peer_322);
verify(neighborFinder).issueFindNodeRequest(peer_323);
verify(neighborFinder, never()).issueFindNodeRequest(peer_320, target);
verify(neighborFinder, never()).issueFindNodeRequest(peer_321, target);
verify(neighborFinder, never()).issueFindNodeRequest(peer_322, target);
verify(neighborFinder).issueFindNodeRequest(peer_323, target);
}

@Test
public void shouldIssueRequestToPeerWithGreaterDistanceOnExpirationOfLowerDistancePeerRequest() {
recursivePeerRefreshState.kickstartBootstrapPeers(Collections.singletonList(peer_000));
recursivePeerRefreshState.executeTimeoutEvaluation();

verify(bondingAgent).performBonding(peer_000);
verify(neighborFinder).issueFindNodeRequest(peer_000);
verify(bondingAgent).performBonding(peer_000, true);
verify(neighborFinder).issueFindNodeRequest(peer_000, target);

recursivePeerRefreshState.onNeighboursPacketReceived(neighborsPacketData_000, peer_000);

assertThat(recursivePeerRefreshState.getOutstandingRequestList().size()).isLessThanOrEqualTo(3);

recursivePeerRefreshState.executeTimeoutEvaluation();

verify(neighborFinder, never()).issueFindNodeRequest(peer_010);
verify(neighborFinder).issueFindNodeRequest(peer_011);
verify(neighborFinder).issueFindNodeRequest(peer_012);
verify(neighborFinder).issueFindNodeRequest(peer_013);
verify(neighborFinder, never()).issueFindNodeRequest(peer_010, target);
verify(neighborFinder).issueFindNodeRequest(peer_011, target);
verify(neighborFinder).issueFindNodeRequest(peer_012, target);
verify(neighborFinder).issueFindNodeRequest(peer_013, target);

recursivePeerRefreshState.executeTimeoutEvaluation();

assertThat(recursivePeerRefreshState.getOutstandingRequestList().size()).isLessThanOrEqualTo(3);

verify(neighborFinder).issueFindNodeRequest(peer_010);
verify(neighborFinder).issueFindNodeRequest(peer_010, target);
}

private DiscoveryPeer generatePeer(final JsonNode peer) {
Expand Down

0 comments on commit b4f04fe

Please sign in to comment.