Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: MarkIngle on April 03, 2021, 03:06:03 AM



Title: Hello! My name is Mark Ingle....
Post by: MarkIngle on April 03, 2021, 03:06:03 AM
...I am new to this forum and the bitcoin architecture.  After downloading the source code from Github I am having trouble navigating the structure of the code.  My interest is learning the logic that performs the hashing to create the Proof-of-Work and links the blocks.  My goal is to learn this part as I think it has other uses in the technology field potentially IoT and communications (top four layers).

Is there any documentation that explains the code architecture at a high level?

Thanks in advance.....and thanks to the development team that has kept this solution going!  Really awesome work!


Title: Re: Hello! My name is Mark Ingle....
Post by: NotATether on April 03, 2021, 05:38:27 AM
I assume that by source code you mean Bitcoin Core's github repo? https://github.com/bitcoin/bitcoin

My interest is learning the logic that performs the hashing to create the Proof-of-Work and links the blocks.  My goal is to learn this part as I think it has other uses in the technology field potentially IoT and communications (top four layers).

If you want to find the RPC call in which full nodes receive submitted blocks to link them on the chain, that section is located in this file: https://github.com/bitcoin/bitcoin/blob/80a699fda9ff1129546cabbf17e955680a1cc705/src/rpc/mining.cpp#L108-L137 . GitHub has an advanced search feature that let's you find usages of functions so that should help you locate the called functions.

To see how the difficulty adjustment is done look inside src/pow.cpp https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp

If however you want to see the process of hashing to make a valid block then you should look at the source code of a proper miner such as cgminer (https://github.com/ckolivas/cgminer). Bitcoin Core used to have a built-in miner but that was removed a few versions back as CPU mining became inefficient.


Title: Re: Hello! My name is Mark Ingle....
Post by: MarkIngle on April 03, 2021, 01:56:45 PM
Thank you very much!  Yes the bitcoin source code.

I found pow.cpp last night so thanks for confirming!  Really interesting code...I am not that familiar with hashing but I think I am able figure things out with all of the information available.  I think this will an interesting rabbit hole!

Thank you for the info on cgminer too.  I have Bitcoin Core running on my Mac just to help me understand as I am a hands on learner.  I didnt expect it to work but could see that has inbound/outbound connections with little activity.  With the current demands of processing the long chain it make sense the mining task would be off loaded.

I really appreciate the help!

What is the main purpose of Bitcoin Core?....transactions within the Bitcoin network?

Mark


Title: Re: Hello! My name is Mark Ingle....
Post by: NotATether on April 03, 2021, 04:32:29 PM
What is the main purpose of Bitcoin Core?....transactions within the Bitcoin network?

It's purpose is to verify all transactions on the blockchain from head to toe, and to facilitate other full nodes' verification process by sending them copies of blocks and transactions it has. Malformed transactions and blocks are dropped, and the nodes sending them are eventually blocked by their receivers from connecting to them. In this way we create a self-reliant network of nodes which collectively guarantee the integrity of the blockchain.

This works because all kinds of payment terminals like exchanges, web wallets and lightweight clients must all eventually relay their transactions to a full node.


Title: Re: Hello! My name is Mark Ingle....
Post by: Coding Enthusiast on April 04, 2021, 02:47:17 AM
I am not that familiar with hashing but I think I am able figure things out with all of the information available.  I think this will an interesting rabbit hole!
Maybe this visualization can help understand what's happening in the hashing process:
Just know that headers are 80 bytes and SHA256 breaks data into 64-byte blocks when compressing them.
https://i.imgur.com/KhItpCG.jpg
https://bitcointalk.org/index.php?topic=5158000.0


Title: Re: Hello! My name is Mark Ingle....
Post by: MarkIngle on April 04, 2021, 04:22:04 AM
Yes thank you!  Diagrams are the best when it comes to iterative logic in code.  Thank you very much!!!


Title: Re: Hello! My name is Mark Ingle....
Post by: MarkIngle on April 04, 2021, 04:30:23 AM
I also found this video with links doing a step by step of hashing data and how it works....Im putting it here for others falling down the same rabbit hole!

https://www.youtube.com/watch?v=_160oMzblY8



Title: Re: Hello! My name is Mark Ingle....
Post by: MarkIngle on April 06, 2021, 12:47:44 AM
A Block is composed of a block header and transactions(~2000).  The header is composed of the previous block hash, time stamp, nonce, and merkle root.  Two questions :  (1) Where in the process does this information get confirmed once a hash has been determined? (2) When and where is the data decrypted?


Title: Re: Hello! My name is Mark Ingle....
Post by: pooya87 on April 06, 2021, 02:57:36 AM
(1) Where in the process does this information get confirmed once a hash has been determined?
The process of mining is like this:
- Miner selects a bunch of preferred transaction from its node's mempool and creates a new block with them and fills in the header variables (that includes computing the merkle root hash and setting the correct target).
- Then the miner computes the header hash
- Then the miner converts that hash to an integer and compares it with the target. If it is <= target then moves to last step, if not the miner has to change something in the header to perform previous step and this step again. First thing that is changed is the nonce, if they run out of nonces to change they have to change something else, usually an extra nonce in the coinbase transaction that would also change the merkle root hash.
- Last step is to publish the block on the network network to let other peers know that they have found a new block.

Quote
(2) When and where is the data decrypted?
There is no encryption anywhere in bitcoin protocol.


Title: Re: Hello! My name is Mark Ingle....
Post by: MarkIngle on April 06, 2021, 11:35:57 PM
Quote
(2) When and where is the data decrypted?
There is no encryption anywhere in bitcoin protocol.
[/quote]

decrypted - Poor choice of word on my part.  Sorry.  My 2nd question was really about the data used to create the merkle root.  Is the merkle root hash used in some way to breakout the transaction data so that is can be confirmed?


Title: Re: Hello! My name is Mark Ingle....
Post by: pooya87 on April 07, 2021, 03:31:06 AM
My 2nd question was really about the data used to create the merkle root.  Is the merkle root hash used in some way to breakout the transaction data so that is can be confirmed?
Merkle root hash is computed using the transaction hash of all transactions in the block by creating a "merkle tree" then selecting and hashing 2 transaction hashes at a time.

tx1 tx2 tx3 tx4 tx5 tx5duplicate
 └───┘   └───┘   └───┘
  h1&2    h3&4    h5&5  h5&5duplicate
   └───────┘       └──────┘
     h12&34         h55&55
       └──────────────┘
          root hash

Merkle root hash is there to ensure that the transactions in the block can not change in any way (remove, add new, change position,...) because if any of these changes occur the transaction hash is changed and consequently the merkle root hash is changed.