If you haven't read previous part, you can look up this on this link
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
First we need to know What is virtual currency?
In the previous sections, we have understood what is Blockchain, and what is Hash mining to prevent virtual transactions. However, our Blockchain because it is too difficult to create transactions. It becomes completely unattractive and no one is going to spend time mining Hash and stuffing data into our chain. We need to create something to stimulate demand. At this time, the concept of virtual currency was born.
Cryptocurrency is a REWARD for those who work hard to mine hashes, validate transactions and successfully add Blocks to our chain.
Initially, the amount of virtual money (or bonus) mined was very small, leading to competition, the exchange of money (trading) for real money.
Gradually, the money we create will appreciate.
With such a simple thought, let's start editing source code and create our first virtual currency.
Algorithm to create a virtual currency
To create a complete virtual currency, we need to allow transactions in that virtual currency.
In the previous example we created a $data form that can hold transactions. On this tutorial we will change to $datas
But to officially support transactions, we create a new class to store transactions as follows:
Now in the definition of Blockchain, we need to create 2 new variables.
The first is the $coin_temp_transaction array.
This array will contain all transactions that do not have a Hash code, waiting for miners to be added to the Blockchain.
This will allow multiple transactions to be added to the Blockchain simultaneously and on the same block.
Traders and block miners are also independent of each other.
The second is the $coin_reward variable to store the default value of the miner's bonus.
The amount of this bonus, as mentioned in the previous section, will be limited, because at some point it is not possible to create more Hash => no one has any more bonuses.
And that means our $blockchain is scarce, just like gold and silver.
Among the functions of Blockchain, we replace the mining_block() function with the coin_mining() function.
Because a Block can't be easily added to the Blockchain at this point, someone has to dig a Hash for that Block in order for it to be added to the chain.
The virtual currency mining function, as in the commented code, will help the miner to insert pending transactions into the chain. and let the next mining get the bonus value.
Why is it necessary to mine 2nd time to get 1st mining bonus? Like this, stealing people's money?
The purpose of this has 2 effects:
- To avoid diggers "eating young". Take the bounty of this Block and smash it into the other Block.
- It is to encourage digging and digging.
- Newcomers to mining, there will always be a transaction waiting for that person to mine for money.
- In fact, for BitCoin, a transaction in the Blockchain is only confirmed when behind the block containing it, 6 other blocks have been added.
- At this point, your wallet will be added or subtracted.
And only then can you continue trading.
- The time you wait for another 6 blocks to be inserted is often called the "freezing money" time in the system.
- However, this usually only lasts a few minutes.
- Only 6 blocks of transactions are certified in a wallet with coins, which is commonly known as the consensus mechanism in the system.
When there are many people mining the Blockchain containing the Block with your money. They will indirectly confirm that your transaction is trustworthy
Preventing miners from immediately getting that bonus is up to each coin they apply or not. For BitCoin, the answer is yes.
We can completely customize the algorithm to apply to our $Blockchain.
Notice also that the $coin_temp_transaction array after each Hash mining is completed, the entire array will be stuffed into the same Block.
Thus, it is possible to have a Block containing up to 100 transactions.This is purely because our code allows this.
For BitCoin or Etherium, they only allow 2 transactions in the same Block. Therefore, after mining the Hash, we have to pick up 2 transactions in the $coin_temp_transaction array to put in the newly created Block.
The actual virtual currency miner, it will randomly choose any 2 transactions (of course, prioritize the transaction that puts money in your own wallet in the $coin_temp_transaction array).
Then after having the algorithm Coin Mining
We need one more function, which is the Transaction constructor and inserts it into $coin_temp_transaction
We need another function that calculates the amount of money in each person's wallet after trading with our virtual currency.
As the code also said, each wallet does not have a total amount stored anywhere. To calculate the amount of a wallet address in the entire Blockchain, we have to find all the transactions inside the Blockchain in turn to count the amount of a wallet.This will be very safe and honest.
Transparency is important here because anyone who has your wallet address will see all the transactions you have ever done in the system.
Any comment and feedback, Leave your comment here, we can discuss about it
Full source code you can checkout here
Learn Tech Tips - Zidane
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