diff --git a/apps/explorer/lib/explorer/chain/zkevm_lifecycle_txn.ex b/apps/explorer/lib/explorer/chain/zkevm_lifecycle_txn.ex new file mode 100644 index 0000000000..2ab3ed9b17 --- /dev/null +++ b/apps/explorer/lib/explorer/chain/zkevm_lifecycle_txn.ex @@ -0,0 +1,32 @@ +defmodule Explorer.Chain.ZkevmLifecycleTxn do + @moduledoc "Models an L1 lifecycle transaction for zkEVM." + + use Explorer.Schema + + alias Explorer.Chain.{Hash, ZkevmTxnBatch} + + @required_attrs ~w(id hash is_verify)a + + @type t :: %__MODULE__{ + hash: Hash.t(), + is_verify: boolean() + } + + @primary_key {:id, :integer, autogenerate: false} + schema "zkevm_lifecycle_l1_transactions" do + field(:hash, Hash.Full) + field(:is_verify, :boolean) + + has_many(:sequenced_batches, ZkevmTxnBatch, foreign_key: :sequence_id) + has_many(:verified_batches, ZkevmTxnBatch, foreign_key: :verify_id) + + timestamps() + end + + def changeset(%__MODULE__{} = txn, attrs \\ %{}) do + txn + |> cast(attrs, @required_attrs) + |> validate_required(@required_attrs) + |> unique_constraint(:id) + end +end diff --git a/apps/explorer/lib/explorer/chain/zkevm_txn_batch.ex b/apps/explorer/lib/explorer/chain/zkevm_txn_batch.ex new file mode 100644 index 0000000000..e5359bac62 --- /dev/null +++ b/apps/explorer/lib/explorer/chain/zkevm_txn_batch.ex @@ -0,0 +1,45 @@ +defmodule Explorer.Chain.ZkevmTxnBatch do + @moduledoc "Models a batch of transactions for zkEVM." + + use Explorer.Schema + + alias Explorer.Chain.{Hash, ZkevmLifecycleTxn} + + @required_attrs ~w(number timestamp l2_transaction_hashes global_exit_root acc_input_hash state_root sequence_id verify_id)a + + @type t :: %__MODULE__{ + number: non_neg_integer(), + timestamp: DateTime.t(), + l2_transaction_hashes: [Hash.t()], + global_exit_root: Hash.t(), + acc_input_hash: Hash.t(), + state_root: Hash.t(), + sequence_id: non_neg_integer() | nil, + sequence_transaction: %Ecto.Association.NotLoaded{} | ZkevmLifecycleTxn.t() | nil, + verify_id: non_neg_integer() | nil, + verify_transaction: %Ecto.Association.NotLoaded{} | ZkevmLifecycleTxn.t() | nil + } + + @primary_key false + schema "zkevm_transaction_batches" do + field(:number, :integer, primary_key: true) + field(:timestamp, :utc_datetime_usec) + field(:l2_transaction_hashes, {:array, Hash.Full}) + field(:global_exit_root, Hash.Full) + field(:acc_input_hash, Hash.Full) + field(:state_root, Hash.Full) + + belongs_to(:sequence_transaction, ZkevmLifecycleTxn, foreign_key: :sequence_id, references: :id, type: :integer) + belongs_to(:verify_transaction, ZkevmLifecycleTxn, foreign_key: :verify_id, references: :id, type: :integer) + + timestamps() + end + + def changeset(%__MODULE__{} = batches, attrs \\ %{}) do + batches + |> cast(attrs, @required_attrs) + |> validate_required(@required_attrs) + |> foreign_key_constraint(:sequence_id) + |> foreign_key_constraint(:verify_id) + end +end diff --git a/apps/explorer/priv/repo/migrations/20230420110800_create_zkevm_tables.exs b/apps/explorer/priv/repo/migrations/20230420110800_create_zkevm_tables.exs new file mode 100644 index 0000000000..4604d67130 --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20230420110800_create_zkevm_tables.exs @@ -0,0 +1,35 @@ +defmodule Explorer.Repo.Migrations.CreateZkevmTables do + use Ecto.Migration + + def change do + create table(:zkevm_lifecycle_l1_transactions, primary_key: false) do + add(:id, :integer, null: false, primary_key: true) + add(:hash, :bytea, null: false) + add(:is_verify, :boolean, null: false) + timestamps(null: false, type: :utc_datetime_usec) + end + + 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_transaction_hashes, {:array, :bytea}, null: false) + add(:global_exit_root, :bytea, null: false) + add(:acc_input_hash, :bytea, null: false) + add(:state_root, :bytea, null: false) + + add( + :sequence_id, + references(:zkevm_lifecycle_l1_transactions, on_delete: :restrict, on_update: :update_all, type: :integer), + null: true + ) + + add( + :verify_id, + references(:zkevm_lifecycle_l1_transactions, on_delete: :restrict, on_update: :update_all, type: :integer), + null: true + ) + + timestamps(null: false, type: :utc_datetime_usec) + end + end +end