Author

Topic: Generating the candidate block and merkle root (Read 109 times)

legendary
Activity: 3472
Merit: 10611
February 19, 2020, 01:27:30 AM
#4
What I don't know is how to generate the first transaction (coinbase transaction) which includes my wallet address in it so when I find a hash less than target, I can get the reward.

to know that you first have to understand the transaction structure and learn what each part means. for example the scripts https://en.bitcoin.it/wiki/Script
so to answer your question directly, you should create a new txout and set its scriptpub to a new script that corresponds to your address. in pseudo code:
Code:
addresstype = GetType($address)
if (addresstype == base58_p2pkh)
  scriptpub = OP_DUP OP_HASH160 Push(ConvertAddrToHash($address)) OP_EQUALVERIFY OP_CHECKSIG
else if (addresstype == base58_p2sh)
  scriptpub = OP_HASH160 Push(ConvertAddrToHash($address)) OP_EQUAL
else if (addresstype == bech32_p2wpkh)
  scriptpub = OP_0 Push(ConvertAddrToHash($address))
....
the address you posted above is the first condition.
newbie
Activity: 7
Merit: 10
so to hash it as a candidate block what should I do? concatenate all the information and double SHA256 it and convert to little endian?

Code:
(hash (null) & index (UINT32_MAX) & script bytes & height & coinbase script & sequence) --> convert to little endian? 

you should start learning from transactions first then move to blocks, merkle root and then mining. scroll above and read about a transaction structure and its serialization. https://bitcoin.org/en/developer-reference#raw-transaction-format

what you use in merkle root is the transaction hashes that is calculated by computing the double SHA256 of the serialized transaction (version | txInCount | {txin1 | txin2|...} | txOutCount | {txout1 | txout2 |...} | locktime) + witness and its flag if available.

then to compute merkle root you put hashes together 2 by 2 and build a "tree" until you reach 1 hash as the "root" and put that root in the header before hashing the header. https://bitcoin.org/en/developer-reference#merkle-trees
for example a block with 5 transactions:
Code:
tx1 | tx2
          -> hash1
tx3 | tx4            -> root
          -> hash2
tx5 | tx5


Thank you for your reply,

I followed your link https://bitcoin.org/en/developer-reference#raw-transaction-format and still I don't see anywhere that I can place my public wallet address in the following raw transaction format.

Code:
version 	
tx_in count
tx_in
tx_out count
tx_out
lock_time

I briefly know how to generate the merkle root from transactions. I previously watched this video of generating merkle root https://www.youtube.com/watch?v=gUwXCt1qkBU . I understand the transactions should be hashed together to generate the merkle root. What I don't know is how to generate the first transaction (coinbase transaction) which includes my wallet address in it so when I find a hash less than target, I can get the reward.

Please let me know where should I place my wallet address.
legendary
Activity: 3472
Merit: 10611
so to hash it as a candidate block what should I do? concatenate all the information and double SHA256 it and convert to little endian?

Code:
(hash (null) & index (UINT32_MAX) & script bytes & height & coinbase script & sequence) --> convert to little endian? 

you should start learning from transactions first then move to blocks, merkle root and then mining. scroll above and read about a transaction structure and its serialization. https://bitcoin.org/en/developer-reference#raw-transaction-format

what you use in merkle root is the transaction hashes that is calculated by computing the double SHA256 of the serialized transaction (version | txInCount | {txin1 | txin2|...} | txOutCount | {txout1 | txout2 |...} | locktime) + witness and its flag if available.

then to compute merkle root you put hashes together 2 by 2 and build a "tree" until you reach 1 hash as the "root" and put that root in the header before hashing the header. https://bitcoin.org/en/developer-reference#merkle-trees
for example a block with 5 transactions:
Code:
tx1 | tx2
          -> hash1
tx3 | tx4            -> root
          -> hash2
tx5 | tx5
newbie
Activity: 7
Merit: 10
I'm trying to develop a solo miner for bitcoin, now I'm in the stage of generating the merkle root. To make the coinbase transaction I looked at https://bitcoin.org/en/developer-reference#coinbase , but I don't understand the structure etc

as I understand, to generate the merkle root, the transactions should be taken from mempool and should be hashed plus the coinbase transaction should be placed as the first transaction when hashing.

the above link states he coinbase transaction should be as follows, this is the candidate block structure right?
Code:
hash (null)
index (UINT32_MAX)
script bytes
height
coinbase script
sequence

so to hash it as a candidate block what should I do? concatenate all the information and double SHA256 it and convert to little endian?

Code:
(hash (null) & index (UINT32_MAX) & script bytes & height & coinbase script & sequence) --> convert to little endian? 


and where do I place my public wallet address? inside the "script bytes"? Can someone shed some light here?

Code:
script bytes --> Wallet address: 18ib9rJq7LiNZTSbGL7fHdJmZgWW6QEJCD

 
Jump to: