added check that raises BlockExists if id exists in an array of block checksums

This commit is contained in:
Kevin Froman 2020-12-22 22:34:38 +00:00
parent 2797334059
commit 6acde52d2d
2 changed files with 25 additions and 1 deletions

View File

@ -2,6 +2,9 @@ from kasten import Kasten
from time import time from time import time
from math import floor from math import floor
from typing import Iterable
import binascii
MAX_FUTURE_SKEW_SECONDS = 60 * 2 MAX_FUTURE_SKEW_SECONDS = 60 * 2
TOTAL_MAX_SIZE = 6 * (10**6) TOTAL_MAX_SIZE = 6 * (10**6)
@ -11,17 +14,25 @@ class BlockRulesException(Exception):
pass pass
class BlockExpired(Exception): class BlockExpired(BlockRulesException):
pass pass
class BlockFutureSkewedBeyondMax(BlockRulesException): class BlockFutureSkewedBeyondMax(BlockRulesException):
pass pass
class BlockExistsError(BlockRulesException):
pass
class BlockTooLarge(BlockRulesException): class BlockTooLarge(BlockRulesException):
pass 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): def check_block_sanity(raw_bytes):
kasten: Kasten = Kasten(None, raw_bytes, None, auto_check_generator=False) kasten: Kasten = Kasten(None, raw_bytes, None, auto_check_generator=False)

View File

@ -1,5 +1,6 @@
import unittest import unittest
from onionrblocks import universalrules from onionrblocks import universalrules
from onionrblocks.generators import anonvdf
import kasten import kasten
from time import time from time import time
@ -38,4 +39,16 @@ class TestUniversalRules(unittest.TestCase):
universalrules.BlockExpired, universalrules.BlockExpired,
universalrules.check_block_sanity, packed) 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() unittest.main()