Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/production-zksync'
Browse files Browse the repository at this point in the history
  • Loading branch information
Woodpile37 committed Feb 7, 2024
2 parents cb9acbd + dd31aea commit 3e3c6e5
Show file tree
Hide file tree
Showing 57 changed files with 4,000 additions and 513 deletions.
2 changes: 1 addition & 1 deletion .dialyzer-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:0: Unknown type 'Elixir.Hash':t/0
:0: Unknown type 'Elixir.Address':t/0
lib/ethereum_jsonrpc/rolling_window.ex:171
lib/explorer/chain/transaction.ex:171
lib/explorer/smart_contract/solidity/publisher_worker.ex:1
lib/explorer/smart_contract/vyper/publisher_worker.ex:1
lib/explorer/smart_contract/solidity/publisher_worker.ex:8
Expand All @@ -23,4 +24,3 @@ lib/indexer/fetcher/zkevm/transaction_batch.ex:156
lib/indexer/fetcher/zkevm/transaction_batch.ex:252
lib/block_scout_web/views/api/v2/transaction_view.ex:431
lib/block_scout_web/views/api/v2/transaction_view.ex:472
lib/explorer/chain/transaction.ex:170
3 changes: 2 additions & 1 deletion .github/workflows/publish-docker-image-for-zksync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ jobs:
ADMIN_PANEL_ENABLED=false
CACHE_ADDRESS_WITH_BALANCES_UPDATE_INTERVAL=
BLOCKSCOUT_VERSION=v${{ env.RELEASE_VERSION }}-beta.+commit.${{ env.SHORT_SHA }}
RELEASE_VERSION=${{ env.RELEASE_VERSION }}
RELEASE_VERSION=${{ env.RELEASE_VERSION }}
CHAIN_TYPE=zksync
17 changes: 17 additions & 0 deletions apps/block_scout_web/lib/block_scout_web/api_router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ defmodule BlockScoutWeb.ApiRouter do
get("/zkevm-batch/:batch_number", V2.TransactionController, :zkevm_batch)
end

if System.get_env("CHAIN_TYPE") == "zksync" do
get("/zksync-batch/:batch_number", V2.TransactionController, :zksync_batch)
end

if System.get_env("CHAIN_TYPE") == "suave" do
get("/execution-node/:execution_node_hash_param", V2.TransactionController, :execution_node)
end
Expand Down Expand Up @@ -268,6 +272,11 @@ defmodule BlockScoutWeb.ApiRouter do
get("/zkevm/batches/confirmed", V2.ZkevmController, :batches_confirmed)
get("/zkevm/batches/latest-number", V2.ZkevmController, :batch_latest_number)
end

if System.get_env("CHAIN_TYPE") == "zksync" do
get("/zksync/batches/confirmed", V2.ZkSyncController, :batches_confirmed)
get("/zksync/batches/latest-number", V2.ZkSyncController, :batch_latest_number)
end
end

scope "/stats" do
Expand Down Expand Up @@ -308,6 +317,14 @@ defmodule BlockScoutWeb.ApiRouter do
get("/addresses/:address_hash_param/transactions", V2.Proxy.NovesFiController, :address_transactions)
end
end

scope "/zksync" do
if System.get_env("CHAIN_TYPE") == "zksync" do
get("/batches", V2.ZkSyncController, :batches)
get("/batches/count", V2.ZkSyncController, :batches_count)
get("/batches/:batch_number", V2.ZkSyncController, :batch)
end
end
end

scope "/v1", as: :api_v1 do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,35 @@ defmodule BlockScoutWeb.API.V2.BlockController do

@api_true [api?: true]

@block_params [
necessity_by_association: %{
[miner: :names] => :optional,
:uncles => :optional,
:nephews => :optional,
:rewards => :optional,
:transactions => :optional,
:withdrawals => :optional
},
api?: true
]
@block_necessity_by_association %{
[miner: :names] => :optional,
:uncles => :optional,
:nephews => :optional,
:rewards => :optional,
:transactions => :optional,
:withdrawals => :optional
}

action_fallback(BlockScoutWeb.API.V2.FallbackController)

def block(conn, %{"block_hash_or_number" => block_hash_or_number}) do
extended_necessity_by_association =
case Application.get_env(:explorer, :chain_type) do
"zksync" ->
@block_necessity_by_association
|> Map.put(:zksync_batch, :optional)
|> Map.put(:zksync_commit_transaction, :optional)
|> Map.put(:zksync_prove_transaction, :optional)
|> Map.put(:zksync_execute_transaction, :optional)

_ ->
@block_necessity_by_association
end

block_params = Keyword.merge(@api_true, necessity_by_association: extended_necessity_by_association)

with {:ok, type, value} <- parse_block_hash_or_number_param(block_hash_or_number),
{:ok, block} <- fetch_block(type, value, @block_params) do
{:ok, block} <- fetch_block(type, value, block_params) do
conn
|> put_status(200)
|> render(:block, %{block: block})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule BlockScoutWeb.API.V2.TransactionController do
alias Explorer.Chain
alias Explorer.Chain.{Hash, Transaction}
alias Explorer.Chain.Zkevm.Reader
alias Explorer.Chain.ZkSync.Reader
alias Indexer.Fetcher.FirstTraceOnDemand

action_fallback(BlockScoutWeb.API.V2.FallbackController)
Expand Down Expand Up @@ -87,6 +88,13 @@ defmodule BlockScoutWeb.API.V2.TransactionController do
|> Map.put(:zkevm_sequence_transaction, :optional)
|> Map.put(:zkevm_verify_transaction, :optional)

"zksync" ->
necessity_by_association_with_actions
|> Map.put(:zksync_batch, :optional)
|> Map.put(:zksync_commit_transaction, :optional)
|> Map.put(:zksync_prove_transaction, :optional)
|> Map.put(:zksync_execute_transaction, :optional)

"suave" ->
necessity_by_association_with_actions
|> Map.put(:logs, :optional)
Expand Down Expand Up @@ -154,6 +162,23 @@ defmodule BlockScoutWeb.API.V2.TransactionController do
|> render(:transactions, %{transactions: transactions |> maybe_preload_ens(), items: true})
end

@doc """
Function to handle GET requests to `/api/v2/transactions/zksync-batch/:batch_number` endpoint.
It renders the list of L2 transactions bound to the specified batch.
"""
@spec zksync_batch(Plug.Conn.t(), map()) :: Plug.Conn.t()
def zksync_batch(conn, %{"batch_number" => batch_number} = _params) do
transactions =
batch_number
|> Reader.batch_transactions(api?: true)
|> Enum.map(fn tx -> tx.hash end)
|> Chain.hashes_to_transactions(api?: true, necessity_by_association: @transaction_necessity_by_association)

conn
|> put_status(200)
|> render(:transactions, %{transactions: transactions, items: true})
end

def execution_node(conn, %{"execution_node_hash_param" => execution_node_hash_string} = params) do
with {:format, {:ok, execution_node_hash}} <- {:format, Chain.string_to_address_hash(execution_node_hash_string)} do
full_options =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
defmodule BlockScoutWeb.API.V2.ZkSyncController do
use BlockScoutWeb, :controller

import BlockScoutWeb.Chain,
only: [
next_page_params: 4,
paging_options: 1,
split_list_by_page: 1
]

alias Explorer.Chain.ZkSync.{Reader, TransactionBatch}

action_fallback(BlockScoutWeb.API.V2.FallbackController)

@batch_necessity_by_association %{
:commit_transaction => :optional,
:prove_transaction => :optional,
:execute_transaction => :optional,
:l2_transactions => :optional
}

@batches_necessity_by_association %{
:commit_transaction => :optional,
:prove_transaction => :optional,
:execute_transaction => :optional
}

@doc """
Function to handle GET requests to `/api/v2/zksync/batches/:batch_number` endpoint.
"""
@spec batch(Plug.Conn.t(), map()) :: Plug.Conn.t()
def batch(conn, %{"batch_number" => batch_number} = _params) do
case Reader.batch(
batch_number,
necessity_by_association: @batch_necessity_by_association,
api?: true
) do
{:ok, batch} ->
conn
|> put_status(200)
|> render(:zksync_batch, %{batch: batch})

{:error, :not_found} = res ->
res
end
end

@doc """
Function to handle GET requests to `/api/v2/zksync/batches` endpoint.
"""
@spec batches(Plug.Conn.t(), map()) :: Plug.Conn.t()
def batches(conn, params) do
{batches, next_page} =
params
|> paging_options()
|> Keyword.put(:necessity_by_association, @batches_necessity_by_association)
|> Keyword.put(:api?, true)
|> Reader.batches()
|> split_list_by_page()

next_page_params =
next_page_params(
next_page,
batches,
params,
fn %TransactionBatch{number: number} -> %{"number" => number} end
)

conn
|> put_status(200)
|> render(:zksync_batches, %{
batches: batches,
next_page_params: next_page_params
})
end

@doc """
Function to handle GET requests to `/api/v2/zksync/batches/count` endpoint.
"""
@spec batches_count(Plug.Conn.t(), map()) :: Plug.Conn.t()
def batches_count(conn, _params) do
conn
|> put_status(200)
|> render(:zksync_batches_count, %{count: Reader.batches_count(api?: true)})
end

@doc """
Function to handle GET requests to `/api/v2/main-page/zksync/batches/confirmed` endpoint.
"""
@spec batches_confirmed(Plug.Conn.t(), map()) :: Plug.Conn.t()
def batches_confirmed(conn, _params) do
batches =
[]
|> Keyword.put(:necessity_by_association, @batches_necessity_by_association)
|> Keyword.put(:api?, true)
|> Keyword.put(:confirmed?, true)
|> Reader.batches()

conn
|> put_status(200)
|> render(:zksync_batches, %{batches: batches})
end

@doc """
Function to handle GET requests to `/api/v2/main-page/zksync/batches/latest-number` endpoint.
"""
@spec batch_latest_number(Plug.Conn.t(), map()) :: Plug.Conn.t()
def batch_latest_number(conn, _params) do
conn
|> put_status(200)
|> render(:zksync_batch_latest_number, %{number: batch_latest_number()})
end

defp batch_latest_number do
case Reader.batch(:latest, api?: true) do
{:ok, batch} -> batch.number
{:error, :not_found} -> 0
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule BlockScoutWeb.API.V2.BlockView do
use BlockScoutWeb, :view

alias BlockScoutWeb.BlockView
alias BlockScoutWeb.API.V2.{ApiView, Helper}
alias BlockScoutWeb.API.V2.{ApiView, Helper, ZkSyncView}
alias Explorer.Chain.Block
alias Explorer.Counters.BlockPriorityFeeCounter

Expand Down Expand Up @@ -127,6 +127,10 @@ defmodule BlockScoutWeb.API.V2.BlockView do
|> Map.put("bitcoin_merged_mining_merkle_proof", block.bitcoin_merged_mining_merkle_proof)
|> Map.put("hash_for_merged_mining", block.hash_for_merged_mining)

"zksync" ->
result
|> ZkSyncView.add_zksync_info(block)

_ ->
result
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule BlockScoutWeb.API.V2.TransactionView do
use BlockScoutWeb, :view

alias BlockScoutWeb.API.V2.{ApiView, Helper, TokenView}
alias BlockScoutWeb.API.V2.{ApiView, Helper, TokenView, ZkSyncView}
alias BlockScoutWeb.{ABIEncodedValueView, TransactionView}
alias BlockScoutWeb.Models.GetTransactionTags
alias BlockScoutWeb.Tokens.Helper, as: TokensHelper
Expand Down Expand Up @@ -470,6 +470,10 @@ defmodule BlockScoutWeb.API.V2.TransactionView do

Map.put(extended_result, "zkevm_status", zkevm_status(extended_result))

"zksync" ->
result
|> ZkSyncView.add_zksync_info(transaction)

"suave" ->
suave_fields(transaction, result, single_tx?, conn, watchlist_names)

Expand Down
Loading

0 comments on commit 3e3c6e5

Please sign in to comment.