Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6643a19

Browse files
Issure resolved. Writing data into file properly
1 parent 05a7518 commit 6643a19

File tree

5 files changed

+189
-6
lines changed

5 files changed

+189
-6
lines changed

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
test.txt
2+
test.txt
3+
test.php

‎Blockchain/Backend/core/Blockchain.php‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
require_once 'Blockchain/Backend/core/Block.php'; // Assuming the block.php file path
33
require_once 'Blockchain/Backend/core/BlockHeader.php'; // Assuming the blockheader.php file path
4+
require_once 'Blockchain/Backend/core/database/BaseDB.php'; // Assuming the blockheader.php file path
45
require_once 'Blockchain/Backend/util/util.php'; // Assuming the util.php file path
56

67
$ZERO_HASH = str_repeat('0', 64);
@@ -13,6 +14,16 @@ public function __construct() {
1314
$this->GenesisBlock();
1415
}
1516

17+
public function writeOnDisk($block) {
18+
$blockchainDB = new BlockchainDB();
19+
$blockchainDB->write($block);
20+
}
21+
22+
public function fetchLastBlock() {
23+
$blockchainDB = new BlockchainDB();
24+
return $blockchainDB->lastBlock();
25+
}
26+
1627
private function GenesisBlock() {
1728
$BlockHeight = 0;
1829
$prevBlockHash = $GLOBALS['ZERO_HASH'];
@@ -27,15 +38,14 @@ public function addBlock($BlockHeight, $prevBlockHash) {
2738
$blockheader = new BlockHeader($GLOBALS['VERSION'], $prevBlockHash, $merkleRoot, $timestamp, $bits);
2839
$blockheader->mine();
2940
$block = new Block($BlockHeight, 1, (array)$blockheader, 1, $Transaction);
30-
$this->chain[] = (array)$block;
31-
print_r(json_encode($this->chain, JSON_PRETTY_PRINT));
41+
$this->writeOnDisk((array)$block);
3242
}
3343

3444
public function main() {
3545
while (true) {
36-
$lastBlock = array_reverse($this->chain);
37-
$BlockHeight = $lastBlock[0]['Height'] + 1;
38-
$prevBlockHash = $lastBlock[0]['BlockHeader']['blockHash'];
46+
$lastBlock = $this->fetchLastBlock();
47+
$BlockHeight = $lastBlock['Height'] + 1;
48+
$prevBlockHash = $lastBlock['BlockHeader']['blockHash'];
3949
$this->addBlock($BlockHeight, $prevBlockHash);
4050
}
4151
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
class BaseDB {
3+
private string $basepath = 'D:\\xampp\\htdocs\\sand_box\\get_job\\custom-php-blockchain\\Blockchain\\data\\';
4+
private string $filepath;
5+
6+
public function __construct($filename) {
7+
$this->filepath = $this->basepath . DIRECTORY_SEPARATOR . $filename;
8+
}
9+
10+
public function read() {
11+
if (!file_exists($this->filepath)) {
12+
echo "File named {$this->filepath} does not exist." . PHP_EOL;
13+
return false;
14+
}
15+
16+
$raw = file_get_contents($this->filepath);
17+
18+
if (strlen($raw) > 0) {
19+
$data = json_decode($raw, true);
20+
} else {
21+
$data = [];
22+
}
23+
24+
return $data;
25+
}
26+
27+
public function write($item) {
28+
$data = $this->read();
29+
if ($data) {
30+
$data[] = $item;
31+
} else {
32+
$data = [$item];
33+
}
34+
35+
file_put_contents($this->filepath, json_encode($data, JSON_PRETTY_PRINT));
36+
}
37+
}
38+
39+
class BlockchainDB extends BaseDB {
40+
public function __construct() {
41+
parent::__construct('blockchain');
42+
}
43+
44+
public function lastBlock() {
45+
$data = $this->read();
46+
47+
if ($data) {
48+
return end($data);
49+
}
50+
}
51+
}

‎Blockchain/data/blockchain‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
[
2+
{
3+
"Height": 0,
4+
"Blocksize": 1,
5+
"BlockHeader": {
6+
"bits": "ffff001f",
7+
"timestamp": 1692918870,
8+
"merkleRoot": "01d3c8ffac385f49854b12bc69248645efddb80a0435dd09631643bdc605d1d1",
9+
"prevBlockHash": "0000000000000000000000000000000000000000000000000000000000000000",
10+
"version": 1,
11+
"nonce": 57706,
12+
"blockHash": "0000d0f7be9722a4d33a1147995f54e00dd09304c3de35b86abf3c73d493f269"
13+
},
14+
"TxCount": 1,
15+
"Txs": "Code Architect sent 0 Bitcoins to Indranil"
16+
},
17+
{
18+
"Height": 1,
19+
"Blocksize": 1,
20+
"BlockHeader": {
21+
"bits": "ffff001f",
22+
"timestamp": 1692918885,
23+
"merkleRoot": "ede8645e1159205c35365fc6b765fbea7acab3d30d83f2094e87b1dc2829b69b",
24+
"prevBlockHash": "0000d0f7be9722a4d33a1147995f54e00dd09304c3de35b86abf3c73d493f269",
25+
"version": 1,
26+
"nonce": 30400,
27+
"blockHash": "0000145cbce04e57681a3492198f05ca5f68f730d2a49ced6f62a22e1e0fb89e"
28+
},
29+
"TxCount": 1,
30+
"Txs": "Code Architect sent 1 Bitcoins to Indranil"
31+
},
32+
{
33+
"Height": 2,
34+
"Blocksize": 1,
35+
"BlockHeader": {
36+
"bits": "ffff001f",
37+
"timestamp": 1692918893,
38+
"merkleRoot": "ee3d44bff24ade0bd29fbf90bcfa1a8a2ef2c991a92bf92e8e8d7bf85cf75e30",
39+
"prevBlockHash": "0000145cbce04e57681a3492198f05ca5f68f730d2a49ced6f62a22e1e0fb89e",
40+
"version": 1,
41+
"nonce": 57819,
42+
"blockHash": "0000b8ef9e602a62196ef75a05dc898ac1e599b4cfe69fd23fcef020ffe70e82"
43+
},
44+
"TxCount": 1,
45+
"Txs": "Code Architect sent 2 Bitcoins to Indranil"
46+
},
47+
{
48+
"Height": 3,
49+
"Blocksize": 1,
50+
"BlockHeader": {
51+
"bits": "ffff001f",
52+
"timestamp": 1692918912,
53+
"merkleRoot": "d0249e654794fa76a823925895732057dc5c51075131f4784a65d31f93754113",
54+
"prevBlockHash": "0000b8ef9e602a62196ef75a05dc898ac1e599b4cfe69fd23fcef020ffe70e82",
55+
"version": 1,
56+
"nonce": 91549,
57+
"blockHash": "000046cfcf21c1ef5ae5abdc09ee2f5d2a95dc4b19dea21e312b1ce3f8cc2718"
58+
},
59+
"TxCount": 1,
60+
"Txs": "Code Architect sent 3 Bitcoins to Indranil"
61+
},
62+
{
63+
"Height": 4,
64+
"Blocksize": 1,
65+
"BlockHeader": {
66+
"bits": "ffff001f",
67+
"timestamp": 1692918942,
68+
"merkleRoot": "3bf90cb05ba2fb3325fec0def457cd7cc6126b204c8963d4939350b2add27c6d",
69+
"prevBlockHash": "000046cfcf21c1ef5ae5abdc09ee2f5d2a95dc4b19dea21e312b1ce3f8cc2718",
70+
"version": 1,
71+
"nonce": 88482,
72+
"blockHash": "00008541d660a151dc1d3d4ca81c5dd844c0c37b07e643322192eace6b5e50b7"
73+
},
74+
"TxCount": 1,
75+
"Txs": "Code Architect sent 4 Bitcoins to Indranil"
76+
},
77+
{
78+
"Height": 5,
79+
"Blocksize": 1,
80+
"BlockHeader": {
81+
"bits": "ffff001f",
82+
"timestamp": 1692918975,
83+
"merkleRoot": "5957ff49fe734ffde6fb685fd313afff54a59671e98acab4f5c84fa8ded0b177",
84+
"prevBlockHash": "00008541d660a151dc1d3d4ca81c5dd844c0c37b07e643322192eace6b5e50b7",
85+
"version": 1,
86+
"nonce": 8339,
87+
"blockHash": "00005c7fd514f61cd3173a7cbd5fa54a0eb90b0ba28e9c442bc0a666fdb1dc71"
88+
},
89+
"TxCount": 1,
90+
"Txs": "Code Architect sent 5 Bitcoins to Indranil"
91+
},
92+
{
93+
"Height": 6,
94+
"Blocksize": 1,
95+
"BlockHeader": {
96+
"bits": "ffff001f",
97+
"timestamp": 1692918978,
98+
"merkleRoot": "fc73585acb5dfb029ad461c0a2caa7259d33e3e9c613eec7ea9926a7fdceb92c",
99+
"prevBlockHash": "00005c7fd514f61cd3173a7cbd5fa54a0eb90b0ba28e9c442bc0a666fdb1dc71",
100+
"version": 1,
101+
"nonce": 268283,
102+
"blockHash": "0000210da7a197c7734b55f27751ecfdc7bb3f881bf49f230d5866625fd3dd54"
103+
},
104+
"TxCount": 1,
105+
"Txs": "Code Architect sent 6 Bitcoins to Indranil"
106+
}
107+
]

‎test.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
require_once 'Blockchain/Backend/core/database/BaseDB.php';
3+
4+
$blockchainDB = new BlockchainDB();
5+
$lastBlock = $blockchainDB->lastBlock();
6+
if ($lastBlock) {
7+
print_r($lastBlock);
8+
} else {
9+
echo "Blockchain is empty.\n";
10+
}
11+
echo "\n";
12+
print_r(__DIR__);
13+
echo "\n";
14+
print_r(__FILE__);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /