Add case-insensitive search to importers

pull/2/head
Doc Ritezel 7 years ago
parent 0afe26a004
commit e45c47f177
  1. 13
      lib/explorer/importers/block_importer.ex
  2. 12
      lib/explorer/importers/transaction_importer.ex
  3. 11
      test/explorer/importers/block_importer_test.exs
  4. 11
      test/explorer/importers/transaction_importer_test.exs

@ -1,6 +1,7 @@
defmodule Explorer.BlockImporter do defmodule Explorer.BlockImporter do
@moduledoc "Imports a block." @moduledoc "Imports a block."
import Ecto.Query
import Ethereumex.HttpClient, only: [eth_get_block_by_number: 2] import Ethereumex.HttpClient, only: [eth_get_block_by_number: 2]
alias Explorer.Block alias Explorer.Block
@ -12,14 +13,18 @@ defmodule Explorer.BlockImporter do
raw_block = download_block(block_number) raw_block = download_block(block_number)
changes = extract_block(raw_block) changes = extract_block(raw_block)
block = Repo.get_by(Block, hash: changes.hash) || %Block{} changes.hash |> find() |> Block.changeset(changes) |> Repo.insert_or_update!
block
|> Block.changeset(changes)
|> Repo.insert_or_update!
import_transactions(raw_block["transactions"]) import_transactions(raw_block["transactions"])
end end
def find(hash) do
query = from b in Block,
where: fragment("lower(?)", b.hash) == ^String.downcase(hash),
limit: 1
(query |> Repo.one()) || %Block{}
end
@dialyzer {:nowarn_function, download_block: 1} @dialyzer {:nowarn_function, download_block: 1}
def download_block(block_number) do def download_block(block_number) do
{:ok, block} = eth_get_block_by_number(block_number, true) {:ok, block} = eth_get_block_by_number(block_number, true)

@ -1,6 +1,7 @@
defmodule Explorer.TransactionImporter do defmodule Explorer.TransactionImporter do
@moduledoc "Imports a transaction given a unique hash." @moduledoc "Imports a transaction given a unique hash."
import Ecto.Query
import Ethereumex.HttpClient, only: [eth_get_transaction_by_hash: 1] import Ethereumex.HttpClient, only: [eth_get_transaction_by_hash: 1]
alias Explorer.Address alias Explorer.Address
@ -22,8 +23,8 @@ defmodule Explorer.TransactionImporter do
def persist_transaction(raw_transaction) do def persist_transaction(raw_transaction) do
changes = extract_attrs(raw_transaction) changes = extract_attrs(raw_transaction)
transaction = Repo.get_by(Transaction, hash: changes.hash) || %Transaction{} changes.hash
transaction |> find()
|> Transaction.changeset(changes) |> Transaction.changeset(changes)
|> Repo.insert_or_update! |> Repo.insert_or_update!
|> create_from_address(raw_transaction["from"]) |> create_from_address(raw_transaction["from"])
@ -31,6 +32,13 @@ defmodule Explorer.TransactionImporter do
|> create_block_transaction(raw_transaction["blockHash"]) |> create_block_transaction(raw_transaction["blockHash"])
end end
def find(hash) do
query = from t in Transaction,
where: fragment("lower(?)", t.hash) == ^String.downcase(hash),
limit: 1
(query |> Repo.one()) || %Transaction{}
end
def download_transaction(hash) do def download_transaction(hash) do
{:ok, payload} = eth_get_transaction_by_hash(hash) {:ok, payload} = eth_get_transaction_by_hash(hash)
payload payload

@ -34,6 +34,17 @@ defmodule Explorer.BlockImporterTest do
end end
end end
describe "find/1" do
test "returns an empty block when there is no block with the given hash" do
assert BlockImporter.find("0xC001") == %Block{}
end
test "returns the block with the requested hash" do
block = insert(:block, hash: "0xBEA75")
assert BlockImporter.find("0xBEA75").id == block.id
end
end
describe "download_block/1" do describe "download_block/1" do
test "downloads the block" do test "downloads the block" do
use_cassette "block_importer_download_block_1_downloads_the_block" do use_cassette "block_importer_download_block_1_downloads_the_block" do

@ -126,6 +126,17 @@ defmodule Explorer.TransactionImporterTest do
end end
end end
describe "find/1" do
test "returns an empty transaction when there is no transaction with the given hash" do
assert TransactionImporter.find("0xC001") == %Transaction{}
end
test "returns the transaction with the requested hash" do
transaction = insert(:transaction, hash: "0xBEA75")
assert TransactionImporter.find("0xBEA75").id == transaction.id
end
end
describe "download_transaction/1" do describe "download_transaction/1" do
test "downloads a transaction" do test "downloads a transaction" do
use_cassette "transaction_importer_download_transaction" do use_cassette "transaction_importer_download_transaction" do

Loading…
Cancel
Save