Permalink
Browse files

Remove spaces from empty lines

  • Loading branch information...
1 parent 11f4954 commit 085b630f641c9e31fdae30f74b64637e6029d876 @CFSworks CFSworks committed Nov 3, 2011
View
@@ -1,4 +1,4 @@
-# Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
+# Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
# CFSworks <CFSworks@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -39,15 +39,15 @@ def formatNumber(n):
else:
break
return '%s%s %s' % (whole, decimal, prefixes[i])
-
+
class ConsoleLogger(object):
"""This class will handle printing messages to the console."""
-
+
TIME_FORMAT = '[%d/%m/%Y %H:%M:%S]'
-
+
UPDATE_TIME = 1.0
-
- def __init__(self, miner, verbose=False):
+
+ def __init__(self, miner, verbose=False):
self.verbose = verbose
self.miner = miner
self.lastUpdate = time() - 1
@@ -56,25 +56,25 @@ def __init__(self, miner, verbose=False):
self.invalid = 0
self.lineLength = 0
self.connectionType = None
-
+
def reportRate(self, rate, update=True):
"""Used to tell the logger the current Khash/sec."""
self.rate = rate
if update:
self.updateStatus()
-
+
def reportType(self, type):
self.connectionType = type
-
+
def reportBlock(self, block):
self.log('Currently on block: ' + str(block))
-
+
def reportFound(self, hash, accepted):
if accepted:
self.accepted += 1
else:
self.invalid += 1
-
+
hexHash = hash[::-1]
hexHash = hexHash[:8].encode('hex')
if self.verbose:
@@ -83,23 +83,23 @@ def reportFound(self, hash, accepted):
else:
self.log('Result: %s %s' % (hexHash[8:],
'accepted' if accepted else 'rejected'))
-
+
def reportMsg(self, message):
self.log(('MSG: ' + message), True, True)
-
+
def reportConnected(self, connected):
if connected:
self.log('Connected to server')
else:
self.log('Disconnected from server')
-
+
def reportConnectionFailed(self):
self.log('Failed to connect, retrying...')
-
+
def reportDebug(self, message):
if self.verbose:
self.log(message)
-
+
def updateStatus(self, force=False):
#only update if last update was more than a second ago
dt = time() - self.lastUpdate
@@ -112,7 +112,7 @@ def updateStatus(self, force=False):
"[" + str(self.invalid) + " Rejected]" + type)
self.say(status)
self.lastUpdate = time()
-
+
def say(self, message, newLine=False, hideTimestamp=False):
#add new line if requested
if newLine:
@@ -121,9 +121,9 @@ def say(self, message, newLine=False, hideTimestamp=False):
timestamp = ''
else:
timestamp = datetime.now().strftime(self.TIME_FORMAT) + ' '
-
+
message = timestamp + message
-
+
#erase the previous line
if self.lineLength > 0:
sys.stdout.write('\b \b' * self.lineLength)
@@ -133,7 +133,7 @@ def say(self, message, newLine=False, hideTimestamp=False):
#print the line
sys.stdout.write(message)
sys.stdout.flush()
-
+
#cache the current line length
if newLine:
self.lineLength = 0
@@ -144,4 +144,3 @@ def log(self, message, update=True, hideTimestamp=False):
self.say(message, True, hideTimestamp)
if update:
self.updateStatus(True)
-
View
@@ -1,4 +1,4 @@
-# Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
+# Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
# CFSworks <CFSworks@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -32,7 +32,7 @@ class KernelOption(object):
"""This works like a property, and is used in defining easy option tables
for kernels.
"""
-
+
def __init__(self, name, type, help=None, default=REQUIRED,
advanced=False, **kwargs):
self.localValues = {}
@@ -41,134 +41,134 @@ def __init__(self, name, type, help=None, default=REQUIRED,
self.help = help
self.default = default
self.advanced = advanced
-
+
def __get__(self, instance, owner):
if instance in self.localValues:
return self.localValues[instance]
else:
return instance.interface._getOption(
self.name, self.type, self.default)
-
+
def __set__(self, instance, value):
self.localValues[instance] = value
class CoreInterface(object):
"""An internal class provided for kernels to use when reporting info for
one core.
-
+
Only KernelInterface should create this.
"""
-
+
def __init__(self, kernelInterface):
self.kernelInterface = kernelInterface
self.averageSamples = []
self.kernelInterface.miner._addCore(self)
-
+
def updateRate(self, rate):
"""Called by a kernel core to report its current rate."""
-
+
numSamples = self.kernelInterface.miner.options.getAvgSamples()
-
+
self.averageSamples.append(rate)
self.averageSamples = self.averageSamples[-numSamples:]
-
+
self.kernelInterface.miner.updateAverage()
-
+
def getRate(self):
"""Retrieve the average rate for this core."""
-
+
if not self.averageSamples:
return 0
-
+
return sum(self.averageSamples)/len(self.averageSamples)
-
+
def getKernelInterface(self):
return self.kernelInterface
-
+
class KernelInterface(object):
"""This is an object passed to kernels as an API back to the Phoenix
framework.
"""
-
+
def __init__(self, miner):
self.miner = miner
self._core = None
-
+
def _getOption(self, name, type, default):
"""KernelOption uses this to read the actual value of the option."""
if not name in self.miner.options.kernelOptions:
if default == REQUIRED:
self.fatal('Required option %s not provided!' % name)
else:
return default
-
+
givenOption = self.miner.options.kernelOptions[name]
if type == bool:
# The following are considered true
return givenOption is None or \
givenOption.lower() in ('t', 'true', 'on', '1', 'y', 'yes')
-
+
try:
return type(givenOption)
except (TypeError, ValueError):
self.fatal('Option %s expects a value of type %s!' % (name, type))
-
+
def getRevision(self):
"""Return the Phoenix core revision, so that kernels can require a
minimum revision before operating (such as if they rely on a certain
feature added in a certain revision)
"""
-
+
return self.miner.REVISION
-
+
def setWorkFactor(self, workFactor):
"""Deprecated. Kernels are now responsible for requesting optimal size
work"""
-
+
def setMeta(self, var, value):
"""Set metadata for this kernel."""
-
+
self.miner.connection.setMeta(var, value)
-
+
def fetchRange(self, size=None):
"""Fetch a range from the WorkQueue, optionally specifying a size
(in nonces) to include in the range.
"""
-
+
if size is None:
return self.miner.queue.fetchRange()
else:
return self.miner.queue.fetchRange(size)
-
+
def addStaleCallback(self, callback):
"""Register a new function to be called, with no arguments, whenever
a new block comes out that would render all previous work stale,
requiring a kernel to switch immediately.
"""
-
+
# This should be implemented in a better way in the future...
if callback not in self.miner.queue.staleCallbacks:
self.miner.queue.staleCallbacks.append(callback)
-
+
def removeStaleCallback(self, callback):
"""Undo an addStaleCallback."""
-
+
# Likewise.
if callback in self.miner.queue.staleCallbacks:
self.miner.queue.staleCallbacks.remove(callback)
-
+
def addCore(self):
"""Return a CoreInterface for a new core."""
return CoreInterface(self)
-
+
def checkTarget(self, hash, target):
"""Utility function that the kernel can use to see if a nonce meets a
target before sending it back to the core.
-
+
Since the target is checked before submission anyway, this is mostly
intended to be used in hardware sanity-checks.
"""
-
+
# This for loop compares the bytes of the target and hash in reverse
# order, because both are 256-bit little endian.
for t,h in zip(target[::-1], hash[::-1]):
@@ -177,7 +177,7 @@ def checkTarget(self, hash, target):
elif ord(t) < ord(h):
return False
return True
-
+
def calculateHash(self, nr, nonce):
"""Given a NonceRange and a nonce, calculate the SHA-256 hash of the
solution. The resulting hash is returned as a string, which may be
@@ -187,28 +187,28 @@ def calculateHash(self, nr, nonce):
# accidentally set bits outside of the 32-bit space. If the resulting
# nonce is invalid, it will be caught anyway...
nonce &= 0xFFFFFFFF
-
+
staticDataUnpacked = unpack('<' + 'I'*19, nr.unit.data[:76])
staticData = pack('>' + 'I'*19, *staticDataUnpacked)
hashInput = pack('>76sI', staticData, nonce)
return sha256(sha256(hashInput).digest()).digest()
-
+
def foundNonce(self, nr, nonce):
"""Called by kernels when they may have found a nonce."""
-
+
# Sometimes kernels send weird nonces down the pipe. We can assume they
# accidentally set bits outside of the 32-bit space. If the resulting
# nonce is invalid, it will be caught anyway...
nonce &= 0xFFFFFFFF
-
+
# Check if the block has changed while this NonceRange was being
# processed by the kernel. If so, don't send it to the server.
if self.miner.queue.isRangeStale(nr):
return False
-
+
# Check if the hash meets the full difficulty before sending.
hash = self.calculateHash(nr, nonce)
-
+
if self.checkTarget(hash, nr.unit.target):
formattedResult = pack('<76sI', nr.unit.data[:76], nonce)
d = self.miner.connection.sendResult(formattedResult)
@@ -220,22 +220,22 @@ def callback(accepted):
self.miner.logger.reportDebug("Result didn't meet full "
"difficulty, not sending")
return False
-
+
def debug(self, msg):
"""Log information as debug so that it can be viewed only when -v is
enabled.
"""
self.miner.logger.reportDebug(msg)
-
+
def log(self, msg, withTimestamp=True, withIdentifier=True):
"""Log some general kernel information to the console."""
self.miner.logger.log(msg, True, not withTimestamp)
-
+
def error(self, msg=None):
"""The kernel has an issue that requires user attention."""
if msg is not None:
self.miner.logger.log('Kernel error: ' + msg)
-
+
def fatal(self, msg=None):
"""The kernel has an issue that is preventing it from continuing to
operate.
@@ -245,4 +245,3 @@ def fatal(self, msg=None):
if reactor.running:
reactor.stop()
os._exit(0)
-
Oops, something went wrong.

0 comments on commit 085b630

Please sign in to comment.