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);
}
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);
}
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);
}
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();
}
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);
}
Aggregations