Blockchain Technology [Part 4] – Create virtual currency mining technology
Welcome to Learn Tech Tips blog, Today I will continue to share how to create virtual currency mining technology. If you havn’t read part 1, part 2 and part 3, you should view back
In the Block class we add a $no, this is a variable used when calculating the Hash.
// Email: huuvi168@gmail.com
Since we need to increase the difficulty every time the Hash, the calc_hash function remains the same, but we create a simple algorithm, that is, it does not accept the generated Hash code that does not meet the requirements.
Our requirement here is that the Hash code must start with 2 or 3 or 4 or even 5 consecutive “0”.
Since the SHA256 algorithm never changes the output for an input string, it is imperative to change the input string to find a satisfactory output Hash.
Therefore, we write a function called mining_block function to repeat the process of “mining” the Hash code for the current Block.
And this mining difficulty depends on the $nonce variable.
If you increase the number of consecutive 0 digits, it takes a long time to loop to find a satisfactory Hash string.
Example:
$nonce = 2, it takes 0.001s to find a Hash with 2 consecutive zeros at the beginning.
$nonce = 3, it takes 0.5s to find a Hash with 3 consecutive 0s at the beginning.
$nonce = 4, it takes 3s to find a Hash with 4 consecutive 0s at the beginning.
$nonce = 5, it takes 20s to find a Hash with 5 consecutive zeros at the beginning.
For the BitCoin virtual currency, the difficulty of the algorithm is adjusted so that every 10 minutes there will be a Hash code.
Of course, BitCoin’s algorithm is not as simple as the one here. So you already figured it out.
Proof-Of-Work is also abbreviated as PoW.
The PoW concept applied to the Blockchain is similar to the anti-SPAM and DDOS mechanism for a website using ReCapcha, users who want to verify that they are not an automatic robot have to spend time typing a strange string of characters.
// Email: huuvi168@gmail.com
We need to create a variable $nonce in the Blockchain.
Then we call $block->mining_block($this->nonce); so that every time we create a new Block (add_new_block function), we have to dig a new Hash code for it.
Leave your comment if you need discuss anything about Blockchain technology
Learn Tech Tips – Zidane
Full source code you can checkout here – BlockchainsControllers_v2.php
Related Posts:
Blockchain Part 1: What is Blockchain: https://learn-tech-tips.blogspot.com/2021/12/what-is-blockchain.html
Blockchain Part 2: Using PHP develop Blockchain Technology: https://learn-tech-tips.blogspot.com/2021/12/using-php-develop-blockchain-technology.html
Blockchain Part 3: Loophples of the current Blockchain:
https://learn-tech-tips.blogspot.com/2021/12/loophples-of-the-current-blockchain.html
Blockchain Part 4: Create virtual currency mining technology:
Blockchain Part 5: How to create own bitcoin virtual currency
Blockchain Part 6: Apply blockchain application in the life
https://learn-tech-tips.blogspot.com/2022/01/block-chain-application-in-the-life.html