added check that raises BlockExists if id exists in an array of block checksums
This commit is contained in:
parent
2797334059
commit
6acde52d2d
@ -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)
|
||||
|
||||
|
@ -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()
|
Loading…
Reference in New Issue
Block a user