Yo guys, what's up.
I know this topic is a bit weird but I am getting bored creating websites with PHP that's why I thought to create Blockchain with PHP.
Introduction
Blockchain is the most used tech nowadays, it is the latest and most secure tech.
Use cases of Blockchain
There are many uses of Blockchain
- Messaging App
- Healthcare App
- Transaction System
- Secure data storing
- and many more…
What You are Learning with this Tutorial
- Basics of PHP
- PHP Classed and Functions
- Working of Blockchain
- Working Ok POW (Proof of Work)
So let’s get started….
Requirements
That’s good if you know the basics of PHP and PHP must be installed on your device. To install PHP just simply download XAMPP or WAMP.
Setup
If you are using XAMPP then create a folder in “htdocs” folder, I am creating a folder blockchain in “htdocs” then create a folder blockchain.php in this folder.
Open XAMPP and run Apache Web Server then open “localhost/blockchain/blockchain.php” on your preferred browser.
Creating Blockchain Class
Define blockchain class
class Blockchain {}
Then define variables - chain, and difficulty
public chain
private difficulty
It will look like this
Define construct function
In this function, we are going to define the chain variable and set the difficulty to find the hash and after that, we are going to create a genesis block;
function __construct() {
$this->chain = [] // empty
$this->difficulty = 4 // preffered to set between 3 - 6
$this->createBlock(1, '0'); // genesis block
}
Define createBlock function
The example block looks like this
create a createBlock function with parameters — proof and previous_hash
function createBlock($proof, $previous_hash){
$block = array(
'index' => $this->get_chain_length() + 1,
'timestamp' => time(),
'proof' => $proof,
'previous_hash' => $previous_hash
);
array_push($this->chain, $block); // add block to the chain
return $block;
}
after that we need to define get_chain_length function
Define get_chain_length function
This will return the current chain index
function get_chain_length(){
$i = 0;
foreach($this->chain as $ch){
$i += 1;
}
return $i;
}
Now we need the previous block to hash it and create a new block
Define print_previous_block function
This will return the current block
function print_previous_block() {
return $this->chain[$this->get_chain_length() - 1];
}
Now we have to hash the current block to create a new block
Define hash function
This will hash the block
function hash($block){
return hash('sha256', json_encode($block));
}
Define proof_of_work function
We have to validate or mine the block using the sha256 hash algorithm. If you set the difficulty to 4 then we have to hash the new_proof which is by default set to 1 and previous_proof which we will get from the previous block, then we have to hash the square of new_proof minus the square of previous_proof if the hash contains 4 zeros in the start then we will get the new_proof as correct else we will increment the new_proof till we get 4 zeros.
Now you can mine the block and create new block
Define mine function (outside class)
Complete Code
The final code will look like this
Response
New Block has been created
Index: 2
Timestamp: 1649453381
Proof: 533
Previous Hash: f345ea860cb2c5fb4e6c5845ecb2bce3a09512fb7187fb60f5c7d6e85075442fNew Block has been created
Index: 3
Timestamp: 1649453381
Proof: 45293
Previous Hash: 26bcd51f317c9258beb826912184c4a6f42b4e1c17f8f41cf05b40dfee7ec62f
In this tutorial, you learned about basic blockchain and PHP classes and functions. So please like and follow my page, by doing that I will get appreciation to post more like this.
Thank You