├── tests ├── simplechain.php └── hackchain.php ├── README.md ├── block.php └── blockchain.php /tests/simplechain.php: -------------------------------------------------------------------------------- 1 | push(new Block(1, strtotime("now"), "amount: 4")); 12 | 13 | echo "mining block 2...\n"; 14 | $testCoin->push(new Block(2, strtotime("now"), "amount: 10")); 15 | 16 | echo json_encode($testCoin, JSON_PRETTY_PRINT); 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP BlockChain 2 | 3 | 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. 4 | 5 | To run, download the source code, and, from the command line, run: 6 | 7 | php tests/simplechain.php 8 | 9 | This will output a simple blockchain with 3 blocks. 10 | 11 | php tests/hackchain.php 12 | 13 | This will validate the chain before and after a block is changed and shows the immutability of blockchains. -------------------------------------------------------------------------------- /block.php: -------------------------------------------------------------------------------- 1 | index = $index; 9 | $this->timestamp = $timestamp; 10 | $this->data = $data; 11 | $this->previousHash = $previousHash; 12 | $this->hash = $this->calculateHash(); 13 | $this->nonce = 0; 14 | } 15 | 16 | public function calculateHash() 17 | { 18 | return hash("sha256", $this->index.$this->previousHash.$this->timestamp.((string)$this->data).$this->nonce); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/hackchain.php: -------------------------------------------------------------------------------- 1 | push(new Block(1, strtotime("now"), "amount: 4")); 12 | 13 | echo "mining block 2...\n"; 14 | $testCoin->push(new Block(2, strtotime("now"), "amount: 10")); 15 | 16 | echo "Chain valid: ".($testCoin->isValid() ? "true" : "false")."\n"; 17 | 18 | echo "Changing second block...\n"; 19 | $testCoin->chain[1]->data = "amount: 1000"; 20 | $testCoin->chain[1]->hash = $testCoin->chain[1]->calculateHash(); 21 | 22 | echo "Chain valid: ".($testCoin->isValid() ? "true" : "false")."\n"; 23 | -------------------------------------------------------------------------------- /blockchain.php: -------------------------------------------------------------------------------- 1 | chain = [$this->createGenesisBlock()]; 15 | $this->difficulty = 4; 16 | } 17 | 18 | /** 19 | * Creates the genesis block. 20 | */ 21 | private function createGenesisBlock() 22 | { 23 | return new Block(0, strtotime("2017-01-01"), "Genesis Block"); 24 | } 25 | 26 | /** 27 | * Gets the last block of the chain. 28 | */ 29 | public function getLastBlock() 30 | { 31 | return $this->chain[count($this->chain)-1]; 32 | } 33 | 34 | /** 35 | * Pushes a new block onto the chain. 36 | */ 37 | public function push($block) 38 | { 39 | $block->previousHash = $this->getLastBlock()->hash; 40 | $this->mine($block); 41 | array_push($this->chain, $block); 42 | } 43 | 44 | /** 45 | * Mines a block. 46 | */ 47 | public function mine($block) 48 | { 49 | while (substr($block->hash, 0, $this->difficulty) !== str_repeat("0", $this->difficulty)) { 50 | $block->nonce++; 51 | $block->hash = $block->calculateHash(); 52 | } 53 | 54 | echo "Block mined: ".$block->hash."\n"; 55 | } 56 | 57 | /** 58 | * Validates the blockchain's integrity. True if the blockchain is valid, false otherwise. 59 | */ 60 | public function isValid() 61 | { 62 | for ($i = 1; $i < count($this->chain); $i++) { 63 | $currentBlock = $this->chain[$i]; 64 | $previousBlock = $this->chain[$i-1]; 65 | 66 | if ($currentBlock->hash != $currentBlock->calculateHash()) { 67 | return false; 68 | } 69 | 70 | if ($currentBlock->previousHash != $previousBlock->hash) { 71 | return false; 72 | } 73 | } 74 | 75 | return true; 76 | } 77 | } 78 | --------------------------------------------------------------------------------