Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. # Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
  2. # CFSworks <CFSworks@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21.  
  22. from time import time
  23. import platform
  24. import os
  25.  
  26. from twisted.internet import reactor
  27.  
  28. from minerutil.MMPProtocol import MMPClient
  29. from KernelInterface import KernelInterface
  30.  
  31. class Miner(object):
  32. """The main managing class for the miner itself."""
  33.  
  34. # This gets updated automatically by SVN.
  35. REVISION = int('$Rev: 101 $'[6:-2])
  36. VERSION = '1.50'
  37.  
  38. def __init__(self):
  39. self.logger = None
  40. self.options = None
  41. self.connection = None
  42. self.kernel = None
  43. self.queue = None
  44. self.idle = True
  45.  
  46. self.cores = []
  47. self.lastMetaRate = 0.0
  48. self.lastRateUpdate = time()
  49.  
  50. # Connection callbacks...
  51. def onFailure(self):
  52. self.logger.reportConnectionFailed()
  53. def onConnect(self):
  54. self.logger.reportConnected(True)
  55. def onDisconnect(self):
  56. self.logger.reportConnected(False)
  57. def onBlock(self, block):
  58. self.logger.reportBlock(block)
  59. def onMsg(self, msg):
  60. self.logger.reportMsg(msg)
  61. def onWork(self, work):
  62. self.logger.reportDebug('Server gave new work; passing to WorkQueue')
  63. self.queue.storeWork(work)
  64. def onLongpoll(self, lp):
  65. self.logger.reportType('RPC' + (' (+LP)' if lp else ''))
  66. def onPush(self, ignored):
  67. self.logger.log('LP: New work pushed')
  68.  
  69. def start(self, options):
  70. """Configures the Miner via the options specified and begins mining."""
  71.  
  72. self.options = options
  73.  
  74. self.logger = self.options.makeLogger(self, self)
  75. self.connection = self.options.makeConnection(self)
  76. self.kernel = self.options.makeKernel(KernelInterface(self))
  77. self.queue = self.options.makeQueue(self)
  78.  
  79. #log a message to let the user know that phoenix is starting
  80. self.logger.log("Phoenix %s starting..." % self.VERSION)
  81.  
  82. #this will need to be changed to add new protocols
  83. if isinstance(self.connection, MMPClient):
  84. self.logger.reportType('MMP')
  85. else:
  86. self.logger.reportType('RPC')
  87.  
  88. self.applyMeta()
  89.  
  90. # Go!
  91. self.connection.connect()
  92. self.kernel.start()
  93. reactor.addSystemEventTrigger('before', 'shutdown', self.kernel.stop)
  94.  
  95. def applyMeta(self):
  96. """Applies any static metafields to the connection, such as version,
  97. kernel, hardware, etc.
  98. """
  99.  
  100. # It's important to note here that the name is already put in place by
  101. # the Options's makeConnection function, since the Options knows the
  102. # user's desired name for this miner anyway.
  103.  
  104. self.connection.setVersion(
  105. 'phoenix', 'Phoenix Miner', self.VERSION)
  106. system = platform.system() + ' ' + platform.version()
  107. self.connection.setMeta('os', system)
  108.  
  109. def _addCore(self, core):
  110. """Temporary function. Practically already deprecated."""
  111. self.cores.append(core)
  112.  
  113. #used by WorkQueue to report when the miner is idle
  114. def reportIdle(self, idle):
  115.  
  116. #if idle status has changed force an update
  117. if self.idle != idle:
  118. if idle:
  119. self.idle = idle
  120. self.logger.log("Warning: work queue empty, miner is idle")
  121. self.logger.reportRate(0, True)
  122. self.connection.setMeta('rate', 0)
  123. self.lastMetaRate = time()
  124. if reactor.running:
  125. reactor.stop()
  126. os._exit(0)
  127. self.idleFixer()
  128. else:
  129. self.idle = idle
  130. self.logger.updateStatus(True)
  131.  
  132. #since i can't find the actual cause of the idle bug im going to add a simple
  133. #workaround that spams work requests every 15 seconds while idle.
  134. def idleFixer(self):
  135. if self.idle:
  136. self.connection.requestWork()
  137. reactor.callLater(15, self.idleFixer)
  138.  
  139. def updateAverage(self):
  140. #Query all mining cores for their Khash/sec rate and sum.
  141.  
  142. total = 0
  143. if not self.idle:
  144. for core in self.cores:
  145. total += core.getRate()
  146.  
  147. self.logger.reportRate(total)
  148.  
  149. # Let's not spam the server with rate messages.
  150. if self.lastMetaRate+30 < time():
  151. self.connection.setMeta('rate', total)
  152. self.lastMetaRate = time()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement