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 "Create virtual currency mining technology" (Part 4)

Wednesday, 29 December 2021
|
Read: Completed in minutes

[Knowledge] Blockchain Technology "Create virtual currency mining technology" (Part 4)

Blockchain Technology "Create virtual currency mining technology" (Part 4) 

Blockchain Technology "Create virtual currency mining technology"

 

Welcome to Webzone Tech Tips Zidane, Today I will continue to share how to create virtual currency mining technology. If you haven'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.


// Author: Zidane - Learn Tech Tips,

// Email: huuvi168@gmail.com

class Block

{

    public $time;

    public $data;

    public $previous_hash;

    public $hash;

    public $no;

    public $mining_time_stamp;

    public $start_mining_time;

    public $end_mining_time;


    public function __construct($time, $data, $previous_hash = '')

    {

        $this->time = $time;

        $this->data = $data;

        $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->data))

        {

            $data_content = json_encode($this->data);

        } else 

        {

            $data_content = $this->data;

        }


        $hash_string = $this->time . $data_content . $this->previous_hash . $this->no;

        return hash('sha256', $hash_string); 

    }


    // mining_block: the core of algorithm mining block

    public function mining_block($nonce) {


        while (substr($this->hash, 0, $nonce) !==  str_pad('',  $nonce, "0") ) {

            $this->no++;

            $this->hash = $this->calc_hash();

        }

    }

}


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.

//Author: Zidane - Webzone Tech Tips

// Email: huuvi168@gmail.com

class Blockchain extends Block

{

    public $chain = array();

    public $nonce = 0;


    public function __construct()

    {

        $this->chain[] = new Block(time(), "Genesis Block");

        $this->nonce = 3;

    }


    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, $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;

        }

    }

}



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: 

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

🙇🏼🙇🏼 Your Feedback Is Valuable and Helpful to Us - Webzone, all things Tech Tips web development Zidane
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