BackendGuy
php blockchain

PHP Blockchain implementation

Please this is a Featured post promoting a minimal implementation of PHP in blockchain by https://github.com/knowledgearcdotorg

 

At the heart of cryptocurrencies lies the concept of a blockchain, which, as the name implies, chains a number of blocks together, one after the other, with each block holding information whether transactions, smart contracts, data structures or anything else which needs to record something in an immutable way.

While revolutionary, the concept of the blockchain is quite simple; it is really just a linked list data structure. And it’s very easy to develop your own blockchain in pretty much any programming language you can think of.

There are already a number of tutorials on how to build blockchains in various languages and most cryptocurrency code is open sourced and available on Github, so simply browsing source is also a great way to learn how blockchains work. C, C++, Python and Java are the more popular languages for building blockchain technologies but there are also tutorials for Javascript, DotNet and others. Surprisingly, I was unable to turn up a PHP blockchain implementation so I thought I would develop a simple one myself with the aim of better understanding how the blockchain works. Using a blockchain tutorial for Javascript, I coded up a PHP-based blockchain and have made it publicly available on Github in the hope that others may also find it of interest.

Currently the implementation is very basic but it highlights the principles of a block chain; blocks can hold arbitrary data, are linked together via a blockchain and cannot be changed without corrupting the entire chain. I have also added basic mining to demonstrate the concept of proof-of-work.

The project contains at least 4 files which can be easily implemented from the command line.

please the PHP blockchain file or github repository does not in any way belong to me, neither am i affiliated with it, the codes here are implementation of the php blockchain open source project which can be found HERE ON GITHUB

Create a file blockchain.php and include this code

 
<?php require_once("./block.php"); 
/** * A simple blockchain class 
with proof-of-work (mining). */ 

class BlockChain 
{ /** * Instantiates a new Blockchain. */
 public function __construct() 
{ $this->chain = [$this->createGenesisBlock()];
$this->difficulty = 4;
}
/**
* Creates the genesis block.
*/
private function createGenesisBlock()
{
return new Block(0, strtotime("2017-01-01"), "Genesis Block");
}
/**
* Gets the last block of the chain.
*/
public function getLastBlock()
{
return $this->chain[count($this->chain)-1];
}
/**
* Pushes a new block onto the chain.
*/
public function push($block)
{
$block->previousHash = $this->getLastBlock()->hash;
$this->mine($block);
array_push($this->chain, $block);
}
/**
* Mines a block.
*/
public function mine($block)
{
while (substr($block->hash, 0, $this->difficulty) !== str_repeat("0", $this->difficulty)) {
$block->nonce++;
$block->hash = $block->calculateHash();
}
echo "Block mined: ".$block->hash."\n";
}
/**
* Validates the blockchain's integrity. True if the blockchain is valid, false otherwise.
*/
public function isValid()
{
for ($i = 1; $i < count($this->chain); $i++) {
$currentBlock = $this->chain[$i];
$previousBlock = $this->chain[$i-1];
if ($currentBlock->hash != $currentBlock->calculateHash()) {
return false;
}
if ($currentBlock->previousHash != $previousBlock->hash) {
return false;
}
}
return true;
}
}
?>

Now create another file name block.php

 <?php 
class Block
{
public $nonce;
public function __construct($index, $timestamp, $data, $previousHash = null)
{
$this->index = $index;
$this->timestamp = $timestamp;
$this->data = $data;
$this->previousHash = $previousHash;
$this->hash = $this->calculateHash();
$this->nonce = 0;
}
public function calculateHash()
{
return hash("sha256", $this->index.$this->previousHash.$this->timestamp.((string)$this->data).$this->nonce);
}
} 
?>

How to run According to the ReadMD file of the repo on github, click on the link above.

PHP Blockchain is a simple implementation of the blockchain data structure and proof-of-work using the PHP programming language. It is designed as an introduction to blockchain technology for PHP programmers.

To run, download the source code, and, from the command line, run:

php tests/simplechain.php

This will output a simple blockchain with 3 blocks.

php tests/hackchain.php

This will validate the chain before and after a block is changed and shows the immutability of blockchains.

BackendGuy

Add comment

Your Header Sidebar area is currently empty. Hurry up and add some widgets.