fix: proper hex-encoded transaction hash recognition in ZkSync batches status checker (#10255)

* proper hex-encoded transaction hash recognition

* mix format

---------

Co-authored-by: Viktor Baranov <baranov.viktor.27@gmail.com>
pull/10259/head
Alexander Kolotov 5 months ago committed by GitHub
parent 6a269b1e7f
commit 9448ccb7c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 70
      apps/indexer/lib/indexer/fetcher/zksync/utils/rpc.ex

@ -4,7 +4,8 @@ defmodule Indexer.Fetcher.ZkSync.Utils.Rpc do
"""
import EthereumJSONRPC, only: [json_rpc: 2, quantity_to_integer: 1]
import Indexer.Fetcher.ZkSync.Utils.Logging, only: [log_error: 1]
alias Indexer.Helper, as: IndexerHelper
@zero_hash "0000000000000000000000000000000000000000000000000000000000000000"
@zero_hash_binary <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>
@ -200,7 +201,8 @@ defmodule Indexer.Fetcher.ZkSync.Utils.Rpc do
error_message = &"Cannot call zks_getL1BatchDetails. Error: #{inspect(&1)}"
{:ok, resp} = repeated_call(&json_rpc/2, [req, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
{:ok, resp} =
IndexerHelper.repeated_call(&json_rpc/2, [req, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
transform_batch_details_to_map(resp)
end
@ -219,11 +221,7 @@ defmodule Indexer.Fetcher.ZkSync.Utils.Rpc do
@spec fetch_tx_by_hash(binary(), EthereumJSONRPC.json_rpc_named_arguments()) :: map()
def fetch_tx_by_hash(raw_hash, json_rpc_named_arguments)
when is_binary(raw_hash) and is_list(json_rpc_named_arguments) do
hash =
case raw_hash do
"0x" <> _ -> raw_hash
_ -> "0x" <> Base.encode16(raw_hash)
end
hash = prepare_tx_hash(raw_hash)
req =
EthereumJSONRPC.request(%{
@ -234,7 +232,8 @@ defmodule Indexer.Fetcher.ZkSync.Utils.Rpc do
error_message = &"Cannot call eth_getTransactionByHash for hash #{hash}. Error: #{inspect(&1)}"
{:ok, resp} = repeated_call(&json_rpc/2, [req, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
{:ok, resp} =
IndexerHelper.repeated_call(&json_rpc/2, [req, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
resp
end
@ -253,11 +252,7 @@ defmodule Indexer.Fetcher.ZkSync.Utils.Rpc do
@spec fetch_tx_receipt_by_hash(binary(), EthereumJSONRPC.json_rpc_named_arguments()) :: map()
def fetch_tx_receipt_by_hash(raw_hash, json_rpc_named_arguments)
when is_binary(raw_hash) and is_list(json_rpc_named_arguments) do
hash =
case raw_hash do
"0x" <> _ -> raw_hash
_ -> "0x" <> Base.encode16(raw_hash)
end
hash = prepare_tx_hash(raw_hash)
req =
EthereumJSONRPC.request(%{
@ -268,7 +263,8 @@ defmodule Indexer.Fetcher.ZkSync.Utils.Rpc do
error_message = &"Cannot call eth_getTransactionReceipt for hash #{hash}. Error: #{inspect(&1)}"
{:ok, resp} = repeated_call(&json_rpc/2, [req, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
{:ok, resp} =
IndexerHelper.repeated_call(&json_rpc/2, [req, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
resp
end
@ -289,7 +285,8 @@ defmodule Indexer.Fetcher.ZkSync.Utils.Rpc do
error_message = &"Cannot call zks_L1BatchNumber. Error: #{inspect(&1)}"
{:ok, resp} = repeated_call(&json_rpc/2, [req, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
{:ok, resp} =
IndexerHelper.repeated_call(&json_rpc/2, [req, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
quantity_to_integer(resp)
end
@ -318,7 +315,12 @@ defmodule Indexer.Fetcher.ZkSync.Utils.Rpc do
error_message = &"Cannot call eth_getBlockByNumber. Error: #{inspect(&1)}"
{:ok, responses} =
repeated_call(&json_rpc/2, [requests_list, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
IndexerHelper.repeated_call(
&json_rpc/2,
[requests_list, json_rpc_named_arguments],
error_message,
@rpc_resend_attempts
)
responses
end
@ -347,7 +349,12 @@ defmodule Indexer.Fetcher.ZkSync.Utils.Rpc do
error_message = &"Cannot call zks_getL1BatchDetails. Error: #{inspect(&1)}"
{:ok, responses} =
repeated_call(&json_rpc/2, [requests_list, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
IndexerHelper.repeated_call(
&json_rpc/2,
[requests_list, json_rpc_named_arguments],
error_message,
@rpc_resend_attempts
)
responses
end
@ -377,27 +384,22 @@ defmodule Indexer.Fetcher.ZkSync.Utils.Rpc do
error_message = &"Cannot call zks_getL1BatchBlockRange. Error: #{inspect(&1)}"
{:ok, responses} =
repeated_call(&json_rpc/2, [requests_list, json_rpc_named_arguments], error_message, @rpc_resend_attempts)
IndexerHelper.repeated_call(
&json_rpc/2,
[requests_list, json_rpc_named_arguments],
error_message,
@rpc_resend_attempts
)
responses
end
defp repeated_call(func, args, error_message, retries_left) do
case apply(func, args) do
{:ok, _} = res ->
res
{:error, message} = err ->
retries_left = retries_left - 1
if retries_left <= 0 do
log_error(error_message.(message))
err
else
log_error("#{error_message.(message)} Retrying...")
:timer.sleep(3000)
repeated_call(func, args, error_message, retries_left)
end
# Converts a transaction hash represented as binary to a hexadecimal string
@spec prepare_tx_hash(binary()) :: binary()
defp prepare_tx_hash(raw_hash) do
case raw_hash do
"0x" <> <<_::binary-size(64)>> -> raw_hash
_ -> "0x" <> Base.encode16(raw_hash, case: :lower)
end
end
end

Loading…
Cancel
Save