Guest User

Untitled

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