Skip to content

Commit

Permalink
Merge pull request blockscout#14 from mantlenetworkio/shijiang/l1l2re…
Browse files Browse the repository at this point in the history
…nder

l1->l2,l2->l1 render
  • Loading branch information
guoshijiang committed Nov 4, 2022
2 parents 68d936e + 3ce8131 commit 9dcbbcf
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ defmodule BlockScoutWeb.ChainController do
total_gas_usage: total_gas_usage,
transactions_path: recent_transactions_path(conn, :index),
txn_batches_path: recent_txn_batches_path(conn, :index),
# l1l2_batches_path: recent_l1l2_batches_path(conn, :index),
transaction_stats: transaction_stats,
block_count: block_count,
gas_price: Application.get_env(:block_scout_web, :gas_price)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ defmodule BlockScoutWeb.L1ToL2TxnController do
|> update_page_parameters(Chain.default_page_size(), Keyword.get(options, :paging_options))
)

%{total_transactions_count: transactions_count, transactions: transactions_plus_one} =
Chain.recent_collated_transactions_for_rap(full_options)
%{total_transactions_count: transactions_count, l1_to_l2: txn_batch_plus_one} =
Chain.recent_collated_l1_to_l2_for_rap(full_options)

{transactions, next_page} =
{l1_to_l2, next_page} =
if fetch_page_number(params) == 1 do
split_list_by_page(transactions_plus_one)
split_list_by_page(txn_batch_plus_one)
else
{transactions_plus_one, nil}
{txn_batch_plus_one, nil}
end

next_page_params =
Expand All @@ -79,7 +79,7 @@ defmodule BlockScoutWeb.L1ToL2TxnController do

pages_limit = transactions_count |> Kernel./(page_size) |> Float.ceil() |> trunc()

case next_page_params(next_page, transactions, params) do
case next_page_params(next_page, l1_to_l2, params) do
nil ->
nil

Expand All @@ -99,12 +99,11 @@ defmodule BlockScoutWeb.L1ToL2TxnController do
conn,
%{
items:
Enum.map(transactions, fn transaction ->
Enum.map(l1_to_l2, fn txn_batch ->
View.render_to_string(
L1ToL2TxnView,
TxnBatchView,
"_tile.html",
transaction: transaction,
burn_address_hash: @burn_address_hash,
txn_batch: txn_batch,
conn: conn
)
end),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ defmodule BlockScoutWeb.L2ToL1TxnController do
|> update_page_parameters(Chain.default_page_size(), Keyword.get(options, :paging_options))
)

%{total_transactions_count: transactions_count, transactions: transactions_plus_one} =
Chain.recent_collated_transactions_for_rap(full_options)
%{total_transactions_count: transactions_count, l2_to_l1: txn_batch_plus_one} =
Chain.recent_collated_l2_to_l1_for_rap(full_options)

{transactions, next_page} =
{l2_to_l1, next_page} =
if fetch_page_number(params) == 1 do
split_list_by_page(transactions_plus_one)
split_list_by_page(txn_batch_plus_one)
else
{transactions_plus_one, nil}
{txn_batch_plus_one, nil}
end

next_page_params =
Expand All @@ -79,7 +79,7 @@ defmodule BlockScoutWeb.L2ToL1TxnController do

pages_limit = transactions_count |> Kernel./(page_size) |> Float.ceil() |> trunc()

case next_page_params(next_page, transactions, params) do
case next_page_params(next_page, l2_to_l1, params) do
nil ->
nil

Expand All @@ -99,12 +99,11 @@ defmodule BlockScoutWeb.L2ToL1TxnController do
conn,
%{
items:
Enum.map(transactions, fn transaction ->
Enum.map(l2_to_l1, fn txn_batch ->
View.render_to_string(
L2ToL1TxnView,
TxnBatchView,
"_tile.html",
transaction: transaction,
burn_address_hash: @burn_address_hash,
txn_batch: txn_batch,
conn: conn
)
end),
Expand Down
101 changes: 66 additions & 35 deletions apps/explorer/lib/explorer/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ defmodule Explorer.Chain do
Transaction,
Wei,
StateBatch,
TxnBatch
TxnBatch,
L1ToL2,
L2ToL1,
}

alias Explorer.Chain.Block.{EmissionReward, Reward}
Expand Down Expand Up @@ -695,13 +697,6 @@ defmodule Explorer.Chain do
)
end








def where_batch_index_in_period(base_query, from_index, to_index) when is_nil(from_index) and not is_nil(to_index) do
from(q in base_query,
where: q.batch_index <= ^to_index
Expand Down Expand Up @@ -3195,6 +3190,11 @@ defmodule Explorer.Chain do
#end
end

@spec recent_collated_l1_to_l2([paging_options]) :: [TxnBatch.t()]
def recent_collated_l1_to_l2(options \\ []) when is_list(options) do
paging_options = Keyword.get(options, :paging_options, @default_paging_options)
fetch_recent_collated_l1_to_l2(paging_options)
end
# RAP - random access pagination
@spec recent_collated_transactions_for_rap([paging_options | necessity_by_association_option]) :: %{
:total_transactions_count => non_neg_integer(),
Expand Down Expand Up @@ -3236,12 +3236,33 @@ defmodule Explorer.Chain do
def recent_collated_state_batches_for_rap(options \\ []) when is_list(options) do
#necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
paging_options = Keyword.get(options, :paging_options, @default_paging_options)

#total_transactions_count = transactions_available_count()
fetched_state_batches =fetch_recent_collated_state_batch_for_rap(paging_options)
%{total_transactions_count: 1000, state_batches: fetched_state_batches}
end

@spec recent_collated_l2_to_l1_for_rap([paging_options]) :: %{
:total_transactions_count => non_neg_integer(),
:l2_to_l1 => [L2ToL1.t()]
}

def recent_collated_l2_to_l1_for_rap(options \\ []) when is_list(options) do
paging_options = Keyword.get(options, :paging_options, @default_paging_options)
fetched_l2_to_l1 =fetch_recent_collated_l2_to_l1_for_rap(paging_options)
%{total_transactions_count: 1000, l2_to_l1: fetched_l2_to_l1}
end

@spec recent_collated_l1_to_l2_for_rap([paging_options]) :: %{
:total_transactions_count => non_neg_integer(),
:l1_to_l2 => [L1ToL2.t()]
}

def recent_collated_l1_to_l2_for_rap(options \\ []) when is_list(options) do
paging_options = Keyword.get(options, :paging_options, @default_paging_options)
fetched_l1_to_l2=fetch_recent_collated_l1_to_l2_for_rap(paging_options)
%{total_transactions_count: 1000, l1_to_l2: fetched_l1_to_l2}
end

def fetch_recent_collated_transactions_for_rap(paging_options, necessity_by_association) do
fetch_transactions_for_rap()
|> where([transaction], not is_nil(transaction.block_number) and not is_nil(transaction.index))
Expand All @@ -3251,37 +3272,27 @@ defmodule Explorer.Chain do
|> Repo.all()
end

def fetch_recent_collated_state_batch_for_rap(paging_options) do
#query =
# from(
# state in StateBatch,
# where: state.size > 0,
# )
def fetch_recent_collated_l2_to_l1_for_rap(paging_options) do
fetch_l2_to_l1_for_rap()
|> handle_random_access_paging_options(paging_options)
|> Repo.all()
end

#query
#|> limit(1)
#|> Repo.all()
def fetch_recent_collated_l1_to_l2_for_rap(paging_options) do
fetch_l1_to_l2_for_rap()
|> handle_random_access_paging_options(paging_options)
|> Repo.all()
end

def fetch_recent_collated_state_batch_for_rap(paging_options) do
fetch_state_batch_for_rap()
|> handle_random_access_paging_options(paging_options)
#|> join_associations(necessity_by_association)
|> Repo.all()
end

def fetch_recent_collated_txn_batch_for_rap(paging_options) do
#query =
# from(
# state in StateBatch,
# where: state.size > 0,
# )

#query
#|> limit(1)
#|> Repo.all()

fetch_txn_batch_for_rap()
|> handle_random_access_paging_options(paging_options)
#|> join_associations(necessity_by_association)
|> Repo.all()
end

Expand All @@ -3293,7 +3304,6 @@ defmodule Explorer.Chain do
def recent_collated_txn_batches_for_rap(options \\ []) when is_list(options) do
#necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
paging_options = Keyword.get(options, :paging_options, @default_paging_options)

#total_transactions_count = transactions_available_count()
fetched_txn_batches =fetch_recent_collated_txn_batch_for_rap(paging_options)
%{total_transactions_count: 1000, txn_batches: fetched_txn_batches}
Expand All @@ -3315,6 +3325,16 @@ defmodule Explorer.Chain do
|> order_by([txn_batch], desc: txn_batch.batch_index)
end

defp fetch_l1_to_l2_for_rap do
L1ToL2
|> order_by([l1_to_l2], desc: l1_to_l2.batch_index)
end

defp fetch_l2_to_l1_for_rap do
L2ToL1
|> order_by([l2_to_l1], desc: l2_to_l1.msg_nonce)
end

def transactions_available_count do
Transaction
|> where([transaction], not is_nil(transaction.block_number) and not is_nil(transaction.index))
Expand All @@ -3333,7 +3353,6 @@ defmodule Explorer.Chain do
|> Repo.all()
end


def fetch_recent_collated_txn_batches(paging_options) do
paging_options
|> fetch_txn_batches()
Expand All @@ -3343,7 +3362,11 @@ defmodule Explorer.Chain do
|> Repo.all()
end


def fetch_recent_collated_l1_to_l2(paging_options) do
paging_options
|> fetch_l1_to_l2()
|> Repo.all()
end

@doc """
Return the list of pending transactions that occurred recently.
Expand Down Expand Up @@ -4304,22 +4327,30 @@ defmodule Explorer.Chain do
end

defp fetch_transactions(paging_options \\ nil, from_block \\ nil, to_block \\ nil) do

Transaction
|> order_by([transaction], desc: transaction.block_number, desc: transaction.index)
|> where_block_number_in_period(from_block, to_block)
|> handle_paging_options(paging_options)
end


defp fetch_txn_batches(paging_options \\ nil, from_index \\ nil, to_index \\ nil) do
TxnBatch
|> order_by([txn_batch], desc: txn_batch.batch_index)
|> where_batch_index_in_period(from_index, to_index)
|> handle_paging_options(paging_options)
end

defp fetch_l1_to_l2(paging_options \\ nil, from_index \\ nil, to_index \\ nil) do
L1ToL2
|> order_by([l1_to_l2], desc: l1_to_l2.queue_index)
|> handle_paging_options(paging_options)
end

defp fetch_l2_to_l1(paging_options \\ nil, from_index \\ nil, to_index \\ nil) do
L2ToL1
|> order_by([l2_to_l1], desc: l2_to_l1.msg_nonce)
|> handle_paging_options(paging_options)
end

defp fetch_transactions_in_ascending_order_by_index(paging_options) do
Transaction
Expand Down
36 changes: 36 additions & 0 deletions apps/explorer/lib/explorer/chain/l1_to_l2.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Explorer.Chain.L1ToL2 do
use Ecto.Schema
import Ecto.Changeset

@type t :: %__MODULE__{
hash: Hash.Full.t(),
l2_hash: Hash.Full.t(),
block: integer(),
batch_index: integer(),
timestamp: DateTime.t(),
tx_origin: %Ecto.Association.NotLoaded{} | Address.t(),
queue_index: integer(),
target: %Ecto.Association.NotLoaded{} | Address.t(),
gas_limit: Gas.t(),
}

schema "l1_to_l2" do
field(:hash, :string)
field(:l2_hash, :string)
field(:block, :integer)
field(:batch_index, :integer)
field(:timestamp, :utc_datetime_usec)
field(:tx_origin, :string)
field(:queue_index, :integer)
field(:target, :string)
field(:gas_limit, :decimal)
timestamps()
end

@doc false
def changeset(l1_to_l2, attrs) do
l1_to_l2
|> cast(attrs, [])
|> validate_required([])
end
end
37 changes: 37 additions & 0 deletions apps/explorer/lib/explorer/chain/l2_to_l1.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule Explorer.Chain.L2ToL1 do
use Ecto.Schema
import Ecto.Changeset

@type t :: %__MODULE__{
hash: Hash.Full.t(),
l2_hash: Hash.Full.t(),
block: integer(),
msg_nonce: integer(),
from_address: %Ecto.Association.NotLoaded{} | Address.t(),
txn_batch_index: integer(),
state_batch_index: integer(),
timestamp: DateTime.t(),
status: String.t(),
gas_limit: Gas.t(),
}
schema "l2_to_l1" do
field(:hash, :string)
field(:l2_hash, :string)
field(:block, :integer)
field(:msg_nonce, :integer)
field(:from_address, :string)
field(:txn_batch_index, :integer)
field(:state_batch_index, :integer)
field(:timestamp, :utc_datetime_usec)
field(:status, :string)
field(:gas_limit, :decimal)
timestamps()
end

@doc false
def changeset(l2_to_l1, attrs) do
l2_to_l1
|> cast(attrs, [])
|> validate_required([])
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ defmodule Explorer.Repo.Migrations.CreateL1ToL2 do
add(:timestamp, :utc_datetime_usec, null: false)
add(:tx_origin, :bytea, null: false)
add(:queue_index, :bigint, null: false)
add(:data, :bytea, null: false)
add(:target, :bytea, null: false)
add(:gas_limit, :numeric, precision: 100, null: false)
add(:gas_used, :numeric, precision: 100, null: false)
add(:gas_price, :numeric, precision: 100, null: false)
add(:fee_scalar, :bigint, null: false)

timestamps(null: false, type: :utc_datetime_usec)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ defmodule Explorer.Repo.Migrations.CreateL2ToL1 do
add(:timestamp, :utc_datetime_usec, null: false)
add(:status, :string, null: false)
add(:gas_limit, :numeric, precision: 100, null: false)
add(:gas_used, :numeric, precision: 100, null: false)
add(:gas_price, :numeric, precision: 100, null: false)
add(:fee_scalar, :bigint, null: false)

timestamps(null: false, type: :utc_datetime_usec)
end
Expand Down

0 comments on commit 9dcbbcf

Please sign in to comment.