diff --git a/.gitignore b/.gitignore index 4f0c04b..623089d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea test.txt test.php + diff --git a/Blockchain/Backend/core/Blockchain.php b/Blockchain/Backend/core/Blockchain.php index 7e13c18..c810fc8 100644 --- a/Blockchain/Backend/core/Blockchain.php +++ b/Blockchain/Backend/core/Blockchain.php @@ -1,6 +1,7 @@ GenesisBlock(); } + public function writeOnDisk($block) { + $blockchainDB = new BlockchainDB(); + $blockchainDB->write($block); + } + + public function fetchLastBlock() { + $blockchainDB = new BlockchainDB(); + return $blockchainDB->lastBlock(); + } + private function GenesisBlock() { $BlockHeight = 0; $prevBlockHash = $GLOBALS['ZERO_HASH']; @@ -27,15 +38,14 @@ public function addBlock($BlockHeight, $prevBlockHash) { $blockheader = new BlockHeader($GLOBALS['VERSION'], $prevBlockHash, $merkleRoot, $timestamp, $bits); $blockheader->mine(); $block = new Block($BlockHeight, 1, (array)$blockheader, 1, $Transaction); - $this->chain[] = (array)$block; - print_r(json_encode($this->chain, JSON_PRETTY_PRINT)); + $this->writeOnDisk((array)$block); } public function main() { while (true) { - $lastBlock = array_reverse($this->chain); - $BlockHeight = $lastBlock[0]['Height'] + 1; - $prevBlockHash = $lastBlock[0]['BlockHeader']['blockHash']; + $lastBlock = $this->fetchLastBlock(); + $BlockHeight = $lastBlock['Height'] + 1; + $prevBlockHash = $lastBlock['BlockHeader']['blockHash']; $this->addBlock($BlockHeight, $prevBlockHash); } } diff --git a/Blockchain/Backend/core/database/BaseDB.php b/Blockchain/Backend/core/database/BaseDB.php new file mode 100644 index 0000000..c9b1e3d --- /dev/null +++ b/Blockchain/Backend/core/database/BaseDB.php @@ -0,0 +1,51 @@ +filepath = $this->basepath . DIRECTORY_SEPARATOR . $filename; + } + + public function read() { + if (!file_exists($this->filepath)) { + echo "File named {$this->filepath} does not exist." . PHP_EOL; + return false; + } + + $raw = file_get_contents($this->filepath); + + if (strlen($raw) > 0) { + $data = json_decode($raw, true); + } else { + $data = []; + } + + return $data; + } + + public function write($item) { + $data = $this->read(); + if ($data) { + $data[] = $item; + } else { + $data = [$item]; + } + + file_put_contents($this->filepath, json_encode($data, JSON_PRETTY_PRINT)); + } +} + +class BlockchainDB extends BaseDB { + public function __construct() { + parent::__construct('blockchain'); + } + + public function lastBlock() { + $data = $this->read(); + + if ($data) { + return end($data); + } + } +} \ No newline at end of file diff --git a/Blockchain/data/blockchain b/Blockchain/data/blockchain new file mode 100644 index 0000000..a2b05ae --- /dev/null +++ b/Blockchain/data/blockchain @@ -0,0 +1,107 @@ +[ + { + "Height": 0, + "Blocksize": 1, + "BlockHeader": { + "bits": "ffff001f", + "timestamp": 1692918870, + "merkleRoot": "01d3c8ffac385f49854b12bc69248645efddb80a0435dd09631643bdc605d1d1", + "prevBlockHash": "0000000000000000000000000000000000000000000000000000000000000000", + "version": 1, + "nonce": 57706, + "blockHash": "0000d0f7be9722a4d33a1147995f54e00dd09304c3de35b86abf3c73d493f269" + }, + "TxCount": 1, + "Txs": "Code Architect sent 0 Bitcoins to Indranil" + }, + { + "Height": 1, + "Blocksize": 1, + "BlockHeader": { + "bits": "ffff001f", + "timestamp": 1692918885, + "merkleRoot": "ede8645e1159205c35365fc6b765fbea7acab3d30d83f2094e87b1dc2829b69b", + "prevBlockHash": "0000d0f7be9722a4d33a1147995f54e00dd09304c3de35b86abf3c73d493f269", + "version": 1, + "nonce": 30400, + "blockHash": "0000145cbce04e57681a3492198f05ca5f68f730d2a49ced6f62a22e1e0fb89e" + }, + "TxCount": 1, + "Txs": "Code Architect sent 1 Bitcoins to Indranil" + }, + { + "Height": 2, + "Blocksize": 1, + "BlockHeader": { + "bits": "ffff001f", + "timestamp": 1692918893, + "merkleRoot": "ee3d44bff24ade0bd29fbf90bcfa1a8a2ef2c991a92bf92e8e8d7bf85cf75e30", + "prevBlockHash": "0000145cbce04e57681a3492198f05ca5f68f730d2a49ced6f62a22e1e0fb89e", + "version": 1, + "nonce": 57819, + "blockHash": "0000b8ef9e602a62196ef75a05dc898ac1e599b4cfe69fd23fcef020ffe70e82" + }, + "TxCount": 1, + "Txs": "Code Architect sent 2 Bitcoins to Indranil" + }, + { + "Height": 3, + "Blocksize": 1, + "BlockHeader": { + "bits": "ffff001f", + "timestamp": 1692918912, + "merkleRoot": "d0249e654794fa76a823925895732057dc5c51075131f4784a65d31f93754113", + "prevBlockHash": "0000b8ef9e602a62196ef75a05dc898ac1e599b4cfe69fd23fcef020ffe70e82", + "version": 1, + "nonce": 91549, + "blockHash": "000046cfcf21c1ef5ae5abdc09ee2f5d2a95dc4b19dea21e312b1ce3f8cc2718" + }, + "TxCount": 1, + "Txs": "Code Architect sent 3 Bitcoins to Indranil" + }, + { + "Height": 4, + "Blocksize": 1, + "BlockHeader": { + "bits": "ffff001f", + "timestamp": 1692918942, + "merkleRoot": "3bf90cb05ba2fb3325fec0def457cd7cc6126b204c8963d4939350b2add27c6d", + "prevBlockHash": "000046cfcf21c1ef5ae5abdc09ee2f5d2a95dc4b19dea21e312b1ce3f8cc2718", + "version": 1, + "nonce": 88482, + "blockHash": "00008541d660a151dc1d3d4ca81c5dd844c0c37b07e643322192eace6b5e50b7" + }, + "TxCount": 1, + "Txs": "Code Architect sent 4 Bitcoins to Indranil" + }, + { + "Height": 5, + "Blocksize": 1, + "BlockHeader": { + "bits": "ffff001f", + "timestamp": 1692918975, + "merkleRoot": "5957ff49fe734ffde6fb685fd313afff54a59671e98acab4f5c84fa8ded0b177", + "prevBlockHash": "00008541d660a151dc1d3d4ca81c5dd844c0c37b07e643322192eace6b5e50b7", + "version": 1, + "nonce": 8339, + "blockHash": "00005c7fd514f61cd3173a7cbd5fa54a0eb90b0ba28e9c442bc0a666fdb1dc71" + }, + "TxCount": 1, + "Txs": "Code Architect sent 5 Bitcoins to Indranil" + }, + { + "Height": 6, + "Blocksize": 1, + "BlockHeader": { + "bits": "ffff001f", + "timestamp": 1692918978, + "merkleRoot": "fc73585acb5dfb029ad461c0a2caa7259d33e3e9c613eec7ea9926a7fdceb92c", + "prevBlockHash": "00005c7fd514f61cd3173a7cbd5fa54a0eb90b0ba28e9c442bc0a666fdb1dc71", + "version": 1, + "nonce": 268283, + "blockHash": "0000210da7a197c7734b55f27751ecfdc7bb3f881bf49f230d5866625fd3dd54" + }, + "TxCount": 1, + "Txs": "Code Architect sent 6 Bitcoins to Indranil" + } +] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..576900e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Blockchain with php +A personal project \ No newline at end of file diff --git a/test.php b/test.php new file mode 100644 index 0000000..05a28a8 --- /dev/null +++ b/test.php @@ -0,0 +1,14 @@ +lastBlock(); +if ($lastBlock) { + print_r($lastBlock); +} else { + echo "Blockchain is empty.\n"; +} +echo "\n"; +print_r(__DIR__); +echo "\n"; +print_r(__FILE__); \ No newline at end of file