Search in sources :

Example 1 with PeerAddressEntry

use of com.radixdlt.network.p2p.addressbook.AddressBookEntry.PeerAddressEntry in project radixdlt by radixdlt.

the class PeerManagerStaticTest method should_cleanup_existing_entries_from_same_host_port_on_successful_connect.

@Test
public void should_cleanup_existing_entries_from_same_host_port_on_successful_connect() {
    var self = makeNodeUri("10.0.0.1", 30000);
    var addressBook = new AddressBook(self, mock(P2PConfig.class), cmock(new TypeLiteral<EventDispatcher<PeerEvent>>() {
    }), new InMemoryAddressBookPersistence());
    var peerManager = new PeerManager(self, P2PConfig.fromRuntimeProperties(defaultProperties()), Addressing.ofNetwork(Network.LOCALNET), () -> addressBook, () -> mock(PendingOutboundChannelsManager.class), mock(SystemCounters.class));
    var peer1 = makeNodeUri("10.0.0.2", 30000);
    // second address for same peer
    var peer2 = makeNodeUri(peer1.getNodeId().getPublicKey(), "10.0.0.2", 30001);
    addressBook.addUncheckedPeers(Set.of(peer1, peer2));
    // Before connect
    addressBook.findById(peer2.getNodeId()).ifPresentOrElse(entry -> assertEquals(2L, entry.getKnownAddresses().stream().count()), Assert::fail);
    var peerChanel = mock(PeerChannel.class);
    var inboundMessages = cmock(new TypeLiteral<Flowable<InboundMessage>>() {
    });
    // new key, but same host/port as peer2
    var peer = makeNodeUri("10.0.0.2", 30000);
    when(peerChanel.getUri()).thenReturn(Optional.of(peer));
    when(peerChanel.inboundMessages()).thenReturn(inboundMessages);
    when(inboundMessages.toObservable()).thenReturn(cmock(new TypeLiteral<Observable<InboundMessage>>() {
    }));
    when(peerChanel.isOutbound()).thenReturn(true);
    when(peerChanel.getRemoteNodeId()).thenReturn(peer.getNodeId());
    peerManager.peerEventProcessor().process(new PeerConnected(peerChanel));
    // One address is removed
    addressBook.findById(peer2.getNodeId()).ifPresentOrElse(entry -> assertEquals(1L, entry.getKnownAddresses().stream().count()), Assert::fail);
    addressBook.findById(peer2.getNodeId()).ifPresentOrElse(entry -> assertEquals(peer2, entry.getKnownAddresses().stream().map(PeerAddressEntry::getUri).iterator().next()), Assert::fail);
}
Also used : PeerConnected(com.radixdlt.network.p2p.PeerEvent.PeerConnected) SystemCounters(com.radixdlt.counters.SystemCounters) InboundMessage(com.radixdlt.network.messaging.InboundMessage) Assert(org.junit.Assert) AddressBook(com.radixdlt.network.p2p.addressbook.AddressBook) TypeLiteral(com.google.inject.TypeLiteral) PeerAddressEntry(com.radixdlt.network.p2p.addressbook.AddressBookEntry.PeerAddressEntry) InMemoryAddressBookPersistence(com.radixdlt.network.p2p.addressbook.InMemoryAddressBookPersistence) Flowable(io.reactivex.rxjava3.core.Flowable) Test(org.junit.Test)

Aggregations

TypeLiteral (com.google.inject.TypeLiteral)1 SystemCounters (com.radixdlt.counters.SystemCounters)1 InboundMessage (com.radixdlt.network.messaging.InboundMessage)1 PeerConnected (com.radixdlt.network.p2p.PeerEvent.PeerConnected)1 AddressBook (com.radixdlt.network.p2p.addressbook.AddressBook)1 PeerAddressEntry (com.radixdlt.network.p2p.addressbook.AddressBookEntry.PeerAddressEntry)1 InMemoryAddressBookPersistence (com.radixdlt.network.p2p.addressbook.InMemoryAddressBookPersistence)1 Flowable (io.reactivex.rxjava3.core.Flowable)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1