-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrnslist.php
More file actions
178 lines (151 loc) · 5.48 KB
/
trnslist.php
File metadata and controls
178 lines (151 loc) · 5.48 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
require_once __DIR__ . '/includes/cassandra.inc';
function get_transactions($session, $addr, $limit, $offset) {
$needed = $offset + $limit;
$page_size = TXS_CASSANDRA_QUERY_PAGE_SIZE;
$pending_cutoff = time() - TXS_PENDING_CUTOFF_AGE;
$iters = [
paged_rows($session->execute(
new Cassandra\SimpleStatement("SELECT * FROM testtransactions WHERE add1 = ? AND status = 0 ORDER BY time DESC"),
['arguments' => [$addr], 'page_size' => $page_size]
)),
paged_rows($session->execute(
new Cassandra\SimpleStatement("SELECT * FROM testtransactions WHERE add1 = ? AND status = 1 AND time>=". $pending_cutoff ." ORDER BY time DESC"),
['arguments' => [$addr], 'page_size' => $page_size]
)),
];
// Remove exhausted iterators
$iters = array_filter($iters, function ($it) {
return $it->valid();
});
$seen = [];
$seen_idx = [];
$txs = [];
$txs_count = 0;
$enough_but_remaining_seen = false;
// Merge all streams by time DESC, deduplicating
while ($iters) {
// Check if we have enough but still have pending transactions to close
if ($txs_count >= $needed) {
if (empty($seen_idx)) {
break;
}
$enough_but_remaining_seen = true;
}
// Find iterator with highest time (most recent), hash as tiebreaker
$best_key = null;
$best_rank = null;
foreach ($iters as $key => $iter) {
$row = $iter->current();
$rank = [$row['time']->value(), $row['hash']];
if ($best_rank === null || $rank > $best_rank) {
$best_key = $key;
$best_rank = $rank;
}
}
// Get row and advance iterator
$best_iter = $iters[$best_key];
$row = $best_iter->current();
$best_iter->next();
if (!$best_iter->valid()) {
unset($iters[$best_key]);
}
if ($enough_but_remaining_seen) {
// Allow to look for more transactions in the past to
// close possible pending transactions
$last_time = $row['time']->value();
$found = false;
foreach ($seen_idx as $h => $sidx) {
if ($txs[$sidx]['time']->value() - $last_time < TXS_PENDING_CLOSURE_PAST_LOOKUP_LIMIT) {
$found = true;
break;
}
unset($seen_idx[$h]); // too old
}
if (!$found) break;
$enough_but_remaining_seen = false;
}
// Deduplicate by hash
$hash = $row['hash'];
if (isset($seen[$hash])) {
// Status 0 is final.
if ($seen[$hash] == 0) {
continue;
}
// Replace previously stored row with the lower-status one
if (isset($seen_idx[$hash])) {
$txs[$seen_idx[$hash]] = $row;
// Once we keep a status 0 version, we no longer need an index tracked.
if ($row['status'] == 0) {
unset($seen_idx[$hash]);
}
}
if ($txs_count >= $needed)
continue;
$seen[$hash] = $row['status'];
continue;
}
if ($txs_count >= $needed)
continue;
$seen[$hash] = $row['status'];
// Only track index when status > 0; status 0 is final and won't be replaced.
if ($row['status'] > 0) {
$seen_idx[$hash] = $txs_count;
}
$txs[] = $row;
$txs_count++;
}
// Apply pagination
$txs = array_slice($txs, $offset);
// Format output
$output = [];
foreach ($txs as $row) {
if ($row['direction'] == 1) {
$row['addr_from'] = $row['add1'];
$row['addr_to'] = $row['add2'];
} else {
$row['addr_from'] = $row['add2'];
$row['addr_to'] = $row['add1'];
}
$row['time'] = $row['time']->value();
// for old transaction without receivedat
$row['receivedat'] = !is_null($row['receivedat'])
? $row['receivedat']->value()
: $row['time'];
$output[] = json_encode($row);
}
return $output;
}
// @codeCoverageIgnoreStart
// Main entry point - only runs when executed directly
if (realpath($_SERVER['SCRIPT_FILENAME']) === realpath(__FILE__)) {
/**
* Page size for Cassandra queries.
*/
define('TXS_CASSANDRA_QUERY_PAGE_SIZE', 50);
/**
* Maximum age in seconds for pending transactions to be included.
*/
define('TXS_PENDING_CUTOFF_AGE', 3600);
/**
* How far back in time (seconds) to look for confirmed versions of pending transactions.
*/
define('TXS_PENDING_CLOSURE_PAST_LOOKUP_LIMIT', 24 * 3600);
header('Access-Control-Allow-Origin: *');
// Validate and parse input
if (strlen($_GET['addr'] ?? '') != 42) {
echo "Bye!";
exit;
}
$addr = strtolower(preg_replace("/[^a-zA-Z0-9]+/", "", $_GET['addr']));
$limit = is_numeric($_GET['count'] ?? '') ? (int)$_GET['count'] : 5;
$offset = is_numeric($_GET['offset'] ?? '') ? (int)$_GET['offset'] : 0;
// Connect to Cassandra
$cluster = Cassandra::cluster('127.0.0.1')
->withCredentials("transactions_ro", "Public_transactions")
->build();
$session = $cluster->connect('comchain');
echo json_encode(get_transactions($session, $addr, $limit, $offset));
}
// @codeCoverageIgnoreEnd
?>