Search in sources :

Example 1 with MockPeerDiscoveryAgent

use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent in project besu by hyperledger.

the class PeerDiscoveryAgentTest method peerTable_disallowPeer.

@Test
public void peerTable_disallowPeer() {
    // Setup peer
    final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
    assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
    final DiscoveryPeer remotePeer = otherNode.getAdvertisedPeer().get();
    final PeerPermissions peerPermissions = mock(PeerPermissions.class);
    final MockPeerDiscoveryAgent agent = helper.createDiscoveryAgent(helper.agentBuilder().bootstrapPeers(remotePeer).peerPermissions(peerPermissions));
    when(peerPermissions.isPermitted(any(), any(), any())).thenReturn(true);
    when(peerPermissions.isPermitted(any(), eq(remotePeer), eq(Action.DISCOVERY_ALLOW_IN_PEER_TABLE))).thenReturn(false);
    agent.start(999);
    assertThat(agent.streamDiscoveredPeers()).hasSize(0);
}
Also used : PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Example 2 with MockPeerDiscoveryAgent

use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent in project besu by hyperledger.

the class PeerDiscoveryAgentTest method bond_supplyGenericPeer.

@Test
public void bond_supplyGenericPeer() {
    final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
    assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
    final DiscoveryPeer remotePeer = otherNode.getAdvertisedPeer().get();
    final Peer genericPeer = DefaultPeer.fromEnodeURL(remotePeer.getEnodeURL());
    final PeerPermissions peerPermissions = mock(PeerPermissions.class);
    final MockPeerDiscoveryAgent agent = helper.createDiscoveryAgent(helper.agentBuilder().peerPermissions(peerPermissions));
    when(peerPermissions.isPermitted(any(), any(), any())).thenReturn(true);
    // Start agent and bond
    assertThat(agent.start(30303)).isCompleted();
    assertThat(agent.streamDiscoveredPeers()).isEmpty();
    agent.bond(genericPeer);
    // We should send an outgoing ping
    List<IncomingPacket> remoteIncomingPackets = otherNode.getIncomingPackets();
    assertThat(remoteIncomingPackets).hasSize(2);
    final IncomingPacket firstMsg = remoteIncomingPackets.get(0);
    assertThat(firstMsg.packet.getType()).isEqualTo(PacketType.PING);
    // The remote peer will send a PING and we'll respond with a return PONG
    assertThat(firstMsg.fromAgent).isEqualTo(agent);
    final IncomingPacket secondMsg = remoteIncomingPackets.get(1);
    assertThat(secondMsg.packet.getType()).isEqualTo(PacketType.PONG);
    assertThat(secondMsg.fromAgent).isEqualTo(agent);
    // The peer should now be bonded
    assertThat(agent.streamDiscoveredPeers()).contains(remotePeer);
}
Also used : IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Example 3 with MockPeerDiscoveryAgent

use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent in project besu by hyperledger.

the class PeerDiscoveryAgentTest method peerTable_allowPeer.

@Test
public void peerTable_allowPeer() {
    // Setup peer
    final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
    assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
    final DiscoveryPeer remotePeer = otherNode.getAdvertisedPeer().get();
    final PeerPermissions peerPermissions = mock(PeerPermissions.class);
    final MockPeerDiscoveryAgent agent = helper.createDiscoveryAgent(helper.agentBuilder().bootstrapPeers(remotePeer).peerPermissions(peerPermissions));
    when(peerPermissions.isPermitted(any(), any(), any())).thenReturn(false);
    when(peerPermissions.isPermitted(any(), eq(remotePeer), eq(Action.DISCOVERY_ALLOW_IN_PEER_TABLE))).thenReturn(true);
    agent.start(999);
    assertThat(agent.streamDiscoveredPeers()).hasSize(1);
}
Also used : PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Example 4 with MockPeerDiscoveryAgent

use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent in project besu by hyperledger.

the class PeerDiscoveryAgentTest method bonding_disallowIncomingBonding.

@Test
public void bonding_disallowIncomingBonding() {
    // Start an agent with no bootstrap peers.
    final PeerPermissions peerPermissions = mock(PeerPermissions.class);
    final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(Collections.emptyList(), peerPermissions);
    assertThat(agent.getAdvertisedPeer().isPresent()).isTrue();
    final Peer localNode = agent.getAdvertisedPeer().get();
    // Setup peer and permissions
    final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
    assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
    final Peer remotePeer = otherNode.getAdvertisedPeer().get();
    when(peerPermissions.isPermitted(eq(localNode), any(), any())).thenReturn(true);
    when(peerPermissions.isPermitted(eq(localNode), eq(remotePeer), eq(Action.DISCOVERY_ACCEPT_INBOUND_BONDING))).thenReturn(false);
    // Bond
    bondViaIncomingPing(agent, otherNode);
    // Check peer was not allowed to connect
    assertThat(agent.streamDiscoveredPeers()).hasSize(0);
    // Check that peer did not receive a return pong
    assertThat(otherNode.getIncomingPackets()).isEmpty();
}
Also used : Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Example 5 with MockPeerDiscoveryAgent

use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent in project besu by hyperledger.

the class PeerDiscoveryAgentTest method bonding_allowIncomingBonding.

@Test
public void bonding_allowIncomingBonding() {
    // Start an agent with no bootstrap peers.
    final PeerPermissions peerPermissions = mock(PeerPermissions.class);
    final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(Collections.emptyList(), peerPermissions);
    assertThat(agent.getAdvertisedPeer().isPresent()).isTrue();
    final DiscoveryPeer localNode = agent.getAdvertisedPeer().get();
    // Setup peer and permissions
    final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
    assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
    final Peer remotePeer = otherNode.getAdvertisedPeer().get();
    when(peerPermissions.isPermitted(eq(localNode), any(), any())).thenReturn(false);
    when(peerPermissions.isPermitted(eq(localNode), eq(remotePeer), eq(Action.DISCOVERY_ACCEPT_INBOUND_BONDING))).thenReturn(true);
    when(peerPermissions.isPermitted(any(), eq(remotePeer), eq(Action.DISCOVERY_ALLOW_IN_PEER_TABLE))).thenReturn(true);
    // Bond
    otherNode.bond(localNode);
    List<IncomingPacket> remoteIncomingPackets = otherNode.getIncomingPackets();
    assertThat(remoteIncomingPackets).hasSize(2);
    final IncomingPacket firstMsg = remoteIncomingPackets.get(0);
    assertThat(firstMsg.packet.getType()).isEqualTo(PacketType.PING);
    assertThat(firstMsg.fromAgent).isEqualTo(agent);
    // Check that peer received a return pong
    final IncomingPacket secondMsg = remoteIncomingPackets.get(1);
    assertThat(secondMsg.packet.getType()).isEqualTo(PacketType.PONG);
    assertThat(secondMsg.fromAgent).isEqualTo(agent);
}
Also used : IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Aggregations

MockPeerDiscoveryAgent (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent)36 Test (org.junit.Test)34 IncomingPacket (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket)18 PeerPermissions (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions)16 Packet (org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet)9 List (java.util.List)8 Peer (org.hyperledger.besu.ethereum.p2p.peers.Peer)8 Optional (java.util.Optional)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 Bytes (org.apache.tuweni.bytes.Bytes)6 PacketType (org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType)6 DefaultPeer (org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer)6 Collections (java.util.Collections)5 Collectors (java.util.stream.Collectors)5 KeyPair (org.hyperledger.besu.crypto.KeyPair)5 FindNeighborsPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData)5 Collections.emptyList (java.util.Collections.emptyList)4 Collectors.toList (java.util.stream.Collectors.toList)4 NodeRecord (org.ethereum.beacon.discovery.schema.NodeRecord)4 AgentBuilder (org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryTestHelper.AgentBuilder)4