X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=p2pool%2Fp2p.py;h=bc61982eca3c0548e4f19aa7f18d6fef705230c9;hb=9541a77695bc98cccff001541ccb3880bca6d875;hp=dffa3c34eaafdbbc76695da5ea1c743bb8cf82eb;hpb=27de695afc01ad16bb534cc2a6777b8d338c3638;p=p2pool.git diff --git a/p2pool/p2p.py b/p2pool/p2p.py index dffa3c3..bc61982 100644 --- a/p2pool/p2p.py +++ b/p2pool/p2p.py @@ -43,7 +43,7 @@ class Protocol(p2protocol.Protocol): self.addr = self.transport.getPeer().host, self.transport.getPeer().port self.send_version( - version=1100, + version=1300, services=0, addr_to=dict( services=0, @@ -67,7 +67,7 @@ class Protocol(p2protocol.Protocol): max_id=2**256, func=lambda id, hashes, parents, stops: self.send_sharereq(id=id, hashes=hashes, parents=parents, stops=stops), timeout=15, - on_timeout=self.transport.loseConnection, + on_timeout=self.disconnect, ) self.remote_tx_hashes = set() # view of peer's known_txs # not actually initially empty, but sending txs instead of tx hashes won't hurt @@ -80,12 +80,7 @@ class Protocol(p2protocol.Protocol): def _connect_timeout(self): self.timeout_delayed = None print 'Handshake timed out, disconnecting from %s:%i' % self.addr - if hasattr(self.transport, 'abortConnection'): - # Available since Twisted 11.1 - self.transport.abortConnection() - else: - # This doesn't always close timed out connections! - self.transport.loseConnection() + self.disconnect() def packetReceived(self, command, payload2): try: @@ -99,19 +94,14 @@ class Protocol(p2protocol.Protocol): def badPeerHappened(self): if p2pool.DEBUG: print "Bad peer banned:", self.addr - self.transport.loseConnection() + self.disconnect() if self.transport.getPeer().host != '127.0.0.1': # never ban localhost self.node.bans[self.transport.getPeer().host] = time.time() + 60*60 def _timeout(self): self.timeout_delayed = None print 'Connection timed out, disconnecting from %s:%i' % self.addr - if hasattr(self.transport, 'abortConnection'): - # Available since Twisted 11.1 - self.transport.abortConnection() - else: - # This doesn't always close timed out connections! - self.transport.loseConnection() + self.disconnect() message_version = pack.ComposedType([ ('version', pack.IntType(32)), @@ -126,7 +116,7 @@ class Protocol(p2protocol.Protocol): def handle_version(self, version, services, addr_to, addr_from, nonce, sub_version, mode, best_share_hash): if self.other_version is not None: raise PeerMisbehavingError('more than one version message') - if version < 8: + if version < 1300: raise PeerMisbehavingError('peer too old') self.other_version = version @@ -138,7 +128,7 @@ class Protocol(p2protocol.Protocol): if nonce in self.node.peers: if p2pool.DEBUG: print 'Detected duplicate connection, disconnecting from %s:%i' % self.addr - self.transport.loseConnection() + self.disconnect() return self.nonce = nonce @@ -167,9 +157,6 @@ class Protocol(p2protocol.Protocol): if best_share_hash is not None: self.node.handle_share_hashes([best_share_hash], self) - if self.other_version < 8: - return - def update_remote_view_of_my_known_txs(before, after): added = set(after) - set(before) removed = set(before) - set(after) @@ -265,32 +252,62 @@ class Protocol(p2protocol.Protocol): ('shares', pack.ListType(p2pool_data.share_type)), ]) def handle_shares(self, shares): - self.node.handle_shares([p2pool_data.load_share(share, self.node.net, self.addr) for share in shares if share['type'] >= 9], self) - - def sendShares(self, shares, tracker, known_txs, include_txs_with=[]): - if self.other_version >= 8: - tx_hashes = set() - for share in shares: - if share.hash in include_txs_with: - x = share.get_other_tx_hashes(tracker) - if x is not None: - tx_hashes.update(x) - - hashes_to_send = [x for x in tx_hashes if x not in self.node.mining_txs_var.value and x in known_txs] + result = [] + for wrappedshare in shares: + if wrappedshare['type'] < 9: continue + share = p2pool_data.load_share(wrappedshare, self.node.net, self.addr) + if wrappedshare['type'] >= 13: + txs = [] + for tx_hash in share.share_info['new_transaction_hashes']: + 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 + txs.append(tx) + else: + txs = None - new_remote_remembered_txs_size = self.remote_remembered_txs_size + sum(100 + bitcoin_data.tx_type.packed_size(known_txs[x]) for x in hashes_to_send) - if new_remote_remembered_txs_size > self.max_remembered_txs_size: - raise ValueError('shares have too many txs') - self.remote_remembered_txs_size = new_remote_remembered_txs_size + result.append((share, txs)) - fragment(self.send_remember_tx, tx_hashes=[x for x in hashes_to_send if x in self.remote_tx_hashes], txs=[known_txs[x] for x in hashes_to_send if x not in self.remote_tx_hashes]) + self.node.handle_shares(result, self) + + def sendShares(self, shares, tracker, known_txs, include_txs_with=[]): + tx_hashes = set() + for share in shares: + if share.VERSION >= 13: + # send full transaction for every new_transaction_hash that peer does not know + for tx_hash in share.share_info['new_transaction_hashes']: + assert tx_hash in known_txs, 'tried to broadcast share without knowing all its new transactions' + if tx_hash not in self.remote_tx_hashes: + tx_hashes.add(tx_hash) + continue + if share.hash in include_txs_with: + x = share.get_other_tx_hashes(tracker) + if x is not None: + tx_hashes.update(x) + + hashes_to_send = [x for x in tx_hashes if x not in self.node.mining_txs_var.value and x in known_txs] + + new_remote_remembered_txs_size = self.remote_remembered_txs_size + sum(100 + bitcoin_data.tx_type.packed_size(known_txs[x]) for x in hashes_to_send) + if new_remote_remembered_txs_size > self.max_remembered_txs_size: + raise ValueError('shares have too many txs') + self.remote_remembered_txs_size = new_remote_remembered_txs_size + + fragment(self.send_remember_tx, tx_hashes=[x for x in hashes_to_send if x in self.remote_tx_hashes], txs=[known_txs[x] for x in hashes_to_send if x not in self.remote_tx_hashes]) fragment(self.send_shares, shares=[share.as_share() for share in shares]) - if self.other_version >= 8: - self.send_forget_tx(tx_hashes=hashes_to_send) - - self.remote_remembered_txs_size -= sum(100 + bitcoin_data.tx_type.packed_size(known_txs[x]) for x in hashes_to_send) + self.send_forget_tx(tx_hashes=hashes_to_send) + + self.remote_remembered_txs_size -= sum(100 + bitcoin_data.tx_type.packed_size(known_txs[x]) for x in hashes_to_send) message_sharereq = pack.ComposedType([ @@ -350,7 +367,7 @@ class Protocol(p2protocol.Protocol): for tx_hash in tx_hashes: if tx_hash in self.remembered_txs: print >>sys.stderr, 'Peer referenced transaction twice, disconnecting' - self.transport.loseConnection() + self.disconnect() return if tx_hash in self.node.known_txs_var.value: @@ -363,7 +380,7 @@ class Protocol(p2protocol.Protocol): break else: print >>sys.stderr, 'Peer referenced unknown transaction %064x, disconnecting' % (tx_hash,) - self.transport.loseConnection() + self.disconnect() return self.remembered_txs[tx_hash] = tx @@ -374,7 +391,7 @@ class Protocol(p2protocol.Protocol): tx_hash = bitcoin_data.hash256(bitcoin_data.tx_type.pack(tx)) if tx_hash in self.remembered_txs: print >>sys.stderr, 'Peer referenced transaction twice, disconnecting' - self.transport.loseConnection() + self.disconnect() return if tx_hash in self.node.known_txs_var.value and not warned: