def work_poller(): while stop_signal.times == 0: flag = self.factory.new_block.get_deferred() try: self.dashd_work.set((yield helper.getwork(self.dashd, self.net, self.dashd_work.value['use_getblocktemplate']))) except: log.err() yield defer.DeferredList([flag, deferral.sleep(15)], fireOnOneCallback=True)
def start(self): stop_signal = variable.Event() self.stop = stop_signal.happened # DASHD WORK self.dashd_work = variable.Variable( (yield helper.getwork(self.dashd, self.net))) @defer.inlineCallbacks def work_poller(): while stop_signal.times == 0: flag = self.factory.new_block.get_deferred() try: self.dashd_work.set((yield helper.getwork( self.dashd, self.net, self.dashd_work.value['use_getblocktemplate']))) except: log.err() yield defer.DeferredList([flag, deferral.sleep(15)], fireOnOneCallback=True) work_poller() # PEER WORK self.best_block_header = variable.Variable(None) def handle_header(new_header): # check that header matches current target if not (self.net.PARENT.POW_FUNC( dash_data.block_header_type.pack(new_header)) <= self.dashd_work.value['bits'].target): return dashd_best_block = self.dashd_work.value['previous_block'] if (self.best_block_header.value is None or (new_header['previous_block'] == dashd_best_block and self.net.PARENT.BLOCKHASH_FUNC( dash_data.block_header_type.pack( self.best_block_header.value)) == dashd_best_block ) # new is child of current and previous is current or (self.net.PARENT.BLOCKHASH_FUNC( dash_data.block_header_type.pack(new_header)) == dashd_best_block and self.best_block_header.value['previous_block'] != dashd_best_block) ): # new is current and previous is not a child of current self.best_block_header.set(new_header) self.handle_header = handle_header @defer.inlineCallbacks def poll_header(): if self.factory.conn.value is None: return handle_header((yield self.factory.conn.value.get_block_header( self.dashd_work.value['previous_block']))) self.dashd_work.changed.watch(lambda _: poll_header()) yield deferral.retry('Error while requesting best block header:')( poll_header)() # BEST SHARE self.known_txs_var = variable.VariableDict({}) # hash -> tx self.mining_txs_var = variable.Variable({}) # hash -> tx self.get_height_rel_highest = yield height_tracker.get_height_rel_highest_func( self.dashd, self.factory, lambda: self.dashd_work.value['previous_block'], self.net) self.best_share_var = variable.Variable(None) self.desired_var = variable.Variable(None) self.dashd_work.changed.watch(lambda _: self.set_best_share()) self.set_best_share() # setup p2p logic and join p2pool network # update mining_txs according to getwork results @self.dashd_work.changed.run_and_watch def _(_=None): new_mining_txs = {} added_known_txs = {} for tx_hash, tx in zip(self.dashd_work.value['transaction_hashes'], self.dashd_work.value['transactions']): new_mining_txs[tx_hash] = tx added_known_txs[tx_hash] = tx self.mining_txs_var.set(new_mining_txs) self.known_txs_var.add(added_known_txs) # add p2p transactions from dashd to known_txs @self.factory.new_tx.watch def _(tx): self.known_txs_var.add({ dash_data.hash256(dash_data.tx_type.pack(tx)): tx, }) # forward transactions seen to dashd @self.known_txs_var.transitioned.watch @defer.inlineCallbacks def _(before, after): yield deferral.sleep(random.expovariate(1 / 1)) if self.factory.conn.value is None: return for tx_hash in set(after) - set(before): self.factory.conn.value.send_tx(tx=after[tx_hash]) @self.tracker.verified.added.watch def _(share): if not (share.pow_hash <= share.header['bits'].target): return block = share.as_block(self.tracker, self.known_txs_var.value) if block is None: print >> sys.stderr, 'GOT INCOMPLETE BLOCK FROM PEER! %s dash: %s%064x' % ( p2pool_data.format_hash( share.hash), self.net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash) return helper.submit_block(block, True, self.factory, self.dashd, self.dashd_work, self.net) print print 'GOT BLOCK FROM PEER! Passing to dashd! %s dash: %s%064x' % ( p2pool_data.format_hash(share.hash), self.net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash) print self.factory.new_block.happened(share.hash) def forget_old_txs(): new_known_txs = {} if self.p2p_node is not None: for peer in self.p2p_node.peers.itervalues(): new_known_txs.update(peer.remembered_txs) new_known_txs.update(self.mining_txs_var.value) for share in self.tracker.get_chain( self.best_share_var.value, min(120, self.tracker.get_height(self.best_share_var.value))): for tx_hash in share.new_transaction_hashes: if tx_hash in self.known_txs_var.value: new_known_txs[tx_hash] = self.known_txs_var.value[ tx_hash] self.known_txs_var.set(new_known_txs) t = deferral.RobustLoopingCall(forget_old_txs) t.start(10) stop_signal.watch(t.stop) t = deferral.RobustLoopingCall(self.clean_tracker) t.start(5) stop_signal.watch(t.stop)
def start(self): stop_signal = variable.Event() self.stop = stop_signal.happened # DASHD WORK self.dashd_work = variable.Variable((yield helper.getwork(self.dashd, self.net))) @defer.inlineCallbacks def work_poller(): while stop_signal.times == 0: flag = self.factory.new_block.get_deferred() try: self.dashd_work.set((yield helper.getwork(self.dashd, self.net, self.dashd_work.value['use_getblocktemplate']))) except: log.err() yield defer.DeferredList([flag, deferral.sleep(15)], fireOnOneCallback=True) work_poller() # PEER WORK self.best_block_header = variable.Variable(None) def handle_header(new_header): # check that header matches current target if not (self.net.PARENT.POW_FUNC(dash_data.block_header_type.pack(new_header)) <= self.dashd_work.value['bits'].target): return dashd_best_block = self.dashd_work.value['previous_block'] if (self.best_block_header.value is None or ( new_header['previous_block'] == dashd_best_block and self.net.PARENT.BLOCKHASH_FUNC(dash_data.block_header_type.pack(self.best_block_header.value)) == dashd_best_block ) # new is child of current and previous is current or ( self.net.PARENT.BLOCKHASH_FUNC(dash_data.block_header_type.pack(new_header)) == dashd_best_block and self.best_block_header.value['previous_block'] != dashd_best_block )): # new is current and previous is not a child of current self.best_block_header.set(new_header) self.handle_header = handle_header @defer.inlineCallbacks def poll_header(): if self.factory.conn.value is None: return handle_header((yield self.factory.conn.value.get_block_header(self.dashd_work.value['previous_block']))) self.dashd_work.changed.watch(lambda _: poll_header()) yield deferral.retry('Error while requesting best block header:')(poll_header)() # BEST SHARE self.known_txs_var = variable.VariableDict({}) # hash -> tx self.mining_txs_var = variable.Variable({}) # hash -> tx self.get_height_rel_highest = yield height_tracker.get_height_rel_highest_func(self.dashd, self.factory, lambda: self.dashd_work.value['previous_block'], self.net) self.best_share_var = variable.Variable(None) self.desired_var = variable.Variable(None) self.dashd_work.changed.watch(lambda _: self.set_best_share()) self.set_best_share() # setup p2p logic and join p2pool network # update mining_txs according to getwork results @self.dashd_work.changed.run_and_watch def _(_=None): new_mining_txs = {} added_known_txs = {} for tx_hash, tx in zip(self.dashd_work.value['transaction_hashes'], self.dashd_work.value['transactions']): new_mining_txs[tx_hash] = tx added_known_txs[tx_hash] = tx self.mining_txs_var.set(new_mining_txs) self.known_txs_var.add(added_known_txs) # add p2p transactions from dashd to known_txs @self.factory.new_tx.watch def _(tx): self.known_txs_var.add({ dash_data.hash256(dash_data.tx_type.pack(tx)): tx, }) # forward transactions seen to dashd @self.known_txs_var.transitioned.watch @defer.inlineCallbacks def _(before, after): yield deferral.sleep(random.expovariate(1/1)) if self.factory.conn.value is None: return for tx_hash in set(after) - set(before): self.factory.conn.value.send_tx(tx=after[tx_hash]) @self.tracker.verified.added.watch def _(share): if not (share.pow_hash <= share.header['bits'].target): return block = share.as_block(self.tracker, self.known_txs_var.value) if block is None: print >>sys.stderr, 'GOT INCOMPLETE BLOCK FROM PEER! %s dash: %s%064x' % (p2pool_data.format_hash(share.hash), self.net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash) return helper.submit_block(block, True, self.factory, self.dashd, self.dashd_work, self.net) print print 'GOT BLOCK FROM PEER! Passing to dashd! %s dash: %s%064x' % (p2pool_data.format_hash(share.hash), self.net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash) print self.factory.new_block.happened(share.hash) def forget_old_txs(): new_known_txs = {} if self.p2p_node is not None: for peer in self.p2p_node.peers.itervalues(): new_known_txs.update(peer.remembered_txs) new_known_txs.update(self.mining_txs_var.value) for share in self.tracker.get_chain(self.best_share_var.value, min(120, self.tracker.get_height(self.best_share_var.value))): for tx_hash in share.new_transaction_hashes: if tx_hash in self.known_txs_var.value: new_known_txs[tx_hash] = self.known_txs_var.value[tx_hash] self.known_txs_var.set(new_known_txs) t = deferral.RobustLoopingCall(forget_old_txs) t.start(10) stop_signal.watch(t.stop) t = deferral.RobustLoopingCall(self.clean_tracker) t.start(5) stop_signal.watch(t.stop)