diff --git a/src/Neo/Cryptography/BloomFilter.cs b/src/Neo/Cryptography/BloomFilter.cs index 73141d9e11..30cab45392 100644 --- a/src/Neo/Cryptography/BloomFilter.cs +++ b/src/Neo/Cryptography/BloomFilter.cs @@ -41,12 +41,13 @@ public class BloomFilter /// /// Initializes a new instance of the class. /// - /// The size of the bit array used by the bloom filter. - /// The number of hash functions used by the bloom filter. + /// The size of the bit array used by the bloom filter, and must be greater than 0. + /// The number of hash functions used by the bloom filter, and must be greater than 0. /// Used to generate the seeds of the murmur hash functions. + /// Thrown when or is less than or equal to 0. public BloomFilter(int m, int k, uint nTweak) { - if (k < 0 || m < 0) throw new ArgumentOutOfRangeException(); + if (k <= 0 || m <= 0) throw new ArgumentOutOfRangeException(); seeds = Enumerable.Range(0, k).Select(p => (uint)p * 0xFBA4C795 + nTweak).ToArray(); bits = new BitArray(m) { @@ -58,13 +59,14 @@ public BloomFilter(int m, int k, uint nTweak) /// /// Initializes a new instance of the class. /// - /// The size of the bit array used by the bloom filter. - /// The number of hash functions used by the bloom filter. + /// The size of the bit array used by the bloom filter, and must be greater than 0. + /// The number of hash functions used by the bloom filter, and must be greater than 0. /// Used to generate the seeds of the murmur hash functions. /// The initial elements contained in this object. + /// Thrown when or is less than or equal to 0. public BloomFilter(int m, int k, uint nTweak, ReadOnlyMemory elements) { - if (k < 0 || m < 0) throw new ArgumentOutOfRangeException(); + if (k <= 0 || m <= 0) throw new ArgumentOutOfRangeException(); seeds = Enumerable.Range(0, k).Select(p => (uint)p * 0xFBA4C795 + nTweak).ToArray(); bits = new BitArray(elements.ToArray()) { diff --git a/tests/Neo.UnitTests/Cryptography/UT_BloomFilter.cs b/tests/Neo.UnitTests/Cryptography/UT_BloomFilter.cs index f7bc1789dd..8693157724 100644 --- a/tests/Neo.UnitTests/Cryptography/UT_BloomFilter.cs +++ b/tests/Neo.UnitTests/Cryptography/UT_BloomFilter.cs @@ -78,5 +78,16 @@ public void TestGetBits() foreach (byte value in result) value.Should().Be(0); } + + [TestMethod] + public void TestInvalidArguments() + { + uint nTweak = 123456; + Action action = () => new BloomFilter(0, 3, nTweak); + action.Should().Throw(); + + action = () => new BloomFilter(3, 0, nTweak); + action.Should().Throw(); + } } }