async def mine_new_block(): """Get a new block and start mining. If a mining process has already been started, update the process to mine the new block. """ block = await self.create_block_async_func() if not block: self.input_q.put((None, {})) return mining_params = self.get_mining_param_func() mining_params["consensus_type"] = self.consensus_type # handle mining simulation's timing if "target_block_time" in mining_params: target_block_time = mining_params["target_block_time"] mining_params["target_time"] = ( block.header.create_time + self._get_block_time(block, target_block_time)) work = MiningWork( block.header.get_hash_for_mining(), block.header.height, block.header.difficulty, ) self.work_map[work.hash] = block if self.process: self.input_q.put((work, mining_params)) return self.process = AioProcess( target=self.mine_loop, args=(work, mining_params, self.input_q, self.output_q), ) self.process.start() await handle_mined_block()
async def receive_file(self, receiver): self.progress_update.start() parent, child = AioPipe(False) start_time = time() receive_process = AioProcess(target=receiver.fetch_data, args=(self.pipe[1], child)) receive_process.start() await receive_process.coro_join() end_time = time() - start_time self.progress_update.wait() message = QMessageBox() message.information( self, "Information", f"Download complete, time taken {round(end_time / 60, 2)} minutes") receiver.save_location = parent.recv() self.ui.label_4.setText("Writing File") self.ui.progressBar.setValue(0) path = await receiver.write_data(receiver.save_location, self) rmtree(path) self.ui.receiveButton.setEnabled(True) self.ui.label_4.setVisible(False) self.ui.progressBar.setVisible(False) self.ui.label_4.setText("Download in Progress") self.ui.progressBar.setValue(0)
async def mine_new_block(instance: Miner): """Get a new block and start mining. If a mining process has already been started, update the process to mine the new block. """ block = await instance.create_block_async_func() mining_params = instance.get_mining_param_func() if instance.process: instance.input_q.put((block, mining_params)) return instance.process = AioProcess( target=instance.mine_func, args=(block, instance.input_q, instance.output_q, mining_params), ) instance.process.start() await handle_mined_block(instance)
def get_work(): # hash -> shard nonlocal work_map, self # shard -> work existing_work = {} # type: Dict[int, MiningWork] while True: for config in self.configs: shard_id = config["shard_id"] try: work = get_work_rpc(shard_id) except Exception: # ignore network errors and try next one logger.error("Failed to get work") continue # skip duplicate work if ( shard_id in existing_work and existing_work[shard_id].hash == work.hash ): continue mining_params = { "consensus_type": config["consensus_type"], "shard": shard_id, } if self.process: self.input_q.put((work, mining_params)) logger.info( "Pushed work to %s height %d" % (repr_shard(shard_id), work.height) ) else: # start the process to mine self.process = AioProcess( target=Miner.mine_loop, args=(work, mining_params, self.input_q, self.output_q), ) self.process.start() logger.info( "Started mining process for %s" % repr_shard(shard_id) ) # bookkeeping existing_work[shard_id] = work work_map[work.hash] = (work, shard_id) # random sleep 1~2 secs time.sleep(random.uniform(1.0, 2.0))
async def send_file(self, sender): self.progress_update.start() start_time = time() send_process = AioProcess(target=sender.send_data, args=(self.pipe[1], )) send_process.start() await send_process.coro_join() end_time = time() - start_time self.progress_update.wait() message = QMessageBox() message.information( self, "Information", f"Transfer complete, time taken {round(end_time / 60, 2)} minutes") self.ui.progressBar.setValue(0) self.ui.sendButton.setEnabled(True) self.ui.labelProgress.setVisible(False) self.ui.progressBar.setVisible(False)