slush

Stratum reconnections

Oct 10th, 2012
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import sys
  3. import random
  4.  
  5. import stratum
  6. import stratum.settings as settings
  7. from twisted.internet import reactor
  8. from twisted.application import service, internet    
  9. from stratum.services import ServiceEventHandler, GenericService
  10. from stratum import socket_transport
  11. from stratum.connection_registry import ConnectionRegistry
  12.  
  13. class MiningService(GenericService):
  14.     service_type = 'mining'
  15.     service_vendor = 'custom'
  16.     is_default = True
  17.  
  18.     @classmethod
  19.     def _reconnect(cls, switchto):
  20.         next_reconnect = random.randint(5,10)
  21.         reactor.callLater(next_reconnect, cls._reconnect, switchto)
  22.        
  23.         selected = random.choice(switchto)
  24.         print switchto
  25.         print "Reconnecting to", selected
  26.        
  27.         for conn in ConnectionRegistry.iterate():
  28.             conn = conn()
  29.             if not conn:
  30.                 continue
  31.            
  32.             conn.rpc('client.reconnect', [None, selected, 2], is_notification=True)
  33.            
  34.         print "Next reconnect in %d sec" % next_reconnect
  35.        
  36.          
  37.     def _finish_after_subscribe(self, result):
  38.         self.connection_ref().rpc('mining.notify',
  39.             ["124b", "9eb4d8f01c4522cb1191e93896fff7204bae4aebec50a7030000016700000000",
  40.             "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff2103a21703062f503253482f042c67755008",
  41.             "072f736c7573682f0000000001026f312a010000001976a914e8e6ace10f10ce5ed479c7188c9b4061e53aa90688ac00000000",
  42.             ["11d30680b28e3101141731fa04daf7c42fb90d4fa8b59bb1411ff4ed31bed51f",
  43.              "968b36b8a85d22695970764a8b8e6ba2f8fd864c2a003cd44eed5947a60a467a",
  44.              "2c691ebaaedb4d53d8c03c1c7d7d4f82dba8d832d39337347a03232bfc57f1de",
  45.              "239eec3b0098cd7f82c4071ff41cb2d235558a55a0d9341d7b9f188ebdc4140f",
  46.              "608fe54d1e97ee8b6516018cd82918012cbf876dfe3ad166c3d37f5dbd7c4546",
  47.              "8841944a4b2679cd297158f6175a655a885adc80116e0608842821a248c8d3eb",
  48.              "59411a7a593424ec42b57bc0f91ee56bbf717564001786ba67e4702e0a47ef1e"],
  49.              "00000002", "1a057e08", "5075672c", True], is_notification=True
  50.         )
  51.  
  52.         return result
  53.        
  54.     def authorize(self, worker_name, password):
  55.         return True
  56.    
  57.     def subscribe(self):
  58.         self.connection_ref().on_finish.addCallback(self._finish_after_subscribe)
  59.         return ([], ''.join([ "%x" % random.randint(0,15) for x in range(8) ]), 4)
  60.    
  61.     def submit(self, worker_name, job_id, extranonce2, ntime, nonce):
  62.         return True
  63.    
  64. if __name__ == '__main__':    
  65.     try:
  66.         listening_port = int(sys.argv[1])
  67.         print "Listening on port:", listening_port
  68.     except:
  69.         print "./reconnect_demo.py <listening_port> [<switchto1> <switchto2> <switchtoN>]"
  70.         sys.exit()
  71.    
  72.     switchto = [ int(x) for x in sys.argv[2:] ]
  73.     print "I'll randomly switch connections to:", switchto
  74.    
  75.     reactor.listenTCP(listening_port, socket_transport.SocketTransportFactory(
  76.             debug=True, event_handler=ServiceEventHandler))
  77.    
  78.     MiningService._reconnect(switchto)
  79.    
  80.     reactor.run()
RAW Paste Data