Search in sources :

Example 1 with RadixNodeUri

use of com.radixdlt.network.p2p.RadixNodeUri in project radixdlt by radixdlt.

the class MockP2PNetwork method createChannel.

void createChannel(int clientPeerIndex, RadixNodeUri serverPeerUri) {
    final var clientPeer = nodes.get(clientPeerIndex);
    final var serverPeerOpt = nodes.stream().filter(p -> p.uri.getHost().equals(serverPeerUri.getHost()) && p.uri.getPort() == serverPeerUri.getPort()).findAny();
    final var clientSocketChannel = mock(SocketChannel.class);
    final var clientChannel = new PeerChannel(clientPeer.injector.getInstance(P2PConfig.class), Addressing.ofNetwork(Network.LOCALNET), 1, "fork1", clientPeer.injector.getInstance(SystemCounters.class), clientPeer.injector.getInstance(Serialization.class), new SecureRandom(), ECKeyOps.fromKeyPair(clientPeer.keyPair), clientPeer.injector.getInstance(new Key<EventDispatcher<PeerEvent>>() {
    }), Optional.of(serverPeerUri), clientSocketChannel, Optional.empty());
    if (serverPeerOpt.isEmpty()) {
        clientChannel.channelActive(null);
        clientChannel.channelInactive(null);
        return;
    }
    final var serverPeer = serverPeerOpt.get();
    final var serverSocketChannel = mock(SocketChannel.class);
    final var serverChannel = new PeerChannel(serverPeer.injector.getInstance(P2PConfig.class), Addressing.ofNetwork(Network.LOCALNET), 1, "fork1", serverPeer.injector.getInstance(SystemCounters.class), serverPeer.injector.getInstance(Serialization.class), new SecureRandom(), ECKeyOps.fromKeyPair(serverPeer.keyPair), serverPeer.injector.getInstance(new Key<EventDispatcher<PeerEvent>>() {
    }), Optional.empty(), serverSocketChannel, Optional.empty());
    when(clientSocketChannel.writeAndFlush(any())).thenAnswer(inv -> {
        final var rawData = inv.getArgument(0);
        serverChannel.channelRead0(null, (ByteBuf) rawData);
        return null;
    });
    when(serverSocketChannel.writeAndFlush(any())).thenAnswer(inv -> {
        final var rawData = inv.getArgument(0);
        clientChannel.channelRead0(null, (ByteBuf) rawData);
        return null;
    });
    when(clientSocketChannel.close()).thenAnswer(inv -> {
        final var mockChannel = mock(ChannelHandlerContext.class);
        when(mockChannel.channel()).thenReturn(mock(Channel.class));
        clientChannel.channelInactive(mockChannel);
        serverChannel.channelInactive(mockChannel);
        return null;
    });
    when(serverSocketChannel.close()).thenAnswer(inv -> {
        final var mockChannel = mock(ChannelHandlerContext.class);
        when(mockChannel.channel()).thenReturn(mock(Channel.class));
        serverChannel.channelInactive(mockChannel);
        clientChannel.channelInactive(mockChannel);
        return null;
    });
    serverChannel.channelActive(null);
    clientChannel.channelActive(null);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SystemCounters(com.radixdlt.counters.SystemCounters) Addressing(com.radixdlt.networks.Addressing) EventDispatcher(com.radixdlt.environment.EventDispatcher) Network(com.radixdlt.networks.Network) Serialization(com.radixdlt.serialization.Serialization) Key(com.google.inject.Key) ECKeyOps(com.radixdlt.crypto.ECKeyOps) RadixNodeUri(com.radixdlt.network.p2p.RadixNodeUri) Mockito.when(org.mockito.Mockito.when) PeerChannel(com.radixdlt.network.p2p.transport.PeerChannel) Channel(io.netty.channel.Channel) SecureRandom(java.security.SecureRandom) P2PConfig(com.radixdlt.network.p2p.P2PConfig) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) PeerEvent(com.radixdlt.network.p2p.PeerEvent) ImmutableList(com.google.common.collect.ImmutableList) ByteBuf(io.netty.buffer.ByteBuf) Optional(java.util.Optional) SocketChannel(io.netty.channel.socket.SocketChannel) Mockito.mock(org.mockito.Mockito.mock) Serialization(com.radixdlt.serialization.Serialization) PeerEvent(com.radixdlt.network.p2p.PeerEvent) P2PConfig(com.radixdlt.network.p2p.P2PConfig) PeerChannel(com.radixdlt.network.p2p.transport.PeerChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) SystemCounters(com.radixdlt.counters.SystemCounters) SecureRandom(java.security.SecureRandom) PeerChannel(com.radixdlt.network.p2p.transport.PeerChannel) Key(com.google.inject.Key)

Example 2 with RadixNodeUri

use of com.radixdlt.network.p2p.RadixNodeUri in project radixdlt by radixdlt.

the class P2PTestNetworkRunner method createInjector.

private static Injector createInjector(MockP2PNetwork p2pNetwork, DeterministicNetwork network, P2PConfig p2pConfig, ECKeyPair nodeKey, RadixNodeUri selfUri, int selfNodeIndex) throws ParseException {
    final var properties = new RuntimeProperties(new JSONObject(), new String[] {});
    return Guice.createInjector(Modules.override(new P2PModule(properties)).with(new AbstractModule() {

        @Override
        protected void configure() {
            bind(TestCounters.class).toInstance(new TestCounters());
            bind(P2PConfig.class).toInstance(p2pConfig);
            bind(RadixNodeUri.class).annotatedWith(Self.class).toInstance(selfUri);
            bind(SystemCounters.class).to(SystemCountersImpl.class).in(Scopes.SINGLETON);
        }

        @Provides
        public PeerOutboundBootstrap peerOutboundBootstrap(TestCounters testCounters) {
            return uri -> {
                testCounters.outboundChannelsBootstrapped += 1;
                p2pNetwork.createChannel(selfNodeIndex, uri);
            };
        }
    }), new PeerDiscoveryModule(), new PeerLivenessMonitorModule(), new DispatcherModule(), new AbstractModule() {

        @Override
        protected void configure() {
            final var dbDir = new TemporaryFolder();
            try {
                dbDir.create();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            bindConstant().annotatedWith(NetworkId.class).to(Network.LOCALNET.getId());
            bind(Addressing.class).toInstance(Addressing.ofNetwork(Network.LOCALNET));
            bindConstant().annotatedWith(DatabaseLocation.class).to(dbDir.getRoot().getAbsolutePath());
            bindConstant().annotatedWith(DatabaseCacheSize.class).to(100_000L);
            bind(ECKeyPair.class).annotatedWith(Self.class).toInstance(nodeKey);
            bind(ECPublicKey.class).annotatedWith(Self.class).toInstance(nodeKey.getPublicKey());
            bind(BFTNode.class).annotatedWith(Self.class).toInstance(BFTNode.create(nodeKey.getPublicKey()));
            bind(String.class).annotatedWith(Self.class).toInstance(Addressing.ofNetwork(Network.LOCALNET).forValidators().of(nodeKey.getPublicKey()).substring(0, 10));
            bind(ECKeyOps.class).toInstance(ECKeyOps.fromKeyPair(nodeKey));
            bind(Environment.class).toInstance(network.createSender(BFTNode.create(nodeKey.getPublicKey())));
            bind(RuntimeProperties.class).toInstance(properties);
            bind(Serialization.class).toInstance(DefaultSerialization.getInstance());
            bind(DeterministicProcessor.class);
            Multibinder.newSetBinder(binder(), StartProcessorOnRunner.class);
            bind(ForkConfig.class).annotatedWith(NewestForkConfig.class).toInstance(new FixedEpochForkConfig("genesis", null, 0L));
        }
    });
}
Also used : Key(com.google.inject.Key) DefaultSerialization(com.radixdlt.DefaultSerialization) DatabaseCacheSize(com.radixdlt.store.DatabaseCacheSize) DispatcherModule(com.radixdlt.modules.DispatcherModule) JSONObject(org.json.JSONObject) ForkConfig(com.radixdlt.statecomputer.forks.ForkConfig) PeerOutboundBootstrap(com.radixdlt.network.p2p.transport.PeerOutboundBootstrap) SystemCounters(com.radixdlt.counters.SystemCounters) DatabaseEnvironment(com.radixdlt.store.DatabaseEnvironment) Multibinder(com.google.inject.multibindings.Multibinder) Objects(java.util.Objects) SystemCountersImpl(com.radixdlt.counters.SystemCountersImpl) P2PModule(com.radixdlt.network.p2p.P2PModule) FixedEpochForkConfig(com.radixdlt.statecomputer.forks.FixedEpochForkConfig) ParseException(org.apache.commons.cli.ParseException) NetworkId(com.radixdlt.networks.NetworkId) StartProcessorOnRunner(com.radixdlt.environment.StartProcessorOnRunner) IntStream(java.util.stream.IntStream) PeerManager(com.radixdlt.network.p2p.PeerManager) Serialization(com.radixdlt.serialization.Serialization) Modules(com.google.inject.util.Modules) PeerLivenessMonitorModule(com.radixdlt.network.p2p.PeerLivenessMonitorModule) ECPublicKey(com.radixdlt.crypto.ECPublicKey) DeterministicNetwork(com.radixdlt.environment.deterministic.network.DeterministicNetwork) RuntimeProperties(com.radixdlt.properties.RuntimeProperties) ImmutableList(com.google.common.collect.ImmutableList) DatabaseLocation(com.radixdlt.store.DatabaseLocation) MessageSelector(com.radixdlt.environment.deterministic.network.MessageSelector) PeerDiscoveryModule(com.radixdlt.network.p2p.PeerDiscoveryModule) AddressBook(com.radixdlt.network.p2p.addressbook.AddressBook) Addressing(com.radixdlt.networks.Addressing) NewestForkConfig(com.radixdlt.statecomputer.forks.NewestForkConfig) Network(com.radixdlt.networks.Network) ECKeyOps(com.radixdlt.crypto.ECKeyOps) Environment(com.radixdlt.environment.Environment) RadixNodeUri(com.radixdlt.network.p2p.RadixNodeUri) IOException(java.io.IOException) DeterministicProcessor(com.radixdlt.environment.deterministic.DeterministicProcessor) Scopes(com.google.inject.Scopes) Injector(com.google.inject.Injector) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) P2PConfig(com.radixdlt.network.p2p.P2PConfig) Provides(com.google.inject.Provides) ECKeyPair(com.radixdlt.crypto.ECKeyPair) Guice(com.google.inject.Guice) Self(com.radixdlt.hotstuff.bft.Self) MessageMutator(com.radixdlt.environment.deterministic.network.MessageMutator) TemporaryFolder(org.junit.rules.TemporaryFolder) AbstractModule(com.google.inject.AbstractModule) DeterministicProcessor(com.radixdlt.environment.deterministic.DeterministicProcessor) Self(com.radixdlt.hotstuff.bft.Self) StartProcessorOnRunner(com.radixdlt.environment.StartProcessorOnRunner) TemporaryFolder(org.junit.rules.TemporaryFolder) FixedEpochForkConfig(com.radixdlt.statecomputer.forks.FixedEpochForkConfig) RuntimeProperties(com.radixdlt.properties.RuntimeProperties) PeerLivenessMonitorModule(com.radixdlt.network.p2p.PeerLivenessMonitorModule) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) ForkConfig(com.radixdlt.statecomputer.forks.ForkConfig) FixedEpochForkConfig(com.radixdlt.statecomputer.forks.FixedEpochForkConfig) NewestForkConfig(com.radixdlt.statecomputer.forks.NewestForkConfig) P2PConfig(com.radixdlt.network.p2p.P2PConfig) ECKeyPair(com.radixdlt.crypto.ECKeyPair) IOException(java.io.IOException) AbstractModule(com.google.inject.AbstractModule) JSONObject(org.json.JSONObject) ECPublicKey(com.radixdlt.crypto.ECPublicKey) SystemCountersImpl(com.radixdlt.counters.SystemCountersImpl) DispatcherModule(com.radixdlt.modules.DispatcherModule) P2PModule(com.radixdlt.network.p2p.P2PModule) PeerDiscoveryModule(com.radixdlt.network.p2p.PeerDiscoveryModule)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 Key (com.google.inject.Key)2 SystemCounters (com.radixdlt.counters.SystemCounters)2 ECKeyOps (com.radixdlt.crypto.ECKeyOps)2 P2PConfig (com.radixdlt.network.p2p.P2PConfig)2 RadixNodeUri (com.radixdlt.network.p2p.RadixNodeUri)2 Addressing (com.radixdlt.networks.Addressing)2 Network (com.radixdlt.networks.Network)2 Serialization (com.radixdlt.serialization.Serialization)2 AbstractModule (com.google.inject.AbstractModule)1 Guice (com.google.inject.Guice)1 Injector (com.google.inject.Injector)1 Provides (com.google.inject.Provides)1 Scopes (com.google.inject.Scopes)1 Multibinder (com.google.inject.multibindings.Multibinder)1 Modules (com.google.inject.util.Modules)1 DefaultSerialization (com.radixdlt.DefaultSerialization)1 SystemCountersImpl (com.radixdlt.counters.SystemCountersImpl)1 ECKeyPair (com.radixdlt.crypto.ECKeyPair)1 ECPublicKey (com.radixdlt.crypto.ECPublicKey)1