-
Notifications
You must be signed in to change notification settings - Fork 25
/
dmnblockcomputeexpected.script.inc.php
executable file
·138 lines (124 loc) · 5.03 KB
/
dmnblockcomputeexpected.script.inc.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
<?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/>.
*/
if (!defined('DMN_SCRIPT') || !defined('DMN_CONFIG') || (DMN_SCRIPT !== true) || (DMN_CONFIG !== true)) {
die('Not executable');
}
define('DMN_VERSION','1.0.0');
xecho('dmnblockcomputeexpected v'.DMN_VERSION."\n");
if (($argc < 3) || ($argc > 5)) {
xecho("Usage: dmnblockcomputeexpected testnet blockfrom [blockto]\n");
die(1);
}
$testnet = intval($argv[1]);
if ($testnet < 0) {
$testnet = 0;
}
if ($testnet > 1) {
$testnet = 1;
}
$blockfrom = intval($argv[2]);
if ($argc == 4) {
$blockto = intval($argv[3]);
}
else {
$blockto = $blockfrom;
}
xecho("Computing expected for block $blockfrom to $blockto (".($blockto-$blockfrom+1)." blocks/testnet=$testnet):\n");
xecho("Connecting to MySQL...\n");
$mysqli = new mysqli(DMNCTLMYSQLHOST, DMNCTLMYSQLUSER, DMNCTLMYSQLPASS, DMNCTLMYSQLDATABASE);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
xecho("Connected: ".$mysqli->host_info." (".$mysqli->server_version.")\n");
$sql = <<<EOT
DROP TABLE IF EXISTS _cibh_nodecount;
CREATE TEMPORARY TABLE IF NOT EXISTS
_cibh_nodecount ENGINE=MEMORY AS (
SELECT
MP.BlockHeight BlockHeight,
BlockMNPayee,
COUNT(NodeID) CountNode,
MAX(BlockMNRatio) BlockMNRatio
FROM
(SELECT
BlockHeight,
MAX(Protocol) Protocol
FROM
cmd_info_blocks_history2
WHERE
BlockHeight >= %d
AND BlockHeight <= %d
AND BlockTestNet = %d
GROUP BY
BlockHeight
) MP
LEFT JOIN
cmd_info_blocks_history2 cibh
ON
(cibh.BlockHeight=MP.BlockHeight
AND cibh.Protocol=MP.Protocol)
GROUP BY
BlockHeight,
BlockMNPayee
);
DROP TABLE IF EXISTS _cibh_maxnodecount;
CREATE TEMPORARY TABLE IF NOT EXISTS _cibh_maxnodecount ENGINE=MEMORY AS (
SELECT
BlockHeight,
MAX(CountNode) MaxCountNode
FROM
_cibh_nodecount
GROUP BY
BlockHeight
);
SELECT NC.BlockHeight BlockHeight, BlockMNPayee, BlockMNRatio FROM _cibh_maxnodecount MNC, _cibh_nodecount NC WHERE MNC.BlockHeight = NC.BlockHeight AND MNC.MaxCountNode = NC.CountNode;
EOT;
$sql = sprintf($sql,$blockfrom,$blockto,$testnet);
xecho("Executing query....\n");
//echo $sql."\n";
$blockhist = array();
if ($mysqli->multi_query($sql) &&
$mysqli->more_results() && $mysqli->next_result() &&
$mysqli->more_results() && $mysqli->next_result() &&
$mysqli->more_results() && $mysqli->next_result() &&
$mysqli->more_results() && $mysqli->next_result() &&
($result = $mysqli->store_result())) {
$update = array();
while($row = $result->fetch_assoc()){
$update[] = sprintf("(%d,%d,'%s',%.9f)",
$testnet,
intval($row['BlockHeight']),
$row['BlockMNPayee'],
floatval($row['BlockMNRatio']));
}
xecho(" Done (".count($update)." computed)\n");
$sql = "INSERT INTO cmd_info_blocks (BlockTestnet, BlockId, BlockMNPayeeExpected, BlockMNValueRatioExpected) VALUES ".implode(",",$update)
." ON DUPLICATE KEY UPDATE BlockMNPayeeExpected = VALUES(BlockMNPayeeExpected), BlockMNValueRatioExpected = VALUES(BlockMNValueRatioExpected)";
// echo $sql."\n";
xecho("Updating expected values in block database:\n");
if ($result = $mysqli->query($sql)) {
xecho(" Done (".$mysqli->info.")\n");
}
else {
xecho(" Error (".$mysqli->errno.': '.$mysqli->error.")\n");
}
}
else {
xecho(" Failed (".$mysqli->errno.": ".$mysqli->error.")\n");
die(2);
}
?>