Example #1
0
        public void AllEntriesExpired()
        {
            var invalidBlockHashStore = new InvalidBlockHashStore(DateTimeProvider.Default, 6);

            // Create some hashes that will be banned temporarily, but not after 5 seconds.
            var hashesBannedTemporarily = new uint256[]
            {
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000011"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000012"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000013"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000014"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000015"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000016"),
            };

            foreach (uint256 hash in hashesBannedTemporarily)
            {
                invalidBlockHashStore.MarkInvalid(hash, DateTime.UtcNow.AddMilliseconds(rng.Next(2000, 5000)));
            }

            // Check all hashes are banned now.
            foreach (uint256 hash in hashesBannedTemporarily)
            {
                Assert.True(invalidBlockHashStore.IsInvalid(hash));
            }

            // Wait 5 seconds and touch all the entries, so they are removed from the store's dictionary.
            Thread.Sleep(5000);

            // Check all hashes are no longer banned.
            foreach (uint256 hash in hashesBannedTemporarily)
            {
                Assert.False(invalidBlockHashStore.IsInvalid(hash));
            }

            // Add a new hash, which should remove all the other entries from the store's circular array as well.
            uint256 lastHash = uint256.Parse("0000000000000000000000000000000000000000000000000000000000000031");

            invalidBlockHashStore.MarkInvalid(lastHash);

            // Check all removed hashes are no longer banned.
            foreach (uint256 hash in hashesBannedTemporarily)
            {
                Assert.False(invalidBlockHashStore.IsInvalid(hash));
            }

            // Check the number of entries is now 1.
            var orderedHashList = invalidBlockHashStore.GetMemberValue("orderedHashList") as CircularArray <uint256>;

            Assert.Equal(1, orderedHashList.Count);

            // Check the last entry is banned.
            Assert.True(invalidBlockHashStore.IsInvalid(lastHash));
        }
Example #2
0
        public void ReachingStoreCapacity_CircularArraySkipsExpiredEntries()
        {
            var invalidBlockHashStore = new InvalidBlockHashStore(DateTimeProvider.Default, 10);

            // Create some hashes that will be banned forever.
            var hashesBannedPermanently1 = new uint256[]
            {
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000001"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000002"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000003"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000004"),
            };

            foreach (uint256 hash in hashesBannedPermanently1)
            {
                invalidBlockHashStore.MarkInvalid(hash);
            }

            // Create some hashes that will be banned temporarily, but not after 5 seconds.
            var hashesBannedTemporarily = new uint256[]
            {
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000011"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000012"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000013"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000014"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000015"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000016"),
            };

            foreach (uint256 hash in hashesBannedTemporarily)
            {
                invalidBlockHashStore.MarkInvalid(hash, DateTime.UtcNow.AddMilliseconds(rng.Next(2000, 5000)));
            }

            // Add more forever bans.
            var hashesBannedPermanently2 = new uint256[]
            {
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000021"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000022"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000023"),
                uint256.Parse("0000000000000000000000000000000000000000000000000000000000000024"),
            };

            foreach (uint256 hash in hashesBannedPermanently2)
            {
                invalidBlockHashStore.MarkInvalid(hash);
            }

            // Now check that all hashes from the first group are no longer banned and all others still are.
            var allBannedHashes = new List <uint256>(hashesBannedTemporarily);

            allBannedHashes.AddRange(hashesBannedPermanently2);

            foreach (uint256 hash in hashesBannedPermanently1)
            {
                Assert.False(invalidBlockHashStore.IsInvalid(hash));
            }

            foreach (uint256 hash in allBannedHashes)
            {
                Assert.True(invalidBlockHashStore.IsInvalid(hash));
            }

            // Now we wait 5 seconds and touch the first four temporarily banned hashes,
            // which should remove them from the dictionary.
            Thread.Sleep(5000);

            Assert.False(invalidBlockHashStore.IsInvalid(hashesBannedTemporarily[0]));
            Assert.False(invalidBlockHashStore.IsInvalid(hashesBannedTemporarily[1]));
            Assert.False(invalidBlockHashStore.IsInvalid(hashesBannedTemporarily[2]));
            Assert.False(invalidBlockHashStore.IsInvalid(hashesBannedTemporarily[3]));

            // Then we add a new hash, which should remove the four hashes from the circular array as well.
            invalidBlockHashStore.MarkInvalid(uint256.Parse("0000000000000000000000000000000000000000000000000000000000000031"));

            var orderedHashList = invalidBlockHashStore.GetMemberValue("orderedHashList") as CircularArray <uint256>;

            Assert.Equal(6, orderedHashList.Count);
        }