Guest User

Untitled

a guest
Aug 2nd, 2017
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.69 KB | None | 0 0
  1. #!/usr/bin/python
  2. import base64, binascii, json, hashlib, hmac, math, socket, struct, sys, threading, time, urlparse
  3.  
  4. # DayMiner (ah-ah-ah), fighter of the...
  5. USER_AGENT = "NightMiner"
  6. VERSION = [0, 1]
  7.  
  8. # You're a master of Karate and friendship for everyone.
  9.  
  10.  
  11. # Which algorithm for proof-of-work to use
  12. ALGORITHM_SCRYPT = 'scrypt'
  13. ALGORITHM_SHA256D = 'sha256d'
  14.  
  15. ALGORITHMS = [ ALGORITHM_SCRYPT, ALGORITHM_SHA256D ]
  16.  
  17.  
  18. # Verbosity and log level
  19. QUIET = False
  20. DEBUG = False
  21. DEBUG_PROTOCOL = False
  22.  
  23. LEVEL_PROTOCOL = 'protocol'
  24. LEVEL_INFO = 'info'
  25. LEVEL_DEBUG = 'debug'
  26. LEVEL_ERROR = 'error'
  27.  
  28.  
  29. # These control which scrypt implementation to use
  30. SCRYPT_LIBRARY_AUTO = 'auto'
  31. SCRYPT_LIBRARY_LTC = 'ltc_scrypt (https://github.com/forrestv/p2pool)'
  32. SCRYPT_LIBRARY_SCRYPT = 'scrypt (https://pypi.python.org/pypi/scrypt/)'
  33. SCRYPT_LIBRARY_PYTHON = 'pure python'
  34. SCRYPT_LIBRARIES = [ SCRYPT_LIBRARY_AUTO, SCRYPT_LIBRARY_LTC, SCRYPT_LIBRARY_SCRYPT, SCRYPT_LIBRARY_PYTHON ]
  35.  
  36.  
  37. def log(message, level):
  38. '''Conditionally write a message to stdout based on command line options and level.'''
  39.  
  40. global DEBUG
  41. global DEBUG_PROTOCOL
  42. global QUIET
  43.  
  44. if QUIET and level != LEVEL_ERROR: return
  45. if not DEBUG_PROTOCOL and level == LEVEL_PROTOCOL: return
  46. if not DEBUG and level == LEVEL_DEBUG: return
  47.  
  48. if level != LEVEL_PROTOCOL: message = '[%s] %s' % (level.upper(), message)
  49.  
  50. print ("[%s] %s" % (time.strftime("%Y-%m-%d %H:%M:%S"), message))
  51.  
  52.  
  53. # Convert from/to binary and hexidecimal strings (could be replaced with .encode('hex') and .decode('hex'))
  54. hexlify = binascii.hexlify
  55. unhexlify = binascii.unhexlify
  56.  
  57.  
  58. def sha256d(message):
  59. '''Double SHA256 Hashing function.'''
  60.  
  61. return hashlib.sha256(hashlib.sha256(message).digest()).digest()
  62.  
  63.  
  64. def swap_endian_word(hex_word):
  65. '''Swaps the endianness of a hexidecimal string of a word and converts to a binary string.'''
  66.  
  67. message = unhexlify(hex_word)
  68. if len(message) != 4: raise ValueError('Must be 4-byte word')
  69. return message[::-1]
  70.  
  71.  
  72. def swap_endian_words(hex_words):
  73. '''Swaps the endianness of a hexidecimal string of words and converts to binary string.'''
  74.  
  75. message = unhexlify(hex_words)
  76. if len(message) % 4 != 0: raise ValueError('Must be 4-byte word aligned')
  77. return ''.join([ message[4 * i: 4 * i + 4][::-1] for i in range(0, len(message) // 4) ])
  78.  
  79.  
  80. def human_readable_hashrate(hashrate):
  81. '''Returns a human readable representation of hashrate.'''
  82.  
  83. if hashrate < 1000:
  84. return '%2f hashes/s' % hashrate
  85. if hashrate < 10000000:
  86. return '%2f khashes/s' % (hashrate / 1000)
  87. if hashrate < 10000000000:
  88. return '%2f Mhashes/s' % (hashrate / 1000000)
  89. return '%2f Ghashes/s' % (hashrate / 1000000000)
  90.  
  91.  
  92. def scrypt(password, salt, N, r, p, dkLen):
  93. """Returns the result of the scrypt password-based key derivation function.
  94.  
  95. This is used as the foundation of the proof-of-work for litecoin and other
  96. scrypt-based coins, using the parameters:
  97. password = bloack_header
  98. salt = block_header
  99. N = 1024
  100. r = 1
  101. p = 1
  102. dkLen = 256 bits (=32 bytes)
  103.  
  104. Please note, that this is a pure Python implementation, and is slow. VERY
  105. slow. It is meant only for completeness of a pure-Python, one file stratum
  106. server for Litecoin.
  107.  
  108. I have included the ltc_scrypt C-binding from p2pool (https://github.com/forrestv/p2pool)
  109. which is several thousand times faster. The server will automatically attempt to load
  110. the faster module (use set_scrypt_library to choose a specific library).
  111. """
  112.  
  113. def array_overwrite(source, source_start, dest, dest_start, length):
  114. '''Overwrites the dest array with the source array.'''
  115.  
  116. for i in xrange(0, length):
  117. dest[dest_start + i] = source[source_start + i]
  118.  
  119.  
  120. def blockxor(source, source_start, dest, dest_start, length):
  121. '''Performs xor on arrays source and dest, storing the result back in dest.'''
  122.  
  123. for i in xrange(0, length):
  124. dest[dest_start + i] = chr(ord(dest[dest_start + i]) ^ ord(source[source_start + i]))
  125.  
  126.  
  127. def pbkdf2(passphrase, salt, count, dkLen, prf):
  128. '''Returns the result of the Password-Based Key Derivation Function 2.
  129.  
  130. See http://en.wikipedia.org/wiki/PBKDF2
  131. '''
  132.  
  133. def f(block_number):
  134. '''The function "f".'''
  135.  
  136. U = prf(passphrase, salt + struct.pack('>L', block_number))
  137.  
  138. # Not used for scrpyt-based coins, could be removed, but part of a more general solution
  139. if count > 1:
  140. U = [ c for c in U ]
  141. for i in xrange(2, 1 + count):
  142. blockxor(prf(passphrase, ''.join(U)), 0, U, 0, len(U))
  143. U = ''.join(U)
  144.  
  145. return U
  146.  
  147. # PBKDF2 implementation
  148. size = 0
  149.  
  150. block_number = 0
  151. blocks = [ ]
  152.  
  153. # The iterations
  154. while size < dkLen:
  155. block_number += 1
  156. block = f(block_number)
  157.  
  158. blocks.append(block)
  159. size += len(block)
  160.  
  161. return ''.join(blocks)[:dkLen]
  162.  
  163. def integerify(B, Bi, r):
  164. '''"A bijective function from ({0, 1} ** k) to {0, ..., (2 ** k) - 1".'''
  165.  
  166. Bi += (2 * r - 1) * 64
  167. n = ord(B[Bi]) | (ord(B[Bi + 1]) << 8) | (ord(B[Bi + 2]) << 16) | (ord(B[Bi + 3]) << 24)
  168. return n
  169.  
  170.  
  171. def make_int32(v):
  172. '''Converts (truncates, two's compliments) a number to an int32.'''
  173.  
  174. if v > 0x7fffffff: return -1 * ((~v & 0xffffffff) + 1)
  175. return v
  176.  
  177.  
  178. def R(X, destination, a1, a2, b):
  179. '''A single round of Salsa.'''
  180.  
  181. a = (X[a1] + X[a2]) & 0xffffffff
  182. X[destination] ^= ((a << b) | (a >> (32 - b)))
  183.  
  184.  
  185. def salsa20_8(B):
  186. '''Salsa 20/8 stream cypher; Used by BlockMix. See http://en.wikipedia.org/wiki/Salsa20'''
  187.  
  188. # Convert the character array into an int32 array
  189. B32 = [ make_int32((ord(B[i * 4]) | (ord(B[i * 4 + 1]) << 8) | (ord(B[i * 4 + 2]) << 16) | (ord(B[i * 4 + 3]) << 24))) for i in xrange(0, 16) ]
  190. x = [ i for i in B32 ]
  191.  
  192. # Salsa... Time to dance.
  193. for i in xrange(8, 0, -2):
  194. R(x, 4, 0, 12, 7); R(x, 8, 4, 0, 9); R(x, 12, 8, 4, 13); R(x, 0, 12, 8, 18)
  195. R(x, 9, 5, 1, 7); R(x, 13, 9, 5, 9); R(x, 1, 13, 9, 13); R(x, 5, 1, 13, 18)
  196. R(x, 14, 10, 6, 7); R(x, 2, 14, 10, 9); R(x, 6, 2, 14, 13); R(x, 10, 6, 2, 18)
  197. R(x, 3, 15, 11, 7); R(x, 7, 3, 15, 9); R(x, 11, 7, 3, 13); R(x, 15, 11, 7, 18)
  198. R(x, 1, 0, 3, 7); R(x, 2, 1, 0, 9); R(x, 3, 2, 1, 13); R(x, 0, 3, 2, 18)
  199. R(x, 6, 5, 4, 7); R(x, 7, 6, 5, 9); R(x, 4, 7, 6, 13); R(x, 5, 4, 7, 18)
  200. R(x, 11, 10, 9, 7); R(x, 8, 11, 10, 9); R(x, 9, 8, 11, 13); R(x, 10, 9, 8, 18)
  201. R(x, 12, 15, 14, 7); R(x, 13, 12, 15, 9); R(x, 14, 13, 12, 13); R(x, 15, 14, 13, 18)
  202.  
  203. # Coerce into nice happy 32-bit integers
  204. B32 = [ make_int32(x[i] + B32[i]) for i in xrange(0, 16) ]
  205.  
  206. # Convert back to bytes
  207. for i in xrange(0, 16):
  208. B[i * 4 + 0] = chr((B32[i] >> 0) & 0xff)
  209. B[i * 4 + 1] = chr((B32[i] >> 8) & 0xff)
  210. B[i * 4 + 2] = chr((B32[i] >> 16) & 0xff)
  211. B[i * 4 + 3] = chr((B32[i] >> 24) & 0xff)
  212.  
  213.  
  214. def blockmix_salsa8(BY, Bi, Yi, r):
  215. '''Blockmix; Used by SMix.'''
  216.  
  217. start = Bi + (2 * r - 1) * 64
  218. X = [ BY[i] for i in xrange(start, start + 64) ] # BlockMix - 1
  219.  
  220. for i in xrange(0, 2 * r): # BlockMix - 2
  221. blockxor(BY, i * 64, X, 0, 64) # BlockMix - 3(inner)
  222. salsa20_8(X) # BlockMix - 3(outer)
  223. array_overwrite(X, 0, BY, Yi + (i * 64), 64) # BlockMix - 4
  224.  
  225. for i in xrange(0, r): # BlockMix - 6 (and below)
  226. array_overwrite(BY, Yi + (i * 2) * 64, BY, Bi + (i * 64), 64)
  227.  
  228. for i in xrange(0, r):
  229. array_overwrite(BY, Yi + (i * 2 + 1) * 64, BY, Bi + (i + r) * 64, 64)
  230.  
  231.  
  232. def smix(B, Bi, r, N, V, X):
  233. '''SMix; a specific case of ROMix. See scrypt.pdf in the links above.'''
  234.  
  235. array_overwrite(B, Bi, X, 0, 128 * r) # ROMix - 1
  236.  
  237. for i in xrange(0, N): # ROMix - 2
  238. array_overwrite(X, 0, V, i * (128 * r), 128 * r) # ROMix - 3
  239. blockmix_salsa8(X, 0, 128 * r, r) # ROMix - 4
  240.  
  241. for i in xrange(0, N): # ROMix - 6
  242. j = integerify(X, 0, r) & (N - 1) # ROMix - 7
  243. blockxor(V, j * (128 * r), X, 0, 128 * r) # ROMix - 8(inner)
  244. blockmix_salsa8(X, 0, 128 * r, r) # ROMix - 9(outer)
  245.  
  246. array_overwrite(X, 0, B, Bi, 128 * r) # ROMix - 10
  247.  
  248.  
  249. # Scrypt implementation. Significant thanks to https://github.com/wg/scrypt
  250. if N < 2 or (N & (N - 1)): raise ValueError('Scrypt N must be a power of 2 greater than 1')
  251.  
  252. prf = lambda k, m: hmac.new(key = k, msg = m, digestmod = hashlib.sha256).digest()
  253.  
  254. DK = [ chr(0) ] * dkLen
  255.  
  256. B = [ c for c in pbkdf2(password, salt, 1, p * 128 * r, prf) ]
  257. XY = [ chr(0) ] * (256 * r)
  258. V = [ chr(0) ] * (128 * r * N)
  259.  
  260. for i in xrange(0, p):
  261. smix(B, i * 128 * r, r, N, V, XY)
  262.  
  263. return pbkdf2(password, ''.join(B), 1, dkLen, prf)
  264.  
  265.  
  266. SCRYPT_LIBRARY = None
  267. scrypt_proof_of_work = None
  268. def set_scrypt_library(library = SCRYPT_LIBRARY_AUTO):
  269. '''Sets the scrypt library implementation to use.'''
  270.  
  271. global SCRYPT_LIBRARY
  272. global scrypt_proof_of_work
  273.  
  274. if library == SCRYPT_LIBRARY_LTC:
  275. import ltc_scrypt
  276. scrypt_proof_of_work = ltc_scrypt.getPoWHash
  277. SCRYPT_LIBRARY = library
  278.  
  279. elif library == SCRYPT_LIBRARY_SCRYPT:
  280. import scrypt as NativeScrypt
  281. scrypt_proof_of_work = lambda header: NativeScrypt.hash(header, header, 1024, 1, 1, 32)
  282. SCRYPT_LIBRARY = library
  283.  
  284. # Try to load a faster version of scrypt before using the pure-Python implementation
  285. elif library == SCRYPT_LIBRARY_AUTO:
  286. try:
  287. set_scrypt_library(SCRYPT_LIBRARY_LTC)
  288. except Exception, e:
  289. try:
  290. set_scrypt_library(SCRYPT_LIBRARY_SCRYPT)
  291. except Exception, e:
  292. set_scrypt_library(SCRYPT_LIBRARY_PYTHON)
  293.  
  294. else:
  295. scrypt_proof_of_work = lambda header: scrypt(header, header, 1024, 1, 1, 32)
  296. SCRYPT_LIBRARY = library
  297.  
  298. set_scrypt_library()
  299.  
  300.  
  301. class Job(object):
  302. '''Encapsulates a Job from the network and necessary helper methods to mine.
  303.  
  304. "If you have a procedure with 10 parameters, you probably missed some."
  305. ~Alan Perlis
  306. '''
  307.  
  308. def __init__(self, job_id, prevhash, coinb1, coinb2, merkle_branches, version, nbits, ntime, target, extranounce1, extranounce2_size, proof_of_work):
  309.  
  310. # Job parts from the mining.notify command
  311. self._job_id = job_id
  312. self._prevhash = prevhash
  313. self._coinb1 = coinb1
  314. self._coinb2 = coinb2
  315. self._merkle_branches = [ b for b in merkle_branches ]
  316. self._version = version
  317. self._nbits = nbits
  318. self._ntime = ntime
  319.  
  320. # Job information needed to mine from mining.subsribe
  321. self._target = target
  322. self._extranounce1 = extranounce1
  323. self._extranounce2_size = extranounce2_size
  324.  
  325. # Proof of work algorithm
  326. self._proof_of_work = proof_of_work
  327.  
  328. # Flag to stop this job's mine coroutine
  329. self._done = False
  330.  
  331. # Hash metrics (start time, delta time, total hashes)
  332. self._dt = 0.0
  333. self._hash_count = 0
  334.  
  335. # Accessors
  336. id = property(lambda s: s._job_id)
  337. prevhash = property(lambda s: s._prevhash)
  338. coinb1 = property(lambda s: s._coinb1)
  339. coinb2 = property(lambda s: s._coinb2)
  340. merkle_branches = property(lambda s: [ b for b in s._merkle_branches ])
  341. version = property(lambda s: s._version)
  342. nbits = property(lambda s: s._nbits)
  343. ntime = property(lambda s: s._ntime)
  344.  
  345. target = property(lambda s: s._target)
  346. extranounce1 = property(lambda s: s._extranounce1)
  347. extranounce2_size = property(lambda s: s._extranounce2_size)
  348.  
  349. proof_of_work = property(lambda s: s._proof_of_work)
  350.  
  351.  
  352. @property
  353. def hashrate(self):
  354. '''The current hashrate, or if stopped hashrate for the job's lifetime.'''
  355.  
  356. if self._dt == 0: return 0.0
  357. return self._hash_count / self._dt
  358.  
  359.  
  360. def merkle_root_bin(self, extranounce2_bin):
  361. '''Builds a merkle root from the merkle tree'''
  362.  
  363. coinbase_bin = unhexlify(self._coinb1) + unhexlify(self._extranounce1) + extranounce2_bin + unhexlify(self._coinb2)
  364. coinbase_hash_bin = sha256d(coinbase_bin)
  365.  
  366. merkle_root = coinbase_hash_bin
  367. for branch in self._merkle_branches:
  368. merkle_root = sha256d(merkle_root + unhexlify(branch))
  369. return merkle_root
  370.  
  371.  
  372. def stop(self):
  373. '''Requests the mine coroutine stop after its current iteration.'''
  374.  
  375. self._done = True
  376.  
  377.  
  378. def mine(self, nounce_start = 0, nounce_stride = 1):
  379. '''Returns an iterator that iterates over valid proof-of-work shares.
  380.  
  381. This is a co-routine; that takes a LONG time; the calling thread should look like:
  382.  
  383. for result in job.mine(self):
  384. submit_work(result)
  385.  
  386. nounce_start and nounce_stride are useful for multi-processing if you would like
  387. to assign each process a different starting nounce (0, 1, 2, ...) and a stride
  388. equal to the number of processes.
  389. '''
  390.  
  391. t0 = time.time()
  392.  
  393. # @TODO: test for extranounce != 0... Do I reverse it or not?
  394. for extranounce2 in xrange(0, 0x7fffffff):
  395.  
  396. # Must be unique for any given job id, according to http://mining.bitcoin.cz/stratum-mining/ but never seems enforced?
  397. extranounce2_bin = struct.pack('<I', extranounce2)
  398.  
  399. merkle_root_bin = self.merkle_root_bin(extranounce2_bin)
  400. header_prefix_bin = swap_endian_word(self._version) + swap_endian_words(self._prevhash) + merkle_root_bin + swap_endian_word(self._ntime) + swap_endian_word(self._nbits)
  401. for nounce in xrange(nounce_start, 0x7fffffff, nounce_stride):
  402. # This job has been asked to stop
  403. if self._done:
  404. self._dt += (time.time() - t0)
  405. raise StopIteration()
  406.  
  407. # Proof-of-work attempt
  408. nounce_bin = struct.pack('<I', nounce)
  409. pow = self.proof_of_work(header_prefix_bin + nounce_bin)[::-1].encode('hex')
  410.  
  411. # Did we reach or exceed our target?
  412. if pow <= self.target:
  413. result = dict(
  414. job_id = self.id,
  415. extranounce2 = hexlify(extranounce2_bin),
  416. ntime = str(self._ntime), # Convert to str from json unicode
  417. nounce = hexlify(nounce_bin[::-1])
  418. )
  419. self._dt += (time.time() - t0)
  420.  
  421. yield result
  422.  
  423. t0 = time.time()
  424.  
  425. self._hash_count += 1
  426.  
  427.  
  428. def __str__(self):
  429. return '<Job id=%s prevhash=%s coinb1=%s coinb2=%s merkle_branches=%s version=%s nbits=%s ntime=%s target=%s extranounce1=%s extranounce2_size=%d>' % (self.id, self.prevhash, self.coinb1, self.coinb2, self.merkle_branches, self.version, self.nbits, self.ntime, self.target, self.extranounce1, self.extranounce2_size)
  430.  
  431.  
  432. # Subscription state
  433. class Subscription(object):
  434. '''Encapsulates the Subscription state from the JSON-RPC server'''
  435.  
  436. # Subclasses should override this
  437. def ProofOfWork(header):
  438. raise Exception('Do not use the Subscription class directly, subclass it')
  439.  
  440. class StateException(Exception): pass
  441.  
  442. def __init__(self):
  443. self._id = None
  444. self._difficulty = None
  445. self._extranounce1 = None
  446. self._extranounce2_size = None
  447. self._target = None
  448. self._worker_name = None
  449.  
  450. self._mining_thread = None
  451.  
  452. # Accessors
  453. id = property(lambda s: s._id)
  454. worker_name = property(lambda s: s._worker_name)
  455.  
  456. difficulty = property(lambda s: s._difficulty)
  457. target = property(lambda s: s._target)
  458.  
  459. extranounce1 = property(lambda s: s._extranounce1)
  460. extranounce2_size = property(lambda s: s._extranounce2_size)
  461.  
  462.  
  463. def set_worker_name(self, worker_name):
  464. if self._worker_name:
  465. raise self.StateException('Already authenticated as %r (requesting %r)' % (self._username, username))
  466.  
  467. self._worker_name = worker_name
  468.  
  469.  
  470. def _set_target(self, target):
  471. self._target = '%064x' % target
  472.  
  473.  
  474. def set_difficulty(self, difficulty):
  475. if difficulty < 0: raise self.StateException('Difficulty must be non-negative')
  476.  
  477. # Compute target
  478. if difficulty == 0:
  479. target = 2 ** 256 - 1
  480. else:
  481. target = min(int((0xffff0000 * 2 ** (256 - 64) + 1) / difficulty - 1 + 0.5), 2 ** 256 - 1)
  482.  
  483. self._difficulty = difficulty
  484. self._set_target(target)
  485.  
  486.  
  487. def set_subscription(self, subscription_id, extranounce1, extranounce2_size):
  488. if self._id is not None:
  489. raise self.StateException('Already subscribed')
  490.  
  491. self._id = subscription_id
  492. self._extranounce1 = extranounce1
  493. self._extranounce2_size = extranounce2_size
  494.  
  495.  
  496. def create_job(self, job_id, prevhash, coinb1, coinb2, merkle_branches, version, nbits, ntime):
  497. '''Creates a new Job object populated with all the goodness it needs to mine.'''
  498.  
  499. if self._id is None:
  500. raise self.StateException('Not subscribed')
  501.  
  502. return Job(
  503. job_id = job_id,
  504. prevhash = prevhash,
  505. coinb1 = coinb1,
  506. coinb2 = coinb2,
  507. merkle_branches = merkle_branches,
  508. version = version,
  509. nbits = nbits,
  510. ntime = ntime,
  511. target = self.target,
  512. extranounce1 = self._extranounce1,
  513. extranounce2_size = self.extranounce2_size,
  514. proof_of_work = self.ProofOfWork
  515. )
  516.  
  517.  
  518. def __str__(self):
  519. return '<Subscription id=%s, extranounce1=%s, extranounce2_size=%d, difficulty=%d worker_name=%s>' % (self.id, self.extranounce1, self.extranounce2_size, self.difficulty, self.worker_name)
  520.  
  521.  
  522. class SubscriptionScrypt(Subscription):
  523. '''Subscription for Scrypt-based coins, like Litecoin.'''
  524.  
  525. ProofOfWork = lambda s, h: (scrypt_proof_of_work(h))
  526.  
  527. def _set_target(self, target):
  528. # Why multiply by 2**16? See: https://litecoin.info/Mining_pool_comparison
  529. self._target = '%064x' % (target << 16)
  530.  
  531.  
  532. class SubscriptionSHA256D(Subscription):
  533. '''Subscription for Double-SHA256-based coins, like Bitcoin.'''
  534.  
  535. ProofOfWork = sha256d
  536.  
  537.  
  538. # Maps algorithms to their respective subscription objects
  539. SubscriptionByAlgorithm = { ALGORITHM_SCRYPT: SubscriptionScrypt, ALGORITHM_SHA256D: SubscriptionSHA256D }
  540.  
  541.  
  542. class SimpleJsonRpcClient(object):
  543. '''Simple JSON-RPC client.
  544.  
  545. To use this class:
  546. 1) Create a sub-class
  547. 2) Override handle_reply(self, request, reply)
  548. 3) Call connect(socket)
  549.  
  550. Use self.send(method, params) to send JSON-RPC commands to the server.
  551.  
  552. A new thread is created for listening to the connection; so calls to handle_reply
  553. are synchronized. It is safe to call send from withing handle_reply.
  554. '''
  555.  
  556. class ClientException(Exception): pass
  557.  
  558. class RequestReplyException(Exception):
  559. def __init__(self, message, reply, request = None):
  560. Exception.__init__(self, message)
  561. self._reply = reply
  562. self._request = request
  563.  
  564. request = property(lambda s: s._request)
  565. reply = property(lambda s: s._reply)
  566.  
  567. class RequestReplyWarning(RequestReplyException):
  568. '''Sub-classes can raise this to inform the user of JSON-RPC server issues.'''
  569. pass
  570.  
  571. def __init__(self):
  572. self._socket = None
  573. self._lock = threading.RLock()
  574. self._rpc_thread = None
  575. self._message_id = 1
  576. self._requests = dict()
  577.  
  578.  
  579. def _handle_incoming_rpc(self):
  580. data = ""
  581. while True:
  582. # Get the next line if we have one, otherwise, read and block
  583. if '\n' in data:
  584. (line, data) = data.split('\n', 1)
  585. else:
  586. chunk = self._socket.recv(1024)
  587. data += chunk
  588. continue
  589.  
  590. log('JSON-RPC Server > ' + line, LEVEL_PROTOCOL)
  591.  
  592. # Parse the JSON
  593. try:
  594. reply = json.loads(line)
  595. except Exception, e:
  596. log("JSON-RPC Error: Failed to parse JSON %r (skipping)" % line, LEVEL_ERROR)
  597. continue
  598.  
  599. try:
  600. request = None
  601. with self._lock:
  602. if 'id' in reply and reply['id'] in self._requests:
  603. request = self._requests[reply['id']]
  604. self.handle_reply(request = request, reply = reply)
  605. except self.RequestReplyWarning, e:
  606. output = e.message
  607. if e.request:
  608. output += '\n ' + e.request
  609. output += '\n ' + e.reply
  610. log(output, LEVEL_ERROR)
  611.  
  612.  
  613. def handle_reply(self, request, reply):
  614. # Override this method in sub-classes to handle a message from the server
  615. raise self.RequestReplyWarning('Override this method')
  616.  
  617.  
  618. def send(self, method, params):
  619. '''Sends a message to the JSON-RPC server'''
  620.  
  621. if not self._socket:
  622. raise self.ClientException('Not connected')
  623.  
  624. request = dict(id = self._message_id, method = method, params = params)
  625. message = json.dumps(request)
  626. with self._lock:
  627. self._requests[self._message_id] = request
  628. self._message_id += 1
  629. self._socket.send(message + '\n')
  630.  
  631. log('JSON-RPC Server < ' + message, LEVEL_PROTOCOL)
  632.  
  633. return request
  634.  
  635.  
  636. def connect(self, socket):
  637. '''Connects to a remove JSON-RPC server'''
  638.  
  639. if self._rpc_thread:
  640. raise self.ClientException('Already connected')
  641.  
  642. self._socket = socket
  643.  
  644. self._rpc_thread = threading.Thread(target = self._handle_incoming_rpc)
  645. self._rpc_thread.daemon = True
  646. self._rpc_thread.start()
  647.  
  648.  
  649. # Miner client
  650. class Miner(SimpleJsonRpcClient):
  651. '''Simple mining client'''
  652.  
  653. class MinerWarning(SimpleJsonRpcClient.RequestReplyWarning):
  654. def __init__(self, message, reply, request = None):
  655. SimpleJsonRpcClient.RequestReplyWarning.__init__(self, 'Mining Sate Error: ' + message, reply, request)
  656.  
  657. class MinerAuthenticationException(SimpleJsonRpcClient.RequestReplyException): pass
  658.  
  659. def __init__(self, url, username, password, algorithm = ALGORITHM_SCRYPT):
  660. SimpleJsonRpcClient.__init__(self)
  661.  
  662. self._url = url
  663. self._username = username
  664. self._password = password
  665.  
  666. self._subscription = SubscriptionByAlgorithm[algorithm]()
  667.  
  668. self._job = None
  669.  
  670. self._accepted_shares = 0
  671.  
  672. # Accessors
  673. url = property(lambda s: s._url)
  674. username = property(lambda s: s._username)
  675. password = property(lambda s: s._password)
  676.  
  677.  
  678. # Overridden from SimpleJsonRpcClient
  679. def handle_reply(self, request, reply):
  680.  
  681. # New work, stop what we were doing before, and start on this.
  682. if reply.get('method') == 'mining.notify':
  683. if 'params' not in reply or len(reply['params']) != 9:
  684. raise self.MinerWarning('Malformed mining.notify message', reply)
  685.  
  686. (job_id, prevhash, coinb1, coinb2, merkle_branches, version, nbits, ntime, clean_jobs) = reply['params']
  687. self._spawn_job_thread(job_id, prevhash, coinb1, coinb2, merkle_branches, version, nbits, ntime)
  688.  
  689. log('New job: job_id=%s' % job_id, LEVEL_DEBUG)
  690.  
  691. # The server wants us to change our difficulty (on all *future* work)
  692. elif reply.get('method') == 'mining.set_difficulty':
  693. if 'params' not in reply or len(reply['params']) != 1:
  694. raise self.MinerWarning('Malformed mining.set_difficulty message', reply)
  695.  
  696. (difficulty, ) = reply['params']
  697. self._subscription.set_difficulty(difficulty)
  698.  
  699. log('Change difficulty: difficulty=%s' % difficulty, LEVEL_DEBUG)
  700.  
  701. # This is a reply to...
  702. elif request:
  703.  
  704. # ...subscribe; set-up the work and request authorization
  705. if request.get('method') == 'mining.subscribe':
  706. if 'result' not in reply or len(reply['result']) != 3 or len(reply['result'][0]) != 2:
  707. raise self.MinerWarning('Reply to mining.subscribe is malformed', reply, request)
  708.  
  709. ((mining_notify, subscription_id), extranounce1, extranounce2_size) = reply['result']
  710.  
  711. self._subscription.set_subscription(subscription_id, extranounce1, extranounce2_size)
  712.  
  713. log('Subscribed: subscription_id=%s' % subscription_id, LEVEL_DEBUG)
  714.  
  715. # Request authentication
  716. self.send(method = 'mining.authorize', params = [ self.username, self.password ])
  717.  
  718. # ...authorize; if we failed to authorize, quit
  719. elif request.get('method') == 'mining.authorize':
  720. if 'result' not in reply or not reply['result']:
  721. raise self.MinerAuthenticationException('Failed to authenticate worker', reply, request)
  722.  
  723. worker_name = request['params'][0]
  724. self._subscription.set_worker_name(worker_name)
  725.  
  726. log('Authorized: worker_name=%s' % worker_name, LEVEL_DEBUG)
  727.  
  728. # ...submit; complain if the server didn't accept our submission
  729. elif request.get('method') == 'mining.submit':
  730. if 'result' not in reply or not reply['result']:
  731. log('Share - Invalid', LEVEL_INFO)
  732. raise self.MinerWarning('Failed to accept submit', reply, request)
  733.  
  734. self._accepted_shares += 1
  735. log('Accepted shares: %d' % self._accepted_shares, LEVEL_INFO)
  736.  
  737. # ??? *shrug*
  738. else:
  739. raise self.MinerWarning('Unhandled message', reply, request)
  740.  
  741. # ??? *double shrug*
  742. else:
  743. raise self.MinerWarning('Bad message state', reply)
  744.  
  745.  
  746. def _spawn_job_thread(self, job_id, prevhash, coinb1, coinb2, merkle_branches, version, nbits, ntime):
  747. '''Stops any previous job and begins a new job.'''
  748.  
  749. # Stop the old job (if any)
  750. if self._job: self._job.stop()
  751.  
  752. # Create the new job
  753. self._job = self._subscription.create_job(
  754. job_id = job_id,
  755. prevhash = prevhash,
  756. coinb1 = coinb1,
  757. coinb2 = coinb2,
  758. merkle_branches = merkle_branches,
  759. version = version,
  760. nbits = nbits,
  761. ntime = ntime
  762. )
  763.  
  764. def run(job):
  765. try:
  766. for result in job.mine():
  767. params = [ self._subscription.worker_name ] + [ result[k] for k in ('job_id', 'extranounce2', 'ntime', 'nounce') ]
  768. self.send(method = 'mining.submit', params = params)
  769. log("Found share: " + str(params), LEVEL_INFO)
  770. log("Hashrate: %s" % human_readable_hashrate(job.hashrate), LEVEL_INFO)
  771. except Exception, e:
  772. log("ERROR: %s" % e, LEVEL_ERROR)
  773.  
  774. thread = threading.Thread(target = run, args = (self._job, ))
  775. thread.daemon = True
  776. thread.start()
  777.  
  778.  
  779. def serve_forever(self):
  780. '''Begins the miner. This method does not return.'''
  781.  
  782. # Figure out the hostname and port
  783. url = urlparse.urlparse(self.url)
  784. hostname = url.hostname or ''
  785. port = url.port or 9333
  786.  
  787. log('Starting server on %s:%d' % (hostname, port), LEVEL_INFO)
  788.  
  789. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  790. sock.connect((hostname, port))
  791. self.connect(sock)
  792.  
  793. self.send(method = 'mining.subscribe', params = [ "%s/%s" % (USER_AGENT, '.'.join(str(p) for p in VERSION)) ])
  794.  
  795. # Forever...
  796. while True:
  797. time.sleep(10)
  798.  
  799.  
  800. def test_subscription():
  801. '''Test harness for mining, using a known valid share.'''
  802.  
  803. log('TEST: Scrypt algorithm = %r' % SCRYPT_LIBRARY, LEVEL_DEBUG)
  804. log('TEST: Testing Subscription', LEVEL_DEBUG)
  805.  
  806. subscription = SubscriptionScrypt()
  807.  
  808. # Set up the subscription
  809. reply = json.loads('{"error": null, "id": 1, "result": [["mining.notify", "ae6812eb4cd7735a302a8a9dd95cf71f"], "f800880e", 4]}')
  810. log('TEST: %r' % reply, LEVEL_DEBUG)
  811. ((mining_notify, subscription_id), extranounce1, extranounce2_size) = reply['result']
  812. subscription.set_subscription(subscription_id, extranounce1, extranounce2_size)
  813.  
  814. # Set the difficulty
  815. reply = json.loads('{"params": [32], "id": null, "method": "mining.set_difficulty"}')
  816. log('TEST: %r' % reply, LEVEL_DEBUG)
  817. (difficulty, ) = reply['params']
  818. subscription.set_difficulty(difficulty)
  819.  
  820. # Create a job
  821. reply = json.loads('{"params": ["1db7", "0b29bfff96c5dc08ee65e63d7b7bab431745b089ff0cf95b49a1631e1d2f9f31", "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff2503777d07062f503253482f0405b8c75208", "0b2f436f696e48756e74722f0000000001603f352a010000001976a914c633315d376c20a973a758f7422d67f7bfed9c5888ac00000000", ["f0dbca1ee1a9f6388d07d97c1ab0de0e41acdf2edac4b95780ba0a1ec14103b3", "8e43fd2988ac40c5d97702b7e5ccdf5b06d58f0e0d323f74dd5082232c1aedf7", "1177601320ac928b8c145d771dae78a3901a089fa4aca8def01cbff747355818", "9f64f3b0d9edddb14be6f71c3ac2e80455916e207ffc003316c6a515452aa7b4", "2d0b54af60fad4ae59ec02031f661d026f2bb95e2eeb1e6657a35036c017c595"], "00000002", "1b148272", "52c7b81a", true], "id": null, "method": "mining.notify"}')
  822. log('TEST: %r' % reply, LEVEL_DEBUG)
  823. (job_id, prevhash, coinb1, coinb2, merkle_branches, version, nbits, ntime, clean_jobs) = reply['params']
  824. job = subscription.create_job(
  825. job_id = job_id,
  826. prevhash = prevhash,
  827. coinb1 = coinb1,
  828. coinb2 = coinb2,
  829. merkle_branches = merkle_branches,
  830. version = version,
  831. nbits = nbits,
  832. ntime = ntime
  833. )
  834.  
  835. # Scan that job (if I broke something, this will run for a long time))
  836. for result in job.mine(nounce_start = 1210450368 - 3):
  837. log('TEST: found share - %r' % repr(result), LEVEL_DEBUG)
  838. break
  839.  
  840. valid = { 'ntime': '52c7b81a', 'nounce': '482601c0', 'extranounce2': '00000000', 'job_id': u'1db7' }
  841. log('TEST: Correct answer %r' % valid, LEVEL_DEBUG)
  842.  
  843.  
  844.  
  845. # CLI for cpu mining
  846. if __name__ == '__main__':
  847. import argparse
  848.  
  849. # Parse the command line
  850. parser = argparse.ArgumentParser(description = "CPU-Miner for Cryptocurrency using the stratum protocol")
  851.  
  852. parser.add_argument('-o', '--url', help = 'stratum mining server url (eg: stratum+tcp://foobar.com:3333)')
  853. parser.add_argument('-u', '--user', dest = 'username', default = '', help = 'username for mining server', metavar = "USERNAME")
  854. parser.add_argument('-p', '--pass', dest = 'password', default = '', help = 'password for mining server', metavar = "PASSWORD")
  855.  
  856. parser.add_argument('-O', '--userpass', help = 'username:password pair for mining server', metavar = "USERNAME:PASSWORD")
  857.  
  858. parser.add_argument('-a', '--algo', default = ALGORITHM_SCRYPT, choices = ALGORITHMS, help = 'hashing algorithm to use for proof of work')
  859.  
  860. parser.add_argument('-B', '--background', action ='store_true', help = 'run in the background as a daemon')
  861.  
  862. parser.add_argument('-q', '--quiet', action ='store_true', help = 'suppress non-errors')
  863. parser.add_argument('-P', '--dump-protocol', dest = 'protocol', action ='store_true', help = 'show all JSON-RPC chatter')
  864. parser.add_argument('-d', '--debug', action ='store_true', help = 'show extra debug information')
  865.  
  866. parser.add_argument('-v', '--version', action = 'version', version = '%s/%s' % (USER_AGENT, '.'.join(str(v) for v in VERSION)))
  867.  
  868. options = parser.parse_args(sys.argv[1:])
  869.  
  870. message = None
  871.  
  872. # Get the username/password
  873. username = options.username
  874. password = options.password
  875.  
  876. if options.userpass:
  877. if username or password:
  878. message = 'May not use -O/-userpass in conjunction with -u/--user or -p/--pass'
  879. else:
  880. try:
  881. (username, password) = options.userpass.split(':')
  882. except Exception, e:
  883. message = 'Could not parse username:password for -O/--userpass'
  884.  
  885. # Was there an issue? Show the help screen and exit.
  886. if message:
  887. parser.print_help()
  888. print
  889. print message
  890. sys.exit(1)
  891.  
  892. # Set the logging level
  893. if options.debug:DEBUG = True
  894. if options.protocol: DEBUG_PROTOCOL = True
  895. if options.quiet: QUIET = True
  896.  
  897. if DEBUG:
  898. for library in SCRYPT_LIBRARIES:
  899. set_scrypt_library(library)
  900. test_subscription()
  901.  
  902. # Set us to a faster library if available
  903. set_scrypt_library()
  904. if options.algo == ALGORITHM_SCRYPT:
  905. log('Using scrypt library %r' % SCRYPT_LIBRARY, LEVEL_DEBUG)
  906.  
  907. # The want a daemon, give them a daemon
  908. if options.background:
  909. import os
  910. if os.fork() or os.fork(): sys.exit()
  911.  
  912. # Heigh-ho, heigh-ho, it's off to work we go...
  913. if options.url:
  914. miner = Miner(options.url, username, password, algorithm = options.algo)
  915. miner.serve_forever()
Add Comment
Please, Sign In to add comment