Bitcoin Forum
May 02, 2024, 07:30:21 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: miner client - just to understand mining  (Read 1143 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?

1714678221
Hero Member
*
Offline Offline

Posts: 1714678221

View Profile Personal Message (Offline)

Ignore
1714678221
Reply with quote  #2

1714678221
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714678221
Hero Member
*
Offline Offline

Posts: 1714678221

View Profile Personal Message (Offline)

Ignore
1714678221
Reply with quote  #2

1714678221
Report to moderator
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!