ban peers that send shares that fail to validate
[p2pool.git] / p2pool / p2p.py
index 189fd2c..94db442 100644 (file)
@@ -25,6 +25,8 @@ def fragment(f, **kwargs):
         fragment(f, **dict((k, v[len(v)//2:]) for k, v in kwargs.iteritems()))
 
 class Protocol(p2protocol.Protocol):
+    VERSION = 1300
+    
     max_remembered_txs_size = 2500000
     
     def __init__(self, node, incoming):
@@ -43,7 +45,7 @@ class Protocol(p2protocol.Protocol):
         self.addr = self.transport.getPeer().host, self.transport.getPeer().port
         
         self.send_version(
-            version=1300,
+            version=self.VERSION,
             services=0,
             addr_to=dict(
                 services=0,
@@ -92,8 +94,7 @@ class Protocol(p2protocol.Protocol):
             self.badPeerHappened()
     
     def badPeerHappened(self):
-        if p2pool.DEBUG:
-            print "Bad peer banned:", self.addr
+        print "Bad peer banned:", self.addr
         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
@@ -116,7 +117,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
@@ -150,9 +151,10 @@ class Protocol(p2protocol.Protocol):
             self.send_ping(),
         random.expovariate(1/100)][-1])
         
-        self._stop_thread2 = deferral.run_repeatedly(lambda: [
-            self.send_addrme(port=self.node.serverfactory.listen_port.getHost().port) if self.node.serverfactory.listen_port is not None else None,
-        random.expovariate(1/(100*len(self.node.peers) + 1))][-1])
+        if self.node.advertise_ip:
+            self._stop_thread2 = deferral.run_repeatedly(lambda: [
+                self.send_addrme(port=self.node.serverfactory.listen_port.getHost().port) if self.node.serverfactory.listen_port is not None else None,
+            random.expovariate(1/(100*len(self.node.peers) + 1))][-1])
         
         if best_share_hash is not None:
             self.node.handle_share_hashes([best_share_hash], self)
@@ -252,11 +254,43 @@ 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)
+        result = []
+        for wrappedshare in shares:
+            if wrappedshare['type'] < p2pool_data.Share.VERSION: 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
+            
+            result.append((share, txs))
+            
+        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:
@@ -296,11 +330,12 @@ class Protocol(p2protocol.Protocol):
         ('result', pack.EnumType(pack.VarIntType(), {0: 'good', 1: 'too long', 2: 'unk2', 3: 'unk3', 4: 'unk4', 5: 'unk5', 6: 'unk6'})),
         ('shares', pack.ListType(p2pool_data.share_type)),
     ])
+    class ShareReplyError(Exception): pass
     def handle_sharereply(self, id, result, shares):
         if result == 'good':
-            res = [p2pool_data.load_share(share, self.node.net, self.addr) for share in shares if share['type'] >= 9]
+            res = [p2pool_data.load_share(share, self.node.net, self.addr) for share in shares if share['type'] >= p2pool_data.Share.VERSION]
         else:
-            res = failure.Failure("sharereply result: " + result)
+            res = failure.Failure(self.ShareReplyError(result))
         self.get_shares.got_response(id, res)
     
     
@@ -389,7 +424,8 @@ class Protocol(p2protocol.Protocol):
         if self.connected2:
             self.factory.proto_disconnected(self, reason)
             self._stop_thread()
-            self._stop_thread2()
+            if self.node.advertise_ip:
+                self._stop_thread2()
             self.connected2 = False
         self.factory.proto_lost_connection(self, reason)
         if p2pool.DEBUG:
@@ -546,7 +582,7 @@ class SingleClientFactory(protocol.ReconnectingClientFactory):
         self.node.lost_conn(proto, reason)
 
 class Node(object):
-    def __init__(self, best_share_hash_func, port, net, addr_store={}, connect_addrs=set(), desired_outgoing_conns=10, max_outgoing_attempts=30, max_incoming_conns=50, preferred_storage=1000, known_txs_var=variable.Variable({}), mining_txs_var=variable.Variable({})):
+    def __init__(self, best_share_hash_func, port, net, addr_store={}, connect_addrs=set(), desired_outgoing_conns=10, max_outgoing_attempts=30, max_incoming_conns=50, preferred_storage=1000, known_txs_var=variable.Variable({}), mining_txs_var=variable.Variable({}), advertise_ip=True):
         self.best_share_hash_func = best_share_hash_func
         self.port = port
         self.net = net
@@ -555,6 +591,7 @@ class Node(object):
         self.preferred_storage = preferred_storage
         self.known_txs_var = known_txs_var
         self.mining_txs_var = mining_txs_var
+        self.advertise_ip = advertise_ip
         
         self.traffic_happened = variable.Event()
         self.nonce = random.randrange(2**64)