def waitAnchor(self, anchor_id: int, timeout: int) -> Anchor: if not isinstance(anchor_id, int): raise TypeError('Invalid input type. Expecting "int".') anchor = None start = time.time() next_try_time = start + \ self.__config_service.getConfiguration().wait_record_interval_default/1000 timeout_time = start + timeout / 1000 old_fib = 0 fib = 1 while True: try: anchor = self.__anchor_repository.getAnchor(anchor_id) if anchor.status == 'Success': break except HttpRequestException: pass current_time = time.time() while current_time < next_try_time and current_time < timeout_time: Utils.sleep(200) current_time = time.time() if current_time > timeout_time: anchor = None break next_try_time += old_fib + fib aux = old_fib old_fib = fib fib = old_fib + aux return anchor
def test_stringify_not_serde(self): d = { 'test': 'Perpetual Testing Initiative', 'data': 'even more data', 'timestamp': datetime(2020, 4, 21, 11, 10, 11) } self.assertEqual( Utils.stringify(d), '{"data":"even more data","test":"Perpetual Testing Initiative","timestamp":"2020-04-21 11:10:11"}', 'Expecting different string.')
def isValid(proof) -> bool: ''' Checks whether the Proof was build with valid parameters or not. Parameters ---------- proof: Proof Proof to validate. Returns ------- bool A Boolean that returns True if the proof is valid, False if not. ''' if isinstance(proof, Proof): try: for l in proof.leaves: if not Utils.isHex(l) or len(l) != 64: return False for n in proof.nodes: if not Utils.isHex(n) or len(n) != 64: return False if (len(proof.depth) != (len(proof.leaves)+len(proof.nodes))*4) \ and Utils.isHex(proof.depth): return False n_elements = len(proof.leaves) + len(proof.nodes) if len(proof.depth) != (n_elements) * 4: return False offset = 1 if n_elements % 8 == 0: offset = 0 if len(proof.bitmap) // 2 < ( (n_elements + 8 * offset - n_elements % 8) // 8): return False return True except Exception: return False return False
def fromDict(data: dict): ''' Given a dictionary returns a Record with its value hashed. Parameters ---------- data : dict Dictionary to convert to Record. Returns ------- Record Record object of the hashed input. ''' return Record.fromString(Utils.stringify(data))
def fromString(string: str): ''' Given a string returns a Record with its value hashed. Parameters ---------- string : str String object. Returns ------- Record Record object of the hashed input. ''' dataArray = Utils.stringToBytes(string) return Record(Record.__hashAlgorithm.generateHash(dataArray))
def isValid(record) -> bool: ''' Given a Record returns True if its contents are valid to be sent to Bloocks's API or False otherwise. Parameters ---------- record : Record Record object. Returns ------- bool Boolean indicating if the Record is susceptible to be sent (True) or not (False). ''' if isinstance(record, Record): _record = record.getHash() if (len(_record) == 64 and Utils.isHex(_record)): return True return False
def test_stringify(self): d = {'test': 'Perpetual Testing Initiative', 'data': 'even more data'} self.assertEqual( Utils.stringify(d), '{"data":"even more data","test":"Perpetual Testing Initiative"}', 'Expecting different string.')
def test_is_hex_invalid_input(self): with self.assertRaises(TypeError): Utils.isHex(34)
def test_is_hex_invalid_char(self): self.assertFalse(Utils.isHex('gg'), 'The input was a hex, hence should be True.')
def test_is_hex_okay(self): self.assertTrue(Utils.isHex('0123456789abcdef'), 'The input was a hex, hence should be True.')