Skip to content

merging branch 1 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
test.txt
test.php

20 changes: 15 additions & 5 deletions Blockchain/Backend/core/Blockchain.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
require_once 'Blockchain/Backend/core/Block.php'; // Assuming the block.php file path
require_once 'Blockchain/Backend/core/BlockHeader.php'; // Assuming the blockheader.php file path
require_once 'Blockchain/Backend/core/database/BaseDB.php'; // Assuming the blockheader.php file path
require_once 'Blockchain/Backend/util/util.php'; // Assuming the util.php file path

$ZERO_HASH = str_repeat('0', 64);
Expand All @@ -13,6 +14,16 @@ public function __construct() {
$this->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'];
Expand All @@ -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);
}
}
Expand Down
51 changes: 51 additions & 0 deletions Blockchain/Backend/core/database/BaseDB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
class BaseDB {
private string $basepath = 'D:\\xampp\\htdocs\\sand_box\\get_job\\custom-php-blockchain\\Blockchain\\data\\';
private string $filepath;

public function __construct($filename) {
$this->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);
}
}
}
107 changes: 107 additions & 0 deletions Blockchain/data/blockchain
Original file line number Diff line number Diff line change
@@ -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"
}
]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Blockchain with php
A personal project
14 changes: 14 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
require_once 'Blockchain/Backend/core/database/BaseDB.php';

$blockchainDB = new BlockchainDB();
$lastBlock = $blockchainDB->lastBlock();
if ($lastBlock) {
print_r($lastBlock);
} else {
echo "Blockchain is empty.\n";
}
echo "\n";
print_r(__DIR__);
echo "\n";
print_r(__FILE__);