use of org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer in project besu by hyperledger.
the class PeerDiscoveryControllerTest method shouldNotBondWithBlacklistedPeer.
@Test
public void shouldNotBondWithBlacklistedPeer() {
final List<DiscoveryPeer> peers = createPeersInLastBucket(localPeer, 3);
final DiscoveryPeer discoPeer = peers.get(0);
final DiscoveryPeer otherPeer = peers.get(1);
final DiscoveryPeer otherPeer2 = peers.get(2);
final PeerPermissionsDenylist blacklist = PeerPermissionsDenylist.create();
final OutboundMessageHandler outboundMessageHandler = mock(OutboundMessageHandler.class);
controller = getControllerBuilder().peers(discoPeer).peerPermissions(blacklist).outboundMessageHandler(outboundMessageHandler).build();
final Endpoint localEndpoint = localPeer.getEndpoint();
// Setup ping to be sent to discoPeer
List<NodeKey> nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
PingPacketData pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), discoPeer.getEndpoint(), UInt64.ONE);
final Packet discoPeerPing = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(discoPeer, discoPeerPing);
controller.start();
verify(outboundMessageHandler, times(1)).send(any(), matchPacketOfType(PacketType.PING));
final Packet pongFromDiscoPeer = MockPacketDataFactory.mockPongPacket(discoPeer, discoPeerPing.getHash());
controller.onMessage(pongFromDiscoPeer, discoPeer);
verify(outboundMessageHandler, times(1)).send(eq(discoPeer), matchPacketOfType(PacketType.FIND_NEIGHBORS));
// Setup ping to be sent to otherPeer after neighbors packet is received
nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), otherPeer.getEndpoint(), UInt64.ONE);
final Packet pingPacket = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(otherPeer, pingPacket);
// Setup ping to be sent to otherPeer2 after neighbors packet is received
nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), otherPeer2.getEndpoint(), UInt64.ONE);
final Packet pingPacket2 = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(otherPeer2, pingPacket2);
// Blacklist peer
blacklist.add(otherPeer);
final Packet neighborsPacket = MockPacketDataFactory.mockNeighborsPacket(discoPeer, otherPeer, otherPeer2);
controller.onMessage(neighborsPacket, discoPeer);
verify(controller, times(0)).bond(otherPeer);
verify(controller, times(1)).bond(otherPeer2);
}
use of org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer in project besu by hyperledger.
the class PeerDiscoveryControllerTest method shouldNotAddNewPeerWhenReceivedPongFromBlacklistedPeer.
@Test
public void shouldNotAddNewPeerWhenReceivedPongFromBlacklistedPeer() {
final List<DiscoveryPeer> peers = createPeersInLastBucket(localPeer, 3);
final DiscoveryPeer discoPeer = peers.get(0);
final DiscoveryPeer otherPeer = peers.get(1);
final DiscoveryPeer otherPeer2 = peers.get(2);
final PeerPermissionsDenylist blacklist = PeerPermissionsDenylist.create();
final OutboundMessageHandler outboundMessageHandler = mock(OutboundMessageHandler.class);
controller = getControllerBuilder().peers(discoPeer).peerPermissions(blacklist).outboundMessageHandler(outboundMessageHandler).build();
final Endpoint localEndpoint = localPeer.getEndpoint();
// Setup ping to be sent to discoPeer
List<NodeKey> nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
PingPacketData pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), discoPeer.getEndpoint(), UInt64.ONE);
final Packet discoPeerPing = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(discoPeer, discoPeerPing);
controller.start();
verify(outboundMessageHandler, times(1)).send(eq(peers.get(0)), matchPacketOfType(PacketType.PING));
final Packet pongFromDiscoPeer = MockPacketDataFactory.mockPongPacket(discoPeer, discoPeerPing.getHash());
controller.onMessage(pongFromDiscoPeer, discoPeer);
verify(outboundMessageHandler, times(1)).send(eq(discoPeer), matchPacketOfType(PacketType.FIND_NEIGHBORS));
// Setup ping to be sent to otherPeer after neighbors packet is received
nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), otherPeer.getEndpoint(), UInt64.ONE);
final Packet pingPacket = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(otherPeer, pingPacket);
// Setup ping to be sent to otherPeer2 after neighbors packet is received
nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), otherPeer2.getEndpoint(), UInt64.ONE);
final Packet pingPacket2 = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(otherPeer2, pingPacket2);
final Packet neighborsPacket = MockPacketDataFactory.mockNeighborsPacket(discoPeer, otherPeer, otherPeer2);
controller.onMessage(neighborsPacket, discoPeer);
verify(outboundMessageHandler, times(peers.size())).send(any(), matchPacketOfType(PacketType.PING));
final Packet pongPacket = MockPacketDataFactory.mockPongPacket(otherPeer, pingPacket.getHash());
controller.onMessage(pongPacket, otherPeer);
// Blacklist otherPeer2 before sending return pong
blacklist.add(otherPeer2);
final Packet pongPacket2 = MockPacketDataFactory.mockPongPacket(otherPeer2, pingPacket2.getHash());
controller.onMessage(pongPacket2, otherPeer2);
assertThat(controller.streamDiscoveredPeers()).hasSize(2);
assertThat(controller.streamDiscoveredPeers()).contains(discoPeer);
assertThat(controller.streamDiscoveredPeers()).contains(otherPeer);
assertThat(controller.streamDiscoveredPeers()).doesNotContain(otherPeer2);
}
use of org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer in project besu by hyperledger.
the class PeerTableTest method addSelf.
@Test
public void addSelf() {
final DiscoveryPeer localPeer = DiscoveryPeer.fromEnode(EnodeURLImpl.builder().nodeId(Peer.randomId()).ipAddress("127.0.0.1").discoveryAndListeningPorts(12345).build());
final PeerTable table = new PeerTable(localPeer.getId(), 16);
final PeerTable.AddResult result = table.tryAdd(localPeer);
assertThat(result.getOutcome()).isEqualTo(AddOutcome.SELF);
assertThat(table.streamAllPeers()).hasSize(0);
}
use of org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer in project besu by hyperledger.
the class PeerTableTest method peerExists_withDifferentIp.
@Test
public void peerExists_withDifferentIp() {
final PeerTable table = new PeerTable(Peer.randomId(), 16);
final Bytes peerId = SIGNATURE_ALGORITHM.get().generateKeyPair().getPublicKey().getEncodedBytes();
final DiscoveryPeer peer = DiscoveryPeer.fromIdAndEndpoint(peerId, new Endpoint("1.1.1.1", 30303, Optional.empty()));
assertThat(table.tryAdd(peer).getOutcome()).isEqualTo(AddOutcome.ADDED);
final DiscoveryPeer duplicatePeer = DiscoveryPeer.fromIdAndEndpoint(peerId, new Endpoint("1.1.1.2", 30303, Optional.empty()));
assertThat(table.tryAdd(duplicatePeer)).satisfies(result -> {
assertThat(result.getOutcome()).isEqualTo(AddOutcome.ALREADY_EXISTED);
assertThat(result.getEvictionCandidate()).isNull();
});
}
use of org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer in project besu by hyperledger.
the class PeerTableTest method evictPeerFromEmptyTableShouldNotEvict.
@Test
public void evictPeerFromEmptyTableShouldNotEvict() {
final PeerTable table = new PeerTable(Peer.randomId(), 16);
final DiscoveryPeer peer = helper.createDiscoveryPeer();
final EvictResult evictResult = table.tryEvict(peer);
assertThat(evictResult.getOutcome()).isEqualTo(EvictOutcome.ABSENT);
}
Aggregations