Jan 1, 2022

Blockchain

A simple blockchain demonstration written in PHP.

#Simple Blockchain in PHP

Welcome to this Simple Blockchain implementation in PHP. This project is designed as a basic demonstration of how a blockchain works, including the creation of blocks, chaining them together, and verifying the integrity of the blockchain. It also includes a simple transaction model to illustrate how blockchains can be used to record transactions, such as financial trades or exchanges.

Click here to go to the github repository

#Project Structure

This project consists of three main components:

  • Block.php: Defines the structure of each block in the blockchain.
  • Blockchain.php: Manages the chain of blocks and ensures the integrity of the blockchain.
  • Transaction.php: Represents a transaction that can be included in a block.

#Block.php

The Block class includes several key properties and methods:

  • Properties like timestamp, transactions, prev_hash, hash, and nonce.
  • __construct(): Constructor to initialize a new block.
  • calculateHash(): Calculates the hash of the block based on its properties.
  • mineBlock($difficulty): Implements proof-of-work by finding a hash that matches the difficulty level.

#Blockchain.php

The Blockchain class is responsible for managing the chain of blocks and includes:

  • Properties like chain (an array of blocks) and difficulty (the proof-of-work difficulty).
  • __construct(): Initializes the blockchain with a genesis block.
  • addBlock($block): Adds a new block to the chain after mining.
  • getLatestBlock(): Returns the most recently added block.
  • isValid(): Verifies the integrity of the blockchain.

#Transaction.php

The Transaction class models a simple transaction with properties:

  • timestamp, type (buy/sell), stock_symbol, shares, price, buyer_public_key, and seller_public_key.
  • __construct(): Constructor to initialize a new transaction.

#Getting Started

To get started with this simple blockchain in PHP, follow these steps:

#Prerequisites

Ensure you have PHP installed on your system. This project was developed with PHP 7.4, but it should work with any PHP version 7.0+.

#Setup

  1. Clone or download this repository to your local machine.
  2. Navigate to the project directory.
  3. There's no need for a web server; the scripts are intended to be run from the command line.

#Running the Blockchain

To interact with the blockchain, you will need to create a PHP script that imports these classes and demonstrates their functionality. Here's a basic example of how to do this:

blockchain.php
require_once 'Block.php';
require_once 'Blockchain.php';
require_once 'Transaction.php';

// Initialize blockchain with difficulty
$blockchain = new Blockchain(4);

// Create some transactions
$transaction1 = new Transaction('buy', 'AAPL', 10, 150.00, 'buyerPublicKey', 'sellerPublicKey');
$transaction2 = new Transaction('sell', 'GOOGL', 5, 1000.00, 'buyerPublicKey', 'sellerPublicKey');

// Add a block containing the transactions
$block1 = new Block(array($transaction1, $transaction2), $blockchain->getLatestBlock()->hash);
$blockchain->addBlock($block1);

// Verify the blockchain
if ($blockchain->isValid()) {
    echo "Blockchain is valid.\n";
} else {
    echo "Blockchain is not valid.\n";
}

// Print the blockchain
echo json_encode($blockchain, JSON_PRETTY_PRINT);

#Contributing

Contributions to this project are welcome! Feel free to fork the repository, make changes, and submit pull requests. If you find any bugs or have suggestions for improvement, please open an issue.

#License

This Simple Blockchain in PHP is open-sourced software licensed under the MIT license. See the LICENSE file for more details.


This project is intended for educational purposes and should not be used as-is for real-world applications. It demonstrates the basic principles of blockchain technology but lacks many features and security measures necessary for a production-ready blockchain system.