def get_height_rel_highest_func(daemon, factory, best_block_func, net): if "\ngetblock " in (yield deferral.retry()(daemon.rpc_help)()): @deferral.DeferredCacher @defer.inlineCallbacks def height_cacher(block_hash): try: x = yield daemon.rpc_getblock("%x" % (block_hash,)) except jsonrpc.Error_for_code(-5): # Block not found if not p2pool.DEBUG: raise deferral.RetrySilentlyException() else: raise defer.returnValue(x["blockcount"] if "blockcount" in x else x["height"]) best_height_cached = variable.Variable((yield deferral.retry()(height_cacher)(best_block_func()))) def get_height_rel_highest(block_hash): this_height = height_cacher.call_now(block_hash, 0) best_height = height_cacher.call_now(best_block_func(), 0) best_height_cached.set(max(best_height_cached.value, this_height, best_height)) return this_height - best_height_cached.value else: get_height_rel_highest = HeightTracker( best_block_func, factory, 5 * net.SHARE_PERIOD * net.CHAIN_LENGTH / net.PARENT.BLOCK_PERIOD ).get_height_rel_highest defer.returnValue(get_height_rel_highest)
def get_height_rel_highest_func(yrmixd, factory, best_block_func, net): if '\ngetblock ' in (yield deferral.retry()(yrmixd.rpc_help)()): @deferral.DeferredCacher @defer.inlineCallbacks def height_cacher(block_hash): try: x = yield yrmixd.rpc_getblock('%x' % (block_hash, )) except jsonrpc.Error_for_code(-5): # Block not found if not p2pool.DEBUG: raise deferral.RetrySilentlyException() else: raise defer.returnValue(x['blockcount'] if 'blockcount' in x else x['height']) best_height_cached = variable.Variable( (yield deferral.retry()(height_cacher)(best_block_func()))) def get_height_rel_highest(block_hash): this_height = height_cacher.call_now(block_hash, 0) best_height = height_cacher.call_now(best_block_func(), 0) best_height_cached.set( max(best_height_cached.value, this_height, best_height)) return this_height - best_height_cached.value else: get_height_rel_highest = HeightTracker( best_block_func, factory, 5 * net.SHARE_PERIOD * net.CHAIN_LENGTH / net.PARENT.BLOCK_PERIOD).get_height_rel_highest defer.returnValue(get_height_rel_highest)
def start(self): assert not self.running self.running = True def attempt_listen(): if self.running: self.listen_port = reactor.listenTCP(self.node.port, self) deferral.retry('Error binding to P2P port:', traceback=False)(attempt_listen)()
def get_height_rel_highest_func(bitcoind, factory, best_block_func, net): if '\ngetblock ' in (yield deferral.retry()(bitcoind.rpc_help)()): @deferral.DeferredCacher @defer.inlineCallbacks def height_cacher(block_hash): try: x = yield bitcoind.rpc_getblock('%x' % (block_hash,)) except jsonrpc.Error, e: if e.code == -5 and not p2pool.DEBUG: raise deferral.RetrySilentlyException() raise defer.returnValue(x['blockcount'] if 'blockcount' in x else x['height']) best_height_cached = variable.Variable((yield deferral.retry()(height_cacher)(best_block_func()))) def get_height_rel_highest(block_hash): this_height = height_cacher.call_now(block_hash, 0) best_height = height_cacher.call_now(best_block_func(), 0) best_height_cached.set(max(best_height_cached.value, this_height, best_height)) return this_height - best_height_cached.value
def test_get_block(self): factory = p2p.ClientFactory(networks.nets['bitcoin']) c = reactor.connectTCP('127.0.0.1', 8333, factory) try: h = 0x000000000000046acff93b0e76cd10490551bf871ce9ac9fad62e67a07ff1d1e block = yield deferral.retry()(defer.inlineCallbacks(lambda: defer.returnValue((yield (yield factory.getProtocol()).get_block(h)))))() assert data.merkle_hash(map(data.hash256, map(data.tx_type.pack, block['txs']))) == block['header']['merkle_root'] assert data.hash256(data.block_header_type.pack(block['header'])) == h finally: factory.stopTrying() c.disconnect()
def test_get_block(self): factory = p2p.ClientFactory(networks.nets['gvidon']) c = reactor.connectTCP('127.0.0.1', 9900, factory) try: h = 0x00000000000132b9afeca5e9a2fdf4477338df6dcff1342300240bc70397c4bb block = yield deferral.retry()(defer.inlineCallbacks(lambda: defer.returnValue((yield (yield factory.getProtocol()).get_block(h)))))() assert data.merkle_hash(map(data.hash256, map(data.tx_type.pack, block['txs']))) == block['header']['merkle_root'] assert data.hash256(data.block_header_type.pack(block['header'])) == h finally: factory.stopTrying() c.disconnect()
def test_get_block(self): factory = p2p.ClientFactory(networks.nets['dash']) c = reactor.connectTCP('127.0.0.1', 8333, factory) try: h = 0x000000000000046acff93b0e76cd10490551bf871ce9ac9fad62e67a07ff1d1e block = yield deferral.retry()(defer.inlineCallbacks(lambda: defer.returnValue((yield (yield factory.getProtocol()).get_block(h)))))() assert data.merkle_hash(map(data.hash256, map(data.tx_type.pack, block['txs']))) == block['header']['merkle_root'] assert data.hash256(data.block_header_type.pack(block['header'])) == h finally: factory.stopTrying() c.disconnect()
def test_get_block(self): factory = p2p.ClientFactory(networks.nets['bitcoin_regtest']) c = reactor.connectTCP('127.0.0.1', 18444, factory) try: h = 0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 block = yield deferral.retry()(defer.inlineCallbacks(lambda: defer.returnValue((yield (yield factory.getProtocol()).get_block(h)))))() assert data.merkle_hash(map(data.hash256, map(data.tx_type.pack, block['txs']))) == block['header']['merkle_root'] assert data.hash256(data.block_header_type.pack(block['header'])) == h finally: factory.stopTrying() c.disconnect()
def test_get_block(self): factory = p2p.ClientFactory(networks.nets['dash']) c = reactor.connectTCP('127.0.0.1', 9999, factory) try: h = 0x00000000000132b9afeca5e9a2fdf4477338df6dcff1342300240bc70397c4bb block = yield deferral.retry()(defer.inlineCallbacks(lambda: defer.returnValue((yield (yield factory.getProtocol()).get_block(h)))))() assert data.merkle_hash(map(data.hash256, map(data.tx_type.pack, block['txs']))) == block['header']['merkle_root'] assert data.hash256(data.block_header_type.pack(block['header'])) == h finally: factory.stopTrying() c.disconnect()
def getwork(bitcoind, use_getblocktemplate=False): def go(): if use_getblocktemplate: return bitcoind.rpc_getblocktemplate(dict(mode='template', rules=['segwit'])) else: return bitcoind.rpc_getmemorypool() try: start = time.time() work = yield go() end = time.time() except jsonrpc.Error_for_code(-32601): # Method not found use_getblocktemplate = not use_getblocktemplate try: start = time.time() work = yield go() end = time.time() except jsonrpc.Error_for_code(-32601): # Method not found print >>sys.stderr, 'Error: Bitcoin version too old! Upgrade to v0.5 or newer!' raise deferral.RetrySilentlyException() packed_transactions = [x['data'].decode('hex') for x in work['transactions'] if len(x.get('depends', [])) == 0] if 'height' not in work: work['height'] = (yield bitcoind.rpc_getblock(work['previousblockhash']))['height'] + 1 elif p2pool.DEBUG: assert work['height'] == (yield bitcoind.rpc_getblock(work['previousblockhash']))['height'] + 1 # navid znode_reward_struct = yield deferral.retry('Error getting znode reward info:', 5)( lambda: bitcoind.rpc_znode('current'))() znode_reward = None if type(znode_reward_struct) is dict: znode_reward = bitcoin_data.address_to_pubkey_hash_no_net(znode_reward_struct['payee']) # znode_reward = znode_reward_struct['payee'] else: print 'Could not fetch znode reward information from wallet !' defer.returnValue(dict( version=work['version'], previous_block=int(work['previousblockhash'], 16), transactions=map(bitcoin_data.tx_type.unpack, packed_transactions), transaction_hashes=map(bitcoin_data.hash256, packed_transactions), transaction_fees=[x.get('fee', None) if isinstance(x, dict) else None for x in work['transactions']], subsidy=work['coinbasevalue'], time=work['time'] if 'time' in work else work['curtime'], bits=bitcoin_data.FloatingIntegerType().unpack(work['bits'].decode('hex')[::-1]) if isinstance(work['bits'], (str, unicode)) else bitcoin_data.FloatingInteger(work['bits']), coinbaseflags=work['coinbaseflags'].decode('hex') if 'coinbaseflags' in work else ''.join(x.decode('hex') for x in work['coinbaseaux'].itervalues()) if 'coinbaseaux' in work else '', height=work['height'], rules=work.get('rules', []), last_update=time.time(), use_getblocktemplate=use_getblocktemplate, latency=end - start, # navid znode_payee=znode_reward ))
def start(self): stop_signal = variable.Event() self.stop = stop_signal.happened # BITCOIND WORK self.bitcoind_work = variable.Variable((yield helper.getwork(self.bitcoind))) @defer.inlineCallbacks def work_poller(): while stop_signal.times == 0: flag = self.factory.new_block.get_deferred() try: self.bitcoind_work.set((yield helper.getwork(self.bitcoind, self.bitcoind_work.value['use_getblocktemplate']))) except: log.err() yield defer.DeferredList([flag, deferral.sleep(5)], fireOnOneCallback=True) work_poller() # PEER WORK self.best_block_header = variable.Variable(None) self.pow_bits = variable.Variable(None) self.pow_subsidy = 0 def handle_header(new_header): self.pow_bits = self.bitcoind_work.value['bits'] self.pow_subsidy = self.bitcoind_work.value['subsidy'] # check that header matches current target if not (bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(new_header)) <= self.bitcoind_work.value['bits'].target): return bitcoind_best_block = self.bitcoind_work.value['previous_block'] if (self.best_block_header.value is None or ( new_header['previous_block'] == bitcoind_best_block and bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(self.best_block_header.value)) == bitcoind_best_block ) # new is child of current and previous is current or ( bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(new_header)) == bitcoind_best_block and self.best_block_header.value['previous_block'] != bitcoind_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.bitcoind_work.value['previous_block']))) self.bitcoind_work.changed.watch(lambda _: poll_header()) yield deferral.retry('Error while requesting best block header:')(poll_header)() # BEST SHARE self.known_txs_var = variable.Variable({}) # hash -> tx self.mining_txs_var = variable.Variable({}) # hash -> tx self.get_height_rel_highest = yield height_tracker.get_height_rel_highest_func(self.bitcoind, self.factory, lambda: self.bitcoind_work.value['previous_block'], self.net) self.best_share_var = variable.Variable(None) self.desired_var = variable.Variable(None) self.bitcoind_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.bitcoind_work.changed.run_and_watch def _(_=None): new_mining_txs = {} new_known_txs = dict(self.known_txs_var.value) for tx_hash, tx in zip(self.bitcoind_work.value['transaction_hashes'], self.bitcoind_work.value['transactions']): new_mining_txs[tx_hash] = tx new_known_txs[tx_hash] = tx self.mining_txs_var.set(new_mining_txs) self.known_txs_var.set(new_known_txs) # add p2p transactions from bitcoind to known_txs @self.factory.new_tx.watch def _(tx): if tx.timestamp > time.time() + 3600: return if tx.timestamp > self.bitcoind_work.value['txn_timestamp']: self.bitcoind_work.value['txn_timestamp'] = tx.timestamp new_known_txs = dict(self.known_txs_var.value) new_known_txs[bitcoin_data.hash256(bitcoin_data.tx_type.pack(tx))] = tx self.known_txs_var.set(new_known_txs) # forward transactions seen to bitcoind @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 bitcoin: %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.bitcoind, self.bitcoind_work, self.net) print print 'GOT BLOCK FROM PEER! Passing to bitcoind! %s bitcoin: %s%064x' % (p2pool_data.format_hash(share.hash), self.net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash) print 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)
def start(self): stop_signal = variable.Event() self.stop = stop_signal.happened # BITCOIND WORK self.bitcoind_work = variable.Variable((yield helper.getwork(self.bitcoind))) @defer.inlineCallbacks def work_poller(): while stop_signal.times == 0: flag = self.factory.new_block.get_deferred() try: self.bitcoind_work.set((yield helper.getwork( self.bitcoind, self.bitcoind_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( bitcoin_data.block_header_type.pack(new_header)) <= self.bitcoind_work.value['bits'].target): return bitcoind_best_block = self.bitcoind_work.value['previous_block'] if (self.best_block_header.value is None or (new_header['previous_block'] == bitcoind_best_block and bitcoin_data.hash256( bitcoin_data.block_header_type.pack( self.best_block_header.value)) == bitcoind_best_block ) # new is child of current and previous is current or (bitcoin_data.hash256( bitcoin_data.block_header_type.pack(new_header)) == bitcoind_best_block and self.best_block_header.value['previous_block'] != bitcoind_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.bitcoind_work.value['previous_block']))) self.bitcoind_work.changed.watch(lambda _: poll_header()) yield deferral.retry('Error while requesting best block header:')( poll_header)() # BEST SHARE self.known_txs_var = variable.Variable({}) # hash -> tx self.mining_txs_var = variable.Variable({}) # hash -> tx self.get_height_rel_highest = yield height_tracker.get_height_rel_highest_func( self.bitcoind, self.factory, lambda: self.bitcoind_work.value['previous_block'], self.net) self.best_share_var = variable.Variable(None) self.desired_var = variable.Variable(None) self.bitcoind_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.bitcoind_work.changed.run_and_watch def _(_=None): new_mining_txs = {} new_known_txs = dict(self.known_txs_var.value) for tx_hash, tx in zip( self.bitcoind_work.value['transaction_hashes'], self.bitcoind_work.value['transactions']): new_mining_txs[tx_hash] = tx new_known_txs[tx_hash] = tx self.mining_txs_var.set(new_mining_txs) self.known_txs_var.set(new_known_txs) # add p2p transactions from bitcoind to known_txs @self.factory.new_tx.watch def _(tx): new_known_txs = dict(self.known_txs_var.value) new_known_txs[bitcoin_data.hash256( bitcoin_data.tx_type.pack(tx))] = tx self.known_txs_var.set(new_known_txs) # forward transactions seen to bitcoind @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 bitcoin: %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.bitcoind, self.bitcoind_work, self.net) print print 'GOT BLOCK FROM PEER! Passing to bitcoind! %s bitcoin: %s%064x' % ( p2pool_data.format_hash(share.hash), self.net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash) print # Code to send pushover and notification when a block is found by a peer (ikolubr - Mar 31st, 2018) if self.net.USE_PUSHOVER_BLOCK: conn = httplib.HTTPSConnection("api.pushover.net:443") conn.request( "POST", "/1/messages.json", urllib.urlencode({ "token": self.net.PUSHOVER_APP_TOKEN, "user": self.net.PUSHOVER_USER_KEY, "message": 'FOUND BLOCK! %s%064x' % (self.net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash), }), {"Content-type": "application/x-www-form-urlencoded"}) conn.getresponse() 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)