-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBlock.php
99 lines (83 loc) · 2.4 KB
/
Block.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
class Block {
public function __construct($index, $previousHash, $timestamp, $data, $hash,$difficulty,$target, $chainWork, $nonce) {
$this->blockIndex = $index;
$this->previousHash = $previousHash;
$this->timestamp = $timestamp;
$this->data = $data;
$this->hash = $hash;
$this->difficulty = $difficulty;
$this->target = $target;
$this->chainWork = $chainWork;
$this->nonce = $nonce;
}
static function calculateHash($index, $previousHash, $timestamp, $data,$target,$nonce) {
return hash("sha256", $index.$previousHash.$timestamp.Utils::jsonEncode($data).$target.$nonce);
}
public function calculateThisHash() {
return self::calculateHash(
$this->blockIndex,
$this->previousHash,
$this->timestamp,
$this->data,
$this->target,
$this->nonce
);
}
static function isBlockIndexInActiveChain($index) {
$r = DB::query($sql = "SELECT * FROM `blocks` WHERE blockIndex='".DB::esc($index)."'");
if (mysqli_num_rows($r) == 1) {
return mysqli_fetch_assoc($r);
} else {
//solve conflict
$chain = Chain::getLongestChain();
$targetBlockHash = $chain['lastBlockHash'];
$searchHashes = [];
while($row = mysqli_fetch_assoc($r)) {
if ( $row['hash'] == $targetBlockHash ) {
return $row;
}
$searchHashes = [$row['blockHash']];
while(@count($searchHashes) > 0) {
$r2 = DB::query("SELECT * FROM `blocks` WHERE previousHash IN ('".@Implode("','", $searchHashes)."')");
$searchHashes = [];
while($block = mysqli_fetch_assoc($r2)) {
if ( $block['hash'] == $targetBlockHash ) {
return $row;
}
$searchHashes[] = $block['hash'];
}
}
}
}
return false;
}
static function parse($arr) {
$datas = $arr['data'];
if (is_array($datas)) {
foreach($datas as $k=>$data) {
$datas[$k] = Transaction::parse($data);
}
} else {
$datas = Utils::jsonDecode($datas);
if (json_last_error() == JSON_ERROR_NONE) {
foreach($datas as $k=>$data) {
$datas[$k] = Transaction::parse($data);
}
} else {
$datas = $arr['data'];
}
}
return new Block(
$arr['blockIndex'],
$arr['previousHash'],
$arr['timestamp'],
$datas,
$arr['hash'],
$arr['difficulty'],
$arr['target'],
$arr['chainWork'],
$arr['nonce']
);
}
}