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 05a7518

Browse files
created base structure of the blockchain with directory structure
0 parents commit 05a7518

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
test.txt

‎Blockchain/Backend/core/Block.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
class Block {
3+
/**
4+
* Block is a storage container that stores transactions
5+
*/
6+
public function __construct($Height, $blockSize, $blockHeader, $TxCount, $Txs) {
7+
$this->Height = $Height;
8+
$this->Blocksize = $blockSize;
9+
$this->BlockHeader = $blockHeader;
10+
$this->TxCount = $TxCount;
11+
$this->Txs = $Txs;
12+
}
13+
}
14+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
require_once 'Blockchain/Backend/util/util.php';
3+
4+
class BlockHeader {
5+
public function __construct($version, $prevBlockHash, $merkleRoot, $timestamp, $bits) {
6+
$this->bits = $bits;
7+
$this->timestamp = $timestamp;
8+
$this->merkleRoot = $merkleRoot;
9+
$this->prevBlockHash = $prevBlockHash;
10+
$this->version = $version;
11+
$this->nonce = 0;
12+
$this->blockHash = '';
13+
}
14+
15+
16+
public function mine() {
17+
while (substr($this->blockHash, 0, 4) !== '0000') {
18+
$inputString = $this->version . $this->prevBlockHash . $this->merkleRoot . $this->timestamp . $this->bits . $this->nonce;
19+
$inputString = hash256($inputString);
20+
$this->blockHash = bin2hex($inputString); // convert to hexadecimal
21+
$this->nonce++;
22+
echo "Mining started {$this->nonce}\r";
23+
}
24+
echo PHP_EOL; // Add a newline after mining is complete
25+
}
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+

‎Blockchain/Backend/util/util.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
function hash256($s) {
3+
// Two rounds of SHA256
4+
return hash('sha256', hash('sha256', $s, true), true);
5+
}

0 commit comments

Comments
(0)

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