Refactor Indexer.Fetcher.ZkevmTxnBatch

pull/7584/head
POA 2 years ago
parent 8c145ef1da
commit 6efe1e58c0
  1. 88
      apps/indexer/lib/indexer/fetcher/zkevm_txn_batch.ex

@ -17,6 +17,7 @@ defmodule Indexer.Fetcher.ZkevmTxnBatch do
@batch_range_size 20 @batch_range_size 20
@recheck_latest_batch_interval 60 @recheck_latest_batch_interval 60
@zero_hash "0000000000000000000000000000000000000000000000000000000000000000"
def child_spec(start_link_arguments) do def child_spec(start_link_arguments) do
spec = %{ spec = %{
@ -207,27 +208,18 @@ defmodule Indexer.Fetcher.ZkevmTxnBatch do
{sequence_hashes, verify_hashes} = {sequence_hashes, verify_hashes} =
responses responses
|> Enum.reduce({[], []}, fn res, {sequences, verifies} = _acc -> |> Enum.reduce({[], []}, fn res, {sequences, verifies} = _acc ->
send_sequences_tx_hash = send_sequences_tx_hash = get_tx_hash(res.result, "sendSequencesTxHash")
case Map.get(res.result, "sendSequencesTxHash") do verify_batch_tx_hash = get_tx_hash(res.result, "verifyBatchTxHash")
"0x" <> send_sequences_tx_hash -> send_sequences_tx_hash
nil -> "0000000000000000000000000000000000000000000000000000000000000000"
end
verify_batch_tx_hash =
case Map.get(res.result, "verifyBatchTxHash") do
"0x" <> verify_batch_tx_hash -> verify_batch_tx_hash
nil -> "0000000000000000000000000000000000000000000000000000000000000000"
end
sequences = sequences =
if send_sequences_tx_hash != "0000000000000000000000000000000000000000000000000000000000000000" do if send_sequences_tx_hash != @zero_hash do
[Base.decode16!(send_sequences_tx_hash, case: :mixed) | sequences] [Base.decode16!(send_sequences_tx_hash, case: :mixed) | sequences]
else else
sequences sequences
end end
verifies = verifies =
if verify_batch_tx_hash != "0000000000000000000000000000000000000000000000000000000000000000" do if verify_batch_tx_hash != @zero_hash do
[Base.decode16!(verify_batch_tx_hash, case: :mixed) | verifies] [Base.decode16!(verify_batch_tx_hash, case: :mixed) | verifies]
else else
verifies verifies
@ -263,49 +255,15 @@ defmodule Indexer.Fetcher.ZkevmTxnBatch do
acc_input_hash = Map.get(res.result, "accInputHash") acc_input_hash = Map.get(res.result, "accInputHash")
state_root = Map.get(res.result, "stateRoot") state_root = Map.get(res.result, "stateRoot")
send_sequences_tx_hash =
case Map.get(res.result, "sendSequencesTxHash") do
"0x" <> send_sequences_tx_hash -> send_sequences_tx_hash
nil -> "0000000000000000000000000000000000000000000000000000000000000000"
end
verify_batch_tx_hash =
case Map.get(res.result, "verifyBatchTxHash") do
"0x" <> verify_batch_tx_hash -> verify_batch_tx_hash
nil -> "0000000000000000000000000000000000000000000000000000000000000000"
end
{sequence_id, l1_txs, next_id, hash_to_id} = {sequence_id, l1_txs, next_id, hash_to_id} =
if send_sequences_tx_hash != "0000000000000000000000000000000000000000000000000000000000000000" do res.result
sequence_tx_hash = Base.decode16!(send_sequences_tx_hash, case: :mixed) |> get_tx_hash("sendSequencesTxHash")
|> handle_tx_hash(hash_to_id, next_id, l1_txs, false)
id = Map.get(hash_to_id, sequence_tx_hash)
if is_nil(id) do
{next_id, l1_txs ++ [%{id: next_id, hash: sequence_tx_hash, is_verify: false}], next_id + 1,
Map.put(hash_to_id, sequence_tx_hash, next_id)}
else
{id, l1_txs, next_id, hash_to_id}
end
else
{nil, l1_txs, next_id, hash_to_id}
end
{verify_id, l1_txs, next_id, hash_to_id} = {verify_id, l1_txs, next_id, hash_to_id} =
if verify_batch_tx_hash != "0000000000000000000000000000000000000000000000000000000000000000" do res.result
verify_tx_hash = Base.decode16!(verify_batch_tx_hash, case: :mixed) |> get_tx_hash("verifyBatchTxHash")
|> handle_tx_hash(hash_to_id, next_id, l1_txs, true)
id = Map.get(hash_to_id, verify_tx_hash)
if is_nil(id) do
{next_id, l1_txs ++ [%{id: next_id, hash: verify_tx_hash, is_verify: true}], next_id + 1,
Map.put(hash_to_id, verify_tx_hash, next_id)}
else
{id, l1_txs, next_id, hash_to_id}
end
else
{nil, l1_txs, next_id, hash_to_id}
end
batch = %{ batch = %{
number: number, number: number,
@ -362,6 +320,30 @@ defmodule Indexer.Fetcher.ZkevmTxnBatch do
{latest_batch_number, virtual_batch_number, verified_batch_number} {latest_batch_number, virtual_batch_number, verified_batch_number}
end end
defp get_tx_hash(result, type) do
case Map.get(result, type) do
"0x" <> tx_hash -> tx_hash
nil -> "0000000000000000000000000000000000000000000000000000000000000000"
end
end
defp handle_tx_hash(encoded_tx_hash, hash_to_id, next_id, l1_txs, is_verify) do
if encoded_tx_hash != @zero_hash do
tx_hash = Base.decode16!(encoded_tx_hash, case: :mixed)
id = Map.get(hash_to_id, tx_hash)
if is_nil(id) do
{next_id, l1_txs ++ [%{id: next_id, hash: tx_hash, is_verify: is_verify}], next_id + 1,
Map.put(hash_to_id, tx_hash, next_id)}
else
{id, l1_txs, next_id, hash_to_id}
end
else
{nil, l1_txs, next_id, hash_to_id}
end
end
defp repeated_call(func, args, error_message, retries_left) do defp repeated_call(func, args, error_message, retries_left) do
case apply(func, args) do case apply(func, args) do
{:ok, _} = res -> {:ok, _} = res ->

Loading…
Cancel
Save