Ejemplo n.º 1
0
        public void CanSelectRandomPeerToConnectTo_AllPeersAreNew()
        {
            var ipAddress  = IPAddress.Parse("::ffff:192.168.0.1");
            var addressOne = new NetworkAddress(ipAddress, 80);

            ipAddress = IPAddress.Parse("::ffff:192.168.0.2");
            var addressTwo = new NetworkAddress(ipAddress, 80);

            ipAddress = IPAddress.Parse("::ffff:192.168.0.3");
            var addressThree = new NetworkAddress(ipAddress, 80);

            ipAddress = IPAddress.Parse("::ffff:192.168.0.4");
            var addressFour = new NetworkAddress(ipAddress, 80);

            var peerFolder     = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager"));
            var addressManager = new PeerAddressManager(peerFolder);

            addressManager.AddPeer(addressOne, IPAddress.Loopback, PeerIntroductionType.Add);
            addressManager.AddPeer(addressTwo, IPAddress.Loopback, PeerIntroductionType.Add);
            addressManager.AddPeer(addressThree, IPAddress.Loopback, PeerIntroductionType.Add);
            addressManager.AddPeer(addressFour, IPAddress.Loopback, PeerIntroductionType.Add);

            var randomPeer = addressManager.SelectPeerToConnectTo(PeerIntroductionType.Add);

            addressManager.PeerAttempted(randomPeer.Endpoint, DateTimeOffset.Now);

            Assert.Null(addressManager.Peers.New(PeerIntroductionType.Add).FirstOrDefault(p => p.Match(randomPeer.Endpoint)));
        }
        public void CanDiscoverAndConnectToPeersOnTheNetwork()
        {
            var parameters = new NodeConnectionParameters();

            var testFolder = TestDirectory.Create("CanDiscoverAndConnectToPeersOnTheNetwork");

            var nodeSettings = new NodeSettings
            {
                DataDir = testFolder.FolderName
            };

            nodeSettings.DataFolder = new DataFolder(nodeSettings);

            var addressManager          = new PeerAddressManager(nodeSettings.DataFolder);
            var addressManagerBehaviour = new PeerAddressManagerBehaviour(new DateTimeProvider(), addressManager)
            {
                PeersToDiscover = 3
            };

            parameters.TemplateBehaviors.Add(addressManagerBehaviour);

            var peerDiscoveryLoop = new PeerDiscoveryLoop(
                new AsyncLoopFactory(new LoggerFactory()),
                Network.Main,
                parameters,
                new NodeLifetime(),
                addressManager);

            peerDiscoveryLoop.DiscoverPeers();

            //Wait until we have discovered 3 peers
            while (addressManager.Peers.Count < 3)
            {
                Task.Delay(TimeSpans.Second).GetAwaiter().GetResult();
            }

            var  peerOne = addressManager.SelectPeerToConnectTo(PeerIntroductionType.Discover);
            Node node    = Node.Connect(Network.Main, peerOne, parameters);

            node.VersionHandshake();
            node.Disconnect();

            var  peerTwo = addressManager.SelectPeerToConnectTo(PeerIntroductionType.Discover);
            Node node2   = Node.Connect(Network.Main, peerTwo, parameters);

            node.Disconnect();
        }
        public void CanDiscoverAndConnectToPeersOnTheNetwork()
        {
            var parameters = new NetworkPeerConnectionParameters();

            var testFolder = TestDirectory.Create("CanDiscoverAndConnectToPeersOnTheNetwork");

            var nodeSettings = new NodeSettings
            {
                DataDir = testFolder.FolderName
            };

            nodeSettings.DataFolder = new DataFolder(nodeSettings);

            var addressManager          = new PeerAddressManager(nodeSettings.DataFolder);
            var addressManagerBehaviour = new PeerAddressManagerBehaviour(new DateTimeProvider(), addressManager)
            {
                PeersToDiscover = 3
            };

            parameters.TemplateBehaviors.Add(addressManagerBehaviour);

            var loggerFactory = new ExtendedLoggerFactory();

            loggerFactory.AddConsoleWithFilters();

            INetworkPeerFactory networkPeerFactory = new NetworkPeerFactory(DateTimeProvider.Default, loggerFactory);
            var peerDiscoveryLoop = new PeerDiscoveryLoop(
                new AsyncLoopFactory(loggerFactory),
                Network.Main,
                parameters,
                new NodeLifetime(),
                addressManager,
                networkPeerFactory);

            peerDiscoveryLoop.DiscoverPeers();

            // Wait until we have discovered 3 peers.
            TestHelper.WaitLoop(() => addressManager.Peers.Count > 3);

            // Wait until at least one successful connection
            // has been made.
            while (true)
            {
                try
                {
                    var         peerOne = addressManager.SelectPeerToConnectTo(PeerIntroductionType.Discover);
                    NetworkPeer node    = networkPeerFactory.CreateConnectedNetworkPeer(Network.Main, peerOne, parameters);
                    node.VersionHandshake();
                    node.Disconnect();

                    break;
                }
                catch
                {
                }
            }
            ;
        }