|
| 1 | +<?php |
| 2 | +require_once 'Blockchain/Backend/core/Block.php'; // Assuming the block.php file path |
| 3 | +require_once 'Blockchain/Backend/core/BlockHeader.php'; // Assuming the blockheader.php file path |
| 4 | +require_once 'Blockchain/Backend/util/util.php'; // Assuming the util.php file path |
| 5 | + |
| 6 | +$ZERO_HASH = str_repeat('0', 64); |
| 7 | +$VERSION = 1; |
| 8 | + |
| 9 | +class Blockchain { |
| 10 | + private $chain = []; |
| 11 | + |
| 12 | + public function __construct() { |
| 13 | + $this->GenesisBlock(); |
| 14 | + } |
| 15 | + |
| 16 | + private function GenesisBlock() { |
| 17 | + $BlockHeight = 0; |
| 18 | + $prevBlockHash = $GLOBALS['ZERO_HASH']; |
| 19 | + $this->addBlock($BlockHeight, $prevBlockHash); |
| 20 | + } |
| 21 | + |
| 22 | + public function addBlock($BlockHeight, $prevBlockHash) { |
| 23 | + $timestamp = time(); |
| 24 | + $Transaction = "Code Architect sent {$BlockHeight} Bitcoins to Indranil"; |
| 25 | + $merkleRoot = bin2hex(hash256($Transaction)); |
| 26 | + $bits = 'ffff001f'; |
| 27 | + $blockheader = new BlockHeader($GLOBALS['VERSION'], $prevBlockHash, $merkleRoot, $timestamp, $bits); |
| 28 | + $blockheader->mine(); |
| 29 | + $block = new Block($BlockHeight, 1, (array)$blockheader, 1, $Transaction); |
| 30 | + $this->chain[] = (array)$block; |
| 31 | + print_r(json_encode($this->chain, JSON_PRETTY_PRINT)); |
| 32 | + } |
| 33 | + |
| 34 | + public function main() { |
| 35 | + while (true) { |
| 36 | + $lastBlock = array_reverse($this->chain); |
| 37 | + $BlockHeight = $lastBlock[0]['Height'] + 1; |
| 38 | + $prevBlockHash = $lastBlock[0]['BlockHeader']['blockHash']; |
| 39 | + $this->addBlock($BlockHeight, $prevBlockHash); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +$blockchain = new Blockchain(); |
| 46 | +$blockchain->main(); |
| 47 | + |
0 commit comments