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());
        }
Пример #2
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());
        }
Пример #3
0
        public void PeerFile_CanSaveAndLoadPeers_PeerConnected()
        {
            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, this.loggerFactory);

            addressManager.AddPeer(networkAddress, IPAddress.Loopback);

            var applicableDate = DateTime.UtcNow.Date;

            addressManager.PeerAttempted(networkAddress.Endpoint, applicableDate);
            addressManager.PeerConnected(networkAddress.Endpoint, applicableDate);

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

            var savedPeer = addressManager.FindPeer(networkAddress.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.Null(savedPeer.LastConnectionHandshake);
            Assert.Equal("127.0.0.1", savedPeer.Loopback.ToString());
        }
Пример #4
0
        public void PeerFile_CanSaveAndLoadPeers_PeerConnected()
        {
            var ipAddress = IPAddress.Parse("::ffff:192.168.0.1");
            var endpoint  = new IPEndPoint(ipAddress, 80);

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

            addressManager.AddPeer(endpoint, IPAddress.Loopback);

            var applicableDate = DateTime.UtcNow.Date;

            addressManager.PeerAttempted(endpoint, applicableDate);
            addressManager.PeerConnected(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.Null(savedPeer.LastConnectionHandshake);
            Assert.Equal("127.0.0.1", savedPeer.Loopback.ToString());
        }
Пример #5
0
        public void PeerFile_CanSaveAndLoadPeers_ResetBannedPeers()
        {
            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(this.LoggerFactory.Object, this.connectionManagerSettings));
            var addedPeer      = addressManager.AddPeer(endpoint, IPAddress.Loopback);

            addedPeer.BanReason    = "test";
            addedPeer.BanScore     = 0;
            addedPeer.BanTimeStamp = DateTime.UtcNow.AddHours(-2);
            addedPeer.BanUntil     = DateTime.UtcNow.AddHours(-1);

            addressManager.SavePeers();

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

            PeerAddress savedPeer = addressManager.FindPeer(endpoint);

            Assert.Null(savedPeer.BanReason);
            Assert.Null(savedPeer.BanScore);
            Assert.Null(savedPeer.BanTimeStamp);
            Assert.Null(savedPeer.BanUntil);
        }
        public void PeerBanning_Resetting_Expired_BannedPeer()
        {
            DataFolder dataFolder = CreateDataFolder(this);

            var loggerFactory = new ExtendedLoggerFactory();

            loggerFactory.AddConsoleWithFilters();

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

            var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, dataFolder, loggerFactory, new Mock <ISelfEndpointTracker>().Object);

            peerAddressManager.AddPeer(endpoint, IPAddress.Loopback);

            var nodeSettings = new NodeSettings(new StratisRegTest());
            var connectionManagerSettings = new ConnectionManagerSettings(nodeSettings);

            var peerCollection = new Mock <IReadOnlyNetworkPeerCollection>();

            peerCollection.Setup(p => p.FindByIp(It.IsAny <IPAddress>())).Returns(new List <INetworkPeer>());

            var connectionManager = new Mock <IConnectionManager>();

            connectionManager.Setup(c => c.ConnectionSettings).Returns(connectionManagerSettings);
            connectionManager.Setup(c => c.ConnectedPeers).Returns(peerCollection.Object);

            var peerBanning = new PeerBanning(connectionManager.Object, loggerFactory, DateTimeProvider.Default, peerAddressManager);

            peerBanning.BanAndDisconnectPeer(endpoint, 1, nameof(PeerBanningTest));

            peerAddressManager.SavePeers();

            // Wait one second for ban to expire.
            Thread.Sleep(1000);

            peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, dataFolder, loggerFactory, new Mock <ISelfEndpointTracker>().Object);
            peerAddressManager.LoadPeers();

            PeerAddress peer = peerAddressManager.FindPeer(endpoint);

            Assert.Null(peer.BanTimeStamp);
            Assert.Null(peer.BanUntil);
            Assert.Empty(peer.BanReason);
        }
Пример #7
0
        public void PeerFile_CanSaveAndLoadPeers_ResetAttemptThresholdReachedPeers()
        {
            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(this.LoggerFactory.Object, this.connectionManagerSettings));

            addressManager.AddPeer(endpoint, IPAddress.Loopback);

            DateTime applicableDate = DateTimeProvider.Default.GetUtcNow();

            // Set the peer's failed attempts count to 4.
            for (int i = 0; i < 4; i++)
            {
                addressManager.PeerAttempted(endpoint, applicableDate.AddHours(-i));
            }

            // Ensure that the last attempt (5) was more than 12 hours ago.
            addressManager.PeerAttempted(endpoint, applicableDate.AddHours(-13));

            // Ensure that the peer is still returned from the selector.
            var peer = addressManager.PeerSelector.SelectPeer();

            Assert.Equal(peer.Endpoint, endpoint);

            // Persist the peers to the json file.
            addressManager.SavePeers();

            // Creat a new address manager instance and load the peers from file.
            addressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.LoggerFactory.Object, new SelfEndpointTracker(this.LoggerFactory.Object, this.connectionManagerSettings));
            addressManager.LoadPeers();

            PeerAddress savedPeer = addressManager.FindPeer(endpoint);

            // The peer's attempt thresholds should now be reset.
            Assert.False(savedPeer.Attempted);
            Assert.Equal(0, savedPeer.ConnectionAttempts);
            Assert.Null(savedPeer.LastAttempt);
        }
Пример #8
0
        public void PeerBanning_SavingAndLoading_BannedPeerToAddressManager()
        {
            DataFolder dataFolder = CreateDataFolder(this);

            var loggerFactory = ExtendedLoggerFactory.Create();

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

            var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, dataFolder, loggerFactory, new Mock <ISelfEndpointTracker>().Object);

            peerAddressManager.AddPeer(endpoint, IPAddress.Loopback);

            var nodeSettings = new NodeSettings(new StraxTest());
            var connectionManagerSettings = new ConnectionManagerSettings(nodeSettings);

            var peerCollection = new Mock <IReadOnlyNetworkPeerCollection>();

            peerCollection.Setup(p => p.FindByIp(It.IsAny <IPAddress>())).Returns(new List <INetworkPeer>());

            var connectionManager = new Mock <IConnectionManager>();

            connectionManager.Setup(c => c.ConnectionSettings).Returns(connectionManagerSettings);
            connectionManager.Setup(c => c.ConnectedPeers).Returns(peerCollection.Object);

            var peerBanning = new PeerBanning(connectionManager.Object, loggerFactory, DateTimeProvider.Default, peerAddressManager);

            peerBanning.BanAndDisconnectPeer(endpoint, connectionManagerSettings.BanTimeSeconds, nameof(PeerBanningTest));

            peerAddressManager.SavePeers();
            peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, dataFolder, loggerFactory, new Mock <ISelfEndpointTracker>().Object);
            peerAddressManager.LoadPeers();

            PeerAddress peer = peerAddressManager.FindPeer(endpoint);

            Assert.NotNull(peer.BanTimeStamp);
            Assert.NotNull(peer.BanUntil);
            Assert.NotEmpty(peer.BanReason);
        }