I still have trouble with my mining example.
Thanks to mskwik I have example data to work with. Examining that data I think my script is wrong.
Could you please look at the example below and point me out where things go wrong?
So, the example code from mskwik is:
{"hash1"=>"00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000", "target"=>"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000", "submitold"=>true, "identifier"=>"91138", "data"=>"00000002fc517a2df2b283474b135215a00604af276318262f5eebc00000043100000000db9fcfcc3781c1342c2750214e46407286cbf29985e688d0392e6b8005c4c8245032580a1a07a85e00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000", "midstate"=>"03ad4305c1cad2bf14a99b82f3557b5722a060d6ac207450e939cb9f8143a605"}
In a more human-readable format the essence of this is the data and the target:
Data:
00000002 fc517a2df2b283474b135215a00604af276318262f5eebc00000043100000000 db9fcfcc3781c1342c2750214e46407286cbf29985e688d0392e6b8005c4c824 5032580a 1a07a85e 00000000 000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000
Target:
ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000
We also know that the right solution is: (nonce = 0x8e631c12)
Solution:
00000002 fc517a2df2b283474b135215a00604af276318262f5eebc00000043100000000 db9fcfcc3781c1342c2750214e46407286cbf29985e688d0392e6b8005c4c824 5032580a 1a07a85e [b]8e631c12[/b]
000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000
Now I'll calculate the hash of this, and it should be less than, or equal to the target.
I do the endian change first (I guess that's the point where I screw up things), and then I
convert it to binary from hexa, because SHA2-256 hashing needs binary input:
After endian correction:
02000000 00000000000004312f5eebc027631826a00604af4b135215f2b28347fc517a2d 05c4c824392e6b8085e688d086cbf2994e4640722c2750213781c134db9fcfcc 0a583250 5ea8071a 121c638e 020000002d7a51fc4783b2f21552134baf0406a026186327c0eb5e2f3104000000000000cccf9fdb34c181372150272c
Binary string representation:
"\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x041/^\xEB\xC0'c\x18&\xA0\x06\x04\xAFK\x13R\x15\xF2\xB2\x83G\xFCQz-\x05\xC4\xC8.k\x80\x85\xE6\x88\xD0\x86\xCB\xF2\x99NF@r,'P!7\x81\xC14\xDB\x9F\xCF\xCC\nX2P^\xA8\a\x1A\x12\x1Cc\x8E\x02\x00\x00\x00-zQ\xFCG\x83\xB2\xF2\x15R\x13K\xAF\x04\x06\xA0&\x18c'\xC0\xEB^/1\x04\x00\x00\x00\x00\x00\x00\xCC\xCF\x9F\xDB4\xC1\x817!P',"
From this point it's easy. I calculate the hash of the binary data, and compare it with
the (reversed) target. Both of them are hexa numbers:
The hash of the binary data:
"bd702df8e1212ab65f23972dfd3b1b601b132efb61a09e4523acdbaefae80e73"
Target to compare with:
"00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
Result: BAD NEWS: the hash is greater than the target :(
So, my program tells me that it can't be the good solution, because bd702df8e... > 00000000f...
Where's the mistake? Or: where are the mistakes?