public void PeerState_TestReturnFromPeerHandshakedSet()
        {
            var ipAddress         = IPAddress.Parse("::ffff:192.168.0.1");
            var networkAddressOne = new NetworkAddress(ipAddress, 80);

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

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

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

            var peerAddressManager = new PeerAddressManager(peerFolder, this.extendedLoggerFactory);

            peerAddressManager.AddPeer(networkAddressOne, IPAddress.Loopback);
            peerAddressManager.AddPeer(networkAddressTwo, IPAddress.Loopback);
            peerAddressManager.AddPeer(networkAddressThree, IPAddress.Loopback);

            peerAddressManager.PeerConnected(networkAddressOne.Endpoint, DateTime.UtcNow);
            peerAddressManager.PeerHandshaked(networkAddressOne.Endpoint, DateTime.UtcNow);

            peerAddressManager.PeerConnected(networkAddressTwo.Endpoint, DateTime.UtcNow.AddSeconds(-80));
            peerAddressManager.PeerHandshaked(networkAddressTwo.Endpoint, DateTime.UtcNow.AddSeconds(-80));

            peerAddressManager.PeerAttempted(networkAddressThree.Endpoint, DateTime.UtcNow);

            var peers = peerAddressManager.PeerSelector.Handshaked();

            Assert.Single(peers);
            Assert.Contains(peers, p => p.EndPoint.Match(networkAddressTwo.Endpoint));
        }
        public void PeerState_TestReturnFromPeerHandshakedSet_IgnoringBanned()
        {
            IPAddress ipAddress   = IPAddress.Parse("::ffff:192.168.0.1");
            var       endPointOne = new IPEndPoint(ipAddress, 80);

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

            DataFolder peerFolder = CreateDataFolder(this);

            var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.extendedLoggerFactory, new SelfEndpointTracker());

            peerAddressManager.AddPeer(endPointOne, IPAddress.Loopback);
            peerAddressManager.AddPeer(endPointTwo, IPAddress.Loopback);

            peerAddressManager.FindPeer(endPointTwo).BanUntil = DateTime.UtcNow.AddMinutes(1);

            peerAddressManager.PeerConnected(endPointOne, DateTime.UtcNow.AddSeconds(-80));
            peerAddressManager.PeerHandshaked(endPointOne, DateTime.UtcNow.AddSeconds(-80));

            peerAddressManager.PeerConnected(endPointTwo, DateTime.UtcNow.AddSeconds(-80));
            peerAddressManager.PeerHandshaked(endPointTwo, DateTime.UtcNow.AddSeconds(-80));

            IEnumerable <PeerAddress> peers = peerAddressManager.PeerSelector.Handshaked();

            Assert.Single(peers);
            Assert.Contains(peers, p => p.Endpoint.Match(endPointOne));
        }
        public void PeerState_TestReturnFromPeerHandshakedSet()
        {
            IPAddress ipAddress   = IPAddress.Parse("::ffff:192.168.0.1");
            var       endPointOne = new IPEndPoint(ipAddress, 80);

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

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

            DataFolder         peerFolder         = CreateDataFolder(this);
            PeerAddressManager peerAddressManager = this.CreatePeerAddressManager(peerFolder);

            peerAddressManager.AddPeer(endPointOne, IPAddress.Loopback);
            peerAddressManager.AddPeer(endPointTwo, IPAddress.Loopback);
            peerAddressManager.AddPeer(endPointThree, IPAddress.Loopback);

            peerAddressManager.PeerConnected(endPointOne, DateTime.UtcNow);
            peerAddressManager.PeerHandshaked(endPointOne, DateTime.UtcNow);

            peerAddressManager.PeerConnected(endPointTwo, DateTime.UtcNow.AddSeconds(-80));
            peerAddressManager.PeerHandshaked(endPointTwo, DateTime.UtcNow.AddSeconds(-80));

            peerAddressManager.PeerAttempted(endPointThree, DateTime.UtcNow);

            IEnumerable <PeerAddress> peers = peerAddressManager.PeerSelector.Handshaked();

            Assert.Single(peers);
            Assert.Contains(peers, p => p.Endpoint.Match(endPointTwo));
        }
        public void PeerSelector_ReturnPeersForGetAddrPayload_Scenario2()
        {
            var peersToAdd = new List <NetworkAddress>();

            for (int i = 1; i <= 15; i++)
            {
                var ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i));
                peersToAdd.Add(new NetworkAddress(ipAddress, 80));
            }

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

            var peerAddressManager = new PeerAddressManager(peerFolder, this.extendedLoggerFactory);

            peerAddressManager.AddPeers(peersToAdd.ToArray(), IPAddress.Loopback);

            for (int i = 1; i <= 7; i++)
            {
                var ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i));
                peerAddressManager.PeerConnected(new NetworkAddress(ipAddress, 80).Endpoint, DateTime.UtcNow.AddSeconds(-80));
                peerAddressManager.PeerHandshaked(new NetworkAddress(ipAddress, 80).Endpoint, DateTime.UtcNow.AddSeconds(-80));
            }

            var peers = peerAddressManager.PeerSelector.SelectPeersForGetAddrPayload(15);

            Assert.Equal(15, peers.Count());
            Assert.Equal(7, peers.Count(p => p.Handshaked));
            Assert.Equal(8, peers.Count(p => p.Fresh));
        }
        public void PeerFile_CanSaveAndLoadPeers_PeerSeen()
        {
            var ipAddress = IPAddress.Parse("::ffff:192.168.0.1");
            var endpoint  = new IPEndPoint(ipAddress, 80);

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

            var addressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.loggerFactory, new SelfEndpointTracker());

            addressManager.AddPeer(endpoint, IPAddress.Loopback);

            var applicableDate = DateTime.UtcNow.Date;

            addressManager.PeerAttempted(endpoint, applicableDate);
            addressManager.PeerConnected(endpoint, applicableDate);
            addressManager.PeerHandshaked(endpoint, applicableDate);
            addressManager.PeerSeen(endpoint, applicableDate);

            addressManager.SavePeers();
            addressManager.LoadPeers();

            var savedPeer = addressManager.FindPeer(endpoint);

            Assert.Equal("::ffff:192.168.0.1", savedPeer.Endpoint.Address.ToString());
            Assert.Equal(80, savedPeer.Endpoint.Port);
            Assert.Equal(0, savedPeer.ConnectionAttempts);
            Assert.Equal(applicableDate, savedPeer.LastConnectionSuccess.Value.Date);
            Assert.Equal(applicableDate, savedPeer.LastConnectionHandshake.Value.Date);
            Assert.Equal(applicableDate, savedPeer.LastSeen.Value.Date);
            Assert.Equal("127.0.0.1", savedPeer.Loopback.ToString());
        }
예제 #6
0
        private IPeerAddressManager CreateTestPeerAddressManager(List <Tuple <NetworkAddress, DateTimeOffset> > testDataSet)
        {
            ConcurrentDictionary <IPEndPoint, PeerAddress> peers = new ConcurrentDictionary <IPEndPoint, PeerAddress>();

            string dataFolderDirectory = Path.Combine(AppContext.BaseDirectory, "WhitelistTests");

            if (Directory.Exists(dataFolderDirectory))
            {
                Directory.Delete(dataFolderDirectory, true);
            }
            Directory.CreateDirectory(dataFolderDirectory);

            var peerFolder = new DataFolder(new NodeSettings {
                DataDir = dataFolderDirectory
            });

            Mock <ILogger>        mockLogger        = new Mock <ILogger>();
            Mock <ILoggerFactory> mockLoggerFactory = new Mock <ILoggerFactory>();

            mockLoggerFactory.Setup(l => l.CreateLogger(It.IsAny <string>())).Returns(mockLogger.Object);
            ILoggerFactory loggerFactory = mockLoggerFactory.Object;

            IPeerAddressManager peerAddressManager = new PeerAddressManager(peerFolder, loggerFactory);

            foreach (Tuple <NetworkAddress, DateTimeOffset> testData in testDataSet)
            {
                peerAddressManager.AddPeer(testData.Item1, IPAddress.Loopback);
                peerAddressManager.PeerHandshaked(testData.Item1.Endpoint, testData.Item2);
            }

            return(peerAddressManager);
        }
예제 #7
0
        public void CanSaveAndLoadPeerAddressFile_PeerHandshaked()
        {
            var ipAddress      = IPAddress.Parse("::ffff:192.168.0.1");
            var networkAddress = new NetworkAddress(ipAddress, 80);

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

            var addressManager = new PeerAddressManager(peerFolder);

            addressManager.AddPeer(networkAddress, IPAddress.Loopback, PeerIntroductionType.Add);

            addressManager.PeerAttempted(networkAddress.Endpoint, DateTimeOffset.Now);
            addressManager.PeerConnected(networkAddress.Endpoint, DateTimeOffset.Now);
            addressManager.PeerHandshaked(networkAddress.Endpoint, DateTimeOffset.Now);

            addressManager.SavePeers();
            addressManager.LoadPeers();

            var savedPeer = addressManager.FindPeer(networkAddress.Endpoint);

            Assert.Equal("::ffff:192.168.0.1", savedPeer.NetworkAddress.Endpoint.Address.ToString());
            Assert.Equal(DateTimeOffset.Now.Date, savedPeer.NetworkAddress.Time.Date);
            Assert.Equal(80, savedPeer.NetworkAddress.Endpoint.Port);
            Assert.Equal(0, savedPeer.ConnectionAttempts);
            Assert.Equal(DateTime.Today.Date, savedPeer.LastConnectionSuccess.Value.Date);
            Assert.Equal(DateTime.Today.Date, savedPeer.LastConnectionHandshake.Value.Date);
            Assert.Equal("127.0.0.1", savedPeer.Loopback.ToString());
        }
        public void PeerSelector_ReturnPeersForGetAddrPayload_Scenario2()
        {
            var peersToAdd = new List <IPEndPoint>();

            for (int i = 1; i <= 15; i++)
            {
                IPAddress ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i));
                peersToAdd.Add(new IPEndPoint(ipAddress, 80));
            }

            DataFolder         peerFolder         = CreateDataFolder(this);
            PeerAddressManager peerAddressManager = this.CreatePeerAddressManager(peerFolder);

            peerAddressManager.AddPeers(peersToAdd.ToArray(), IPAddress.Loopback);

            for (int i = 1; i <= 7; i++)
            {
                IPAddress ipAddress = IPAddress.Parse(string.Format("::ffff:192.168.0.{0}", i));
                peerAddressManager.PeerConnected(new IPEndPoint(ipAddress, 80), DateTime.UtcNow.AddSeconds(-80));
                peerAddressManager.PeerHandshaked(new IPEndPoint(ipAddress, 80), DateTime.UtcNow.AddSeconds(-80));
            }

            IEnumerable <PeerAddress> peers = peerAddressManager.PeerSelector.SelectPeersForGetAddrPayload(15);

            Assert.Equal(15, peers.Count());
            Assert.Equal(7, peers.Count(p => p.Handshaked));
            Assert.Equal(8, peers.Count(p => p.Fresh));
        }
예제 #9
0
        public void PeerFile_CanSaveAndLoadPeers_PeerHandshaked()
        {
            IPAddress ipAddress = IPAddress.Parse("::ffff:192.168.0.1");
            var       endpoint  = new IPEndPoint(ipAddress, 80);

            DataFolder peerFolder = CreateDataFolder(this);

            var addressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.LoggerFactory.Object, new SelfEndpointTracker());

            addressManager.AddPeer(endpoint, IPAddress.Loopback);

            DateTime applicableDate = DateTime.UtcNow.Date;

            addressManager.PeerAttempted(endpoint, applicableDate);
            addressManager.PeerConnected(endpoint, applicableDate);
            addressManager.PeerHandshaked(endpoint, applicableDate);

            addressManager.SavePeers();
            addressManager.LoadPeers();

            PeerAddress savedPeer = addressManager.FindPeer(endpoint);

            Assert.Equal("::ffff:192.168.0.1", savedPeer.Endpoint.Address.ToString());
            Assert.Equal(80, savedPeer.Endpoint.Port);
            Assert.Equal(0, savedPeer.ConnectionAttempts);
            Assert.Equal(applicableDate, savedPeer.LastConnectionSuccess.Value.Date);
            Assert.Equal(applicableDate, savedPeer.LastConnectionHandshake.Value.Date);
            Assert.Equal("127.0.0.1", savedPeer.Loopback.ToString());
        }