forked from 3xplcom/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.php
More file actions
195 lines (152 loc) · 4.76 KB
/
Debug.php
File metadata and controls
195 lines (152 loc) · 4.76 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php declare(strict_types = 1);
/* Copyright (c) 2023 Nikita Zhavoronkov, nikzh@nikzh.com
* Copyright (c) 2023 3xpl developers, 3@3xpl.com
* Distributed under the MIT software license, see the accompanying file LICENSE.md */
/* This utility should be used to debug the modules.
* Usage: `php Debug.php`, or
* `php Debug.php <module> <action> <...params>`
* You can either input arguments as you go, or invoke the script with them.
* Example: `php Debug.php bnb-bep-20 M` to monitor for new BEP-20 transfers */
require_once __DIR__ . '/Init.php';
require_once __DIR__ . '/Engine/DebugHelpers.php';
$input_argv = [];
// Greeting
echo cli_format_blue_reverse(' HELLO ') . N;
// Selecting the module
echo cli_format_bold('Please select a module (number or name): ') . N;
$available_modules = env('MODULES');
$i_to_module = $to_echo = [];
$i = 0;
foreach ($available_modules as $available_module)
{
$to_echo[] = $available_module . ' ' . cli_format_reverse('<' . $i. '>');
$i_to_module[$i++] = $available_module;
}
echo join(', ', $to_echo) . N;
if (isset($argv[1]))
{
$chosen_module = $argv[1];
echo ":> {$chosen_module}\n";
}
else
{
$chosen_module = (string)readline(':> ');
}
$input_argv[] = $chosen_module;
if (is_numeric($chosen_module)) // Allow both selecting by number or by full module name
{
if (!isset($i_to_module[$chosen_module]))
die(cli_format_error('Wrong choice for 1st param') . N);
echo 'Chosen module: ' . $i_to_module[$chosen_module] . N2;
$module = new (module_name_to_class($i_to_module[$chosen_module]))();
}
else
{
if (!in_array($chosen_module, $available_modules))
die(cli_format_error('Wrong choice for 1st param') . N);
echo 'Chosen module: ' . $chosen_module . N2;
$module = new (module_name_to_class($chosen_module))();
}
// Module actions
echo cli_format_bold('Please select an action: ') . N;
echo 'Get latest block number ' . cli_format_reverse('<L>') .
', Process block ' . cli_format_reverse('<B>') .
', Monitor blockchain ' . cli_format_reverse('<M>') . N;
if (isset($argv[2]))
{
$chosen_option = $argv[2];
echo ":> {$chosen_option}\n";
}
else
{
$chosen_option = (string)readline(':> ');
}
$input_argv[] = $chosen_option;
if (!in_array($chosen_option, ['L', 'B', 'M']))
die(cli_format_error('Wrong choice for 2nd param') . N);
echo N;
if ($chosen_option === 'L')
{
$best_block = $module->inquire_latest_block();
ddd($best_block);
}
elseif ($chosen_option === 'B')
{
echo cli_format_bold('Block number please...') . N;
if (isset($argv[3]))
{
$chosen_block_id = (int)$argv[3];
echo ":> {$chosen_block_id}\n";
}
else
{
$chosen_block_id = (int)readline(':> ');
}
$input_argv[] = $chosen_block_id;
if ($chosen_block_id !== MEMPOOL)
$module->process_block($chosen_block_id);
else
$module->process_mempool();
echo N;
echo cli_format_bold("What's next? <D> to show all events, <{:transaction}>|<{:address}> to filter, <T> for 10 first events, or <C> for currencies") . N;
if (isset($argv[4]))
{
$filter = $argv[4];
echo ":> {$argv[4]}\n";
}
else
{
$filter = readline(':> ');
}
$input_argv[] = $filter;
if ($filter === 'C')
{
ddd($module->get_return_currencies());
}
$events = $module->get_return_events();
if ($filter === 'D')
{
ddd($events);
}
elseif ($filter === 'T')
{
ddd(array_chunk($events, 10)[0]);
}
else
{
$output_events = [];
foreach ($events as $event)
{
if (str_contains($event['transaction'], $filter) || str_contains($event['address'], $filter))
$output_events[] = $event;
}
ddd($output_events);
}
}
elseif ($chosen_option === 'M')
{
$best_known_block = $module->inquire_latest_block();
echo cli_format_bold('Monitoring the blockchain for new blocks...');
while (true)
{
$current_block = $module->inquire_latest_block();
if ($current_block > $best_known_block)
{
for ($i = $best_known_block + 1; $i <= $current_block; $i++)
{
$t0 = microtime(true);
$module->process_block($i);
$event_count = count($module->get_return_events() ?? []);
$currency_count = count($module->get_return_currencies() ?? []);
$time = number_format(microtime(true) - $t0, 4);
echo "\nNew block #{$i} with {$event_count} events and {$currency_count} currencies in {$time} seconds";
}
$best_known_block = $current_block;
}
else
{
echo cli_format_dim('.');
}
usleep(250000);
}
}