def make_work_request(self, work, diff): timeout = crypto.int_to_bytes(self.pow_timeout, n_bytes=4) boundary = ethash.difficulty_to_boundary(diff) # requests are bytes bytes_reqs = [ self.key.keypair_bytes.public, # 33 bytes work["header"], # 32 bytes work["block_num"], # 8 bytes boundary, # 32 bytes timeout, # 4 bytes ] # convert to bytes to sign bytes_to_sign = b"".join(bytes_reqs) assert len(bytes_to_sign) == 33 + 32 * 2 + 8 + 4 signature = self.key.sign( bytes_to_sign) # signature is hex string, len 128 assert len(signature) == 128 signature = "0x" + signature # convert to hex string starts with "0x" req = [crypto.bytes_to_hex_str_0x(b) for b in bytes_reqs] # append signature req.append(signature) return req
def make_verify_request(self, work, diff, verify=True): boundary = ethash.difficulty_to_boundary(diff) byte_verify = b"\x01" if verify else b"\x00" # requests are bytes bytes_reqs = [ self.key.keypair_bytes.public, # 33 bytes byte_verify, # 1 bytes work["header"], # 32 bytes boundary, # 32 bytes ] # convert to bytes to sign bytes_to_sign = b"".join(bytes_reqs) assert len(bytes_to_sign) == 33 + 1 + 32 * 2 signature = self.key.sign( bytes_to_sign) # signature is hex string, len 128 assert len(signature) == 128 signature = "0x" + signature # convert to hex string starts with "0x" req = [crypto.bytes_to_hex_str_0x(b) for b in bytes_reqs] # append signature req.append(signature) return req
def new_work(cls, header: str, block_num: int, boundary: str, pub_key="", signature="", timeout=120): start_time = datetime.utcnow() expire_time = start_time + timedelta(seconds=timeout) seed = ethash.block_num_to_seed(block_num) seed = crypto.bytes_to_hex_str_0x(seed) return cls(header=header, seed=seed, boundary=boundary, pub_key=pub_key, signature=signature, block_num=block_num, start_time=start_time, expire_time=expire_time)
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 add_new_work(block_num, difficulty, timeout, node=0): print(f"Generate a work for block {block_num}, difficulty {difficulty}") header = crypto.rand_hex_str_0x(64) seed = crypto.bytes_to_hex_str_0x(ethash.block_num_to_seed(block_num)) boundary = crypto.bytes_to_hex_str_0x(ethash.difficulty_to_boundary_divided(difficulty)) print(f" header : {header}") print(f" seed : {seed}") print(f" boundary : {boundary}") pub_key = debug_data.nodes[node][0] print(f"save work, timeout = {timeout}, pub_key = {pub_key}") work = pow.PowWork.new_work(header, seed, boundary, pub_key=pub_key, signature="", timeout=timeout) work = work.save() if work: print(f"success, {work}") else: print(f"failed")
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), } }