Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 204 lines (173 sloc) 7.636 kb
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
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 import platform
23 from time import time
24 from twisted.internet import reactor
25 from minerutil.MMPProtocol import MMPClient
26 from KernelInterface import KernelInterface
27
28 #The main managing class for the miner itself.
29 class Miner(object):
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
30
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
31 # This must be manually set for Git
a658ef2 jedi95 Several fixes for X-Work-Identifier. Bump version to 1.7.5
jedi95 authored
32 VER = (1, 7, 5)
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
33 REVISION = reduce(lambda x,y: x*100+y, VER)
34 VERSION = 'v%s' % '.'.join(str(x) for x in VER)
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
35
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
36 def __init__(self):
37 self.logger = None
38 self.options = None
39 self.connection = None
40 self.kernel = None
41 self.queue = None
42 self.idle = True
43 self.cores = []
44 self.backup = False
45 self.failures = 0
46 self.lastMetaRate = 0.0
47 self.lastRateUpdate = time()
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
48
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
49 # Connection callbacks...
50 def onFailure(self):
51 self.logger.reportConnectionFailed()
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
52
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
53 #handle failover if url2 has been specified
54 if self.options.url2 is not None:
55 self.failoverCheck()
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
56
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
57 def onConnect(self):
58 self.logger.reportConnected(True)
59 def onDisconnect(self):
60 self.logger.reportConnected(False)
61 def onBlock(self, block):
62 self.logger.reportBlock(block)
63 def onMsg(self, msg):
64 self.logger.reportMsg(msg)
65 def onWork(self, work):
66 self.logger.reportDebug('Server gave new work; passing to WorkQueue')
67 self.queue.storeWork(work)
68 def onLongpoll(self, lp):
69 self.logger.reportType('RPC' + (' (+LP)' if lp else ''))
70 def onPush(self, ignored):
71 self.logger.log('LP: New work pushed')
72 def onLog(self, message):
73 self.logger.log(message)
74 def onDebug(self, message):
75 self.logger.reportDebug(message)
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
76
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
77 def failoverCheck(self):
78 if self.backup:
79 if (self.failures >= 1):
80 #disconnect and set connection to none
81 self.connection.disconnect()
82 self.connection = None
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
83
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
84 #log
85 self.logger.log("Backup server failed,")
86 self.logger.log("attempting to return to primary server.")
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
87
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
88 #reset failure count and return to primary server
89 self.failures = 0
90 self.backup = False
91 self.connection = self.options.makeConnection(self)
92 self.connection.connect()
93 else:
94 self.failures += 1
95 else:
96 #The main pool must fail 3 times before moving to the backup pool
97 if (self.failures >= 2):
98 #disconnect and set connection to none
99 self.connection.disconnect()
100 self.connection = None
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
101
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
102 #log
103 self.logger.log("Primary server failed too many times,")
104 self.logger.log("attempting to connect to backup server.")
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
105
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
106 #reset failure count and connect to backup server
107 self.failures = 0
108 self.backup = True
109 self.connection = self.options.makeConnection(self, True)
110 self.connection.connect()
111 else:
112 self.failures += 1
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
113
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
114 #since the main pool may fail from time to time, decrement the
115 #failure count after 5 minutes so we don't end up moving to the
116 #back pool when it isn't nessesary
117 def decrementFailures():
118 if self.failures > 1 and (not self.backup):
119 self.failures -= 1
120 reactor.callLater(300, decrementFailures)
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
121
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
122 def start(self, options):
123 #Configures the Miner via the options specified and begins mining.
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
124
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
125 self.options = options
126 self.logger = self.options.makeLogger(self, self)
127 self.connection = self.options.makeConnection(self)
128 self.kernel = self.options.makeKernel(KernelInterface(self))
129 self.queue = self.options.makeQueue(self)
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
130
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
131 #log a message to let the user know that phoenix is starting
132 self.logger.log("Phoenix %s starting..." % self.VERSION)
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
133
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
134 #this will need to be changed to add new protocols
135 if isinstance(self.connection, MMPClient):
136 self.logger.reportType('MMP')
137 else:
138 self.logger.reportType('RPC')
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
139
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
140 self.applyMeta()
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
141
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
142 # Go!
143 self.connection.connect()
144 self.kernel.start()
11f4954 Sam Edwards Have the connection close upon Phoenix shutdown...
CFSworks authored
145 reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown)
146
147 def shutdown(self):
148 """Disconnect from the server and kill the kernel."""
149 self.kernel.stop()
150 self.connection.disconnect()
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
151
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
152 def applyMeta(self):
153 #Applies any static metafields to the connection, such as version,
154 #kernel, hardware, etc.
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
155
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
156 # It's important to note here that the name is already put in place by
157 # the Options's makeConnection function, since the Options knows the
158 # user's desired name for this miner anyway.
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
159
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
160 self.connection.setVersion(
161 'phoenix', 'Phoenix Miner', self.VERSION)
162 system = platform.system() + ' ' + platform.version()
163 self.connection.setMeta('os', system)
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
164
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
165 #called by CoreInterface to add cores for total hashrate calculation
166 def _addCore(self, core):
167 self.cores.append(core)
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
168
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
169 #used by WorkQueue to report when the miner is idle
170 def reportIdle(self, idle):
171
172 #if idle status has changed force an update
173 if self.idle != idle:
174 if idle:
175 self.idle = idle
176 self.logger.log("Warning: work queue empty, miner is idle")
177 self.logger.reportRate(0, True)
178 self.connection.setMeta('rate', 0)
179 self.lastMetaRate = time()
180 self.idleFixer()
181 else:
182 self.idle = idle
183 self.logger.updateStatus(True)
184
185 #request work from the protocol every 15 seconds while idle
186 def idleFixer(self):
187 if self.idle:
188 self.connection.requestWork()
189 reactor.callLater(15, self.idleFixer)
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
190
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
191 def updateAverage(self):
192 #Query all mining cores for their Khash/sec rate and sum.
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
193
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
194 total = 0
195 if not self.idle:
196 for core in self.cores:
197 total += core.getRate()
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
198
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
199 self.logger.reportRate(total)
085b630 Sam Edwards Remove spaces from empty lines
CFSworks authored
200
30f8386 jedi95 Screwed up with Git big time, had to redo repo.
jedi95 authored
201 # Let's not spam the server with rate messages.
202 if self.lastMetaRate+30 < time():
203 self.connection.setMeta('rate', total)
204 self.lastMetaRate = time()
Something went wrong with that request. Please try again.