Store l2 transactions count in the batch table

pull/7584/head
POA 1 year ago
parent 06ce6d7088
commit 03566d53b5
  1. 42
      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 defmodule BlockScoutWeb.API.V2.ZkevmView do
use BlockScoutWeb, :view use BlockScoutWeb, :view
import Ecto.Query, only: [from: 2]
alias Explorer.Chain.ZkevmBatchTxn
alias Explorer.Repo
def render("zkevm_batch.json", %{batch: batch}) do def render("zkevm_batch.json", %{batch: batch}) do
sequence_tx_hash = sequence_tx_hash =
if not is_nil(batch.sequence_transaction) do if not is_nil(batch.sequence_transaction) do
@ -64,33 +59,18 @@ defmodule BlockScoutWeb.API.V2.ZkevmView do
end end
defp render_zkevm_batches(batches) do defp render_zkevm_batches(batches) do
batches Enum.map(batches, fn batch ->
|> Enum.map(fn batch -> sequence_tx_hash =
Task.async(fn -> if not is_nil(batch.sequence_transaction) do
tx_count = batch.sequence_transaction.hash
Repo.replica().aggregate( end
from(
t in ZkevmBatchTxn,
where: t.batch_number == ^batch.number
),
:count,
timeout: :infinity
)
sequence_tx_hash =
if not is_nil(batch.sequence_transaction) do
batch.sequence_transaction.hash
end
%{ %{
"number" => batch.number, "number" => batch.number,
"timestamp" => batch.timestamp, "timestamp" => batch.timestamp,
"tx_count" => tx_count, "tx_count" => batch.l2_transactions_count,
"sequence_tx_hash" => sequence_tx_hash "sequence_tx_hash" => sequence_tx_hash
} }
end)
end) end)
|> Task.yield_many(:infinity)
|> Enum.map(fn {_task, {:ok, item}} -> item end)
end end
end end

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

@ -7,11 +7,12 @@ defmodule Explorer.Chain.ZkevmTxnBatch do
@optional_attrs ~w(sequence_id verify_id)a @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__{ @type t :: %__MODULE__{
number: non_neg_integer(), number: non_neg_integer(),
timestamp: DateTime.t(), timestamp: DateTime.t(),
l2_transactions_count: non_neg_integer(),
global_exit_root: Hash.t(), global_exit_root: Hash.t(),
acc_input_hash: Hash.t(), acc_input_hash: Hash.t(),
state_root: Hash.t(), state_root: Hash.t(),
@ -25,6 +26,7 @@ defmodule Explorer.Chain.ZkevmTxnBatch do
@primary_key {:number, :integer, autogenerate: false} @primary_key {:number, :integer, autogenerate: false}
schema "zkevm_transaction_batches" do schema "zkevm_transaction_batches" do
field(:timestamp, :utc_datetime_usec) field(:timestamp, :utc_datetime_usec)
field(:l2_transactions_count, :integer)
field(:global_exit_root, Hash.Full) field(:global_exit_root, Hash.Full)
field(:acc_input_hash, Hash.Full) field(:acc_input_hash, Hash.Full)
field(:state_root, 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 create table(:zkevm_transaction_batches, primary_key: false) do
add(:number, :integer, null: false, primary_key: true) add(:number, :integer, null: false, primary_key: true)
add(:timestamp, :"timestamp without time zone", null: false) add(:timestamp, :"timestamp without time zone", null: false)
add(:l2_transactions_count, :integer, null: false)
add(:global_exit_root, :bytea, null: false) add(:global_exit_root, :bytea, null: false)
add(:acc_input_hash, :bytea, null: false) add(:acc_input_hash, :bytea, null: false)
add(:state_root, :bytea, null: false) add(:state_root, :bytea, null: false)

@ -262,16 +262,6 @@ defmodule Indexer.Fetcher.ZkevmTxnBatch do
|> get_tx_hash("verifyBatchTxHash") |> get_tx_hash("verifyBatchTxHash")
|> handle_tx_hash(hash_to_id, next_id, l1_txs, true) |> 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_txs_append =
l2_transaction_hashes l2_transaction_hashes
|> Kernel.||([]) |> Kernel.||([])
@ -282,6 +272,17 @@ defmodule Indexer.Fetcher.ZkevmTxnBatch do
} }
end) 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} {[batch | batches], l2_txs ++ l2_txs_append, l1_txs, next_id, hash_to_id}
end) end)

Loading…
Cancel
Save