2

I have been wondering how to make a Python Monero miner. I know about py-cryptonight, but does anyone know anything about how to actually code with it? I understand that cn_slow_hash is for mining, but when I try to create a hash, I get a bunch of random characters. I know that Python miners are slow, but this is just an experiment. I know how to code in Python fairly well, but not great. Can someone please guide me along? Thanks!

jtgrassie
  • 19,601
  • 4
  • 17
  • 54
Crypthusiast
  • 169
  • 2
  • 8

1 Answers1

7

The general steps are as follows:

  1. Call your Monero daemon's RPC method get_block_template.
  2. Take the blockhashing_blob from the response and convert the hex to binary.
  3. Set a 4 byte nonce in bytes 39..42
  4. Hash the data with cn_slow_hash, you'll get 32 bytes back, that's your hash. Note, this result hash is binary - it's a 32 byte number. If you want to display it, you need to convert it to a hex string.
  5. Compare the result hash against the next block difficulty (again, converted to binary). The calculation to see if your hash meets the difficulty is as follows:
    • b / h (where b is the base difficulty, 2^256-1, and h is your result hash)
    • If the result is greater than or equal to the next block difficulty (as returned from get_block_template at step 1), you've mined a block and can submit it to the network using the RPC call submit_block.
    • If it's not, repeat from step 3 with a new nonce.

Doing this all in Python is pretty straightforward because Python supports big numbers, but, it will be terribly slow.

I've omitted specifically how to call cn_slow_hash as the README already shows how trivial it is to call.

jtgrassie
  • 19,601
  • 4
  • 17
  • 54