new queries for blocks and transactions

pull/1269/head
Gustavo Santos Ferreira 6 years ago
parent f139317f88
commit af8b92aa0d
  1. 18
      apps/explorer/lib/explorer/chain.ex
  2. 9
      apps/explorer/lib/explorer/chain/block.ex
  3. 10
      apps/explorer/lib/explorer/chain/transaction.ex
  4. 16
      apps/explorer/test/explorer/chain/block_test.exs
  5. 27
      apps/explorer/test/explorer/chain/transaction_test.exs

@ -936,6 +936,24 @@ defmodule Explorer.Chain do
Repo.all(query) Repo.all(query)
end end
@doc """
Finds blocks without a reward associated, up to the specified limit
"""
def get_blocks_without_reward(limit \\ 250) do
Block.get_blocks_without_reward()
|> limit(^limit)
|> Repo.all()
end
@doc """
Finds all transactions of a certain block number
"""
def get_transactions_of_block_number(block_number) do
block_number
|> Transaction.transactions_with_block_number()
|> Repo.all()
end
@doc """ @doc """
Finds all Blocks validated by the address given. Finds all Blocks validated by the address given.

@ -100,6 +100,15 @@ defmodule Explorer.Chain.Block do
|> unique_constraint(:hash, name: :blocks_pkey) |> unique_constraint(:hash, name: :blocks_pkey)
end end
def get_blocks_without_reward(query \\ __MODULE__) do
from(
b in query,
left_join: r in Reward,
on: [block_hash: b.hash],
where: is_nil(r.block_hash)
)
end
@doc """ @doc """
Adds to the given block's query a `where` with conditions to filter by the type of block; Adds to the given block's query a `where` with conditions to filter by the type of block;
`Uncle`, `Reorg`, or `Block`. `Uncle`, `Reorg`, or `Block`.

@ -596,6 +596,16 @@ defmodule Explorer.Chain.Transaction do
) )
end end
@doc """
Builds an `Ecto.Query` to fetch transactions with the specified block_number
"""
def transactions_with_block_number(block_number) do
from(
t in Transaction,
where: t.block_number == ^block_number
)
end
@doc """ @doc """
Builds an `Ecto.Query` to fetch the last nonce from the given address hash. Builds an `Ecto.Query` to fetch the last nonce from the given address hash.

@ -42,4 +42,20 @@ defmodule Explorer.Chain.BlockTest do
|> Repo.insert() |> Repo.insert()
end end
end end
describe "get_blocks_without_reward/1" do
test "finds only blocks without rewards" do
rewarded_block = insert(:block)
insert(:reward, address_hash: insert(:address).hash, block_hash: rewarded_block.hash)
unrewarded_block = insert(:block)
results =
Block.get_blocks_without_reward()
|> Repo.all()
|> Enum.map(& &1.hash)
refute Enum.member?(results, rewarded_block.hash)
assert Enum.member?(results, unrewarded_block.hash)
end
end
end end

@ -178,6 +178,33 @@ defmodule Explorer.Chain.TransactionTest do
end end
end end
describe "transaction_hash_to_block_number/1" do
test "returns only transactions with the specified block number" do
target_block = insert(:block, number: 1_000_000)
:transaction
|> insert()
|> with_block(target_block)
:transaction
|> insert()
|> with_block(target_block)
:transaction
|> insert()
|> with_block(insert(:block, number: 1_001_101))
result =
1_000_000
|> Transaction.transactions_with_block_number()
|> Repo.all()
|> Enum.map(& &1.block_number)
refute Enum.any?(result, fn block_number -> 1_001_101 == block_number end)
assert Enum.all?(result, fn block_number -> 1_000_000 == block_number end)
end
end
describe "last_nonce_by_address_query/1" do describe "last_nonce_by_address_query/1" do
test "returns the nonce value from the last block" do test "returns the nonce value from the last block" do
address = insert(:address) address = insert(:address)

Loading…
Cancel
Save