Bitcoin Forum
May 05, 2024, 03:06:38 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: « 1 2 [3]  All
  Print  
Author Topic: New pure-python CPU miner, for fun and testing  (Read 25532 times)
TehRainbowGuy
Newbie
*
Offline Offline

Activity: 8
Merit: 0



View Profile
April 04, 2011, 09:16:01 PM
 #41

Thanks!  Smiley
1714921598
Hero Member
*
Offline Offline

Posts: 1714921598

View Profile Personal Message (Offline)

Ignore
1714921598
Reply with quote  #2

1714921598
Report to moderator
1714921598
Hero Member
*
Offline Offline

Posts: 1714921598

View Profile Personal Message (Offline)

Ignore
1714921598
Reply with quote  #2

1714921598
Report to moderator
1714921598
Hero Member
*
Offline Offline

Posts: 1714921598

View Profile Personal Message (Offline)

Ignore
1714921598
Reply with quote  #2

1714921598
Report to moderator
Even in the event that an attacker gains more than 50% of the network's computational power, only transactions sent by the attacker could be reversed or double-spent. The network would not be destroyed.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714921598
Hero Member
*
Offline Offline

Posts: 1714921598

View Profile Personal Message (Offline)

Ignore
1714921598
Reply with quote  #2

1714921598
Report to moderator
1714921598
Hero Member
*
Offline Offline

Posts: 1714921598

View Profile Personal Message (Offline)

Ignore
1714921598
Reply with quote  #2

1714921598
Report to moderator
jgarzik (OP)
Legendary
*
Offline Offline

Activity: 1596
Merit: 1091


View Profile
April 05, 2011, 07:56:40 AM
 #42

Updated the git repo with many bug fixes.  pyminer.py successfully generated a block on testnet, and a share in slush's pool.

Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
fix
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
April 17, 2011, 04:45:06 PM
 #43

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?
dsp77
Newbie
*
Offline Offline

Activity: 8
Merit: 0


View Profile
June 03, 2011, 09:59:06 PM
 #44

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.
dsp77
Newbie
*
Offline Offline

Activity: 8
Merit: 0


View Profile
June 03, 2011, 10:21:21 PM
Last edit: June 03, 2011, 10:39:18 PM by dsp77
 #45

...
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

dsp77
Newbie
*
Offline Offline

Activity: 8
Merit: 0


View Profile
June 03, 2011, 10:57:19 PM
 #46

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.
psychocoder
Newbie
*
Offline Offline

Activity: 49
Merit: 0


View Profile
June 06, 2011, 04:26:52 PM
 #47

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
dsp77
Newbie
*
Offline Offline

Activity: 8
Merit: 0


View Profile
June 08, 2011, 08:05:50 AM
Last edit: June 08, 2011, 08:18:09 AM by dsp77
 #48

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.
elmom
Newbie
*
Offline Offline

Activity: 21
Merit: 0


View Profile
June 08, 2011, 10:19:57 AM
 #49

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?
tcash21
Newbie
*
Offline Offline

Activity: 1
Merit: 0


View Profile
June 10, 2011, 05:32:02 PM
 #50

...
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?
Unthinkingbit
Hero Member
*****
Offline Offline

Activity: 935
Merit: 1015



View Profile
June 12, 2011, 12:44:05 AM
 #51

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("<I", nonce))

# 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("<I", nonce)
-
  # 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("<I", nonce))
 
  # 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':

tarzen
Newbie
*
Offline Offline

Activity: 10
Merit: 0


View Profile
February 12, 2014, 03:41:00 AM
 #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?
Pages: « 1 2 [3]  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!