Store l2 transactions count in the batch table

pull/7584/head
POA 1 year ago
parent 06ce6d7088
commit 03566d53b5
  1. 24
      apps/block_scout_web/lib/block_scout_web/views/api/v2/zkevm_view.ex
  2. 4
      apps/explorer/lib/explorer/chain/import/runner/zkevm_txn_batches.ex
  3. 4
      apps/explorer/lib/explorer/chain/zkevm_txn_batch.ex
  4. 1
      apps/explorer/priv/repo/migrations/20230420110800_create_zkevm_tables.exs
  5. 21
      apps/indexer/lib/indexer/fetcher/zkevm_txn_batch.ex

@ -1,11 +1,6 @@
defmodule BlockScoutWeb.API.V2.ZkevmView do
use BlockScoutWeb, :view
import Ecto.Query, only: [from: 2]
alias Explorer.Chain.ZkevmBatchTxn
alias Explorer.Repo
def render("zkevm_batch.json", %{batch: batch}) do
sequence_tx_hash =
if not is_nil(batch.sequence_transaction) do
@ -64,19 +59,7 @@ defmodule BlockScoutWeb.API.V2.ZkevmView do
end
defp render_zkevm_batches(batches) do
batches
|> Enum.map(fn batch ->
Task.async(fn ->
tx_count =
Repo.replica().aggregate(
from(
t in ZkevmBatchTxn,
where: t.batch_number == ^batch.number
),
:count,
timeout: :infinity
)
Enum.map(batches, fn batch ->
sequence_tx_hash =
if not is_nil(batch.sequence_transaction) do
batch.sequence_transaction.hash
@ -85,12 +68,9 @@ defmodule BlockScoutWeb.API.V2.ZkevmView do
%{
"number" => batch.number,
"timestamp" => batch.timestamp,
"tx_count" => tx_count,
"tx_count" => batch.l2_transactions_count,
"sequence_tx_hash" => sequence_tx_hash
}
end)
end)
|> Task.yield_many(:infinity)
|> Enum.map(fn {_task, {:ok, item}} -> item end)
end
end

@ -85,6 +85,7 @@ defmodule Explorer.Chain.Import.Runner.ZkevmTxnBatches do
set: [
# don't update `number` as it is a primary key and used for the conflict target
timestamp: fragment("EXCLUDED.timestamp"),
l2_transactions_count: fragment("EXCLUDED.l2_transactions_count"),
global_exit_root: fragment("EXCLUDED.global_exit_root"),
acc_input_hash: fragment("EXCLUDED.acc_input_hash"),
state_root: fragment("EXCLUDED.state_root"),
@ -96,8 +97,9 @@ defmodule Explorer.Chain.Import.Runner.ZkevmTxnBatches do
],
where:
fragment(
"(EXCLUDED.timestamp, EXCLUDED.global_exit_root, EXCLUDED.acc_input_hash, EXCLUDED.state_root, EXCLUDED.sequence_id, EXCLUDED.verify_id) IS DISTINCT FROM (?, ?, ?, ?, ?, ?)",
"(EXCLUDED.timestamp, EXCLUDED.l2_transactions_count, EXCLUDED.global_exit_root, EXCLUDED.acc_input_hash, EXCLUDED.state_root, EXCLUDED.sequence_id, EXCLUDED.verify_id) IS DISTINCT FROM (?, ?, ?, ?, ?, ?, ?)",
tb.timestamp,
tb.l2_transactions_count,
tb.global_exit_root,
tb.acc_input_hash,
tb.state_root,

@ -7,11 +7,12 @@ defmodule Explorer.Chain.ZkevmTxnBatch do
@optional_attrs ~w(sequence_id verify_id)a
@required_attrs ~w(number timestamp global_exit_root acc_input_hash state_root)a
@required_attrs ~w(number timestamp l2_transactions_count global_exit_root acc_input_hash state_root)a
@type t :: %__MODULE__{
number: non_neg_integer(),
timestamp: DateTime.t(),
l2_transactions_count: non_neg_integer(),
global_exit_root: Hash.t(),
acc_input_hash: Hash.t(),
state_root: Hash.t(),
@ -25,6 +26,7 @@ defmodule Explorer.Chain.ZkevmTxnBatch do
@primary_key {:number, :integer, autogenerate: false}
schema "zkevm_transaction_batches" do
field(:timestamp, :utc_datetime_usec)
field(:l2_transactions_count, :integer)
field(:global_exit_root, Hash.Full)
field(:acc_input_hash, Hash.Full)
field(:state_root, Hash.Full)

@ -14,6 +14,7 @@ defmodule Explorer.Repo.Migrations.CreateZkevmTables do
create table(:zkevm_transaction_batches, primary_key: false) do
add(:number, :integer, null: false, primary_key: true)
add(:timestamp, :"timestamp without time zone", null: false)
add(:l2_transactions_count, :integer, null: false)
add(:global_exit_root, :bytea, null: false)
add(:acc_input_hash, :bytea, null: false)
add(:state_root, :bytea, null: false)

@ -262,16 +262,6 @@ defmodule Indexer.Fetcher.ZkevmTxnBatch do
|> get_tx_hash("verifyBatchTxHash")
|> handle_tx_hash(hash_to_id, next_id, l1_txs, true)
batch = %{
number: number,
timestamp: timestamp,
global_exit_root: global_exit_root,
acc_input_hash: acc_input_hash,
state_root: state_root,
sequence_id: sequence_id,
verify_id: verify_id
}
l2_txs_append =
l2_transaction_hashes
|> Kernel.||([])
@ -282,6 +272,17 @@ defmodule Indexer.Fetcher.ZkevmTxnBatch do
}
end)
batch = %{
number: number,
timestamp: timestamp,
l2_transactions_count: Enum.count(l2_txs_append),
global_exit_root: global_exit_root,
acc_input_hash: acc_input_hash,
state_root: state_root,
sequence_id: sequence_id,
verify_id: verify_id
}
{[batch | batches], l2_txs ++ l2_txs_append, l1_txs, next_id, hash_to_id}
end)

Loading…
Cancel
Save