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 2] - Using PHP develop Blockchain technology

Sunday, 26 December 2021
|
Read: Completed in minutes

[Knowledge] Blockchain Technology [Part 2] - Using PHP develop Blockchain technology

Blockchain Technology - Using PHP develop Blockchain technology

Blockchain Technology - Using PHP develop Blockchain technology

Welcome back to Learn Tech Tips channel, as we discuss on the first topic, This topic will using PHP language develop Blockchain technology. OK let do now

To describe a Block (transaction element) in the Blockchain (array of transactions),
we need a class like this:

Data in a Block is very simple. check below code

The most important function in each Block is the function that calculates the Hash value (encryption) of the entire Block.

Since the Hash calculation function requires a strong encryption algorithm, which is 256-bit SHA encryption, I use hash('sha256', $hash_string);

   // Author: Zidane - Learn Tech Tips,
// Email: huuvi168@gmail.com 
class Block
{
    public $time;
    public $data;
    public $previous_hash;
    public $hash;

    public function __construct($time, $data, $previous_hash = '')
    {
        $this->time = $time;
        $this->data = $data;
        $this->previous_hash = $previous_hash;
        $this->hash = $this->calc_hash();
    }

    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;
        return hash("sha256", $hash_string);
    }
}  
 

We need to create a Blockchain class to hold the array of Block elements.


The add_new_block($time, $data) function is a function that will ensure 2 things:
- Calc entire hash with current block.
- Get the Hash of the last Block to store it in the HashTruocDo variable of the current Block.
=>  Thus, the Blocks will be connected (chain-chained) together through the binding of Hash code.

The validate() function is a very important function that plays the heart role in the Blockchain.

- Every time any user wants to check the entire current Blockchain whether it is safe or not to continue the transaction.
- All Hashes in the string are compared.
- The integrity of each Block and the interconnection of the entire Block must be guaranteed without the wrong Hash code.

- If there is a false positive, the data has been edited by someone in the network.

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

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

    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;
        $this->chain[] = new Block($time, $data, $previous_hash);
    }

    public function validate()
    {
        for ($i = 1; $i < count($this->chain); $i++) {
            $current_block = $this->chain[$i];
            // pr ($current_block->data['Money']);
            // exit;
            $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;
        }
    }
}

 So the self-generated Blockchain technology is done, now we can start testing it:

    
$blockchain = new Blockchain();
$data = array("Send" => "Mr.A", "received" => "Mrs.B", "Money" => "300");
$blockchain->add_new_block(time(), $data);

$data = array("Send" => "Mrs.B", "received" => "Mr.C", "Money" => "200");
$blockchain->add_new_block(time(), $data);

$is_validate = $blockchain->validate(); 
 

=> The result will be show on $blockchain params with two transaction and after validate will return Correct data (because of no one change it before)

Array
(
    [chain] => Array
        (
            [0] => App\Controller\Admin\Block Object
                (
                    [time] => 1640505857
                    [data] => Genesis Block
                    [previous_hash] => 
                    [hash] => 8de7d3c326ac02b8cd447c87726d18eaa715c9b2647b1c2573b4eb5723686429
                )

            [1] => App\Controller\Admin\Block Object
                (
                    [time] => 1640505857
                    [data] => Array
                        (
                            [Send] => Mr.A
                            [received] => Mrs.B
                            [Money] => 300
                        )

                    [previous_hash] => 8de7d3c326ac02b8cd447c87726d18eaa715c9b2647b1c2573b4eb5723686429
                    [hash] => 05fdb82a073d3e70e19c5d4148aa9165956f1bf8468ae899f4a2662c04e0526b
                )

            [2] => App\Controller\Admin\Block Object
                (
                    [time] => 1640505857
                    [data] => Array
                        (
                            [Send] => Mrs.B
                            [received] => Mr.C
                            [Money] => 200
                        )

                    [previous_hash] => 05fdb82a073d3e70e19c5d4148aa9165956f1bf8468ae899f4a2662c04e0526b
                    [hash] => 8267a1a4404d373c5db38060470412929430039f4f4b7000a9ffc99219cfeaba
                )

        )

    [time] => 
    [data] => 
    [previous_hash] => 
    [hash] => 
)

Validate of block chain

Correct Chain

If anyone / hacker change the anydata on some element

    
        $blockchain = new Blockchain();
        $data = array("Send" =>  "Mr.A", "received" => "Mrs.B", "Money" => "300");
        $blockchain->add_new_block(time(), $data);

        $data = array("Send" =>  "Mrs.B", "received" => "Mr.C", "Money" => "200");
        $blockchain->add_new_block(time(), $data);

        $is_validate = $blockchain->validate();

        // hacker change data here
        $blockchain->chain[1]->data['Money'] = 30000;

        // we go to revalidate again
        $is_validate = $blockchain->validate();

        $this->set(compact('blockchain', 'is_validate')); 

=> The result will be

Array
(
    [chain] => Array
        (
            [0] => App\Controller\Admin\Block Object
                (
                    [time] => 1640506057
                    [data] => Genesis Block
                    [previous_hash] => 
                    [hash] => 79f9d544d7a4cf1928a53c0eca23d06fd7f3effeb2b51911fda5e4a87b525ca2
                )

            [1] => App\Controller\Admin\Block Object
                (
                    [time] => 1640506057
                    [data] => Array
                        (
                            [Send] => Mr.A
                            [received] => Mrs.B
                            [Money] => 30000
                        )

                    [previous_hash] => 79f9d544d7a4cf1928a53c0eca23d06fd7f3effeb2b51911fda5e4a87b525ca2
                    [hash] => a5db4e7fdfdf79961a39a3d6254bdf5902621b10a384d15d71df08ea0718cd9f
                )

            [2] => App\Controller\Admin\Block Object
                (
                    [time] => 1640506057
                    [data] => Array
                        (
                            [Send] => Mrs.B
                            [received] => Mr.C
                            [Money] => 200
                        )

                    [previous_hash] => a5db4e7fdfdf79961a39a3d6254bdf5902621b10a384d15d71df08ea0718cd9f
                    [hash] => 582c97c25491c100fc31813200f4fdadd8ec296ab8f0435c75b2fc88228f82c5
                )

        )

    [time] => 
    [data] => 
    [previous_hash] => 
    [hash] => 
)

Validate of block chain

Incorrect Chain

=> you will see the Incorrect Chain. That a core value of Blockchain Technology


Full source code you can checkout here - BlockchainsControllers_v1.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

 

 

Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about Blockchain Technology [Part 2] - Using PHP develop Blockchain technology , please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋  Webzone Tech Tips, all things Tech Tips for web development  - I am Zidane, See you next time soon ✋✋✋✋

🙇🏼🙇🏼 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