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

PAN-2449: Remove NodePermissioningLocalConfig external references #1406

Merged
merged 9 commits into from
May 8, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer;
import tech.pegasys.pantheon.ethereum.p2p.peers.PeerBlacklist;
import tech.pegasys.pantheon.ethereum.p2p.wire.messages.DisconnectMessage;
import tech.pegasys.pantheon.ethereum.permissioning.NodeLocalConfigPermissioningController;
import tech.pegasys.pantheon.ethereum.permissioning.node.NodePermissioningController;
import tech.pegasys.pantheon.metrics.MetricsSystem;
import tech.pegasys.pantheon.util.NetworkUtility;
Expand Down Expand Up @@ -74,7 +73,6 @@ public abstract class PeerDiscoveryAgent implements DisconnectCallback {
protected final List<DiscoveryPeer> bootstrapPeers;
private final List<PeerRequirement> peerRequirements = new CopyOnWriteArrayList<>();
private final PeerBlacklist peerBlacklist;
private final Optional<NodeLocalConfigPermissioningController> nodeWhitelistController;
private final Optional<NodePermissioningController> nodePermissioningController;
private final MetricsSystem metricsSystem;
/* The peer controller, which takes care of the state machine of peers. */
Expand All @@ -98,7 +96,6 @@ public PeerDiscoveryAgent(
final SECP256K1.KeyPair keyPair,
final DiscoveryConfiguration config,
final PeerBlacklist peerBlacklist,
final Optional<NodeLocalConfigPermissioningController> nodeWhitelistController,
final Optional<NodePermissioningController> nodePermissioningController,
final MetricsSystem metricsSystem) {
this.metricsSystem = metricsSystem;
Expand All @@ -108,7 +105,6 @@ public PeerDiscoveryAgent(
validateConfiguration(config);

this.peerBlacklist = peerBlacklist;
this.nodeWhitelistController = nodeWhitelistController;
this.nodePermissioningController = nodePermissioningController;
this.bootstrapPeers =
config.getBootstrapPeers().stream()
Expand Down Expand Up @@ -183,7 +179,6 @@ private PeerDiscoveryController createController() {
PEER_REFRESH_INTERVAL_MS,
PeerRequirement.combine(peerRequirements),
peerBlacklist,
nodeWhitelistController,
nodePermissioningController,
peerBondedObservers,
peerDroppedObservers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import tech.pegasys.pantheon.ethereum.p2p.discovery.internal.TimerUtil;
import tech.pegasys.pantheon.ethereum.p2p.discovery.internal.VertxTimerUtil;
import tech.pegasys.pantheon.ethereum.p2p.peers.PeerBlacklist;
import tech.pegasys.pantheon.ethereum.permissioning.NodeLocalConfigPermissioningController;
import tech.pegasys.pantheon.ethereum.permissioning.node.NodePermissioningController;
import tech.pegasys.pantheon.metrics.MetricCategory;
import tech.pegasys.pantheon.metrics.MetricsSystem;
Expand Down Expand Up @@ -61,16 +60,9 @@ public VertxPeerDiscoveryAgent(
final KeyPair keyPair,
final DiscoveryConfiguration config,
final PeerBlacklist peerBlacklist,
final Optional<NodeLocalConfigPermissioningController> nodeWhitelistController,
final Optional<NodePermissioningController> nodePermissioningController,
final MetricsSystem metricsSystem) {
super(
keyPair,
config,
peerBlacklist,
nodeWhitelistController,
nodePermissioningController,
metricsSystem);
super(keyPair, config, peerBlacklist, nodePermissioningController, metricsSystem);
checkArgument(vertx != null, "vertx instance cannot be null");
this.vertx = vertx;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import tech.pegasys.pantheon.ethereum.p2p.discovery.internal.PeerTable.EvictResult.EvictOutcome;
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer;
import tech.pegasys.pantheon.ethereum.p2p.peers.PeerBlacklist;
import tech.pegasys.pantheon.ethereum.permissioning.NodeLocalConfigPermissioningController;
import tech.pegasys.pantheon.ethereum.permissioning.node.NodePermissioningController;
import tech.pegasys.pantheon.ethereum.permissioning.node.NodeWhitelistUpdatedEvent;
import tech.pegasys.pantheon.metrics.Counter;
import tech.pegasys.pantheon.metrics.LabelledMetric;
import tech.pegasys.pantheon.metrics.MetricCategory;
Expand Down Expand Up @@ -121,7 +119,6 @@ public class PeerDiscoveryController {
private final DiscoveryPeer localPeer;
private final OutboundMessageHandler outboundMessageHandler;
private final PeerBlacklist peerBlacklist;
private final Optional<NodeLocalConfigPermissioningController> nodeWhitelistController;
private final Optional<NodePermissioningController> nodePermissioningController;
private final DiscoveryProtocolLogger discoveryProtocolLogger;
private final LabelledMetric<Counter> interactionCounter;
Expand Down Expand Up @@ -155,7 +152,6 @@ public PeerDiscoveryController(
final long tableRefreshIntervalMs,
final PeerRequirement peerRequirement,
final PeerBlacklist peerBlacklist,
final Optional<NodeLocalConfigPermissioningController> nodeWhitelistController,
final Optional<NodePermissioningController> nodePermissioningController,
final Subscribers<Consumer<PeerBondedEvent>> peerBondedObservers,
final Subscribers<Consumer<PeerDroppedEvent>> peerDroppedObservers,
Expand All @@ -169,7 +165,6 @@ public PeerDiscoveryController(
this.tableRefreshIntervalMs = tableRefreshIntervalMs;
this.peerRequirement = peerRequirement;
this.peerBlacklist = peerBlacklist;
this.nodeWhitelistController = nodeWhitelistController;
this.nodePermissioningController = nodePermissioningController;
this.outboundMessageHandler = outboundMessageHandler;
this.peerBondedObservers = peerBondedObservers;
Expand Down Expand Up @@ -243,9 +238,6 @@ public void start() {
Math.min(REFRESH_CHECK_INTERVAL_MILLIS, tableRefreshIntervalMs),
this::refreshTableIfRequired);
tableRefreshTimerId = OptionalLong.of(timerId);

nodeWhitelistController.ifPresent(
c -> c.subscribeToListUpdatedEvent(this::handleNodeWhitelistUpdatedEvent));
}

public CompletableFuture<?> stop() {
Expand Down Expand Up @@ -375,10 +367,6 @@ private boolean addToPeerTable(final DiscoveryPeer peer) {
return true;
}

private void handleNodeWhitelistUpdatedEvent(final NodeWhitelistUpdatedEvent event) {
event.getRemovedNodes().stream().map(DiscoveryPeer::fromEnode).forEach(this::dropFromPeerTable);
}

@VisibleForTesting
boolean dropFromPeerTable(final DiscoveryPeer peer) {
final EvictResult evictResult = peerTable.tryEvict(peer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import tech.pegasys.pantheon.ethereum.p2p.wire.PeerInfo;
import tech.pegasys.pantheon.ethereum.p2p.wire.SubProtocol;
import tech.pegasys.pantheon.ethereum.p2p.wire.messages.DisconnectMessage.DisconnectReason;
import tech.pegasys.pantheon.ethereum.permissioning.NodeLocalConfigPermissioningController;
import tech.pegasys.pantheon.ethereum.permissioning.node.NodePermissioningController;
import tech.pegasys.pantheon.metrics.Counter;
import tech.pegasys.pantheon.metrics.LabelledMetric;
Expand Down Expand Up @@ -733,8 +732,6 @@ public static class Builder {
private Optional<NodePermissioningController> nodePermissioningController = Optional.empty();
private Blockchain blockchain = null;
private Vertx vertx;
private Optional<NodeLocalConfigPermissioningController>
nodeLocalConfigPermissioningController = Optional.empty();

public P2PNetwork build() {
validate();
Expand Down Expand Up @@ -767,9 +764,6 @@ private void validate() {
!nodePermissioningController.isPresent() || blockchain != null,
"Network permissioning needs to listen to BlockAddedEvents. Blockchain can't be null.");
checkState(vertx != null, "Vertx must be set.");
checkState(
nodeLocalConfigPermissioningController != null,
"NodeLocalConfigPermissioningController must be set.");
}

private PeerDiscoveryAgent createDiscoveryAgent() {
Expand All @@ -779,7 +773,6 @@ private PeerDiscoveryAgent createDiscoveryAgent() {
keyPair,
config.getDiscovery(),
peerBlacklist,
nodeLocalConfigPermissioningController,
nodePermissioningController,
metricsSystem);
}
Expand All @@ -790,21 +783,6 @@ public Builder vertx(final Vertx vertx) {
return this;
}

public Builder nodeLocalConfigPermissioningController(
final Optional<NodeLocalConfigPermissioningController>
nodeLocalConfigPermissioningController) {
checkNotNull(nodeLocalConfigPermissioningController);
this.nodeLocalConfigPermissioningController = nodeLocalConfigPermissioningController;
return this;
}

public Builder nodeLocalConfigPermissioningController(
final NodeLocalConfigPermissioningController nodeLocalConfigPermissioningController) {
this.nodeLocalConfigPermissioningController =
Optional.ofNullable(nodeLocalConfigPermissioningController);
return this;
}

public Builder keyPair(final KeyPair keyPair) {
checkNotNull(keyPair);
this.keyPair = keyPair;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,6 @@ private List<URI> asEnodes(final List<DiscoveryPeer> peers) {
.collect(Collectors.toList());
}

public AgentBuilder whiteList(
final Optional<NodeLocalConfigPermissioningController> whitelist) {
this.whitelist = whitelist;
return this;
}

public AgentBuilder nodePermissioningController(final NodePermissioningController controller) {
this.nodePermissioningController = Optional.ofNullable(controller);
return this;
Expand All @@ -232,12 +226,7 @@ public MockPeerDiscoveryAgent build() {
config.setActive(active);

return new MockPeerDiscoveryAgent(
SECP256K1.KeyPair.generate(),
config,
blacklist,
whitelist,
nodePermissioningController,
agents);
SECP256K1.KeyPair.generate(), config, blacklist, nodePermissioningController, agents);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public void lastSeenAndFirstDiscoveredTimestampsUpdatedOnMessage() {
() -> true,
new PeerBlacklist(),
Optional.empty(),
Optional.empty(),
new Subscribers<>(),
new Subscribers<>(),
new NoOpMetricsSystem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import tech.pegasys.pantheon.ethereum.p2p.discovery.PeerDiscoveryAgent;
import tech.pegasys.pantheon.ethereum.p2p.discovery.internal.PeerDiscoveryController.AsyncExecutor;
import tech.pegasys.pantheon.ethereum.p2p.peers.PeerBlacklist;
import tech.pegasys.pantheon.ethereum.permissioning.NodeLocalConfigPermissioningController;
import tech.pegasys.pantheon.ethereum.permissioning.node.NodePermissioningController;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;
import tech.pegasys.pantheon.util.bytes.BytesValue;
Expand All @@ -41,16 +40,9 @@ public MockPeerDiscoveryAgent(
final KeyPair keyPair,
final DiscoveryConfiguration config,
final PeerBlacklist peerBlacklist,
final Optional<NodeLocalConfigPermissioningController> nodeWhitelistController,
final Optional<NodePermissioningController> nodePermissioningController,
final Map<BytesValue, MockPeerDiscoveryAgent> agentNetwork) {
super(
keyPair,
config,
peerBlacklist,
nodeWhitelistController,
nodePermissioningController,
new NoOpMetricsSystem());
super(keyPair, config, peerBlacklist, nodePermissioningController, new NoOpMetricsSystem());
this.agentNetwork = agentNetwork;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import tech.pegasys.pantheon.util.uint.UInt256Value;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -70,7 +69,6 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -1060,71 +1058,6 @@ public void shouldNotRespondToPingFromNonWhitelistedDiscoveryPeer() {
assertThat(controller.getPeers()).doesNotContain(peers.get(0));
}

@Test
public void whenObservingNodeWhitelistAndNodeIsRemovedShouldEvictPeerFromPeerTable()
throws IOException {
final PeerTable peerTableSpy = spy(peerTable);
final List<DiscoveryPeer> peers = createPeersInLastBucket(localPeer, 1);
final DiscoveryPeer peer = peers.get(0);
peerTableSpy.tryAdd(peer);

final LocalPermissioningConfiguration config = permissioningConfigurationWithTempFile();
final URI peerURI = URI.create(peer.getEnodeURLString());
config.setNodeWhitelist(Lists.newArrayList(peerURI));
final NodeLocalConfigPermissioningController nodeLocalConfigPermissioningController =
new NodeLocalConfigPermissioningController(
config, Collections.emptyList(), selfEnode.getNodeId());

controller =
getControllerBuilder()
.whitelist(nodeLocalConfigPermissioningController)
.peerTable(peerTableSpy)
.build();

controller.start();
nodeLocalConfigPermissioningController.removeNodes(Lists.newArrayList(peerURI.toString()));

verify(peerTableSpy).tryEvict(eq(DiscoveryPeer.fromURI(peerURI)));
}

@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void whenObservingNodeWhitelistAndNodeIsRemovedShouldNotifyPeerDroppedObservers()
throws IOException {
final PeerTable peerTableSpy = spy(peerTable);
final List<DiscoveryPeer> peers = createPeersInLastBucket(localPeer, 1);
final DiscoveryPeer peer = peers.get(0);
peerTableSpy.tryAdd(peer);

final LocalPermissioningConfiguration config = permissioningConfigurationWithTempFile();
final URI peerURI = URI.create(peer.getEnodeURLString());
config.setNodeWhitelist(Lists.newArrayList(peerURI));
final NodeLocalConfigPermissioningController nodeLocalConfigPermissioningController =
new NodeLocalConfigPermissioningController(
config, Collections.emptyList(), selfEnode.getNodeId());

final Consumer<PeerDroppedEvent> peerDroppedEventConsumer = mock(Consumer.class);
final Subscribers<Consumer<PeerDroppedEvent>> peerDroppedSubscribers = new Subscribers();
peerDroppedSubscribers.subscribe(peerDroppedEventConsumer);

doReturn(EvictResult.evicted()).when(peerTableSpy).tryEvict(any());

controller =
getControllerBuilder()
.whitelist(nodeLocalConfigPermissioningController)
.peerTable(peerTableSpy)
.peerDroppedObservers(peerDroppedSubscribers)
.build();

controller.start();
nodeLocalConfigPermissioningController.removeNodes(Lists.newArrayList(peerURI.toString()));

ArgumentCaptor<PeerDroppedEvent> captor = ArgumentCaptor.forClass(PeerDroppedEvent.class);
verify(peerDroppedEventConsumer).accept(captor.capture());
assertThat(captor.getValue().getPeer())
.isEqualTo(DiscoveryPeer.fromURI(peer.getEnodeURLString()));
}

@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void whenPeerIsNotEvictedDropFromTableShouldReturnFalseAndNotifyZeroObservers() {
Expand Down Expand Up @@ -1314,7 +1247,6 @@ PeerDiscoveryController build() {
TABLE_REFRESH_INTERVAL_MS,
PEER_REQUIREMENT,
blacklist,
whitelist,
nodePermissioningController,
peerBondedObservers,
peerDroppedObservers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public void tableRefreshSingleNode() {
() -> true,
new PeerBlacklist(),
Optional.empty(),
Optional.empty(),
new Subscribers<>(),
new Subscribers<>(),
new NoOpMetricsSystem()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ public void rejectIncomingConnectionFromNonWhitelistedPeer() throws Exception {
try (final P2PNetwork localNetwork =
builder()
.nodePermissioningController(nodePermissioningController)
.nodeLocalConfigPermissioningController(localWhitelistController)
.blockchain(blockchain)
.build();
final P2PNetwork remoteNetwork = builder().build()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package tech.pegasys.pantheon.ethereum.permissioning.node;

import tech.pegasys.pantheon.ethereum.permissioning.NodeLocalConfigPermissioningController;
import tech.pegasys.pantheon.ethereum.permissioning.node.provider.SyncStatusNodePermissioningProvider;
import tech.pegasys.pantheon.util.Subscribers;
import tech.pegasys.pantheon.util.enode.EnodeURL;
Expand Down Expand Up @@ -100,4 +101,11 @@ public long subscribeToUpdates(final Runnable callback) {
public boolean unsubscribeFromUpdates(final long id) {
return permissioningUpdateSubscribers.unsubscribe(id);
}

public Optional<NodeLocalConfigPermissioningController> localConfigController() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expose the NodeLocalConfigPermissioningController to supply it to the node whitelist JSON-RPC methods.

return getProviders().stream()
.filter(p -> p instanceof NodeLocalConfigPermissioningController)
.findFirst()
.map(n -> (NodeLocalConfigPermissioningController) n);
}
}
Loading