@ -25,6 +25,7 @@ import org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer;
import org.hyperledger.besu.ethereum.p2p.discovery.PeerBondedObserver ;
import org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryEvent ;
import org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryStatus ;
import org.hyperledger.besu.ethereum.p2p.peers.MaintainedPeers ;
import org.hyperledger.besu.ethereum.p2p.peers.Peer ;
import org.hyperledger.besu.ethereum.p2p.peers.PeerId ;
import org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions ;
@ -158,7 +159,8 @@ public class PeerDiscoveryController {
final PeerRequirement peerRequirement ,
final PeerPermissions peerPermissions ,
final Subscribers < PeerBondedObserver > peerBondedObservers ,
final MetricsSystem metricsSystem ) {
final MetricsSystem metricsSystem ,
final MaintainedPeers maintainedPeers ) {
this . timerUtil = timerUtil ;
this . nodeKey = nodeKey ;
this . localPeer = localPeer ;
@ -174,6 +176,10 @@ public class PeerDiscoveryController {
this . peerPermissions = new PeerDiscoveryPermissions ( localPeer , peerPermissions ) ;
// Listening to the added and removed peer event to update the maintainted peers
maintainedPeers . subscribeAdd ( this : : onPeerAdded ) ;
maintainedPeers . subscribeRemove ( this : : onPeerRemoved ) ;
metricsSystem . createIntegerGauge (
BesuMetricCategory . NETWORK ,
"discovery_inflight_interactions_current" ,
@ -195,6 +201,18 @@ public class PeerDiscoveryController {
"type" ) ;
}
private void onPeerAdded ( final Peer peer , final boolean wasAdded ) {
if ( wasAdded ) {
addToPeerTable ( DiscoveryPeer . fromEnode ( peer . getEnodeURL ( ) ) ) ;
}
}
private void onPeerRemoved ( final Peer peer , final boolean wasRemoved ) {
if ( wasRemoved ) {
peerTable . tryEvict ( peer ) ;
}
}
public static Builder builder ( ) {
return new Builder ( ) ;
}
@ -694,6 +712,7 @@ public class PeerDiscoveryController {
private final List < DiscoveryPeer > bootstrapNodes = new ArrayList < > ( ) ;
private PeerTable peerTable ;
private Subscribers < PeerBondedObserver > peerBondedObservers = Subscribers . create ( ) ;
private MaintainedPeers maintainedPeers = new MaintainedPeers ( ) ;
// Required dependencies
private NodeKey nodeKey ;
@ -724,7 +743,8 @@ public class PeerDiscoveryController {
peerRequirement ,
peerPermissions ,
peerBondedObservers ,
metricsSystem ) ;
metricsSystem ,
maintainedPeers ) ;
}
private void validate ( ) {
@ -816,5 +836,11 @@ public class PeerDiscoveryController {
this . metricsSystem = metricsSystem ;
return this ;
}
public Builder maintainedPeers ( final MaintainedPeers maintainedPeers ) {
checkNotNull ( maintainedPeers ) ;
this . maintainedPeers = maintainedPeers ;
return this ;
}
}
}