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

Commit

Permalink
Remove unnecessary field (#1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaxter authored May 1, 2019
1 parent e5d34e5 commit b7f1349
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@

import java.util.Collection;
import java.util.Optional;
import java.util.function.Supplier;

/**
* A permissioning provider that only provides an answer when we have no peers outside of our
* bootnodes
*/
public class InsufficientPeersPermissioningProvider implements ContextualNodePermissioningProvider {
private final Supplier<Optional<EnodeURL>> selfEnode;
private final P2PNetwork p2pNetwork;
private final Collection<EnodeURL> bootnodeEnodes;
private long nonBootnodePeerConnections;
Expand All @@ -38,15 +36,10 @@ public class InsufficientPeersPermissioningProvider implements ContextualNodePer
* Creates the provider observing the provided p2p network
*
* @param p2pNetwork the p2p network to observe
* @param selfEnode A supplier that provides a representation of the locally running node, if
* available
* @param bootnodeEnodes the bootnodes that this node is configured to connection to
*/
public InsufficientPeersPermissioningProvider(
final P2PNetwork p2pNetwork,
final Supplier<Optional<EnodeURL>> selfEnode,
final Collection<EnodeURL> bootnodeEnodes) {
this.selfEnode = selfEnode;
final P2PNetwork p2pNetwork, final Collection<EnodeURL> bootnodeEnodes) {
this.p2pNetwork = p2pNetwork;
this.bootnodeEnodes = bootnodeEnodes;
this.nonBootnodePeerConnections = countP2PNetworkNonBootnodeConnections();
Expand All @@ -66,7 +59,7 @@ private long countP2PNetworkNonBootnodeConnections() {
@Override
public Optional<Boolean> isPermitted(
final EnodeURL sourceEnode, final EnodeURL destinationEnode) {
Optional<EnodeURL> maybeSelfEnode = selfEnode.get();
Optional<EnodeURL> maybeSelfEnode = p2pNetwork.getLocalEnode();
if (nonBootnodePeerConnections > 0) {
return Optional.empty();
} else if (!maybeSelfEnode.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Optional;
import java.util.function.Consumer;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
Expand All @@ -54,15 +55,19 @@ public class InsufficientPeersPermissioningProviderTest {
EnodeURL.fromString(
"enode://00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005@192.168.0.5:30303");

@Before
public void setup() {
when(p2pNetwork.getLocalEnode()).thenReturn(Optional.of(SELF_ENODE));
}

@Test
public void noResultWhenNoBootnodes() {
final Collection<EnodeURL> bootnodes = Collections.emptyList();

when(p2pNetwork.getPeers()).thenReturn(Collections.emptyList());

final InsufficientPeersPermissioningProvider provider =
new InsufficientPeersPermissioningProvider(
p2pNetwork, () -> Optional.of(SELF_ENODE), bootnodes);
new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes);

assertThat(provider.isPermitted(SELF_ENODE, ENODE_2)).isEmpty();
}
Expand All @@ -76,8 +81,7 @@ public void noResultWhenOtherConnections() {
final Collection<EnodeURL> bootnodes = Collections.singletonList(ENODE_2);

final InsufficientPeersPermissioningProvider provider =
new InsufficientPeersPermissioningProvider(
p2pNetwork, () -> Optional.of(SELF_ENODE), bootnodes);
new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes);

assertThat(provider.isPermitted(SELF_ENODE, ENODE_3)).isEmpty();
assertThat(provider.isPermitted(SELF_ENODE, ENODE_2)).isEmpty();
Expand All @@ -90,8 +94,7 @@ public void allowsConnectionIfBootnodeAndNoConnections() {
when(p2pNetwork.getPeers()).thenReturn(Collections.emptyList());

final InsufficientPeersPermissioningProvider provider =
new InsufficientPeersPermissioningProvider(
p2pNetwork, () -> Optional.of(SELF_ENODE), bootnodes);
new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes);

assertThat(provider.isPermitted(SELF_ENODE, ENODE_2)).contains(true);
assertThat(provider.isPermitted(SELF_ENODE, ENODE_3)).isEmpty();
Expand All @@ -102,9 +105,10 @@ public void noResultWhenLocalNodeNotReady() {
final Collection<EnodeURL> bootnodes = Collections.singletonList(ENODE_2);

when(p2pNetwork.getPeers()).thenReturn(Collections.emptyList());
when(p2pNetwork.getLocalEnode()).thenReturn(Optional.empty());

final InsufficientPeersPermissioningProvider provider =
new InsufficientPeersPermissioningProvider(p2pNetwork, Optional::empty, bootnodes);
new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes);

assertThat(provider.isPermitted(SELF_ENODE, ENODE_2)).isEmpty();
assertThat(provider.isPermitted(SELF_ENODE, ENODE_3)).isEmpty();
Expand All @@ -119,8 +123,7 @@ public void allowsConnectionIfBootnodeAndOnlyBootnodesConnected() {
when(p2pNetwork.getPeers()).thenReturn(Collections.singletonList(bootnodeMatchPeerConnection));

final InsufficientPeersPermissioningProvider provider =
new InsufficientPeersPermissioningProvider(
p2pNetwork, () -> Optional.of(SELF_ENODE), bootnodes);
new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes);

assertThat(provider.isPermitted(SELF_ENODE, ENODE_2)).contains(true);
assertThat(provider.isPermitted(SELF_ENODE, ENODE_3)).isEmpty();
Expand All @@ -143,8 +146,7 @@ public void firesUpdateWhenDisconnectLastNonBootnode() {
when(p2pNetwork.getPeers()).thenReturn(pcs);

final InsufficientPeersPermissioningProvider provider =
new InsufficientPeersPermissioningProvider(
p2pNetwork, () -> Optional.of(SELF_ENODE), bootnodes);
new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes);

final ArgumentCaptor<DisconnectCallback> callbackCaptor =
ArgumentCaptor.forClass(DisconnectCallback.class);
Expand All @@ -171,8 +173,7 @@ public void firesUpdateWhenNonBootnodeConnects() {
when(p2pNetwork.getPeers()).thenReturn(pcs);

final InsufficientPeersPermissioningProvider provider =
new InsufficientPeersPermissioningProvider(
p2pNetwork, () -> Optional.of(SELF_ENODE), bootnodes);
new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes);

@SuppressWarnings("unchecked")
final ArgumentCaptor<Consumer<PeerConnection>> callbackCaptor =
Expand Down Expand Up @@ -205,8 +206,7 @@ public void firesUpdateWhenGettingAndLosingConnection() {
when(p2pNetwork.getPeers()).thenReturn(pcs);

final InsufficientPeersPermissioningProvider provider =
new InsufficientPeersPermissioningProvider(
p2pNetwork, () -> Optional.of(SELF_ENODE), bootnodes);
new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes);

@SuppressWarnings("unchecked")
final ArgumentCaptor<Consumer<PeerConnection>> connectCallbackCaptor =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ public Runner build() {
nodePermissioningController.ifPresent(
n ->
n.setInsufficientPeersPermissioningProvider(
new InsufficientPeersPermissioningProvider(
networkRunner.getNetwork(), network::getLocalEnode, bootnodesAsEnodeURLs)));
new InsufficientPeersPermissioningProvider(network, bootnodesAsEnodeURLs)));

final TransactionPool transactionPool = pantheonController.getTransactionPool();
final MiningCoordinator miningCoordinator = pantheonController.getMiningCoordinator();
Expand Down

0 comments on commit b7f1349

Please sign in to comment.