broadcast shares in serial
[p2pool.git] / p2pool / p2p.py
index 26a9578..871a2d9 100644 (file)
@@ -1,5 +1,6 @@
 from __future__ import division
 
+import math
 import random
 import time
 
@@ -24,6 +25,8 @@ class Protocol(p2protocol.Protocol):
         self.connected2 = False
     
     def connectionMade(self):
+        p2protocol.Protocol.connectionMade(self)
+        
         self.factory.proto_made_connection(self)
         
         self.addr = self.transport.getPeer().host, self.transport.getPeer().port
@@ -47,20 +50,12 @@ class Protocol(p2protocol.Protocol):
             best_share_hash=self.node.best_share_hash_func(),
         )
         
-        reactor.callLater(10, self._connect_timeout)
-        self.timeout_delayed = reactor.callLater(100, self._timeout)
-        
-        old_dataReceived = self.dataReceived
-        def new_dataReceived(data):
-            if not self.timeout_delayed.called:
-                self.timeout_delayed.reset(100)
-            old_dataReceived(data)
-        self.dataReceived = new_dataReceived
+        self.timeout_delayed = reactor.callLater(10, self._connect_timeout)
     
     def _connect_timeout(self):
-        if not self.connected2 and self.transport.connected:
-            print 'Handshake timed out, disconnecting from %s:%i' % self.addr
-            self.transport.loseConnection()
+        self.timeout_delayed = None
+        print 'Handshake timed out, disconnecting from %s:%i' % self.addr
+        self.transport.loseConnection()
     
     def packetReceived(self, command, payload2):
         try:
@@ -68,32 +63,21 @@ class Protocol(p2protocol.Protocol):
                 raise PeerMisbehavingError('first message was not version message')
             p2protocol.Protocol.packetReceived(self, command, payload2)
         except PeerMisbehavingError, e:
+            return
             print 'Peer %s:%i misbehaving, will drop and ban. Reason:' % self.addr, e.message
             self.badPeerHappened()
     
     def badPeerHappened(self):
+        return
         if p2pool.DEBUG:
             print "Bad peer banned:", self.addr
         self.transport.loseConnection()
         self.node.bans[self.transport.getPeer().host] = time.time() + 60*60
     
     def _timeout(self):
-        if self.transport.connected:
-            print 'Connection timed out, disconnecting from %s:%i' % self.addr
-            self.transport.loseConnection()
-    
-    @defer.inlineCallbacks
-    def _think(self):
-        while self.connected2:
-            self.send_ping()
-            yield deferral.sleep(random.expovariate(1/100))
-    
-    @defer.inlineCallbacks
-    def _think2(self):
-        while self.connected2:
-            self.send_addrme(port=self.node.port)
-            #print 'sending addrme'
-            yield deferral.sleep(random.expovariate(1/(100*len(self.node.peers) + 1)))
+        self.timeout_delayed = None
+        print 'Connection timed out, disconnecting from %s:%i' % self.addr
+        self.transport.loseConnection()
     
     message_version = pack.ComposedType([
         ('version', pack.IntType(32)),
@@ -125,10 +109,26 @@ class Protocol(p2protocol.Protocol):
         
         self.nonce = nonce
         self.connected2 = True
+        
+        self.timeout_delayed.cancel()
+        self.timeout_delayed = reactor.callLater(100, self._timeout)
+        
+        old_dataReceived = self.dataReceived
+        def new_dataReceived(data):
+            if self.timeout_delayed is not None:
+                self.timeout_delayed.reset(100)
+            old_dataReceived(data)
+        self.dataReceived = new_dataReceived
+        
         self.factory.proto_connected(self)
         
-        self._think()
-        self._think2()
+        self._stop_thread = deferral.run_repeatedly(lambda: [
+            self.send_ping(),
+        random.expovariate(1/100)][-1])
+        
+        self._stop_thread2 = deferral.run_repeatedly(lambda: [
+            self.send_addrme(port=self.node.port),
+        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)
@@ -202,17 +202,19 @@ 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) for share in shares], self)
+        self.node.handle_shares([p2pool_data.load_share(share, self.node.net, self) for share in shares if share['type'] not in [6, 7]], self)
     
     def sendShares(self, shares):
         def att(f, **kwargs):
             try:
-                f(**kwargs)
+                return f(**kwargs)
             except p2protocol.TooLong:
                 att(f, **dict((k, v[:len(v)//2]) for k, v in kwargs.iteritems()))
-                att(f, **dict((k, v[len(v)//2:]) for k, v in kwargs.iteritems()))
+                return att(f, **dict((k, v[len(v)//2:]) for k, v in kwargs.iteritems()))
         if shares:
-            att(self.send_shares, shares=[share.as_share() for share in shares])
+            return att(self.send_shares, shares=[share.as_share() for share in shares])
+        else:
+            return defer.succeed(None)
     
     
     message_sharereq = pack.ComposedType([
@@ -236,10 +238,19 @@ class Protocol(p2protocol.Protocol):
     def handle_sharereply(self, id, result, shares):
         self.node.handle_share_reply(id, result, shares, self)
     
+    message_bestblock = pack.ComposedType([
+        ('header', bitcoin_data.block_header_type),
+    ])
+    def handle_bestblock(self, header):
+        self.node.handle_bestblock(header, self)
     
     def connectionLost(self, reason):
+        if self.timeout_delayed is not None:
+            self.timeout_delayed.cancel()
         if self.connected2:
             self.factory.proto_disconnected(self, reason)
+            self._stop_thread()
+            self._stop_thread2()
             self.connected2 = False
         self.factory.proto_lost_connection(self, reason)
         if p2pool.DEBUG:
@@ -295,7 +306,7 @@ class ServerFactory(protocol.ServerFactory):
         assert self.running
         self.running = False
         
-        self.listen_port.stopListening()
+        return self.listen_port.stopListening()
 
 class ClientFactory(protocol.ClientFactory):
     def __init__(self, node, desired_conns, max_attempts):
@@ -343,29 +354,28 @@ class ClientFactory(protocol.ClientFactory):
     def start(self):
         assert not self.running
         self.running = True
-        self._think()
+        self._stop_thinking = deferral.run_repeatedly(self._think)
     def stop(self):
         assert self.running
         self.running = False
+        self._stop_thinking()
     
-    @defer.inlineCallbacks
     def _think(self):
-        while self.running:
-            try:
-                if len(self.conns) < self.desired_conns and len(self.attempts) < self.max_attempts and self.node.addr_store:
-                    (host, port), = self.node.get_good_peers(1)
-                    
-                    if self._host_to_ident(host) in self.attempts:
-                        pass
-                    elif host in self.node.bans and self.node.bans[host] > time.time():
-                        pass
-                    else:
-                        #print 'Trying to connect to', host, port
-                        reactor.connectTCP(host, port, self, timeout=5)
-            except:
-                log.err()
-            
-            yield deferral.sleep(random.expovariate(1/1))
+        try:
+            if len(self.conns) < self.desired_conns and len(self.attempts) < self.max_attempts and self.node.addr_store:
+                (host, port), = self.node.get_good_peers(1)
+                
+                if self._host_to_ident(host) in self.attempts:
+                    pass
+                elif host in self.node.bans and self.node.bans[host] > time.time():
+                    pass
+                else:
+                    #print 'Trying to connect to', host, port
+                    reactor.connectTCP(host, port, self, timeout=5)
+        except:
+            log.err()
+        
+        return random.expovariate(1/1)
 
 class SingleClientFactory(protocol.ReconnectingClientFactory):
     def __init__(self, node):
@@ -413,29 +423,30 @@ class Node(object):
         
         self.running = True
         
-        self._think2()
+        self._stop_thinking = deferral.run_repeatedly(self._think)
     
-    @defer.inlineCallbacks
-    def _think2(self):
-        while self.running:
-            try:
-                if len(self.addr_store) < self.preferred_storage and self.peers:
-                    random.choice(self.peers.values()).send_getaddrs(count=8)
-            except:
-                log.err()
-            
-            yield deferral.sleep(random.expovariate(1/20))
+    def _think(self):
+        try:
+            if len(self.addr_store) < self.preferred_storage and self.peers:
+                random.choice(self.peers.values()).send_getaddrs(count=8)
+        except:
+            log.err()
+        
+        return random.expovariate(1/20)
     
+    @defer.inlineCallbacks
     def stop(self):
         if not self.running:
             raise ValueError('already stopped')
         
         self.running = False
         
-        self.clientfactory.stop()
-        self.serverfactory.stop()
+        self._stop_thinking()
+        yield self.clientfactory.stop()
+        yield self.serverfactory.stop()
         for singleclientconnector in self.singleclientconnectors:
-            singleclientconnector.factory.stopTrying() # XXX will this disconnect a current connection?
+            yield singleclientconnector.factory.stopTrying()
+            yield singleclientconnector.disconnect()
         del self.singleclientconnectors
     
     def got_conn(self, conn):
@@ -474,6 +485,11 @@ class Node(object):
     def handle_share_reply(self, id, result, shares, peer):
         raise PeerMisbehavingError('sent share reply without being sent a request')
     
+    def handle_bestblock(self, header, peer):
+        print 'handle_bestblock', header
+    
     def get_good_peers(self, max_count):
         t = time.time()
-        return [x[0] for x in sorted(self.addr_store.iteritems(), key=lambda (k, (services, first_seen, last_seen)): -max(3600, last_seen - first_seen)/max(3600, t - last_seen)*random.expovariate(1))][:max_count]
+        return [x[0] for x in sorted(self.addr_store.iteritems(), key=lambda (k, (services, first_seen, last_seen)):
+            -math.log(max(3600, last_seen - first_seen))/math.log(max(3600, t - last_seen))*random.expovariate(1)
+        )][:max_count]