Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Knowledge] Blockchain Technology [Part 5] - How to create own Bitcoin virtual currency

Friday, 31 December 2021
|
Read: Completed in minutes

[Knowledge] Blockchain Technology [Part 5] - How to create own Bitcoin virtual currency

Welcome to Learn Tech Tips blog, Today We will discuss how to create own Bitcoin virtual currency

How to create own Bitcoin virtual currency

 

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: 

https://learn-tech-tips.blogspot.com/2021/12/blockchain-technology-create-virtual-currency-mining-technology.html

Blockchain Part 5:  How to create own bitcoin virtual currency

https://learn-tech-tips.blogspot.com/2021/12/blockchain-technology-how-to-create-own-bitcoin-virtual-currency.html 

Blockchain Part 6: Apply blockchain application in the life

https://learn-tech-tips.blogspot.com/2022/01/block-chain-application-in-the-life.html







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.

public $coin_temp_transaction = array();
public $coin_reward = 0;
 
// Author: Zidane - Learn Tech Tips
// Email: huuvi168@gmail.com
class Block
{
public $time;
public $datas;
public $previous_hash;
public $hash;
public $no;
public $mining_time_stamp;
public $start_mining_time;
public $end_mining_time;
public $list_transactions;

public function __construct($time, $datas, $previous_hash = '')
{
$this->time = $time;
$this->datas = $datas;
$this->previous_hash = $previous_hash;
$this->hash = $this->calc_hash();
$this->no = 0;
$this->mining_time_stamp = 0;
$this->start_mining_time = 0;
$this->end_mining_time = 0;
}

public function calc_hash()
{
$data_content = "";
if (is_array($this->datas)) {
$data_content = json_encode($this->datas);
} else {
$data_content = $this->datas;
}

$hash_string = $this->time . $data_content . $this->previous_hash . $this->no;
return hash('sha256', $hash_string);
}

public function mining_block($nonce)
{

while (substr($this->hash, 0, $nonce) !== str_pad('', $nonce, "0")) {
$this->no++;
$this->hash = $this->calc_hash();
}
}
}



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.

// Author: Zidane - Learn Tech Tips
// Email: huuvi168@gmail.com
class Blockchain extends Block
{
public $chain = array();
public $nonce = 0;
public $coin_temp_transaction = array();
public $coin_reward = 0;

public function __construct()
{
$this->chain[] = new Block(time(), "Genesis Block");
$this->nonce = 3;

// reward for mining bitcoin
$this->coin_temp_transaction = array();
$this->coin_reward = 200;
}

public function coin_mining($address_wallet)
{
$previous_hash = $this->get_latest_item()->hash;
$block = new Block(time(), $this->coin_temp_transaction, $previous_hash);

$start_mining_time = microtime(true);
$block->start_mining_time = gmdate("Y-m-d\TH:i:s\Z", (int)$start_mining_time);
$block->mining_block($this->nonce);
$end_mining_time = microtime(true);
$block->end_mining_time = gmdate("Y-m-d\TH:i:s\Z", (int)$end_mining_time);

$block->mining_time_stamp = $end_mining_time * 1000 - $start_mining_time * 1000;

$this->chain[] = $block;

// reward for miners
$this->coin_temp_transaction = [
new Transaction(null, $address_wallet, $this->coin_reward),
];

// Note, we dont receive any money here because the transaction not yet add to block
        // for get the reward this time, We must WAITING FOR next mining

}

// create a transaction
public function create_transaction($new_transaction) {
$this->coin_temp_transaction[] = $new_transaction;
}

// check and calc money in wallet
public function check_money_in_wallet($address_wallet) {
$current_money = 0;
foreach ($this->chain as $chain) {

if (isset($chain->datas) && !empty($chain->datas) && is_array($chain->datas)) {
foreach ($chain->datas as $data) {
if ($data->send_address == $address_wallet) {
$current_money -= $data->value;
}
if ($data->receive_address == $address_wallet) {
$current_money += $data->value;
}
}
}
}
return $current_money;
}

public function get_latest_item()
{
return $this->chain[count($this->chain) - 1];
}

public function add_new_block($time, $data)
{
$previous_hash = $this->get_latest_item()->hash;

$block = new Block($time, $data, array(), $previous_hash);

$start_mining_time = microtime(true);
$block->start_mining_time = gmdate("Y-m-d\TH:i:s\Z", (int)$start_mining_time);
$block->mining_block($this->nonce);
$end_mining_time = microtime(true);
$block->end_mining_time = gmdate("Y-m-d\TH:i:s\Z", (int)$end_mining_time);

$mining_time_stamp = $end_mining_time * 1000 - $start_mining_time * 1000;
$block->mining_time_stamp = $mining_time_stamp;

$this->chain[] = $block;
}

public function validate()
{
for ($i = 1; $i < count($this->chain); $i++) {
$current_block = $this->chain[$i];
$previous_block = $this->chain[$i - 1];

if ($current_block->hash != $current_block->calc_hash()) {
return false;
}

if ($current_block->previous_hash != $previous_block->hash) {
return false;
}

return true;
}
}
}



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.

        $blockchain = new Blockchain();
     
        // change create block to create transaction
$blockchain->create_transaction(new Transaction(
'wallet_address_Mr_A',
'wallet_address_Mrs_B',
300,
));

$blockchain->create_transaction(new Transaction(
'wallet_address_Mrs_B',
'wallet_address_Mr_C',
200,
));

$blockchain->create_transaction(new Transaction(
'wallet_address_Mr_D',
'wallet_address_Mr_C',
1000,
));

// mining the first time
$blockchain->coin_mining('0x1231231231'); // my address

// check current balance
$money_before_mining = $blockchain->check_money_in_wallet('0x1231231231');

// mining the secone time;
$blockchain->coin_mining('0x1231231231'); // my address

// check current balance
$money_after_mining = $blockchain->check_money_in_wallet('0x1231231231');

// change any data will cause error
$blockchain->chain[1]->datas[2]->money = 3000;


// show all blockchain after mining coin;
pr ($blockchain);

// check validate blockchain after add coin
$is_validate = $blockchain->validate();




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

class Transaction
{

public $send_address;
public $receive_address;
public $value;

public function __construct($send_address, $receive_address, $value)
{
$this->send_address = $send_address;
$this->receive_address = $receive_address;
$this->value = $value;
}
}


We need another function that calculates the amount of money in each person's wallet after trading with our virtual currency.


public function check_money_in_wallet($address_wallet) {
$current_money = 0;
foreach ($this->chain as $chain) {

if (isset($chain->datas) && !empty($chain->datas) && is_array($chain->datas)) {
foreach ($chain->datas as $data) {
if ($data->send_address == $address_wallet) {
$current_money -= $data->value;
}
if ($data->receive_address == $address_wallet) {
$current_money += $data->value;
}
}
}
}
return $current_money;
}


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: 

https://learn-tech-tips.blogspot.com/2021/12/blockchain-technology-create-virtual-currency-mining-technology.html

Blockchain Part 5:  How to create own bitcoin virtual currency

https://learn-tech-tips.blogspot.com/2021/12/blockchain-technology-how-to-create-own-bitcoin-virtual-currency.html 

Blockchain Part 6: Apply blockchain application in the life

https://learn-tech-tips.blogspot.com/2022/01/block-chain-application-in-the-life.html



🙇🏼🙇🏼 We Appreciate Your Comments and Suggestions - Webzone, all things Tech Tips web development
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com