Block in the consensus chain marked consensus true

pull/802/head
Luke Imhoff 6 years ago
parent 96d5aea311
commit 4b140c87df
  1. 12
      apps/block_scout_web/test/block_scout_web/controllers/chain_controller_test.exs
  2. 4
      apps/explorer/lib/explorer/chain.ex
  3. 7
      apps/explorer/lib/explorer/chain/block.ex
  4. 5
      apps/explorer/priv/repo/migrations/20180117221922_create_blocks.exs
  5. 7
      apps/explorer/test/explorer/chain/import_test.exs
  6. 5
      apps/explorer/test/explorer/chain_test.exs
  7. 1
      apps/explorer/test/support/factory.ex
  8. 5
      apps/indexer/lib/indexer/block/catchup/fetcher.ex
  9. 1
      apps/indexer/lib/indexer/block/realtime/fetcher.ex
  10. 10
      apps/indexer/test/indexer/block/catchup/bound_interval_supervisor_test.exs
  11. 6
      apps/indexer/test/indexer/block/fetcher_test.exs

@ -3,6 +3,8 @@ defmodule BlockScoutWeb.ChainControllerTest do
import BlockScoutWeb.Router.Helpers, only: [chain_path: 2, block_path: 3, transaction_path: 3, address_path: 3]
alias Explorer.Chain.Block
describe "GET index/2" do
test "returns a welcome message", %{conn: conn} do
conn = get(conn, chain_path(BlockScoutWeb.Endpoint, :show))
@ -64,13 +66,21 @@ defmodule BlockScoutWeb.ChainControllerTest do
end
describe "GET q/2" do
test "finds a block by block number", %{conn: conn} do
test "finds a consensus block by block number", %{conn: conn} do
insert(:block, number: 37)
conn = get(conn, "/search?q=37")
assert redirected_to(conn) == block_path(conn, :show, "37")
end
test "does not find non-consensus block by number", %{conn: conn} do
%Block{number: number} = insert(:block, consensus: false)
conn = get(conn, "/search?q=#{number}")
assert conn.status == 404
end
test "finds a transaction by hash", %{conn: conn} do
transaction =
:transaction

@ -1062,7 +1062,7 @@ defmodule Explorer.Chain do
end
@doc """
Finds `t:Explorer.Chain.Block.t/0` with `number`
Finds consensus `t:Explorer.Chain.Block.t/0` with `number`.
## Options
@ -1077,7 +1077,7 @@ defmodule Explorer.Chain do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
Block
|> where(number: ^number)
|> where(consensus: true, number: ^number)
|> join_associations(necessity_by_association)
|> Repo.one()
|> case do

@ -9,7 +9,7 @@ defmodule Explorer.Chain.Block do
alias Explorer.Chain.{Address, Block.SecondDegreeRelation, Gas, Hash, Transaction}
@required_attrs ~w(difficulty gas_limit gas_used hash miner_hash nonce number parent_hash size timestamp
@required_attrs ~w(consensus difficulty gas_limit gas_used hash miner_hash nonce number parent_hash size timestamp
total_difficulty)a
@typedoc """
@ -25,6 +25,9 @@ defmodule Explorer.Chain.Block do
@type block_number :: non_neg_integer()
@typedoc """
* `consensus`
* `true` - this is a block on the longest consensus agreed upon chain.
* `false` - this is an uncle block from a fork.
* `difficulty` - how hard the block was to mine.
* `gas_limit` - If the total number of gas used by the computation spawned by the transaction, including the
original message and any sub-messages that may be triggered, is less than or equal to the gas limit, then the
@ -43,6 +46,7 @@ defmodule Explorer.Chain.Block do
* `transactions` - the `t:Explorer.Chain.Transaction.t/0` in this block.
"""
@type t :: %__MODULE__{
consensus: boolean(),
difficulty: difficulty(),
gas_limit: Gas.t(),
gas_used: Gas.t(),
@ -60,6 +64,7 @@ defmodule Explorer.Chain.Block do
@primary_key {:hash, Hash.Full, autogenerate: false}
schema "blocks" do
field(:consensus, :boolean)
field(:difficulty, :decimal)
field(:gas_limit, :decimal)
field(:gas_used, :decimal)

@ -3,6 +3,7 @@ defmodule Explorer.Repo.Migrations.CreateBlocks do
def change do
create table(:blocks, primary_key: false) do
add(:consensus, :boolean, null: false)
add(:difficulty, :numeric, precision: 50)
add(:gas_limit, :numeric, precision: 100, null: false)
add(:gas_used, :numeric, precision: 100, null: false)
@ -22,7 +23,7 @@ defmodule Explorer.Repo.Migrations.CreateBlocks do
end
create(index(:blocks, [:timestamp]))
create(unique_index(:blocks, [:parent_hash]))
create(unique_index(:blocks, [:number]))
create(index(:blocks, [:parent_hash], unique: true, where: ~s(consensus), name: :one_consensus_child_per_parent))
create(index(:blocks, [:number], unique: true, where: ~s(consensus), name: :one_consensus_block_at_height))
end
end

@ -24,6 +24,7 @@ defmodule Explorer.Chain.ImportTest do
blocks: %{
params: [
%{
consensus: true,
difficulty: 340_282_366_920_938_463_463_374_607_431_768_211_454,
gas_limit: 6_946_336,
gas_used: 50450,
@ -516,6 +517,7 @@ defmodule Explorer.Chain.ImportTest do
blocks: %{
params: [
%{
consensus: true,
difficulty: 340_282_366_920_938_463_463_374_607_431_768_211_454,
gas_limit: 6_926_030,
gas_used: 269_607,
@ -605,6 +607,7 @@ defmodule Explorer.Chain.ImportTest do
blocks: %{
params: [
%{
consensus: true,
difficulty: 340_282_366_920_938_463_463_374_607_431_768_211_454,
gas_limit: 6_926_030,
gas_used: 269_607,
@ -698,6 +701,7 @@ defmodule Explorer.Chain.ImportTest do
blocks: %{
params: [
%{
consensus: true,
difficulty: 340_282_366_920_938_463_463_374_607_431_768_211_454,
gas_limit: 6_926_030,
gas_used: 269_607,
@ -788,6 +792,7 @@ defmodule Explorer.Chain.ImportTest do
blocks: %{
params: [
%{
consensus: true,
difficulty: 340_282_366_920_938_463_463_374_607_431_768_211_454,
gas_limit: 6_946_336,
gas_used: 50450,
@ -911,6 +916,7 @@ defmodule Explorer.Chain.ImportTest do
blocks: %{
params: [
%{
consensus: true,
difficulty: 242_354_495_292_210,
gas_limit: 4_703_218,
gas_used: 1_009_480,
@ -924,6 +930,7 @@ defmodule Explorer.Chain.ImportTest do
total_difficulty: 415_641_295_487_918_824_165
},
%{
consensus: true,
difficulty: 247_148_243_947_046,
gas_limit: 4_704_624,
gas_used: 363_000,

@ -1,6 +1,9 @@
defmodule Explorer.ChainTest do
use Explorer.DataCase
require Ecto.Query
import Ecto.Query
import Explorer.Factory
alias Explorer.{Chain, Factory, PagingOptions, Repo}
@ -670,6 +673,7 @@ defmodule Explorer.ChainTest do
blocks: %{
params: [
%{
consensus: true,
difficulty: 340_282_366_920_938_463_463_374_607_431_768_211_454,
gas_limit: 6_946_336,
gas_used: 50450,
@ -825,6 +829,7 @@ defmodule Explorer.ChainTest do
],
blocks: [
%Block{
consensus: true,
difficulty: ^difficulty,
gas_limit: ^gas_limit,
gas_used: ^gas_used,

@ -105,6 +105,7 @@ defmodule Explorer.Factory do
def block_factory do
%Block{
consensus: true,
number: block_number(),
hash: block_hash(),
parent_hash: block_hash(),

@ -105,7 +105,10 @@ defmodule Indexer.Block.Catchup.Fetcher do
{async_import_remaning_block_data_options, chain_import_options} =
Map.split(options, @async_import_remaning_block_data_options)
with {:ok, results} = ok <- Chain.import(chain_import_options) do
with {:ok, results} = ok <-
chain_import_options
|> put_in([:blocks, :params, Access.all(), :consensus], true)
|> Chain.import() do
async_import_remaining_block_data(
results,
async_import_remaning_block_data_options

@ -104,6 +104,7 @@ defmodule Indexer.Block.Realtime.Fetcher do
options
|> Map.drop(@import_options)
|> put_in([:addresses, :params], balances_addresses_params)
|> put_in([:blocks, :params, Access.all(), :consensus], true)
|> put_in([Access.key(:balances, %{}), :params], balances_params)
|> put_in([Access.key(:internal_transactions, %{}), :params], internal_transactions_params)
|> put_in([Access.key(:token_balances), :params], token_balances),

@ -8,7 +8,7 @@ defmodule Indexer.Block.Catchup.BoundIntervalSupervisorTest do
alias Explorer.Chain.Block
alias Indexer.{BoundInterval, CoinBalance, InternalTransaction, Token, TokenBalance}
alias Indexer.Block.Catchup
alias Indexer.Block.{Catchup, Uncle}
@moduletag capture_log: true
@ -202,6 +202,10 @@ defmodule Indexer.Block.Catchup.BoundIntervalSupervisorTest do
Token.Supervisor.Case.start_supervised!(json_rpc_named_arguments: json_rpc_named_arguments)
TokenBalance.Supervisor.Case.start_supervised!(json_rpc_named_arguments: json_rpc_named_arguments)
Uncle.Supervisor.Case.start_supervised!(
block_fetcher: %Indexer.Block.Fetcher{json_rpc_named_arguments: json_rpc_named_arguments}
)
Catchup.Supervisor.Case.start_supervised!(%{
block_fetcher: %Indexer.Block.Fetcher{json_rpc_named_arguments: json_rpc_named_arguments}
})
@ -324,6 +328,10 @@ defmodule Indexer.Block.Catchup.BoundIntervalSupervisorTest do
Token.Supervisor.Case.start_supervised!(json_rpc_named_arguments: json_rpc_named_arguments)
TokenBalance.Supervisor.Case.start_supervised!(json_rpc_named_arguments: json_rpc_named_arguments)
Uncle.Supervisor.Case.start_supervised!(
block_fetcher: %Indexer.Block.Fetcher{json_rpc_named_arguments: json_rpc_named_arguments}
)
# from `setup :state`
assert_received :catchup_index

@ -10,7 +10,7 @@ defmodule Indexer.Block.FetcherTest do
alias Explorer.Chain
alias Explorer.Chain.{Address, Log, Transaction, Wei}
alias Indexer.{CoinBalance, BufferedTask, InternalTransaction, Token, TokenBalance}
alias Indexer.Block.Fetcher
alias Indexer.Block.{Fetcher, Uncle}
@moduletag capture_log: true
@ -45,6 +45,10 @@ defmodule Indexer.Block.FetcherTest do
Token.Supervisor.Case.start_supervised!(json_rpc_named_arguments: json_rpc_named_arguments)
TokenBalance.Supervisor.Case.start_supervised!(json_rpc_named_arguments: json_rpc_named_arguments)
Uncle.Supervisor.Case.start_supervised!(
block_fetcher: %Fetcher{json_rpc_named_arguments: json_rpc_named_arguments}
)
%{
block_fetcher: %Fetcher{
broadcast: false,

Loading…
Cancel
Save