-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
genesis.php
executable file
·67 lines (51 loc) · 1.64 KB
/
genesis.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
<?php declare(strict_types=1);
namespace Blockchain;
use Exception;
include 'bootstrap.php';
$block = new Block();
$miner = new Miner();
$pow = new Pow();
$nonce = 0;
echo PHP_EOL, Config::getProductName(), ' Genesis Miner ', PHP_EOL;
echo Config::getProductCopyright(), PHP_EOL, PHP_EOL;
$env = $_ENV['ENVIRONMENT'] ?? 'dev';
Console::log('Working on chain: ' . $env);
// use the newest key pair
$account = new Account();
$acct = $account->getNewestAccount();
if ($acct === null) {
// do we need to create a new key pair?
$id = $account->create();
$acct = $account->get($id);
Console::log('new key pair created');
}
$publicKey = $acct['public_key'];
$privateKey = $acct['private_key'];
$candidateBlock = $block->genesis($env, $publicKey, $privateKey);
$blockHeader = $block->generateBlockHeader($candidateBlock);
while (1) {
try {
$result = $miner->mineBlock($blockHeader, $candidateBlock['difficulty'], 1, $nonce, false);
if ($result['result'] === true) {
break;
}
$nonce = $result['nonce'];
} catch (Exception $e) {
echo $e->getMessage(), "\n";
exit(1);
}
}
$candidateBlock['hash'] = $result['hash'];
$candidateBlock['nonce'] = dechex($result['nonce']);
$reason = $block->validateFullBlock($candidateBlock);
print_r($candidateBlock);
print_r($reason);
echo "\nFound in " . number_format($result['hashes'], 0) . " Hashes\n";
echo "Nonce: {$result['nonce']}\n";
echo "Hash: {$result['hash']}\n\n";
$verified = $pow->verifyPow($result['hash'], $blockHeader, dechex($result['nonce']));
if ($verified) {
echo "--> Hash verified\n";
} else {
echo "--X Hash mismatch\n";
}