This repository was 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 pathupdatefactoids
executable file
·116 lines (78 loc) · 4.32 KB
/
updatefactoids
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
#!/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';
const RECENT_CUTOFF = 12345;
echo "updating factoid cache...";
$factoids = array();
list($max) = pg_fetch_row(pg_query('SELECT MAX(number) FROM blocks;'));
$cutoff = $max - RECENT_CUTOFF;
/* -------- */
$req = pg_query('SELECT AVG(size) FROM blocks;');
list($blockAvg) = pg_fetch_row($req);
$factoids['average_block_size'] = $blockAvg;
/* -------- */
$req = pg_query("SELECT AVG(size) FROM blocks WHERE number >= $cutoff;");
list($blockAvg) = pg_fetch_row($req);
$factoids['average_recent_block_size'] = $blockAvg;
/* -------- */
$req = pg_query("SELECT COUNT(*) FROM transactions;");
list($txCount) = pg_fetch_row($req);
$factoids['transaction_count'] = $txCount;
/* -------- */
/* Since the genesis block has number 0, the blockchain has $max + 1 blocks */
$factoids['average_transaction_count'] = $txCount / ($max + 1);
/* -------- */
$req = pg_query("SELECT number, count FROM transaction_count JOIN blocks ON blocks.hash = transaction_count.hash ORDER BY count DESC LIMIT 1;");
list($number, $txCount) = pg_fetch_row($req);
$factoids['block_with_most_transactions'] = array($number, $txCount);
/* -------- */
$req = pg_query("SELECT number, size FROM blocks ORDER BY size DESC LIMIT 1;");
list($number, $size) = pg_fetch_row($req);
$factoids['biggest_block'] = array($number, $size);
/* -------- */
$req = pg_query("SELECT AVG(count) FROM blocks JOIN transaction_count ON transaction_count.hash = blocks.hash WHERE number >= $cutoff;");
list($avg) = pg_fetch_row($req);
$factoids['average_transaction_count_recent'] = $avg;
/* -------- */
$req = pg_query("SELECT AVG(generated) FROM blocks_generated WHERE number >= $cutoff;");
list($avg) = pg_fetch_row($req);
$factoids['average_generated_btc_recent'] = $avg;
/* -------- */
$req = pg_query("SELECT generated, number FROM blocks_generated ORDER BY generated DESC LIMIT 1;");
list($generated, $number) = pg_fetch_row($req);
$factoids['maximum_generated_btc'] = array($number, $generated);
/* -------- */
$req = pg_query("SELECT generated, number FROM blocks_generated WHERE number >= $cutoff ORDER BY generated DESC LIMIT 1;");
list($generated, $number) = pg_fetch_row($req);
$factoids['maximum_generated_btc_recent'] = array($number, $generated);
/* -------- */
$req = pg_query("SELECT number, transactions.transaction_id, transactions.size FROM transactions JOIN blocks_transactions ON blocks_transactions.transaction_id = transactions.transaction_id JOIN blocks ON blocks_transactions.block = blocks.hash ORDER BY transactions.size DESC LIMIT 1;");
list($number, $txId, $size) = pg_fetch_row($req);
$factoids['biggest_transaction_ever'] = array($number, $txId, $size);
/* -------- */
$req = pg_query("SELECT number, transactions.transaction_id, transactions.size FROM transactions JOIN blocks_transactions ON blocks_transactions.transaction_id = transactions.transaction_id JOIN blocks ON blocks_transactions.block = blocks.hash WHERE number >= $cutoff ORDER BY transactions.size DESC LIMIT 1;");
list($number, $txId, $size) = pg_fetch_row($req);
$factoids['biggest_transaction_recent'] = array($number, $txId, $size);
/* -------- */
$req = pg_query("SELECT transaction_id, SUM(amount) FROM tx_out GROUP BY transaction_id ORDER BY SUM(amount) DESC LIMIT 1;");
list($txId, $amount) = pg_fetch_row($req);
$factoids['largest_transaction'] = array($txId, $amount);
/* -------- */
$req = pg_query("SELECT COUNT(DISTINCT address) FROM tx_out;");
list($count) = pg_fetch_row($req);
$factoids['address_count'] = $count;
/* -------- */
$req = pg_query("SELECT address, COUNT(DISTINCT transaction_id) FROM tx_out GROUP BY address ORDER BY COUNT(DISTINCT transaction_id) DESC LIMIT 1;");
list($address, $count) = pg_fetch_row($req);
$factoids['most_popular_address'] = array($address, $count);
/* -------- */
cacheStore('factoids', $factoids);
invalidateCache('factoids'); /* Not a bug: invalidates the cached factoids page, not the factoid data */
invalidateCache('index');
echo "\n";