public async Task Can_handshake() { /* tools */ var cryptoRandom = new CryptoRandom(); var testLogger = new TestLogger(); var logManager = new OneLoggerLogManager(testLogger); /* rlpx + p2p + eth */ _keyA = TestObject.PrivateKeyA; _keyB = TestObject.PrivateKeyB; _keyC = TestObject.PrivateKeyC; var signer = new Signer(); var eciesCipher = new EciesCipher(cryptoRandom); var serializationService = Build.A.SerializationService().WithEncryptionHandshake().WithP2P().WithEth().TestObject; var encryptionHandshakeServiceA = new EncryptionHandshakeService(serializationService, eciesCipher, cryptoRandom, signer, _keyA, logManager); var encryptionHandshakeServiceB = new EncryptionHandshakeService(serializationService, eciesCipher, cryptoRandom, signer, _keyB, logManager); // var encryptionHandshakeServiceC = new EncryptionHandshakeService(serializationService, eciesCipher, cryptoRandom, signer, _keyC, logger); var syncManager = Substitute.For <ISynchronizationManager>(); Block genesisBlock = Build.A.Block.Genesis.TestObject; syncManager.Head.Returns(genesisBlock.Header); syncManager.Genesis.Returns(genesisBlock.Header); var peerServerA = new RlpxPeer(new NodeId(_keyA.PublicKey), PortA, encryptionHandshakeServiceA, serializationService, syncManager, logManager); var peerServerB = new RlpxPeer(new NodeId(_keyB.PublicKey), PortB, encryptionHandshakeServiceB, serializationService, syncManager, logManager); // var peerServerC = new RlpxPeer(_keyC.PublicKey, PortC, encryptionHandshakeServiceC, serializationService, Substitute.For<ISynchronizationManager>(), logger); await Task.WhenAll(peerServerA.Init(), peerServerB.Init()); // await Task.WhenAll(peerServerA.Init(), peerServerB.Init(), peerServerC.Init()); Console.WriteLine("Servers running..."); Console.WriteLine("Connecting A to B..."); await peerServerA.ConnectAsync(new NodeId(_keyB.PublicKey), "127.0.0.1", PortB); Console.WriteLine("A to B connected..."); // Console.WriteLine("Connecting A to C..."); // await peerServerA.ConnectAsync(_keyC.PublicKey, "127.0.0.1", PortC); // Console.WriteLine("A to C connected..."); // // Console.WriteLine("Connecting C to C..."); // await peerServerB.ConnectAsync(_keyA.PublicKey, "127.0.0.1", PortA); // Console.WriteLine("C to C connected..."); Console.WriteLine("Shutting down..."); await Task.WhenAll(peerServerA.Shutdown(), peerServerB.Shutdown()); Console.WriteLine("Goodbye..."); Assert.True(testLogger.LogList.Count(l => l.Contains("ETH received status with")) == 2, "ETH status exchange"); }
private static async Task InitNet(ChainSpec chainSpec) { /* tools */ var serializationService = new MessageSerializationService(); var cryptoRandom = new CryptoRandom(); var signer = new Signer(); /* rlpx */ var eciesCipher = new EciesCipher(cryptoRandom); var eip8Pad = new Eip8MessagePad(cryptoRandom); serializationService.Register(new AuthEip8MessageSerializer(eip8Pad)); serializationService.Register(new AckEip8MessageSerializer(eip8Pad)); var encryptionHandshakeServiceA = new EncryptionHandshakeService(serializationService, eciesCipher, cryptoRandom, signer, _privateKey, NetworkLogger); /* p2p */ serializationService.Register(new HelloMessageSerializer()); serializationService.Register(new DisconnectMessageSerializer()); serializationService.Register(new PingMessageSerializer()); serializationService.Register(new PongMessageSerializer()); /* eth */ serializationService.Register(new StatusMessageSerializer()); serializationService.Register(new TransactionsMessageSerializer()); serializationService.Register(new GetBlockHeadersMessageSerializer()); serializationService.Register(new NewBlockHashesMessageSerializer()); serializationService.Register(new GetBlockBodiesMessageSerializer()); serializationService.Register(new BlockHeadersMessageSerializer()); serializationService.Register(new BlockBodiesMessageSerializer()); serializationService.Register(new NewBlockMessageSerializer()); NetworkLogger.Info("Initializing server..."); var localPeer = new RlpxPeer(_privateKey.PublicKey, _listenPort, encryptionHandshakeServiceA, serializationService, _syncManager, NetworkLogger); await localPeer.Init(); // https://stackoverflow.com/questions/6803073/get-local-ip-address?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa string localIp; using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) { socket.Connect("8.8.8.8", 65530); IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint; Debug.Assert(endPoint != null, "null endpoint when connecting a UDP socket"); localIp = endPoint.Address.ToString(); } NetworkLogger.Info($"Node is up and listening on {localIp}:{_listenPort}... press ENTER to exit"); NetworkLogger.Info($"enode://{_privateKey.PublicKey.ToString(false)}@{localIp}:{_listenPort}"); foreach (Bootnode bootnode in chainSpec.Bootnodes) { NetworkLogger.Info($"Connecting to {bootnode.Description} @ {bootnode.Host}:{bootnode.Port}"); await localPeer.ConnectAsync(bootnode.PublicKey, bootnode.Host, bootnode.Port); NetworkLogger.Info("Testnet connected..."); } Console.ReadLine(); NetworkLogger.Info("Shutting down..."); NetworkLogger.Info("Stopping sync manager..."); await _syncManager.StopAsync(); NetworkLogger.Info("Stopping blockchain processor..."); await _blockchainProcessor.StopAsync(); NetworkLogger.Info("Stopping local peer..."); await localPeer.Shutdown(); NetworkLogger.Info("Goodbye..."); }