Blockchain explorer for Ethereum based network and a tool for inspecting and analyzing EVM based blockchains.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
blockscout/lib/explorer/block.ex

55 lines
1.3 KiB

defmodule Explorer.Block do
@moduledoc """
Stores a web3 block.
"""
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias Explorer.{Block, BlockTransaction, Transaction}
@timestamps_opts [type: Timex.Ecto.DateTime,
autogenerate: {Timex.Ecto.DateTime, :autogenerate, []}]
schema "blocks" do
has_many :block_transactions, BlockTransaction
many_to_many :transactions, Transaction, join_through: "block_transactions"
field :number, :integer
field :hash, :string
field :parent_hash, :string
field :nonce, :string
field :miner, :string
field :difficulty, :decimal
field :total_difficulty, :decimal
field :size, :integer
field :gas_limit, :integer
field :gas_used, :integer
field :timestamp, Timex.Ecto.DateTime
timestamps()
end
@required_attrs ~w(number hash parent_hash nonce miner difficulty
total_difficulty size gas_limit gas_used timestamp)a
@doc false
def changeset(%Block{} = block, attrs) do
block
|> cast(attrs, @required_attrs)
|> validate_required(@required_attrs)
|> update_change(:hash, &String.downcase/1)
|> unique_constraint(:hash)
|> cast_assoc(:transactions)
end
def null do
%Block{number: -1}
end
def latest(query) do
query |> order_by(desc: :number)
end
end