-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmineBlockHeader.php
150 lines (139 loc) · 5.16 KB
/
mineBlockHeader.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
$json = json_decode(file_get_contents('blockheader.json'), true);
$blockHeader = MineBlockHeader::blockMakeHeader(
$json["result"]["version"],
$json["result"]["previousblockhash"],
$json["result"]["merkleroot"],
$json["result"]["curtime"] ?? $json["result"]["time"],
$json["result"]["bits"],
$json["result"]["nonce"],
);
echo $blockHeader . "\n";
$result = MineBlockHeader::mineBlock(
$blockHeader,
// $json["result"]["nonce"] ?? 0
0
);
echo "------------------------------MINDED BLOCK HEADER---------------------------------\n";
print_r($result);
class MineBlockHeader
{
static public function mineBlock($blockHeader, $debugnonceStart = false, $extranonceStart = 0, $timeout = null)
{
$bits = substr($blockHeader, strlen($blockHeader) - 16, 8);
$bits = self::hexToLittleEndian($bits);
$bitsDecimal = hexdec($bits);
$targetHash = bin2hex(self::blockBits2Target($bits));
$timeStart = time();
$hashRate = 0;
$hashRateCount = 0;
$extraNonce = $extranonceStart;
$blockHeader = hex2bin($blockHeader);
while ($extraNonce < 0xffffffff) {
$timeStamp = time();
$nonce = $debugnonceStart ? $debugnonceStart : 0;
while ($nonce <= 0xffffffff) {
$pnonce = bin2hex(pack("V", $nonce));
$blockHeader = substr($blockHeader, 0, 76) . pack("V", $nonce);
$blockHex = bin2hex($blockHeader);
$blockHash = self::blockComputeRawHash($blockHeader);
$blockHashHex = bin2hex($blockHash);
$currenthash = self::hashToGmp($blockHash);
$targHash = self::hashToGmp(hex2bin($targetHash));
if (gmp_cmp($currenthash, $targHash) <= 0) {
$bHash = bin2hex($blockHash);
return [
'hashRate' => $hashRate,
'hashRateCount' => $hashRateCount,
'nonce' => $nonce,
'extraNonce' => $extraNonce,
'bHash' => $bHash
];
}
if ($nonce > 0 && $nonce % 1048576 == 0) {
$hashRate = $hashRate + ((1048576 / (time() - $timeStamp)) - $hashRate) / ($hashRateCount + 1);
$hashRateCount += 1;
$timeStamp = time();
if ($timeout && ($timeStamp - $timeStart) > $timeout) {
return [
'hashRate' => $hashRate,
'hashRateCount' => $hashRateCount,
'nonce' => $nonce,
'extraNonce' => $extraNonce,
'bHash' => null
];
} else {
print_r([
'hashRate' => $hashRate,
'hashRateCount' => $hashRateCount,
'nonce' => $nonce,
'extraNonce' => $extraNonce,
'bHash' => null
]);
}
}
$nonce++;
}
$extraNonce += 1;
}
return [
'hashRate' => $hashRate,
'hashRateCount' => $hashRateCount,
'nonce' => $nonce,
'extraNonce' => $extraNonce,
'bHash' => null
];
}
public static function blockMakeHeader($version, $previousblockhash, $merkleroot, $time, $bits, $nonce)
{
$version = self::littleEndian($version);
$prevBlockHash = self::SwapOrder($previousblockhash);
$rootHash = self::SwapOrder($merkleroot);
$time = self::littleEndian($time);
$bits = self::littleEndian(hexdec($bits));
$nonce = self::littleEndian($nonce);
//concat it all
$header_hex = $version . $prevBlockHash . $rootHash . $time . $bits . $nonce;
return $header_hex;
}
static private function SwapOrder($in)
{
$Split = str_split(strrev($in));
$x = '';
for ($i = 0; $i < count($Split); $i += 2) {
$x .= $Split[$i + 1] . $Split[$i];
}
return $x;
}
//makes the littleEndian
static private function littleEndian($value)
{
return implode(unpack('H*', pack("V*", $value)));
}
static private function blockComputeRawHash($header)
{
$hash1 = hash('sha256', $header, true);
$hash2 = strrev(hash('sha256', $hash1, true));
return $hash2;
}
static private function hexToLittleEndian($hex)
{
$bytes = str_split($hex, 2);
$bytes = array_reverse($bytes);
return implode($bytes);
}
static private function hashToGmp($hash)
{
$gmp = gmp_init('0x' . bin2hex($hash), 16);
return $gmp;
}
static private function blockBits2Target($bits)
{
$bits = hex2bin($bits);
$shift = ord($bits[0]) - 3;
$value = substr($bits, 1);
$target = $value . str_repeat("\x00", $shift);
$target = str_repeat("\x00", 32 - strlen($target)) . $target;
return $target;
}
}