diff --git a/onionrblocks/universalrules.py b/onionrblocks/universalrules.py index f7b2e15..9c02c35 100644 --- a/onionrblocks/universalrules.py +++ b/onionrblocks/universalrules.py @@ -2,6 +2,9 @@ from kasten import Kasten from time import time from math import floor +from typing import Iterable + +import binascii MAX_FUTURE_SKEW_SECONDS = 60 * 2 TOTAL_MAX_SIZE = 6 * (10**6) @@ -11,17 +14,25 @@ class BlockRulesException(Exception): pass -class BlockExpired(Exception): +class BlockExpired(BlockRulesException): pass class BlockFutureSkewedBeyondMax(BlockRulesException): pass +class BlockExistsError(BlockRulesException): + pass class BlockTooLarge(BlockRulesException): pass +def checksum_exists_in_list(checksum: bytes, arr: 'Iterable'): + if checksum in arr: + raise BlockExistsError( + binascii.hexlify(checksum).decode("utf-8") + " Already exists in given set") + + def check_block_sanity(raw_bytes): kasten: Kasten = Kasten(None, raw_bytes, None, auto_check_generator=False) diff --git a/tests/test_universal_rules.py b/tests/test_universal_rules.py index 60fb1bf..90276df 100644 --- a/tests/test_universal_rules.py +++ b/tests/test_universal_rules.py @@ -1,5 +1,6 @@ import unittest from onionrblocks import universalrules +from onionrblocks.generators import anonvdf import kasten from time import time @@ -38,4 +39,16 @@ class TestUniversalRules(unittest.TestCase): universalrules.BlockExpired, universalrules.check_block_sanity, packed) + def test_not_in_set(self): + t = floor(time()) - 6 + bls = [] + for i in range(3): + packed = kasten.generator.pack.pack(b"1"*2*i, b"tst", 0, timestamp=t) + k = anonvdf.AnonVDFGenerator.generate(packed, 1000) + bls.append(k.id) + packed = kasten.generator.pack.pack(b"1"*2*1, b"tst", 0, timestamp=t) + k = anonvdf.AnonVDFGenerator.generate(packed, 1000) + self.assertRaises(universalrules.BlockExistsError, universalrules.checksum_exists_in_list, k.id, bls) + + unittest.main() \ No newline at end of file