-
Notifications
You must be signed in to change notification settings - Fork 23
/
dashblockretrieve
executable file
·119 lines (104 loc) · 3.46 KB
/
dashblockretrieve
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
#!/usr/bin/php
<?php
/*
This file is part of Dash Ninja.
https://github.com/elbereth/dashninja-ctl
Dash Ninja is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Dash Ninja is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dash Ninja. If not, see <http://www.gnu.org/licenses/>.
*/
DEFINE('DMN_SCRIPT',true);
require_once('EasyDash-PHP/easydash.php');
require_once('DashConfig.class.php');
require_once('dmn.config.inc.php');
require_once('dashblocknotify.config.inc.php');
define('DASHBLOCKRETRIEVE','0.2.2-beta');
function xecho($line) {
echo date('Y-m-d H:i:s').' - '.$line;
}
xecho('DashNinja.pl Block Retrieve v'.DASHBLOCKRETRIEVE."\n");
if (($argc < 3) || ($argc > 5)) {
echo 'Usage: '.basename($argv[0]).' uname blockid [blockidto] [forcearchive]'."\n";
die(1);
}
$uname = $argv[1];
$blockidfrom = intval($argv[2]);
if ($argc >= 4) {
$blockidto = intval($argv[3]);
}
else {
$blockidto = $blockidfrom;
}
if ($blockidto < $blockidfrom) {
xecho("Parameter error: $blockidto must be after $blockidfrom\n");
die(2);
}
if (($argc > 4) && ($argv[4] == 'forcearchive')) {
$outdir = DMN_BLOCKPARSER_ARCHIVE;
}
else {
$outdir = "/dev/shm";
}
xecho("Block retrieval from $uname (from $blockidfrom to $blockidto), retrieving RPC configuration: ");
if (isset($unamelist) && is_array($unamelist) && array_key_exists($uname,$unamelist)) {
$config = new DashConfig($uname);
$rpc = new \elbereth\EasyDash($config->getconfig('rpcuser'),$config->getconfig('rpcpassword'),'localhost',$config->getconfig('rpcport'));
echo "OK\n";
}
else {
echo "Not found!\n";
die(2);
}
xecho("Retrieving blocks information:\n");
for ($x = $blockidfrom; $x <= $blockidto; $x++) {
xecho(" Block ID#$x: ");
$blockhash = $rpc->getblockhash($x);
if ($blockhash !== false) {
echo $blockhash;
$try = 0;
do {
echo ".";
$blockjson = $rpc->getblock($blockhash);
if (is_null($blockjson)) {
usleep(200000*$try+100000);
}
$try++;
} while (is_null($blockjson) && ($try <= 10));
if (($blockjson !== false) || (is_null($blockjson))) {
if (!is_dir($outdir."/$uname")) {
mkdir($outdir."/$uname",0777,true);
}
$tmpfilename = tempnam($outdir,"drkblockretrieve.$uname");
file_put_contents($tmpfilename,json_encode($blockjson));
$rename = $outdir."/$uname/block.".$blockjson['height'].".json";
echo "OK\n";
if (!is_dir($outdir."/$uname/tx")) {
mkdir($outdir."/$uname/tx",0777,true);
}
foreach($blockjson['tx'] as $id => $txhash) {
xecho(" Retrieving tx #$id ($txhash): ");
$txraw = $rpc->getrawtransaction($txhash);
if ($txraw !== false) {
$tx = $rpc->decoderawtransaction($txraw);
file_put_contents($outdir."/$uname/tx/transaction.$txhash.json",json_encode($tx));
echo "OK\n";
}
}
rename($tmpfilename,$rename);
}
else {
echo "Error during RPC call for block JSON\n";
}
}
else {
echo "Error during RPC call for block hash\n";
}
}
?>