From af8b92aa0dd191768d3f1205760857828ad62a03 Mon Sep 17 00:00:00 2001 From: Gustavo Santos Ferreira Date: Fri, 21 Dec 2018 11:37:40 -0200 Subject: [PATCH] new queries for blocks and transactions --- apps/explorer/lib/explorer/chain.ex | 18 +++++++++++++ apps/explorer/lib/explorer/chain/block.ex | 9 +++++++ .../lib/explorer/chain/transaction.ex | 10 +++++++ .../test/explorer/chain/block_test.exs | 16 +++++++++++ .../test/explorer/chain/transaction_test.exs | 27 +++++++++++++++++++ 5 files changed, 80 insertions(+) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index aa389cbde0..d293275b69 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -936,6 +936,24 @@ defmodule Explorer.Chain do Repo.all(query) 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 """ Finds all Blocks validated by the address given. diff --git a/apps/explorer/lib/explorer/chain/block.ex b/apps/explorer/lib/explorer/chain/block.ex index f55f83b7df..1886259e50 100644 --- a/apps/explorer/lib/explorer/chain/block.ex +++ b/apps/explorer/lib/explorer/chain/block.ex @@ -100,6 +100,15 @@ defmodule Explorer.Chain.Block do |> unique_constraint(:hash, name: :blocks_pkey) 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 """ Adds to the given block's query a `where` with conditions to filter by the type of block; `Uncle`, `Reorg`, or `Block`. diff --git a/apps/explorer/lib/explorer/chain/transaction.ex b/apps/explorer/lib/explorer/chain/transaction.ex index 19a80baa27..bc549fdd29 100644 --- a/apps/explorer/lib/explorer/chain/transaction.ex +++ b/apps/explorer/lib/explorer/chain/transaction.ex @@ -596,6 +596,16 @@ defmodule Explorer.Chain.Transaction do ) 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 """ Builds an `Ecto.Query` to fetch the last nonce from the given address hash. diff --git a/apps/explorer/test/explorer/chain/block_test.exs b/apps/explorer/test/explorer/chain/block_test.exs index 5f6bfc426e..81268a6d27 100644 --- a/apps/explorer/test/explorer/chain/block_test.exs +++ b/apps/explorer/test/explorer/chain/block_test.exs @@ -42,4 +42,20 @@ defmodule Explorer.Chain.BlockTest do |> Repo.insert() 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 diff --git a/apps/explorer/test/explorer/chain/transaction_test.exs b/apps/explorer/test/explorer/chain/transaction_test.exs index df82de13ff..b12a3b5c76 100644 --- a/apps/explorer/test/explorer/chain/transaction_test.exs +++ b/apps/explorer/test/explorer/chain/transaction_test.exs @@ -178,6 +178,33 @@ defmodule Explorer.Chain.TransactionTest do 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 test "returns the nonce value from the last block" do address = insert(:address)