def _run(self): nonce = random.randint(0, TT64M1) while not self.is_stopped: log_sub.debug('starting mining round') st = time.time() bin_nonce, mixhash = mine(self.block_number, self.difficulty, self.mining_hash, start_nonce=nonce, rounds=self.rounds) elapsed = time.time() - st if bin_nonce: log_sub.debug('nonce found') self.nonce_callback(bin_nonce, mixhash, self.mining_hash) break delay = elapsed * (1 - self.cpu_pct / 100.) hashrate = int(self.rounds // (elapsed + delay)) self.hashrate_callback(hashrate) log_sub.debug('sleeping', delay=delay, elapsed=elapsed, rounds=self.rounds) gevent.sleep(delay + 0.001) nonce += self.rounds # adjust adjust = elapsed / self.max_elapsed self.rounds = int(self.rounds / adjust) log_sub.debug('mining task finished', is_stopped=self.is_stopped)
def mine_next_block(self): """Mine until a valid nonce is found. :returns: the new head """ log.debug('mining next block') block = self.services.chain.chain.head_candidate delta_nonce = 10**6 for start_nonce in count(0, delta_nonce): bin_nonce, mixhash = mine(block.number, block.difficulty, block.mining_hash, start_nonce=start_nonce, rounds=delta_nonce) if bin_nonce: break self.services.pow.recv_found_nonce(bin_nonce, mixhash, block.mining_hash) log.debug('block mined') return self.services.chain.chain.head
def _run(self): nonce = random.randint(0, TT64M1) while not self.is_stopped: log_sub.trace('starting mining round') st = time.time() bin_nonce, mixhash = mine(self.block_number, self.difficulty, self.mining_hash, start_nonce=nonce, rounds=self.rounds) elapsed = time.time() - st if bin_nonce: log_sub.info('nonce found') self.nonce_callback(bin_nonce, mixhash, self.mining_hash) break delay = elapsed * (1 - self.cpu_pct / 100.) hashrate = int(self.rounds // (elapsed + delay)) self.hashrate_callback(hashrate) log_sub.trace('sleeping', delay=delay, elapsed=elapsed, rounds=self.rounds) gevent.sleep(delay + 0.001) nonce += self.rounds # adjust adjust = elapsed / self.max_elapsed self.rounds = int(self.rounds / adjust) log_sub.debug('mining task finished', is_stopped=self.is_stopped)