Search in sources :

Example 1 with IncomingPacket

use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket 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 2 with IncomingPacket

use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket 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)

Example 3 with IncomingPacket

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

the class PeerDiscoveryAgentTest method bond_peerWithDiscoveryDisabled.

@Test
public void bond_peerWithDiscoveryDisabled() {
    // Create a peer with discovery disabled
    final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
    assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
    final DiscoveryPeer remotePeer = otherNode.getAdvertisedPeer().get();
    final EnodeURL enodeWithDiscoveryDisabled = EnodeURLImpl.builder().configureFromEnode(remotePeer.getEnodeURL()).disableDiscovery().build();
    final Peer peerWithDisabledDiscovery = DefaultPeer.fromEnodeURL(enodeWithDiscoveryDisabled);
    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();
    agent.bond(peerWithDisabledDiscovery);
    // We should not send any messages to peer with discovery disabled
    List<IncomingPacket> remoteIncomingPackets = otherNode.getIncomingPackets();
    assertThat(remoteIncomingPackets).hasSize(0);
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) 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 4 with IncomingPacket

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

the class PeerDiscoveryAgentTest method bonding_disallowOutgoingBonding.

@Test
public void bonding_disallowOutgoingBonding() {
    // 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_OUTBOUND_BONDING))).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);
    List<IncomingPacket> remoteIncomingPackets = otherNode.getIncomingPackets();
    assertThat(remoteIncomingPackets).isEmpty();
}
Also used : IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Example 5 with IncomingPacket

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

the class PeerDiscoveryAgentTest method neighbors_allowIncomingRequest.

@Test
public void neighbors_allowIncomingRequest() {
    // 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_OUTBOUND_BONDING))).thenReturn(true);
    when(peerPermissions.isPermitted(any(), eq(remotePeer), eq(Action.DISCOVERY_ALLOW_IN_PEER_TABLE))).thenReturn(true);
    when(peerPermissions.isPermitted(any(), eq(remotePeer), eq(Action.DISCOVERY_SERVE_INBOUND_NEIGHBORS_REQUEST))).thenReturn(true);
    agent.start(999);
    // Send request for neighbors
    requestNeighbors(otherNode, agent);
    assertThat(agent.streamDiscoveredPeers()).hasSize(1);
    List<IncomingPacket> remoteIncomingPackets = otherNode.getIncomingPackets();
    assertThat(remoteIncomingPackets).hasSize(2);
    // Peer should get a ping
    final IncomingPacket firstMsg = remoteIncomingPackets.get(0);
    assertThat(firstMsg.packet.getType()).isEqualTo(PacketType.PING);
    assertThat(firstMsg.fromAgent).isEqualTo(agent);
    // Then a neighbors response
    final IncomingPacket secondMsg = remoteIncomingPackets.get(1);
    assertThat(secondMsg.packet.getType()).isEqualTo(PacketType.NEIGHBORS);
    assertThat(secondMsg.fromAgent).isEqualTo(agent);
}
Also used : IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) 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)16 IncomingPacket (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket)16 Test (org.junit.Test)16 PeerPermissions (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions)13 Peer (org.hyperledger.besu.ethereum.p2p.peers.Peer)6 List (java.util.List)5 Optional (java.util.Optional)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Packet (org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet)5 PacketType (org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType)5 DefaultPeer (org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer)5 Collections (java.util.Collections)4 Collectors (java.util.stream.Collectors)4 FindNeighborsPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData)4 Collections.emptyList (java.util.Collections.emptyList)3 Collectors.toList (java.util.stream.Collectors.toList)3 Bytes (org.apache.tuweni.bytes.Bytes)3 EnodeURL (org.hyperledger.besu.plugin.data.EnodeURL)3 Supplier (com.google.common.base.Supplier)2 Suppliers (com.google.common.base.Suppliers)2