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.sha3(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.sha3(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)
def handle_shares(self, shares, peer): if len(shares) > 5: print 'Processing %i shares from %s...' % ( len(shares), '%s:%i' % peer.addr if peer is not None else None) new_count = 0 all_new_txs = {} for share, new_txs in shares: if new_txs is not None: all_new_txs.update( (bitcoin_data.sha3(bitcoin_data.tx_type.pack(new_tx)), new_tx) for new_tx in new_txs) if share.hash in self.node.tracker.items: #print 'Got duplicate share, ignoring. Hash: %s' % (p2pool_data.format_hash(share.hash),) continue new_count += 1 #print 'Received share %s from %r' % (p2pool_data.format_hash(share.hash), share.peer_addr) self.node.tracker.add(share) new_known_txs = dict(self.node.known_txs_var.value) new_known_txs.update(all_new_txs) self.node.known_txs_var.set(new_known_txs) if new_count: self.node.set_best_share() if len(shares) > 5: print '... done processing %i shares. New: %i Have: %i/~%i' % ( len(shares), new_count, len( self.node.tracker.items), 2 * self.node.net.CHAIN_LENGTH)
def handle_shares(self, shares, peer): if len(shares) > 5: print 'Processing %i shares from %s...' % (len(shares), '%s:%i' % peer.addr if peer is not None else None) new_count = 0 all_new_txs = {} for share, new_txs in shares: if new_txs is not None: all_new_txs.update((bitcoin_data.sha3(bitcoin_data.tx_type.pack(new_tx)), new_tx) for new_tx in new_txs) if share.hash in self.node.tracker.items: #print 'Got duplicate share, ignoring. Hash: %s' % (p2pool_data.format_hash(share.hash),) continue new_count += 1 #print 'Received share %s from %r' % (p2pool_data.format_hash(share.hash), share.peer_addr) self.node.tracker.add(share) new_known_txs = dict(self.node.known_txs_var.value) new_known_txs.update(all_new_txs) self.node.known_txs_var.set(new_known_txs) if new_count: self.node.set_best_share() if len(shares) > 5: print '... done processing %i shares. New: %i Have: %i/~%i' % (len(shares), new_count, len(self.node.tracker.items), 2*self.node.net.CHAIN_LENGTH)
def submit_block_p2p(block, factory, net): if factory.conn.value is None: print >> sys.stderr, 'No bitcoind connection when block submittal attempted! %s%064x' % ( net.PARENT.BLOCK_EXPLORER_URL_PREFIX, bitcoin_data.sha3( bitcoin_data.block_header_type.pack(block['header']))) raise deferral.RetrySilentlyException() factory.conn.value.send_block(block=block)
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.sha3( 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.sha3( 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)
def rpc_submit(self, worker_name, job_id, extranonce2, ntime, nonce): if job_id not in self.handler_map: print >>sys.stderr, '''Couldn't link returned work's job id with its handler. This should only happen if this process was recently restarted!''' return False x, got_response = self.handler_map[job_id] coinb_nonce = extranonce2.decode('hex') assert len(coinb_nonce) == self.wb.COINBASE_NONCE_LENGTH new_packed_gentx = x['coinb1'] + coinb_nonce + x['coinb2'] header = dict( version=x['version'], previous_block=x['previous_block'], merkle_root=bitcoin_data.check_merkle_link(bitcoin_data.sha3(new_packed_gentx), x['merkle_link']), timestamp=pack.IntType(32).unpack(getwork._swap4(ntime.decode('hex'))), bits=x['bits'], nonce=pack.IntType(32).unpack(getwork._swap4(nonce.decode('hex'))), ) return got_response(header, worker_name, coinb_nonce)
def handle_remember_tx(self, tx_hashes, txs): for tx_hash in tx_hashes: if tx_hash in self.remembered_txs: print >> sys.stderr, 'Peer referenced transaction twice, disconnecting' self.disconnect() return if tx_hash in self.node.known_txs_var.value: tx = self.node.known_txs_var.value[tx_hash] else: for cache in self.known_txs_cache.itervalues(): if tx_hash in cache: tx = cache[tx_hash] print 'Transaction %064x rescued from peer latency cache!' % ( tx_hash, ) break else: print >> sys.stderr, 'Peer referenced unknown transaction %064x, disconnecting' % ( tx_hash, ) self.disconnect() return self.remembered_txs[tx_hash] = tx self.remembered_txs_size += 100 + bitcoin_data.tx_type.packed_size( tx) new_known_txs = dict(self.node.known_txs_var.value) warned = False for tx in txs: tx_hash = bitcoin_data.sha3(bitcoin_data.tx_type.pack(tx)) if tx_hash in self.remembered_txs: print >> sys.stderr, 'Peer referenced transaction twice, disconnecting' self.disconnect() return if tx_hash in self.node.known_txs_var.value and not warned: print 'Peer sent entire transaction %064x that was already received' % ( tx_hash, ) warned = True self.remembered_txs[tx_hash] = tx self.remembered_txs_size += 100 + bitcoin_data.tx_type.packed_size( tx) new_known_txs[tx_hash] = tx self.node.known_txs_var.set(new_known_txs) if self.remembered_txs_size >= self.max_remembered_txs_size: raise PeerMisbehavingError('too much transaction data stored')
def handle_remember_tx(self, tx_hashes, txs): for tx_hash in tx_hashes: if tx_hash in self.remembered_txs: print >>sys.stderr, 'Peer referenced transaction twice, disconnecting' self.disconnect() return if tx_hash in self.node.known_txs_var.value: tx = self.node.known_txs_var.value[tx_hash] else: for cache in self.known_txs_cache.itervalues(): if tx_hash in cache: tx = cache[tx_hash] print 'Transaction %064x rescued from peer latency cache!' % (tx_hash,) break else: print >>sys.stderr, 'Peer referenced unknown transaction %064x, disconnecting' % (tx_hash,) self.disconnect() return self.remembered_txs[tx_hash] = tx self.remembered_txs_size += 100 + bitcoin_data.tx_type.packed_size(tx) new_known_txs = dict(self.node.known_txs_var.value) warned = False for tx in txs: tx_hash = bitcoin_data.sha3(bitcoin_data.tx_type.pack(tx)) if tx_hash in self.remembered_txs: print >>sys.stderr, 'Peer referenced transaction twice, disconnecting' self.disconnect() return if tx_hash in self.node.known_txs_var.value and not warned: print 'Peer sent entire transaction %064x that was already received' % (tx_hash,) warned = True self.remembered_txs[tx_hash] = tx self.remembered_txs_size += 100 + bitcoin_data.tx_type.packed_size(tx) new_known_txs[tx_hash] = tx self.node.known_txs_var.set(new_known_txs) if self.remembered_txs_size >= self.max_remembered_txs_size: raise PeerMisbehavingError('too much transaction data stored')
def from_header(cls, header): return cls(bitcoin_data.sha3(bitcoin_data.block_header_type.pack(header)), header['previous_block'])
def _(tx): new_known_txs = dict(self.known_txs_var.value) new_known_txs[bitcoin_data.sha3( bitcoin_data.tx_type.pack(tx))] = tx self.known_txs_var.set(new_known_txs)
def submit_block_p2p(block, factory, net): if factory.conn.value is None: print >>sys.stderr, 'No bitcoind connection when block submittal attempted! %s%064x' % (net.PARENT.BLOCK_EXPLORER_URL_PREFIX, bitcoin_data.sha3(bitcoin_data.block_header_type.pack(block['header']))) raise deferral.RetrySilentlyException() factory.conn.value.send_block(block=block)
def _(tx): new_known_txs = dict(self.known_txs_var.value) new_known_txs[bitcoin_data.sha3(bitcoin_data.tx_type.pack(tx))] = tx self.known_txs_var.set(new_known_txs)