slush

check_servers.py

Sep 24th, 2012
343
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. # author slush, public domain
  3. from twisted.internet import reactor
  4. from twisted.internet import defer
  5.  
  6. from stratum import settings
  7. settings.LOGLEVEL = 'INFO'
  8.  
  9. from stratum.socket_transport import SocketTransportClientFactory
  10. from stratum.services import GenericService, ServiceEventHandler
  11. from stratum.connection_registry import ConnectionRegistry
  12.  
  13. import time
  14.  
  15. debug = False
  16. reg = {}
  17.  
  18. class BlockchainNumblocks(GenericService):
  19. service_type = 'blockchain.numblocks'
  20. service_vendor = 'custom'
  21. is_default = True
  22.  
  23. def subscribe(self, blocks):
  24. session = ConnectionRegistry.get_session(self)
  25. session['blocks'] = blocks
  26.  
  27. print session['hostname'], "Received notification for the numblocks event !!!", blocks
  28.  
  29. '''
  30. class ServerPeers(GenericService):
  31. service_type = 'server.peers'
  32. service_vendor = 'custom'
  33. is_default = True
  34.  
  35. @defer.inlineCallbacks
  36. def subscribe(self, peers):
  37. print "New peers", peers
  38. yield add_peers(peers)
  39. '''
  40.  
  41. @defer.inlineCallbacks
  42. def connect(hostname):
  43. # Try to connect to remote server
  44. f = SocketTransportClientFactory(hostname, 50001,
  45. allow_trusted=True,
  46. allow_untrusted=False,
  47. debug=debug,
  48. signing_key=None,
  49. signing_id=None,
  50. event_handler=ServiceEventHandler)
  51. try:
  52. yield f.on_connect # Wait to on_connect event
  53. except:
  54. print "Failed to connect to", hostname
  55. return
  56.  
  57. reg[hostname] = f
  58.  
  59. r1 = f.rpc('blockchain.numblocks.subscribe', [])
  60. r2 = f.rpc('server.peers.subscribe', [])
  61.  
  62. blocks = (yield r1)
  63. print hostname, 'numblocks', blocks
  64.  
  65. session = ConnectionRegistry.get_session(f.client)
  66. session['hostname'] = hostname
  67. session['blocks'] = blocks
  68.  
  69. peers = [ server[1] for server in (yield r2) ]
  70. yield add_peers(peers)
  71.  
  72. @defer.inlineCallbacks
  73. def add_peers(peers):
  74. for peer in peers:
  75. if peer not in reg:
  76. print "Adding peer", peer
  77. yield connect(peer)
  78.  
  79. def check_blocks():
  80. #print "Checking if all servers have up-to-date blockchain"
  81. max_blocks = 0
  82. for f in reg.values():
  83. session = ConnectionRegistry.get_session(f.client)
  84. max_blocks = max(max_blocks, session.get('blocks', 0))
  85.  
  86. for f in reg.values():
  87. session = ConnectionRegistry.get_session(f.client)
  88. if session['blocks'] < max_blocks - 1:
  89. print '!!!', session['hostname'], '(%d) is behind other servers (%d)!' % (session['blocks'], max_blocks)
  90.  
  91. reactor.callLater(10, check_blocks)
  92.  
  93. if __name__ == '__main__':
  94. connect('california.stratum.bitcoin.cz')
  95. connect('ecdsa.org')
  96. check_blocks()
  97. reactor.run()
RAW Paste Data