This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdatepools
executable file
·135 lines (114 loc) · 3.42 KB
/
updatepools
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
#!/usr/bin/env php
<?php
/* Author : Romain "Artefact2" Dalmaso <[email protected]>
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */
require __DIR__.'/lib/inc.main.php';
@unlink('cookies.txt');
$c = count($foundBy);
$cL = strlen($c);
$i = 0;
foreach($poolsTrust as $pool) {
++$i;
$fI = str_pad($i, $cL, '0', STR_PAD_LEFT);
echo "\r$fI/$c fetched...";
list($type, $in) = call_user_func($foundBy[$pool]);
if(!is_array($in) || count($in) == 0) continue;
if($type == BLOCK_NUMBERS) {
$minNumber = min($in);
$in = implode(',', $in);
$in = "number IN ($in)";
} else if($type == BLOCK_HASHES) {
$in = implode(',', array_map(function($h) { return "B'".hex2bits($h)."'"; }, $in));
$minNumber = pg_fetch_row(pg_query("SELECT MIN(number) FROM blocks WHERE hash IN ($in)"));
$minNumber = $minNumber[0];
$in = "hash IN ($in)";
} else if($type == BLOCK_GENTXID) {
foreach($in as &$tx) {
$tx = 'B\''.hex2bits($tx).'\'';
}
$blocks = pg_query('
SELECT DISTINCT block
FROM blocks_transactions
WHERE transaction_id IN ('.implode(',', $in).')
');
$hashs = array();
while($r = pg_fetch_row($blocks)) {
$hashs[] = "B'".$r[0]."'";
}
$in = implode(',', $hashs);
$in = "hash IN ($in)";
$minNumber = pg_fetch_row(pg_query("SELECT MIN(number) FROM blocks WHERE $in"));
$minNumber = $minNumber[0];
} else {
// WTF ?
continue;
}
pg_query("
BEGIN;
UPDATE blocks SET found_by = null WHERE number >= $minNumber AND found_by = '$pool';
UPDATE blocks SET found_by = '$pool' WHERE $in;
COMMIT;
");
}
echo "\n";
$latestNum = pg_fetch_row(pg_query("SELECT MAX(number) FROM blocks;"));
$latestNum = $latestNum[0];
$identified = cacheFetch('blocks_identified', $success);
if(!$success) $identified = array();
$threshold = $latestNum - $GLOBALS['conf']['maximum_backlog'];
$mostRecentQ = pg_query("
SELECT found_by AS pool, MAX(number) AS most_recent
FROM blocks
WHERE number >= $threshold
GROUP BY found_by
");
$mostRecent = array();
while($r = pg_fetch_row($mostRecentQ)) {
$mostRecent[$r[0]] = $r[1];
}
$toIdentify = pg_query("
SELECT hash, number, found_by
FROM blocks
WHERE number >= $threshold
AND found_by IS NOT NULL
ORDER BY number ASC
");
$updatedPools = array();
$c = $latestNum;
$cL = strlen($latestNum);
while($r = pg_fetch_row($toIdentify)) {
$fI = str_pad($r[1], $cL, '0', STR_PAD_LEFT);
echo "\r$fI/$c identified...";
$hash = bits2hex($r[0]);
$pool = $r[2];
if(isset($identifyPayouts[$pool])) {
$callback = $identifyPayouts[$pool];
if($callback === null || $callback === false) continue;
} else {
$callback = $fallbackIdentify;
}
if(isset($identified[$hash]) && $identified[$hash] === $pool) continue;
pg_query("
UPDATE tx_out SET is_payout = false
FROM blocks_transactions
WHERE blocks_transactions.transaction_id = tx_out.transaction_id
AND block = B'".$r[0]."'
");
call_user_func($callback, $hash);
$identified[$hash] = $pool;
invalidateCache('block', $hash);
$updatedPools[$pool] = true;
}
if(isset($hash)) {
cacheStore('blocks_identified', $identified);
invalidateCache('index');
invalidateCache('more');
foreach($updatedPools as $pool => $nevermind) {
invalidateCache('pool', $pool.'_page*', true);
}
echo "\n";
}