Bitcoin Forum
May 14, 2024, 03:19:04 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Generating the candidate block and merkle root  (Read 107 times)
PrettyGirl741963 (OP)
Newbie
*
Offline Offline

Activity: 7
Merit: 10


View Profile
February 19, 2020, 05:18:13 AM
Merited by bones261 (4), fillippone (3), ABCbits (1), o_e_l_e_o (1)
 #1

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

 
The trust scores you see are subjective; they will change depending on who you have in your trust list.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715656744
Hero Member
*
Offline Offline

Posts: 1715656744

View Profile Personal Message (Offline)

Ignore
1715656744
Reply with quote  #2

1715656744
Report to moderator
1715656744
Hero Member
*
Offline Offline

Posts: 1715656744

View Profile Personal Message (Offline)

Ignore
1715656744
Reply with quote  #2

1715656744
Report to moderator
1715656744
Hero Member
*
Offline Offline

Posts: 1715656744

View Profile Personal Message (Offline)

Ignore
1715656744
Reply with quote  #2

1715656744
Report to moderator
pooya87
Legendary
*
Offline Offline

Activity: 3444
Merit: 10560



View Profile
February 19, 2020, 05:35:39 AM
Merited by bones261 (4), fillippone (3), o_e_l_e_o (2), joniboini (2), ABCbits (1)
 #2

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

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
PrettyGirl741963 (OP)
Newbie
*
Offline Offline

Activity: 7
Merit: 10


View Profile
February 19, 2020, 05:59:40 AM
 #3

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.
pooya87
Legendary
*
Offline Offline

Activity: 3444
Merit: 10560



View Profile
February 19, 2020, 06: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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
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!