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.head_candidate chain = self.services.chain.chain 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) if len(chain.time_queue) > 0: # If we mine two blocks within one second, pyethereum will # force the new block's timestamp to be in the future (see # ethereum1_setup_block()), and when we try to add that block # to the chain (via Chain.add_block()), it will be put in a # queue for later processing. Since we need to ensure the # block has been added before we continue the test, we # have to manually process the time queue. log.debug('block mined too fast, processing time queue') chain.process_time_queue(new_time=block.timestamp) log.debug('block mined') return 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 - old_div(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 = old_div(elapsed, self.max_elapsed) self.rounds = int(old_div(self.rounds, adjust)) log_sub.debug('mining task finished', is_stopped=self.is_stopped)
def __mine__(): result, (header_hash, seed_hash, boundary) = mvs_rpc.eth_get_work() assert (result == 0) result, (height, difficulty) = mvs_rpc.get_info() assert (result == 0) rounds = 100 nonce = 0 while True: bin_nonce, mixhash = mine(block_number=height+1, difficulty=difficulty, mining_hash=header_hash, rounds=rounds, start_nonce=nonce) if bin_nonce: break nonce += rounds return bin_nonce, '0x' + common.toString(header_hash), '0x' + common.toString(mixhash)