Pages:
Author

Topic: New pure-python CPU miner, for fun and testing (Read 25535 times)

newbie
Activity: 10
Merit: 0
February 11, 2014, 11:41:00 PM
#52
i'm trying this with Python 2.7 on Linux and get a an error on line ~158 at

   
Code:
return (nonce + 1, None)

Python complains that nonce has not been defined - which is true since this call is made outside of the loop

        
Code:
for nonce in xrange(self.max_nonce):

which defines nonce

I added a class variable self._nonce in the __init__ function of class Miner and use this t keep track of the nonce value.

A question:
Is it expected behaviour for the hash rate to halve on each iteration of the work function?
hero member
Activity: 935
Merit: 1015
By condensing code in the nonce loop, pyminer gets around 6% more speed on my machine.  The hash part of the loop ends up being:

Code:
# hash final 4b, the nonce value
hash1_o = static_hash.copy()

# encode 32-bit nonce value
hash1_o.update(struct.pack("
# sha256 hash of sha256 hash
hash = hashlib.sha256(hash1_o.digest()).digest()

Below is a diff of the changes.

Code:
--- pyminer.py 2011-06-11 14:47:44.000000000 -0700
+++ pyminer_test.py 2011-06-11 14:47:04.000000000 -0700
@@ -121,18 +121,14 @@
 
  for nonce in xrange(self.max_nonce):
 
- # encode 32-bit nonce value
- nonce_bin = struct.pack("-
  # hash final 4b, the nonce value
  hash1_o = static_hash.copy()
- hash1_o.update(nonce_bin)
- hash1 = hash1_o.digest()
+
+ # encode 32-bit nonce value
+ hash1_o.update(struct.pack(" 
  # sha256 hash of sha256 hash
- hash_o = hashlib.sha256()
- hash_o.update(hash1)
- hash = hash_o.digest()
+ hash = hashlib.sha256(hash1_o.digest()).digest()
 
  # quick test for winning solution: high 32 bits zero?
  if hash[-4:] != '\0\0\0\0':
newbie
Activity: 1
Merit: 0
...
Sorry, not in a patch form.


OK, fixed that. Here is a diff of it.



Code:
diff --git pyminer.py pyminer.py
index 051abb3..b203aa8 100755
--- pyminer.py
+++ pyminer.py
@@ -97,8 +97,9 @@ def wordreverse(in_buf):
  return ''.join(out_words)
 
 class Miner:
- def __init__(self, id):
+ def __init__(self, id, settings=None):
  self.id = id
+    self.settings=settings
  self.max_nonce = MAX_NONCE
 
  def work(self, datastr, targetstr):
@@ -181,11 +182,11 @@ class Miner:
  time_diff = time_end - time_start
 
  self.max_nonce = long(
- (hashes_done * settings['scantime']) / time_diff)
+ (hashes_done * self.settings['scantime']) / time_diff)
  if self.max_nonce > 0xfffffffaL:
  self.max_nonce = 0xfffffffaL
 
- if settings['hashmeter']:
+ if self.settings['hashmeter']:
  print "HashMeter(%d): %d hashes, %.2f Khash/sec" % (
       self.id, hashes_done,
       (hashes_done / 1000.0) / time_diff)
@@ -194,16 +195,16 @@ class Miner:
  self.submit_work(rpc, work['data'], nonce_bin)
 
  def loop(self):
- rpc = BitcoinRPC(settings['host'], settings['port'],
- settings['rpcuser'], settings['rpcpass'])
+ rpc = BitcoinRPC(self.settings['host'], self.settings['port'],
+ self.settings['rpcuser'], self.settings['rpcpass'])
  if rpc is None:
  return
 
  while True:
  self.iterate(rpc)
 
-def miner_thread(id):
- miner = Miner(id)
+def miner_thread(id, settings):
+ miner = Miner(id, settings)
  miner.loop()
 
 if __name__ == '__main__':
@@ -248,7 +249,7 @@ if __name__ == '__main__':
 
  thr_list = []
  for thr_id in range(settings['threads']):
- p = Process(target=miner_thread, args=(thr_id,))
+ p = Process(target=miner_thread, args=(thr_id, settings,))
  p.start()
  thr_list.append(p)
  time.sleep(1) # stagger threads



I'm still getting the BadStatusLine error even after adding these fixes to the script. I'm trying to run on Ubuntu 10.04.2 LTS 64-bit.
Any ideas?
newbie
Activity: 21
Merit: 0
Running it on pypy (recent nightly build), it leaks and fills my memory fast, any idea if the code relies too much on cpython reference counting, or should I report this to pypy devs?

Otherwise, about the slowness of pypy is propalby related to the fact that the hashing is relegated to optimized c-code, but pypy uses a pure python or non-optimized rpython implementation. Has someone done profiling of the latest code?
newbie
Activity: 8
Merit: 0
I also get the error if I use btcguild. I use phython 2.5.3 on linux debian lenny (is this a problem). I run The program with one thread.

...

I ran it under Ubuntu Lucid Lynx with Python 2.6.5 and under Windows with Python 2.7. In both cases I get the error.

It looks like the problem occurs when after the second request another one is started. When adding debug output I see it run in the miner.loop() the self.iterate(rpc) call twice. After it finishes the second loop and starts the 3rd call to self.iterate(rpc) the response with an empty header is returned.

Breaking with the 3rd rpc call is independent of the amount of threads. So with one thread it breaks with the 3rd call and with two threads running it breaks each thread with the 3rd call as well.  Undecided

One interesting thing to note is, that the max_nonce value goes up. I thought it would go down in consecutive runs. In the first run it is set to 1,000,000. In the second run it goes up to 4,885,595.
newbie
Activity: 49
Merit: 0
I also get the error if I use btcguild. I use phython 2.5.3 on linux debian lenny (is this a problem). I run The program with one thread.

Quote
Process Process-1:
Traceback (most recent call last):
  File "/var/lib/python-support/python2.5/multiprocessing/process.py", line 237, in _bootstrap
    self.run()
  File "/var/lib/python-support/python2.5/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "pyminer.py", line 214, in miner_thread
    miner.loop()
  File "pyminer.py", line 210, in loop
    self.iterate(rpc)
  File "pyminer.py", line 174, in iterate
    work = rpc.getwork()
  File "pyminer.py", line 83, in getwork
    return self.rpc('getwork', data)
  File "pyminer.py", line 63, in rpc
    resp = self.conn.getresponse()
  File "/usr/lib/python2.5/httplib.py", line 928, in getresponse
    response.begin()
  File "/usr/lib/python2.5/httplib.py", line 385, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.5/httplib.py", line 349, in _read_status
    raise BadStatusLine(line)
BadStatusLine
newbie
Activity: 8
Merit: 0
I ran the latest git-version with the patch I posted earlier under windows and linux using an account at btcguild.com and got the following error message:

Code:
G:\Dokumente und Einstellungen\anna\Eigene Dateien\pj\pyminer>python pyminer.py btcguild-config.cfg
2 mining threads started
Sat Jun 04 00:41:52 2011 Miner Starts - btcguild.com:8332
HashMeter(0): 1000000 hashes, 136.18 Khash/sec
HashMeter(1): 1000000 hashes, 136.46 Khash/sec
HashMeter(0): 8171047 hashes, 135.41 Khash/sec
Process Process-1:
Traceback (most recent call last):
  File "G:\Python27\lib\multiprocessing\process.py", line 232, in _bootstrap
    self.run()
  File "G:\Python27\lib\multiprocessing\process.py", line 88, in run
    self._target(*self._args, **self._kwargs)
  File "G:\Dokumente und Einstellungen\anna\Eigene Dateien\pj\pyminer\pyminer.py", line 208, in miner_thread
    miner.loop()
  File "G:\Dokumente und Einstellungen\anna\Eigene Dateien\pj\pyminer\pyminer.py", line 204, in loop
    self.iterate(rpc)
  File "G:\Dokumente und Einstellungen\anna\Eigene Dateien\pj\pyminer\pyminer.py", line 168, in iterate
    work = rpc.getwork()
  File "G:\Dokumente und Einstellungen\anna\Eigene Dateien\pj\pyminer\pyminer.py", line 76, in getwork
    return self.rpc('getwork', data)
  File "G:\Dokumente und Einstellungen\anna\Eigene Dateien\pj\pyminer\pyminer.py", line 56, in rpc
    resp = self.conn.getresponse()
  File "G:\Python27\lib\httplib.py", line 1025, in getresponse
    response.begin()
  File "G:\Python27\lib\httplib.py", line 401, in begin
    version, status, reason = self._read_status()
  File "G:\Python27\lib\httplib.py", line 365, in _read_status
    raise BadStatusLine(line)
BadStatusLine: ''
HashMeter(1): 8187772 hashes, 135.47 Khash/sec
Process Process-2:
...
Sat Jun 04 00:43:00 2011 Miner Stops - btcguild.com:8332


Running it with two threads I took out the trace back from the second process.

Anyone knows why there is an empty status line returned?

Thanks for your help.
newbie
Activity: 8
Merit: 0
...
Sorry, not in a patch form.


OK, fixed that. Here is a diff of it.



Code:
diff --git pyminer.py pyminer.py
index 051abb3..b203aa8 100755
--- pyminer.py
+++ pyminer.py
@@ -97,8 +97,9 @@ def wordreverse(in_buf):
  return ''.join(out_words)
 
 class Miner:
- def __init__(self, id):
+ def __init__(self, id, settings=None):
  self.id = id
+    self.settings=settings
  self.max_nonce = MAX_NONCE
 
  def work(self, datastr, targetstr):
@@ -181,11 +182,11 @@ class Miner:
  time_diff = time_end - time_start
 
  self.max_nonce = long(
- (hashes_done * settings['scantime']) / time_diff)
+ (hashes_done * self.settings['scantime']) / time_diff)
  if self.max_nonce > 0xfffffffaL:
  self.max_nonce = 0xfffffffaL
 
- if settings['hashmeter']:
+ if self.settings['hashmeter']:
  print "HashMeter(%d): %d hashes, %.2f Khash/sec" % (
       self.id, hashes_done,
       (hashes_done / 1000.0) / time_diff)
@@ -194,16 +195,16 @@ class Miner:
  self.submit_work(rpc, work['data'], nonce_bin)
 
  def loop(self):
- rpc = BitcoinRPC(settings['host'], settings['port'],
- settings['rpcuser'], settings['rpcpass'])
+ rpc = BitcoinRPC(self.settings['host'], self.settings['port'],
+ self.settings['rpcuser'], self.settings['rpcpass'])
  if rpc is None:
  return
 
  while True:
  self.iterate(rpc)
 
-def miner_thread(id):
- miner = Miner(id)
+def miner_thread(id, settings):
+ miner = Miner(id, settings)
  miner.loop()
 
 if __name__ == '__main__':
@@ -248,7 +249,7 @@ if __name__ == '__main__':
 
  thr_list = []
  for thr_id in range(settings['threads']):
- p = Process(target=miner_thread, args=(thr_id,))
+ p = Process(target=miner_thread, args=(thr_id, settings,))
  p.start()
  thr_list.append(p)
  time.sleep(1) # stagger threads

newbie
Activity: 8
Merit: 0
I'm trying to run this and getting an exception:

Code:
Process Process-1:
Traceback (most recent call last):
  File "C:\Python26\lib\multiprocessing\process.py", line 232, in _bootstrap
    self.run()
  File "C:\Python26\lib\multiprocessing\process.py", line 88, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Chris\workspace\pyminer\pyminer.py", line 197, in miner_thread
    miner.loop()
  File "C:\Users\Chris\workspace\pyminer\pyminer.py", line 187, in loop
    rpc = BitcoinRPC(settings['host'], settings['port'],
KeyError: 'host'
1 mining threads started
Sun Feb 27 15:51:18 2011 Miner Starts - 127.0.0.1:8332
Sun Feb 27 15:51:18 2011 Miner Stops - 127.0.0.1:8332

It seems that the settings dictionary is empty, because when we're using separate processes the children don't have access to the parent's variables. Probably the settings should be passed in as constructor arguments. I might put this up on GitHub and hack on it a bit if you don't mind Smiley

I did some changes to make it work under Windows:

Code:

class Miner:
  def __init__(self, id, settings=None):
    self.id = id
    self.settings=settings
    self.max_nonce = MAX_NONCE
...

  def iterate(self, rpc):
...
    self.max_nonce = long(
      (hashes_done * self.settings['scantime']) / time_diff)
    if self.max_nonce > 0xfffffffaL:
      self.max_nonce = 0xfffffffaL
...
    if self.settings['hashmeter']:
      print "HashMeter(%d): %d hashes, %.2f Khash/sec" % (
            self.id, hashes_done,
            (hashes_done / 1000.0) / time_diff)

...

  def loop(self):
    rpc = BitcoinRPC(self.settings['host'], self.settings['port'],
         self.settings['rpcuser'], self.settings['rpcpass'])
    if rpc is None:
      return

...

def miner_thread(id, settings):
  miner = Miner(id, settings)
  miner.loop()

...

  thr_list = []
  for thr_id in range(settings['threads']):
    p = Process(target=miner_thread, args=(thr_id, settings))
    p.start()
    thr_list.append(p)
    time.sleep(1)     # stagger threads


Sorry, not in a patch form.
fix
newbie
Activity: 4
Merit: 0
Really, really useful. Thank you very much.

But what I don't understand is the meaning of the RPC work data (data, hash1, midstate, target values) and why when a proof-of-work isn't found this isn't replied to the pool. I know, I've got some confusion in my mind.
There's some documentation about this?
legendary
Activity: 1596
Merit: 1091
Updated the git repo with many bug fixes.  pyminer.py successfully generated a block on testnet, and a share in slush's pool.
newbie
Activity: 8
Merit: 0
Thanks!  Smiley
legendary
Activity: 1596
Merit: 1091
Sure, here's a git repo:  https://github.com/jgarzik/pyminer
newbie
Activity: 8
Merit: 0
I am loving the idea of this miner, much simpler than all of the others.

Just one question, any chance of a git repo?
newbie
Activity: 14
Merit: 0
I'm not sure you caught my point.
You're right, I didn't catch your point.  I have to chew on that, though.  What we need is a binary buffer with the right endianness, not a python long integer, right?

No, we really do need a 256-integer, because that is the fundamental proof-of-work test in the bitcoin system, comparing two 256-bit integers:

     hash < target

You may just as well compare the two arrays of binary data:
Code:
>>> a = "aaaa".decode('hex')
>>> b = "bbbb".decode('hex')
>>> a > b
False
>>> b > a
True

Got to love high level languages.

Considered putting this on github as well, btw? Being smaller and higher level, I find it a lot easier to grasp than the C miner. Too bad about all the byte order manipulation, but I guess that can't be helped.
newbie
Activity: 14
Merit: 0
I wonder if this is a bug:

Code:
       # hash final 4b, the nonce value
        hash1_o = hash1_precalc_o
        hash1_o.update(nonce_bin)
        hash1 = hash1_o.digest()

Do I interpret your meaning right when I say that the code — for each value of nonce — intends to calculate the SHA256 digest of the precalculated value (intended to stay constant throughout the loop) updated with the nonce?  If so, that is not what the code does.  Please see this simplified example:

Code:
>>> from hashlib import sha256
>>> pre = sha256('abc')
>>> post = pre
>>> post.update('xyz')
>>> pre.digest() == post.digest()
True
>>>

So pre and post (like hash1_precalc_o and hash1_o) both points to the same object

Instead of assignment, you'd use hashlib's copy() method to duplicate the hash state.

This code should work as intended:
Code:
# hash final 4b, the nonce value
hash1_o = hash1_precalc_o.copy()
hash1_o.update(nonce_bin)
hash1 = hash1_o.digest()
hero member
Activity: 566
Merit: 500
Unselfish actions pay back better
If everyone else is running on Linux but I am running on Windows, this explains why it would work for others but not for me.
It sure does.

Cheers,
Kiv
full member
Activity: 162
Merit: 100
This is probably a Windows-only bug. From the multiprocessing docs:


Since Windows lacks os.fork() it has a few extra restrictions:
Global variables

Bear in mind that if code run in a child process tries to access a global variable, then the value it sees (if any) may not be the same as the value in the parent process at the time that Process.start() was called.

If everyone else is running on Linux but I am running on Windows, this explains why it would work for others but not for me.
hero member
Activity: 566
Merit: 500
Unselfish actions pay back better
I suspect that the program worked fine at one point and I have a more recent version with a bug in it.
The current version runs fine with this config file (username and password edited for obvious reasons):

Code:
host = mining.bitcoin.cz
port = 8332
rpcuser = myRPCUser
rpcpass = myRPCPass
logdir = /tmp
threads = 1
hashmeter = 1

Cheers,
Kiv
full member
Activity: 162
Merit: 100
Thank you, but I did supply the argument. In fact the program will not start without the config file. My config file is the right format as well and has "host=127.0.0.1" in it.

I suspect that the program worked fine at one point and I have a more recent version with a bug in it.


Code:
  File "C:\Users\Chris\workspace\pyminer\pyminer.py", line 187, in loop
    rpc = BitcoinRPC(settings['host'], settings['port'],
KeyError: 'host'
Pyminer takes as its single argument the name of the config file.  Please see message #10 in this thread.

Cheers,
Pages:
Jump to: