Bitcoin Forum
May 24, 2024, 04:42:31 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: PURE Java Miner  (Read 3634 times)
spartacusrex (OP)
Hero Member
*****
Offline Offline

Activity: 718
Merit: 545



View Profile
April 29, 2013, 12:49:43 PM
Last edit: April 29, 2013, 01:04:31 PM by spartacusrex
 #1

Hello,

I am trying to write a pure JAVA miner, a nice webstart app or applet that can be embedded in webpages, and wanted to clarify my understanding of how things work..

1) If when I add a transaction to my current block, the Merkle Root (of all the transactions) in the Block Header changes, doesn't that mean that ALL the work i have done before is for nothing as I will have to start looking for the correct HASH again ? Wouldn't it be better to not add more transactions, and keep working on my current block, with the current Merkle Root ?

2) This may be answered by the answer to 1), but just in case - Since you get 25 btc for finding a block, much more than you make from the transaction fees, Why not just work soley on finding hashes of blocks with no transactions in it ? This way your Merkle root stays constant until you need to use the extraNonce in the coinbase txn ?  Would this not give you the greatest chance of finding a valid hash of a certain difficulty ?

3) In the Bitcoin Protocol Wiki it says -

'The SHA256 hash that identifies each block (and which must have a run of 0 bits) is calculated from the first 6 fields of this structure (version, prev_block, merkle_root, timestamp, bits, nonce, and standard SHA256 padding, making two 64-byte chunks in all) and not from the complete block. To calculate the hash, only two chunks need to be processed by the SHA256 algorithm. Since the nonce field is in the second chunk, the first chunk stays constant during mining and therefore only the second chunk needs to be processed..'

This I don't get.. Where do the 2 * 64 Byte Chunks come from ? When I Hash my Block Header I get a 32 byte hash.. ?

..

I will be presenting my work as soon as it is more than nothing..  Grin

Thank You!


Life is Code.
TierNolan
Legendary
*
Offline Offline

Activity: 1232
Merit: 1083


View Profile
April 29, 2013, 01:27:22 PM
 #2

Hello,

I am trying to write a pure JAVA miner, a nice webstart app or applet that can be embedded in webpages, and wanted to clarify my understanding of how things work..

1) If when I add a transaction to my current block, the Merkle Root (of all the transactions) in the Block Header changes, doesn't that mean that ALL the work i have done before is for nothing as I will have to start looking for the correct HASH again ? Wouldn't it be better to not add more transactions, and keep working on my current block, with the current Merkle Root ?

It doesn't work that way.  If you test a hash and it fails, then that work is already lost.

It is like rolling 10 dice and hoping for all 6's.  If you do it a million times, and haven't succeeded, all that work is already gone.

Each time you update the nonce and fail, you are trying a whole new attempt.

Quote
2) This may be answered by the answer to 1), but just in case - Since you get 25 btc for finding a block, much more than you make from the transaction fees, Why not just work soley on finding hashes of blocks with no transactions in it ? This way your Merkle root stays constant until you need to use the extraNonce in the coinbase txn ?  Would this not give you the greatest chance of finding a valid hash of a certain difficulty ?

That is anti-social.  However, it would work.  There is talk of putting in some ways to deal with it, if it became a problem.

Quote
3) In the Bitcoin Protocol Wiki it says -

'The SHA256 hash that identifies each block (and which must have a run of 0 bits) is calculated from the first 6 fields of this structure (version, prev_block, merkle_root, timestamp, bits, nonce, and standard SHA256 padding, making two 64-byte chunks in all) and not from the complete block. To calculate the hash, only two chunks need to be processed by the SHA256 algorithm. Since the nonce field is in the second chunk, the first chunk stays constant during mining and therefore only the second chunk needs to be processed..'

This I don't get.. Where do the 2 * 64 Byte Chunks come from ? When I Hash my Block Header I get a 32 byte hash.. ?

Sha-2 works on 512 bit "chunks".  When hashing a file, it is padded so that it is an integer multiple of 512.

So, since it is double sha-2

First call to the sha function

Sha2(header)
This is > 64 bytes, so extend to 2X64 bytes
Round 1: bytes 0 to 63 of the header
Round 2: 64 - 79 of the header (the nonce is here)

Sha2(output from round 1)
This is 32 bytes, so it is padded to 64 bytes
Round 1: bytes 0 to 63 of the padded output

The output of the 2nd call is the hash of the header.

Since the nonce is in the first round of the first sha2 call, you can compute it once and save the state.  Each time you change the nonce, it only changes the results from round 2 onwards.

1LxbG5cKXzTwZg9mjL3gaRE835uNQEteWF
daybyter
Legendary
*
Offline Offline

Activity: 965
Merit: 1000


View Profile
April 29, 2013, 01:51:46 PM
 #3

I think Java is great, but Java in webpages is a bit dead these days? If it has to work on a webpage, I would use Javascript?

spartacusrex (OP)
Hero Member
*****
Offline Offline

Activity: 718
Merit: 545



View Profile
April 29, 2013, 05:44:35 PM
 #4

Quote
It doesn't work that way.  If you test a hash and it fails, then that work is already lost.

It is like rolling 10 dice and hoping for all 6's.  If you do it a million times, and haven't succeeded, all that work is already gone.

Each time you update the nonce and fail, you are trying a whole new attempt.

So - because it's total POT luck if you get the correct HASH.. It doesn't matter what search space you try as long as you keep trying.. i see..

THEN - Why is the NONCE not reset to 0, whenever the MerkleRoot or the Timestamp changes ? Can it be ?

& Just for completeness, am I correct in assuming IF the nonce was of variable length, you would not need the extraNonce in the coinbase txn ?

Just want to make sure I fully understand what's going on..  Roll Eyes

Life is Code.
TierNolan
Legendary
*
Offline Offline

Activity: 1232
Merit: 1083


View Profile
April 29, 2013, 06:00:35 PM
 #5

So - because it's total POT luck if you get the correct HASH.. It doesn't matter what search space you try as long as you keep trying.. i see..

yeah

Quote
THEN - Why is the NONCE not reset to 0, whenever the MerkleRoot or the Timestamp changes ? Can it be ?

Yes, you can use any nonce you want.  It is incremented by 1 so as to make things easy.  Also, it allows pools to ask different people to do different ranges.

Quote
& Just for completeness, am I correct in assuming IF the nonce was of variable length, you would not need the extraNonce in the coinbase txn ?

Yes.  In fact, if they just went for a 64-bit fixed number, then it is unlikely that it would be an issue.

1LxbG5cKXzTwZg9mjL3gaRE835uNQEteWF
spartacusrex (OP)
Hero Member
*****
Offline Offline

Activity: 718
Merit: 545



View Profile
April 29, 2013, 06:34:15 PM
 #6

Thank you.. I think I get it now..

Life is Code.
razorfishsl
Sr. Member
****
Offline Offline

Activity: 399
Merit: 250


View Profile WWW
April 30, 2013, 12:34:03 PM
 #7

If you dig about on 'github' you will find miners for both Bitcoin & litecoin in Java.

The litecoin one is  *very* badly implemented, threads are setup incorrectly. Whoever wrote it had little knowledge of how java  'implements' objects. (it actually looks like it is from a c implementation.)

But the implementations are fairly clear.


High Quality USB Hubs for Bitcoin miners
https://bitcointalk.org/index.php?topic=560003
kevinmyers80
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile
April 30, 2013, 03:13:06 PM
 #8

compagnio my university is trying to do something like this in ruby, java in your library that uses?
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!