def valid_addr(wallet_addr: str): wallet_addr = wallet_addr.lower().strip() if not wallet_addr.startswith("0x"): wallet_addr = "0x" + wallet_addr if len(wallet_addr) != 2 + crypto.ADDRESS_STR_LENGTH: return None # noinspection PyBroadException try: crypto.hex_str_to_bytes(wallet_addr) except: return None return wallet_addr
def current_work(): latest_work = pow.PowWork.get_latest_work() block_num = 0 difficulty = 0 start_time = None if latest_work: block_num = latest_work.block_num start_time = latest_work.start_time difficulty = ethash.boundary_to_difficulty( crypto.hex_str_to_bytes(latest_work.boundary)) now = datetime.utcnow() secs_next_pow = pow.PowWork.calc_seconds_to_next_pow() next_pow_time = now + timedelta(seconds=secs_next_pow) return { "block_num": block_num, "difficulty": difficulty, "utc_time": utils.iso_format(now), "start_time": utils.iso_format(start_time), "next_pow_time": utils.iso_format(next_pow_time), "avg_hashrate": miner.HashRate.epoch_hashrate(block_num), "avg_pow_fee": pow.PowWork.avg_pow_fee(block_num), }
def node_stats(pub_key: str): pub_key = crypto.bytes_to_hex_str_0x(crypto.hex_str_to_bytes(pub_key)) node = zilnode.ZilNode.get_by_pub_key(pub_key, authorized=None) if node: working_q = Q(expire_time__gte=datetime.utcnow()) & Q(finished=False) return { "pub_key": node.pub_key, "pow_fee": node.pow_fee, "authorized": node.authorized, "works": node.works_stats(), }
def test_pow(self): block_num = 22 header = crypto.hex_str_to_bytes("372eca2454ead349c3df0ab5d00b0b706b23e49d469387db91811cee0358fc6d") excepted_result = crypto.hex_str_to_bytes("00000b184f1fdd88bfd94c86c39e65db0c36144d5e43f745f722196e730cb614") excepted_mix = b'/t\xcd\xeb\x19\x8a\xf0\xb9\xab\xe6]"\xd3r\xe2/\xb2\xd4t7\x17t\xa9X<\x1c\xc4\'\xa0y9\xf5' nonce = 0x495732e0ed7a801c boundary20 = ethash.difficulty_to_boundary(20) boundary21 = ethash.difficulty_to_boundary(21) calc_mix_digest, calc_result = ethash.pow_hash(block_num, header, nonce) assert calc_result == excepted_result assert calc_mix_digest == excepted_mix assert ethash.verify_pow_work(block_num, header, excepted_mix, nonce, boundary20) assert not ethash.verify_pow_work(block_num, header, excepted_mix, nonce, boundary21) assert ethash.verify_pow_work(0, header, excepted_mix, nonce, boundary20) assert ethash.verify_pow_work(29999, header, excepted_mix, nonce, boundary20) assert not ethash.verify_pow_work(30000, header, excepted_mix, nonce, boundary20) assert not ethash.verify_pow_work(30001, header, excepted_mix, nonce, boundary20)
def test_hex_str(self): hex_str = "DEADBEEF" hex_str_odd = "deadbee" bin_bytes = b"\xde\xad\xbe\xef" bin_bytes_odd = b"\x0d\xea\xdb\xee" assert crypto.hex_str_to_bytes(hex_str) == bin_bytes assert crypto.bytes_to_hex_str(bin_bytes) == hex_str.lower() assert crypto.hex_str_to_int(hex_str) == 0xDEADBEEF assert crypto.bytes_to_int(bin_bytes) == 0xdeadbeef assert crypto.hex_str_to_int(hex_str_odd) == 0xDEADBEE assert crypto.bytes_to_int(bin_bytes_odd) == 0xDEADBEE
def node_stats(pub_key: str): pub_key = crypto.bytes_to_hex_str_0x(crypto.hex_str_to_bytes(pub_key)) node = zilnode.ZilNode.get_by_pub_key(pub_key, authorized=None) if node: working_q = Q(expire_time__gte=datetime.utcnow()) & Q(finished=False) return { "pub_key": node.pub_key, "pow_fee": node.pow_fee, "authorized": node.authorized, "works": { "all": pow.PowWork.count(pub_key=pub_key), "working": pow.PowWork.count(working_q, pub_key=pub_key), "finished": pow.PowWork.count(pub_key=pub_key, finished=True), "verified": pow.PowResult.count(pub_key=pub_key, verified=True), } }
def verify_signature(pub_key, signature, *parameters): if zil_config["verify_sign"] is False: return True key = crypto.ZilKey(str_public=pub_key) msg_to_verify = b"" for param in parameters: if isinstance(param, bytes): b_param = param elif isinstance(param, str): b_param = crypto.hex_str_to_bytes(param) elif isinstance(param, bool): b_param = b"\x01" if param else b"\x00" elif isinstance(param, int): b_param = crypto.int_to_bytes(param, n_bytes=8) else: logging.warning(f"wrong data type") return False msg_to_verify += b_param return key.verify(signature, msg_to_verify)