7311: Refactor PeerManager to be an interface

Signed-off-by: Matilda Clerke <matilda.clerke@consensys.net>
pull/7628/head
Matilda Clerke 2 months ago
parent 08c66fd916
commit 049cae271c
  1. 64
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/DefaultPeerManager.java
  2. 51
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerManager.java
  3. 6
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/peertask/DefaultPeerManagerTest.java

@ -0,0 +1,64 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.eth.manager.peertask;
import org.hyperledger.besu.ethereum.eth.manager.EthPeer;
import org.hyperledger.besu.ethereum.p2p.peers.PeerId;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This is a simple PeerManager implementation that can be used the default implementation in most
* situations
*/
public class DefaultPeerManager implements PeerManager {
private static final Logger LOG = LoggerFactory.getLogger(DefaultPeerManager.class);
// use a synchronized map to ensure the map is never modified by multiple threads at once
private final Map<PeerId, EthPeer> ethPeersByPeerId =
Collections.synchronizedMap(new HashMap<>());
@Override
public EthPeer getPeer(final Predicate<EthPeer> filter) throws NoAvailablePeerException {
LOG.trace("Getting peer from pool of {} peers", ethPeersByPeerId.size());
return ethPeersByPeerId.values().stream()
.filter(filter)
.max(Comparator.naturalOrder())
.orElseThrow(NoAvailablePeerException::new);
}
@Override
public Optional<EthPeer> getPeerByPeerId(final PeerId peerId) {
return Optional.ofNullable(ethPeersByPeerId.get(peerId));
}
@Override
public void addPeer(final EthPeer ethPeer) {
ethPeersByPeerId.put(ethPeer.getConnection().getPeer(), ethPeer);
}
@Override
public void removePeer(final PeerId peerId) {
ethPeersByPeerId.remove(peerId);
}
}

@ -17,23 +17,11 @@ package org.hyperledger.besu.ethereum.eth.manager.peertask;
import org.hyperledger.besu.ethereum.eth.manager.EthPeer;
import org.hyperledger.besu.ethereum.p2p.peers.PeerId;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** "Manages" the EthPeers for the PeerTaskExecutor */
public class PeerManager {
private static final Logger LOG = LoggerFactory.getLogger(PeerManager.class);
// use a synchronized map to ensure the map is never modified by multiple threads at once
private final Map<PeerId, EthPeer> ethPeersByPeerId =
Collections.synchronizedMap(new HashMap<>());
public interface PeerManager {
/**
* Gets the highest reputation peer matching the supplies filter
@ -42,23 +30,28 @@ public class PeerManager {
* @return the highest reputation peer matching the supplies filter
* @throws NoAvailablePeerException If there are no suitable peers
*/
public EthPeer getPeer(final Predicate<EthPeer> filter) throws NoAvailablePeerException {
LOG.trace("Getting peer from pool of {} peers", ethPeersByPeerId.size());
return ethPeersByPeerId.values().stream()
.filter(filter)
.max(Comparator.naturalOrder())
.orElseThrow(NoAvailablePeerException::new);
}
EthPeer getPeer(final Predicate<EthPeer> filter) throws NoAvailablePeerException;
public Optional<EthPeer> getPeerByPeerId(final PeerId peerId) {
return Optional.ofNullable(ethPeersByPeerId.get(peerId));
}
/**
* Attempts to get the EthPeer identified by peerId
*
* @param peerId the peerId of the desired EthPeer
* @return An Optional\<EthPeer\> containing the EthPeer identified by peerId if present in the
* PeerManager, or empty otherwise
*/
Optional<EthPeer> getPeerByPeerId(final PeerId peerId);
public void addPeer(final EthPeer ethPeer) {
ethPeersByPeerId.put(ethPeer.getConnection().getPeer(), ethPeer);
}
/**
* Add the supplied EthPeer to the PeerManager
*
* @param ethPeer the EthPeer to be added to the PeerManager
*/
void addPeer(final EthPeer ethPeer);
public void removePeer(final PeerId peerId) {
ethPeersByPeerId.remove(peerId);
}
/**
* Remove the EthPeer identified by peerId from the PeerManager
*
* @param peerId the PeerId of the EthPeer to be removed from the PeerManager
*/
void removePeer(final PeerId peerId);
}

@ -28,13 +28,13 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class PeerManagerTest {
public class DefaultPeerManagerTest {
public PeerManager peerManager;
public DefaultPeerManager peerManager;
@BeforeEach
public void beforeTest() {
peerManager = new PeerManager();
peerManager = new DefaultPeerManager();
}
@Test
Loading…
Cancel
Save