GithubHelp home page GithubHelp logo

nightminer's Introduction

NightMiner

A very simple pure Python implementation of a CryptoCurrency stratum CPU mining client. Currently supports scrypt (litecoin) and SHA256d (bitcoin).

At a Glance

  • Simple, one file
  • Supports Scrypt (litecoin, dogecoin, etc) and SHA256d (bitcoin, namecoin, etc)
  • Stratum (and only stratum)
  • Zero dependencies (beyond standard Python libraries)
  • 100% pure Python implementation
  • Attempts to detect faster implementations of scrypt (pure Python is SLOW)
  • Enable protocol chatter (-P) to see messages to and from the server

Command Line Interface

python nightminer.py [-h] [-o URL] [-u USERNAME] [-p PASSWORD]
                     [-O USERNAME:PASSWORD] [-a {scrypt,sha256d}] [-B] [-q]
                     [-P] [-d] [-v]

-o URL, --url=              stratum mining server url
-u USERNAME, --user=        username for mining server
-p PASSWORD, --pass=        password for mining server
-O USER:PASS, --userpass=   username:password pair for mining server

-a, --algo                  hashing algorithm to use for proof of work (scrypt, sha256d)

-B, --background            run in the background as a daemon

-q, --quiet                 suppress non-errors
-P, --dump-protocol         show all JSON-RPC chatter
-d, --debug                 show extra debug information

-h, --help                  show the help message and exit
-v, --version               show program's version number and exit


Example:
    python nightminer.py -o stratum+tcp://foobar.com:3333 -u user -p passwd

API

The API can be used by anyone wishing to create their own modified miner to learn more about the protocol, test their own pool or experiment with new algorithms.

import nightminer

Selecting a scrypt implementation (optional)

By default, the fastest detected library will be used; but if you wish to force a specific implementation:

nightminer.set_scrypt_library(library = nightminer.SCRYPT_LIBRARY_AUTO)
print nightminer.SCRYPT_LIBRARY

Subscription

After connecting to a stratum server, there is a small level of handshaking and then occasional messages to maintain state. The Subscription class manages this subscription state with the server.

Properties:

  • id - The subscription ID
  • worker_name - The name of the authenticated worker
  • difficulty, target - The result of the proof of work must be less than target
  • extranounce1 - The extranounce1
  • extranounce2_size - The size of the binary extranounce2 (in bytes)

set_subscription(subscription_id, extranounce1, extranounce2_size) Sets up the subscription details. Reply from the server to mining.subscribe.

set_difficulty(difficulty) Sets the current difficulty. Sent from the server as a mining.set_difficulty message.

set_worker_name(worker_name) Sets the worker's name after the server has authenticated the username/password. Reply from the server to mining.authorize.

create_job(job_id, prevhash, coinb1, coinb2, merkle_branches, version, nbits, ntime) Creates a new job. Sent from the server as a mining.notify message.

Job

When the server has a new job to work on it sends a mining.notiffy message. The Job class manages all the paameters required to perform work and performs the actual mining.

Properties:

  • id - The job ID
  • prevhash - The previous hash
  • coinb, coinb2 - The coinbase prefix and suffix
  • merkle_branches - The Merkle branches
  • version - The version
  • nbits, ntime - The network bits and network time
  • target, extranounce, extranounce2_size - See Subscription class above
  • hashrate - The rate this miner has been hashing at

merkle_root_bin(extranounce2_bin) Calculate the Merkle root, as a binary string.

mine(nounce_start = 0, nounce_stride = 1) Iterates over all solutions for this job. This will run for an extrememly long time, likely far longer than ntime would be valid, so you will likely call stop() at some point and start on a new job.

stop() Causes the mine() method to finish immediately for any thread inside.

Miner

This is a sub-class of SimpleJsonRpcClient which connects to the stratum server and processes work requests from the server updating a Subscription object.

Properties:

  • url - The stratum server URL
  • username, password - The provided username and password

serve_forever() Connect to the server, handshake and block forever while handling work from the server.

Use Cases

Create a standard miner

miner = Miner('stratum+tcp://foobar.com:3333', 'username', 'password')
miner.server_forever()

Experimenting with a new algorithm...

For this example, we will create a CryptoCoin based on MD5.

import hashlib

# Create the Subscription object (proof-of-work should be 32 bytes long)
class SubscriptionMd5(nightminer.Subscription):
  def ProofOfWork(self, header):
    return hashlib.md5(header).digest() + ('0' * 16)

If you wish to manually find a few valid shares:

# Create a subscription (and fill it in a bit with what a proper server would give us)
subs = SubscriptionMd5()
subs.set_subscription('my_subs_id', '00000000', 4)
subs.set_difficulty(1.0 / (2 ** 16))
subs.set_worker_name('my_fake_worker')

# Create a job
job = subs.create_job('my_job', ('0' * 64), ('0' * 118), ('0' * 110), [ ], '00000002', 'deadbeef', '01234567')

# Search for 5 shares
share_count = 0
for valid_share in job.mine():
  print "Found a valid share:", valid_share
  share_count += 1
  if share_count == 5: break

print "Hashrate:", job.hashrate

Or if you already have a server ready to go with your algorithm:

# Register the Subscription
SubscriptionByAlgorithm['my_algo'] = SubscriptionMd5

# Start a miner
miner = Miner('stratum+tcp://localhost:3333', 'username', 'password', 'my_algo')
miner.server_forever()

FAQ

Why would you do this? I was trying to tinker around with Litecoin, but found it difficult to find a simple, complete example of how to decode the endianness of the provided parameters and build the block header. So, the obvious next step is to create a full client to experiment with.

Why is this so slow? It is written in Python. It is not meant to be fast, more of a reference solution or something that can be easily hacked into to test your own pool.

On my MacBook Air, with one thread I get around 3,000 hashes/s using the ltc_scrypt libary but less than 2 hashes/s using the built-in pure Python scrypt.

What is this ltc_scrypt you speak of? It is a Python C-binding for a C implementation of scrypt found in p2pool (https://github.com/forrestv/p2pool). To add to your own system:

> # Download the source
> curl -L https://github.com/forrestv/p2pool/archive/13.4.tar.gz > p2pool-13.4.tar.gz

> # Untar
> tar -xzf p2pool-13.4.tar.gz

> # Build and install
> cd p2pool-13.4/litecoin_scrypt/
> python setup.py build
> sudo python setup.py install

After this is installed, this miner will be about 2,000 times faster.

Why am I am only getting rejected shares? Make sure you are using the correct algoithm, that means --algo=scrypt (the default) for Litecoin or --algo=sha256d for Bitcoin.

How do I get a question I have added? E-mail me at [email protected] with any questions, suggestions, comments, et cetera.

Can I give you my money? Umm... Ok? :-)

Bitcoin - 1LNdGsYtZXWeiKjGba7T997qvzrWqLXLma Litecoin - LXths3ddkRtuFqFAU7sonQ678bSGkXzh5Q Namecoin - N6JLCggCyYcpcUq3ydJtLxv67eEJg4Ntk2

nightminer's People

Contributors

ricmoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nightminer's Issues

Question about stratum protocol and pool mining algorithm

Hi,
Thanks for your code, I think I understand part of the stratum protocol, and there're still something confused, so I post this issue to ask for some help.

Client code like this,

for result in job.mine(data, extranonce1):
    submit_work(result)

We send a mining.submit msg once brute a correct hash, but how server distribute tasks to clients? I guess:

  1. prepare block info, varify chains
  2. generate extranonce1 from range(0, 0xffffffff), then give each client/miner a unique extranonce1

And my question is how this could work? Why the mining algorithm could be distributed?
Take sha256d as an example:

def solo_mining(data):
    for nonce in range(...):
        hash = sha256d(data, nonce)
        if hash.match_difficuly():
            return True
    return False

Of course we send different nonce to different client, the problem could be paralleled, but if a client get the task but do nothing, is that means the server will miss some chunk and finally get wrong result?

Appreciate for any help and advice.

sha256d() takes exactly 1 argument

When I'm mining with nightminer
I found there is an error mining bitcoin with sha256d().

[ERROR] ERROR: sha256d() takes exactly 1 argument (2 given)

my command:
python nightminer.py -o stratum+tcp://stratum.antpool.com:3333 -O USERNAME:PASSSWORD -a sha256d

Is my command wrong?

Python3 support?

Hi, im currently working on integrating this script in a discord bot and for that i need a 3.x version (best is 3.7) of it. Is that possible?

Python int too large to convert to C long

Trying to run script from command line (Windows 8.1, Python 2.7.7) and catch:

C:\miner>python nightminer.py -a scrypt -o stratum+tcp://ltc.ghash.io:3333 -u up
100313695.worker1 -p password123
[2015-08-01 15:05:21] [INFO] Starting server on ltc.ghash.io:3333
[2015-08-01 15:05:22] [ERROR] ERROR: Python int too large to convert to C long
[2015-08-01 15:05:25] [ERROR] ERROR: Python int too large to convert to C long
[2015-08-01 15:05:26] [ERROR] ERROR: Python int too large to convert to C long
[2015-08-01 15:05:55] [ERROR] ERROR: Python int too large to convert to C long
Traceback (most recent call last):
File "nightminer.py", line 957, in <module>
miner.serve_forever()
File "nightminer.py", line 839, in serve_forever
time.sleep(10)
KeyboardInterrupt

With debug flag:

C:\miner>python nightminer.py -a scrypt -o stratum+tcp://ltc.ghash.io:3333 -u up
100313695.worker1 -p password123 -d
[2015-08-01 15:06:20] [DEBUG] TEST: Scrypt algorithm = 'pure python'
[2015-08-01 15:06:20] [DEBUG] TEST: Testing Subscription
[2015-08-01 15:06:20] [DEBUG] TEST: {u'result': [[u'mining.notify', u'ae6812eb4c
d7735a302a8a9dd95cf71f'], u'f800880e', 4], u'id': 1, u'error': None}
[2015-08-01 15:06:20] [DEBUG] TEST: {u'params': [32], u'id': None, u'method': u'
mining.set_difficulty'}
[2015-08-01 15:06:20] [DEBUG] TEST: {u'params': [u'1db7', u'0b29bfff96c5dc08ee65
e63d7b7bab431745b089ff0cf95b49a1631e1d2f9f31', u'0100000001000000000000000000000
0000000000000000000000000000000000000000000ffffffff2503777d07062f503253482f0405b
8c75208', u'0b2f436f696e48756e74722f0000000001603f352a010000001976a914c633315d37
6c20a973a758f7422d67f7bfed9c5888ac00000000', [u'f0dbca1ee1a9f6388d07d97c1ab0de0e
41acdf2edac4b95780ba0a1ec14103b3', u'8e43fd2988ac40c5d97702b7e5ccdf5b06d58f0e0d3
23f74dd5082232c1aedf7', u'1177601320ac928b8c145d771dae78a3901a089fa4aca8def01cbf
f747355818', u'9f64f3b0d9edddb14be6f71c3ac2e80455916e207ffc003316c6a515452aa7b4'
, u'2d0b54af60fad4ae59ec02031f661d026f2bb95e2eeb1e6657a35036c017c595'], u'000000
02', u'1b148272', u'52c7b81a', True], u'id': None, u'method': u'mining.notify'}
Traceback (most recent call last):
File "nightminer.py", line 942, in <module>
test_subscription()
File "nightminer.py", line 878, in test_subscription
for result in job.mine(nounce_start = 1210450368 - 3):
File "nightminer.py", line 434, in mine
for extranounce2 in xrange(0, 0xffffffff):
OverflowError: Python int too large to convert to C long

Connection timed out?

any idea?

Traceback (most recent call last):
File "nightminer.py", line 954, in
miner.serve_forever()
File "nightminer.py", line 829, in serve_forever
sock.connect((hostname, port))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 110] Connection timed out

how to fix this issue

when i start the python miner for sometimes it keep bringing up the same kind of message like this

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(_self.__args, *_self.__kwargs)
File "nightminer.py", line 625, in _handle_incoming_rpc
chunk = self._socket.recv(1024)
error: [Errno 104] Connection reset by peer

Type Error and Syntax on Python2 and 3

So the script actually executes on Py2 but

[2018-02-16 14:55:48] [INFO] Starting server on mcn.pool.minergate.com:45640
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "miner.py", line 648, in _handle_incoming_rpc
output += '\n ' + e.reply
TypeError: cannot concatenate 'str' and 'dict' objects


Python3

File "miner.py", line 327
except Exception, e:
^
SyntaxError: invalid syntax

scrypt mining is hanging after a couple of mints

Description
scrypt mining is hanging after a couple of minutes. I used the native library and ltc-scrypt lib and it hangs with both of them.

Steps

python nightminer.py -a scrypt -o stratum+tcp://hub.miningpoolhub.com:20523 -u test.test -p x -d -P

Actual result
It will start mining and after a couple of minutes, it will hang.

I tried hard to find the reason for this hanging but I can't maybe a hint or suggestion can help.

RPC handler error

The script has an issue when ran with Python 2.7.13

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "nightminer.py", line 648, in _handle_incoming_rpc
    output += '\n  ' + e.reply
TypeError: cannot concatenate 'str' and 'dict' objects

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.