use of tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer in project teku by ConsenSys.
the class Eth2PeerSelectionStrategy method selectAndRemoveRandomPeers.
private List<PeerAddress> selectAndRemoveRandomPeers(final P2PNetwork<?> network, final PeerPools peerPools, final int randomlySelectedPeersToAdd, final List<DiscoveryPeer> allCandidatePeers) {
final List<PeerAddress> selectedPeers = new ArrayList<>();
shuffler.shuffle(allCandidatePeers);
while (!allCandidatePeers.isEmpty() && selectedPeers.size() < randomlySelectedPeersToAdd) {
final DiscoveryPeer candidate = allCandidatePeers.remove(0);
checkCandidate(candidate, network).ifPresent(peerAddress -> {
peerPools.addPeerToPool(peerAddress.getId(), RANDOMLY_SELECTED);
selectedPeers.add(peerAddress);
});
}
return selectedPeers;
}
use of tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer in project teku by ConsenSys.
the class Eth2PeerSelectionStrategy method selectPeersToConnect.
@Override
public List<PeerAddress> selectPeersToConnect(final P2PNetwork<?> network, final PeerPools peerPools, final Supplier<? extends Collection<DiscoveryPeer>> candidates) {
final PeerSubnetSubscriptions peerSubnetSubscriptions = peerSubnetSubscriptionsFactory.create(network);
final int peersRequiredForPeerCount = targetPeerCountRange.getPeersToAdd(network.getPeerCount());
final int randomlySelectedPeerCount = getCurrentRandomlySelectedPeerCount(network, peerPools);
final int randomlySelectedPeersToAdd = targetPeerCountRange.getRandomlySelectedPeersToAdd(randomlySelectedPeerCount);
final int peersRequiredForSubnets = peerSubnetSubscriptions.getSubscribersRequired();
final int scoreBasedPeersToAdd = Math.max(peersRequiredForPeerCount - randomlySelectedPeerCount, peersRequiredForSubnets);
final int maxPeersToAdd = scoreBasedPeersToAdd + randomlySelectedPeersToAdd;
LOG.trace("Connecting to up to {} known peers", maxPeersToAdd);
if (maxPeersToAdd == 0) {
return emptyList();
}
final List<DiscoveryPeer> allCandidatePeers = new ArrayList<>(candidates.get());
final List<PeerAddress> selectedPeers = new ArrayList<>();
if (randomlySelectedPeersToAdd > 0) {
selectedPeers.addAll(selectAndRemoveRandomPeers(network, peerPools, randomlySelectedPeersToAdd, allCandidatePeers));
}
if (scoreBasedPeersToAdd > 0) {
selectedPeers.addAll(selectPeersByScore(network, peerSubnetSubscriptions, scoreBasedPeersToAdd, allCandidatePeers));
}
// Unmodifiable to make errorprone happy
return unmodifiableList(selectedPeers);
}
use of tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer in project teku by ConsenSys.
the class Eth2PeerSelectionStrategyTest method setUp.
@BeforeEach
void setUp() {
when(peerSubnetSubscriptions.createScorer()).thenReturn(peerScorer);
when(reputationManager.isConnectionInitiationAllowed(any())).thenReturn(true);
when(network.createPeerAddress(any(DiscoveryPeer.class))).thenAnswer(invocation -> {
final DiscoveryPeer peer = invocation.getArgument(0);
return new PeerAddress(new MockNodeId(peer.getPublicKey()));
});
}
use of tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer in project teku by ConsenSys.
the class NodeRecordConverterTest method shouldConvertAttnets.
@Test
public void shouldConvertAttnets() {
SszBitvector persistentSubnets = ATT_SUBNET_SCHEMA.ofBits(1, 8, 14, 32);
Bytes encodedPersistentSubnets = persistentSubnets.sszSerialize();
final Optional<DiscoveryPeer> result = convertNodeRecordWithFields(new EnrField(EnrField.IP_V6, IPV6_LOCALHOST), new EnrField(EnrField.TCP_V6, 1234), new EnrField(ATTESTATION_SUBNET_ENR_FIELD, encodedPersistentSubnets));
assertThat(result).contains(new DiscoveryPeer(PUB_KEY, new InetSocketAddress("::1", 1234), ENR_FORK_ID, persistentSubnets, SYNCNETS));
}
use of tech.pegasys.teku.networking.p2p.discovery.DiscoveryPeer in project teku by ConsenSys.
the class NodeRecordConverterTest method shouldConvertIpV6Record.
@Test
public void shouldConvertIpV6Record() {
final Optional<DiscoveryPeer> result = convertNodeRecordWithFields(new EnrField(EnrField.IP_V6, IPV6_LOCALHOST), new EnrField(EnrField.TCP_V6, 1234));
assertThat(result).contains(new DiscoveryPeer(PUB_KEY, new InetSocketAddress("::1", 1234), ENR_FORK_ID, ATTNETS, SYNCNETS));
}
Aggregations