1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Security\Blockchain; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Security\Generators\Uuid; |
19
|
|
|
use O2System\Spl\Iterators\ArrayIterator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Chain |
23
|
|
|
* @package O2System\Security\Blockchain |
24
|
|
|
*/ |
25
|
|
|
class Chain extends ArrayIterator |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Chain::$difficulty |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $difficulty; |
33
|
|
|
|
34
|
|
|
// ------------------------------------------------------------------------ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Chain::__construct |
38
|
|
|
* |
39
|
|
|
* @param int $difficulty |
40
|
|
|
*/ |
41
|
|
|
public function __construct($difficulty = 4) |
42
|
|
|
{ |
43
|
|
|
parent::__construct(); |
44
|
|
|
|
45
|
|
|
$this->difficulty = $difficulty; |
46
|
|
|
|
47
|
|
|
// Creating Genesis Block |
48
|
|
|
$this->addBlock(new Block(0, strtotime("now"), 'GENESIS_BLOCK ' . Uuid::generate())); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// ------------------------------------------------------------------------ |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Chain::addBlock |
55
|
|
|
* |
56
|
|
|
* @param \O2System\Security\Blockchain\Block $block |
57
|
|
|
*/ |
58
|
|
|
public function addBlock(Block $block) |
59
|
|
|
{ |
60
|
|
|
if ($this->count()) { |
61
|
|
|
$lastBlock = $this->last(); |
62
|
|
|
|
63
|
|
|
if ($lastBlock instanceof Block) { |
64
|
|
|
$block->setPreviousHash($lastBlock->getHash()); |
65
|
|
|
|
66
|
|
|
$this->mine($block); |
67
|
|
|
$this->push($block); |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
$this->push($block); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// ------------------------------------------------------------------------ |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Chain::mine |
78
|
|
|
* |
79
|
|
|
* @param \O2System\Security\Blockchain\Block $block |
80
|
|
|
*/ |
81
|
|
|
public function mine(Block $block) |
82
|
|
|
{ |
83
|
|
|
while (substr($block->getHash(), 0, $this->difficulty) !== str_repeat('0', $this->difficulty)) { |
84
|
|
|
$block->regenerateHash(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// ------------------------------------------------------------------------ |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Chain::isValid |
92
|
|
|
* |
93
|
|
|
* Validates the blockchain's integrity. |
94
|
|
|
* |
95
|
|
|
* @return bool Returns FALSE if the blockchain integrity is invalid. |
96
|
|
|
*/ |
97
|
|
|
public function isValid() |
98
|
|
|
{ |
99
|
|
|
for ($i = 1; $i < $this->count(); $i++) { |
100
|
|
|
$currentBlock = $this->offsetGet($i); |
101
|
|
|
$previousBlock = $this->offsetGet($i - 1); |
102
|
|
|
|
103
|
|
|
if ($currentBlock instanceof Block) { |
104
|
|
|
if ($currentBlock->getHash() != $currentBlock->calculateHash()) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($previousBlock instanceof Block) { |
109
|
|
|
if ($currentBlock->getPreviousHash() != $previousBlock->getHash()) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
} |