async def __internal_handle_metadata_and_raw_data(self, metadata, raw_data): try: await self.handle_metadata_and_raw_data(metadata, raw_data) except Exception as e: Logger.log_exception() self.close_with_error( "{}: error processing request: {}".format(self.name, e) )
async def submit_work(self, branch: Branch, header_hash: bytes, nonce: int, mixhash: bytes) -> Optional[bool]: try: return await self.shards[branch].miner.submit_work( header_hash, nonce, mixhash) except Exception: Logger.log_exception() return None
async def get_work(self, branch: Branch) -> Optional[MiningWork]: shard = self.shards.get(branch, None) if not shard: return None try: return await shard.miner.get_work() except Exception: Logger.log_exception() return None
async def handle_submit_work(self, req: SubmitWorkRequest) -> SubmitWorkResponse: try: res = await self.slave_server.submit_work( req.branch, req.header_hash, req.nonce, req.mixhash ) except Exception: Logger.log_exception() return SubmitWorkResponse(error_code=1, success=False) return SubmitWorkResponse(error_code=0, success=res)
async def get_work(self, branch: Branch) -> Optional[MiningWork]: try: shard = self.shards[branch] work, block = await shard.miner.get_work() if shard.state.shard_config.POSW_CONFIG.ENABLED: check(isinstance(block, MinorBlock)) diff = shard.state.posw_diff_adjust(block) work = MiningWork(work.hash, work.height, diff) return work except Exception: Logger.log_exception() return None
async def handle_add_root_block_request(self, req): # TODO: handle expect_switch error_code = 0 switched = False for shard in self.shards.values(): try: switched = await shard.add_root_block(req.root_block) except ValueError: Logger.log_exception() return AddRootBlockResponse(errno.EBADMSG, False) await self.slave_server.create_shards(req.root_block) return AddRootBlockResponse(error_code, switched)
async def loop_once(self): try: metadata, raw_data = await self.read_metadata_and_raw_data() if metadata is None: # Hit EOF self.close() return except Exception as e: Logger.log_exception() self.close_with_error("{}: error reading request: {}".format(self.name, e)) return asyncio.ensure_future( self.__internal_handle_metadata_and_raw_data(metadata, raw_data) )
async def connect_seed(self, ip, port): peer = await self.connect(ip, port) if peer is None: # Fail to connect return # Make sure the peer is ready for incoming messages await peer.wait_until_active() try: op, resp, rpc_id = await peer.write_rpc_request( CommandOp.GET_PEER_LIST_REQUEST, GetPeerListRequest(10)) except Exception as e: Logger.log_exception() return Logger.info("connecting {} peers ...".format(len(resp.peer_info_list))) for peer_info in resp.peer_info_list: asyncio.ensure_future( self.connect(str(ipaddress.ip_address(peer_info.ip)), peer_info.port))
async def sync(self, notify_sync: Callable): try: await self.__run_sync(notify_sync) except Exception as e: Logger.log_exception() self.shard_conn.close_with_error(str(e))
async def sync(self): try: await self.__run_sync() except Exception as e: Logger.log_exception() self.shard_conn.close_with_error(str(e))