-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
internal_transaction.ex
360 lines (298 loc) · 11.6 KB
/
internal_transaction.ex
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
defmodule Indexer.Fetcher.InternalTransaction do
@moduledoc """
Fetches and indexes `t:Explorer.Chain.InternalTransaction.t/0`.
See `async_fetch/1` for details on configuring limits.
"""
use Indexer.Fetcher, restart: :permanent
use Spandex.Decorators
require Logger
import Indexer.Block.Fetcher, only: [async_import_coin_balances: 2]
alias Explorer.Chain
alias Explorer.Chain.Block
alias Explorer.Chain.Cache.{Accounts, Blocks}
alias Explorer.Chain.Import.Runner.Blocks, as: BlocksRunner
alias Indexer.{BufferedTask, Tracer}
alias Indexer.Fetcher.InternalTransaction.Supervisor, as: InternalTransactionSupervisor
alias Indexer.Transform.Addresses
@behaviour BufferedTask
@default_max_batch_size 10
@default_max_concurrency 4
@doc """
Asynchronously fetches internal transactions.
## Limiting Upstream Load
Internal transactions are an expensive upstream operation. The number of
results to fetch is configured by `@max_batch_size` and represents the number
of transaction hashes to request internal transactions in a single JSONRPC
request. Defaults to `#{@default_max_batch_size}`.
The `@max_concurrency` attribute configures the number of concurrent requests
of `@max_batch_size` to allow against the JSONRPC. Defaults to `#{@default_max_concurrency}`.
*Note*: The internal transactions for individual transactions cannot be paginated,
so the total number of internal transactions that could be produced is unknown.
"""
@spec async_fetch([Block.block_number()]) :: :ok
def async_fetch(block_numbers, timeout \\ 5000) when is_list(block_numbers) do
if InternalTransactionSupervisor.disabled?() do
:ok
else
BufferedTask.buffer(__MODULE__, block_numbers, timeout)
end
end
@doc false
def child_spec([init_options, gen_server_options]) do
{state, mergeable_init_options} = Keyword.pop(init_options, :json_rpc_named_arguments)
unless state do
raise ArgumentError,
":json_rpc_named_arguments must be provided to `#{__MODULE__}.child_spec " <>
"to allow for json_rpc calls when running."
end
merged_init_opts =
defaults()
|> Keyword.merge(mergeable_init_options)
|> Keyword.put(:state, state)
Supervisor.child_spec({BufferedTask, [{__MODULE__, merged_init_opts}, gen_server_options]}, id: __MODULE__)
end
@impl BufferedTask
def init(initial, reducer, _json_rpc_named_arguments) do
{:ok, final} =
Chain.stream_blocks_with_unfetched_internal_transactions(
initial,
fn block_number, acc ->
reducer.(block_number, acc)
end,
true
)
final
end
defp params(%{block_number: block_number, hash: hash, index: index}) when is_integer(block_number) do
%{block_number: block_number, hash_data: to_string(hash), transaction_index: index}
end
@impl BufferedTask
@decorate trace(
name: "fetch",
resource: "Indexer.Fetcher.InternalTransaction.run/2",
service: :indexer,
tracer: Tracer
)
def run(block_numbers, json_rpc_named_arguments) do
unique_numbers =
block_numbers
|> Enum.uniq()
|> Chain.filter_consensus_block_numbers()
filtered_unique_numbers =
unique_numbers
|> EthereumJSONRPC.block_numbers_in_range()
|> drop_genesis(json_rpc_named_arguments)
filtered_unique_numbers_count = Enum.count(filtered_unique_numbers)
Logger.metadata(count: filtered_unique_numbers_count)
Logger.debug("fetching internal transactions for blocks")
json_rpc_named_arguments
|> Keyword.fetch!(:variant)
|> case do
variant when variant in [EthereumJSONRPC.Nethermind, EthereumJSONRPC.Erigon, EthereumJSONRPC.Besu] ->
EthereumJSONRPC.fetch_block_internal_transactions(filtered_unique_numbers, json_rpc_named_arguments)
_ ->
try do
fetch_block_internal_transactions_by_transactions(filtered_unique_numbers, json_rpc_named_arguments)
rescue
error ->
{:error, error, __STACKTRACE__}
end
end
|> case do
{:ok, internal_transactions_params} ->
safe_import_internal_transaction(internal_transactions_params, filtered_unique_numbers)
{:error, reason} ->
Logger.error(
fn ->
["failed to fetch internal transactions for blocks: ", Exception.format(:error, reason)]
end,
error_count: filtered_unique_numbers_count
)
handle_not_found_transaction(reason)
# re-queue the de-duped entries
{:retry, filtered_unique_numbers}
{:error, reason, stacktrace} ->
Logger.error(
fn ->
["failed to fetch internal transactions for blocks: ", Exception.format(:error, reason, stacktrace)]
end,
error_count: filtered_unique_numbers_count
)
handle_not_found_transaction(reason)
# re-queue the de-duped entries
{:retry, filtered_unique_numbers}
:ignore ->
:ok
end
end
defp drop_genesis(block_numbers, json_rpc_named_arguments) do
first_block = EthereumJSONRPC.first_block_to_fetch(:trace_first_block)
if first_block in block_numbers do
case EthereumJSONRPC.fetch_blocks_by_numbers([first_block], json_rpc_named_arguments) do
{:ok, %{transactions_params: [_ | _]}} -> block_numbers
_ -> block_numbers -- [first_block]
end
else
block_numbers
end
end
def import_first_trace(internal_transactions_params) do
imports =
Chain.import(%{
internal_transactions: %{params: internal_transactions_params, with: :blockless_changeset},
timeout: :infinity
})
case imports do
{:error, step, reason, _changes_so_far} ->
Logger.error(
fn ->
[
"failed to import first trace for tx: ",
inspect(reason)
]
end,
step: step
)
end
end
defp fetch_block_internal_transactions_by_transactions(unique_numbers, json_rpc_named_arguments) do
Enum.reduce(unique_numbers, {:ok, []}, fn
block_number, {:ok, acc_list} ->
block_number
|> Chain.get_transactions_of_block_number()
|> Enum.map(¶ms(&1))
|> case do
[] ->
{:ok, []}
transactions ->
try do
EthereumJSONRPC.fetch_internal_transactions(transactions, json_rpc_named_arguments)
catch
:exit, error ->
{:error, error, __STACKTRACE__}
end
end
|> case do
{:ok, internal_transactions} -> {:ok, internal_transactions ++ acc_list}
error_or_ignore -> error_or_ignore
end
_, error_or_ignore ->
error_or_ignore
end)
end
defp safe_import_internal_transaction(internal_transactions_params, block_numbers) do
import_internal_transaction(internal_transactions_params, block_numbers)
rescue
Postgrex.Error ->
handle_foreign_key_violation(internal_transactions_params, block_numbers)
{:retry, block_numbers}
end
defp import_internal_transaction(internal_transactions_params, unique_numbers) do
internal_transactions_params_without_failed_creations = remove_failed_creations(internal_transactions_params)
addresses_params =
Addresses.extract_addresses(%{
internal_transactions: internal_transactions_params_without_failed_creations
})
address_hash_to_block_number =
Enum.into(addresses_params, %{}, fn %{fetched_coin_balance_block_number: block_number, hash: hash} ->
{String.downcase(hash), block_number}
end)
empty_block_numbers =
unique_numbers
|> MapSet.new()
|> MapSet.difference(MapSet.new(internal_transactions_params_without_failed_creations, & &1.block_number))
|> Enum.map(&%{block_number: &1})
internal_transactions_and_empty_block_numbers =
internal_transactions_params_without_failed_creations ++ empty_block_numbers
imports =
Chain.import(%{
addresses: %{params: addresses_params},
internal_transactions: %{params: internal_transactions_and_empty_block_numbers, with: :blockless_changeset},
timeout: :infinity
})
case imports do
{:ok, imported} ->
Accounts.drop(imported[:addresses])
Blocks.drop_nonconsensus(imported[:remove_consensus_of_missing_transactions_blocks])
async_import_coin_balances(imported, %{
address_hash_to_fetched_balance_block_number: address_hash_to_block_number
})
{:error, step, reason, _changes_so_far} ->
Logger.error(
fn ->
[
"failed to import internal transactions for blocks: ",
inspect(reason)
]
end,
step: step,
error_count: Enum.count(unique_numbers)
)
# re-queue the de-duped entries
{:retry, unique_numbers}
end
end
defp remove_failed_creations(internal_transactions_params) do
internal_transactions_params
|> Enum.map(fn internal_transaction_param ->
transaction_index = internal_transaction_param[:transaction_index]
block_number = internal_transaction_param[:block_number]
failed_parent =
internal_transactions_params
|> Enum.filter(fn internal_transactions_param ->
internal_transactions_param[:block_number] == block_number &&
internal_transactions_param[:transaction_index] == transaction_index &&
internal_transactions_param[:trace_address] == [] && !is_nil(internal_transactions_param[:error])
end)
|> Enum.at(0)
if failed_parent do
internal_transaction_param
|> Map.delete(:created_contract_address_hash)
|> Map.delete(:created_contract_code)
|> Map.delete(:gas_used)
|> Map.delete(:output)
|> Map.put(:error, failed_parent[:error])
else
internal_transaction_param
end
end)
end
defp handle_foreign_key_violation(internal_transactions_params, block_numbers) do
BlocksRunner.invalidate_consensus_blocks(block_numbers)
transaction_hashes =
internal_transactions_params
|> Enum.map(&to_string(&1.transaction_hash))
|> Enum.uniq()
Logger.error(fn ->
[
"foreign_key_violation on internal transactions import, foreign transactions hashes: ",
Enum.join(transaction_hashes, ", ")
]
end)
end
defp handle_not_found_transaction(errors) when is_list(errors) do
Enum.each(errors, &handle_not_found_transaction/1)
end
defp handle_not_found_transaction(error) do
case error do
%{data: data, message: "historical backend error" <> _} -> invalidate_block_from_error(data)
%{data: data, message: "genesis is not traceable"} -> invalidate_block_from_error(data)
%{data: data, message: "transaction not found"} -> invalidate_block_from_error(data)
_ -> :ok
end
end
defp invalidate_block_from_error(%{"blockNumber" => block_number}),
do: BlocksRunner.invalidate_consensus_blocks([block_number])
defp invalidate_block_from_error(%{block_number: block_number}),
do: BlocksRunner.invalidate_consensus_blocks([block_number])
defp invalidate_block_from_error(_error_data), do: :ok
defp defaults do
[
flush_interval: :timer.seconds(3),
max_concurrency: Application.get_env(:indexer, __MODULE__)[:concurrency] || @default_max_concurrency,
max_batch_size: Application.get_env(:indexer, __MODULE__)[:batch_size] || @default_max_batch_size,
task_supervisor: Indexer.Fetcher.InternalTransaction.TaskSupervisor,
metadata: [fetcher: :internal_transaction]
]
end
end