Bitcoin Forum
November 06, 2024, 09:47:11 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: miner client - just to understand mining  (Read 1150 times)
questionstorm (OP)
Newbie
*
Offline Offline

Activity: 33
Merit: 0


View Profile
October 27, 2012, 05:33:21 PM
 #1

Hi,

I'm trying to implement a simple miner just to understand the steps
of minimg bitcoins.  The implementation is a bit simplistic because the
goal is to understand mining, not to make a fast miner.

This is the code skeleton:

Code:
#!/usr/bin/env ruby

require 'json'
require 'digest'

loop do
        getwork = JSON.parse `.../bitcoind getwork`

        data = getwork['data']
        target = getwork['target']

        version = data.slice(0, 8)
        prev_hash = data.slice(8, 64)
        merkle_root = data.slice(8 + 64, 64)
        timestamp = data.slice(8 + 64 + 64, 8)
        bits = data.slice(8 + 64 + 64 + 8, 8)

        bin = # little vs. big endians
                [version].pack('H*').reverse +
                [prev_hash].pack('H*').reverse +
                [merkle_root].pack('H*').reverse +
                [timestamp].pack('H*').reverse +
                [bits].pack('H*').reverse

        (0..4294967295).each { |nonce|
                if Digest::SHA256.hexdigest(Digest::SHA256.digest(bin + [nonce.to_s(16)].pack('H*').reverse)) <= target
                        puts "yay!!!"
                        # do a 'submitblock' somehow (what is the hex data parameter to it?)
                        #
                        # maybe: ???
                        #
                        #   system ".../bitcoind submitblock '#{nonce.to_s(16)}'"
                        #
                        # or: ???
                        #   system ".../bitcoind submitblock '#{version + prev_hash + merkle_root + timestamp + bits + nonce.to_s(16)}'"
                        break
                end
        }
end


I'd like to ask a little help how to do it.  getwork['data'] is a 256 length hexa string (128 byte),
and I'm not sure what parts of that data have to be hashed, and how to combine it with the
nonce.  In the above code I just use the first 76 bytes (152 hexa digits) of the data, but I'm
afraid it's not what I should do.

As you can see I don't know how to send back when I find a hash.  I suppose I should use the
submitblock request, but what is the proper way to do this?  What is exactly the first paramether
of that call?

What are midstate and hash1 good for?  How do these things speed up mining?

I know that getwork is an ancient way, and there are so-called extensions to the protocol,
long polling, and other scary things.  Is there a brief introductory documentation about all
these things somewhere?

awesomey
Newbie
*
Offline Offline

Activity: 26
Merit: 2



View Profile
October 28, 2012, 03:48:11 AM
 #2

You might try this forum category: Development & Technical Discussion
Pages: [1]
  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!