From e45c47f177c60a803ffcf15549b049ba807c3ecf Mon Sep 17 00:00:00 2001 From: Doc Ritezel Date: Wed, 7 Feb 2018 16:53:48 -0800 Subject: [PATCH] Add case-insensitive search to importers --- lib/explorer/importers/block_importer.ex | 13 +++++++++---- lib/explorer/importers/transaction_importer.ex | 12 ++++++++++-- test/explorer/importers/block_importer_test.exs | 11 +++++++++++ .../importers/transaction_importer_test.exs | 11 +++++++++++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/lib/explorer/importers/block_importer.ex b/lib/explorer/importers/block_importer.ex index 09063f7c62..5ac1a63e41 100644 --- a/lib/explorer/importers/block_importer.ex +++ b/lib/explorer/importers/block_importer.ex @@ -1,6 +1,7 @@ defmodule Explorer.BlockImporter do @moduledoc "Imports a block." + import Ecto.Query import Ethereumex.HttpClient, only: [eth_get_block_by_number: 2] alias Explorer.Block @@ -12,14 +13,18 @@ defmodule Explorer.BlockImporter do raw_block = download_block(block_number) changes = extract_block(raw_block) - block = Repo.get_by(Block, hash: changes.hash) || %Block{} - block - |> Block.changeset(changes) - |> Repo.insert_or_update! + changes.hash |> find() |> Block.changeset(changes) |> Repo.insert_or_update! import_transactions(raw_block["transactions"]) 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} def download_block(block_number) do {:ok, block} = eth_get_block_by_number(block_number, true) diff --git a/lib/explorer/importers/transaction_importer.ex b/lib/explorer/importers/transaction_importer.ex index 4b826c230d..76268bf23f 100644 --- a/lib/explorer/importers/transaction_importer.ex +++ b/lib/explorer/importers/transaction_importer.ex @@ -1,6 +1,7 @@ defmodule Explorer.TransactionImporter do @moduledoc "Imports a transaction given a unique hash." + import Ecto.Query import Ethereumex.HttpClient, only: [eth_get_transaction_by_hash: 1] alias Explorer.Address @@ -22,8 +23,8 @@ defmodule Explorer.TransactionImporter do def persist_transaction(raw_transaction) do changes = extract_attrs(raw_transaction) - transaction = Repo.get_by(Transaction, hash: changes.hash) || %Transaction{} - transaction + changes.hash + |> find() |> Transaction.changeset(changes) |> Repo.insert_or_update! |> create_from_address(raw_transaction["from"]) @@ -31,6 +32,13 @@ defmodule Explorer.TransactionImporter do |> create_block_transaction(raw_transaction["blockHash"]) 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 {:ok, payload} = eth_get_transaction_by_hash(hash) payload diff --git a/test/explorer/importers/block_importer_test.exs b/test/explorer/importers/block_importer_test.exs index baa8970352..dc6b925d14 100644 --- a/test/explorer/importers/block_importer_test.exs +++ b/test/explorer/importers/block_importer_test.exs @@ -34,6 +34,17 @@ defmodule Explorer.BlockImporterTest do 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 test "downloads the block" do use_cassette "block_importer_download_block_1_downloads_the_block" do diff --git a/test/explorer/importers/transaction_importer_test.exs b/test/explorer/importers/transaction_importer_test.exs index 4a284d1d28..7a689d5d48 100644 --- a/test/explorer/importers/transaction_importer_test.exs +++ b/test/explorer/importers/transaction_importer_test.exs @@ -126,6 +126,17 @@ defmodule Explorer.TransactionImporterTest do 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 test "downloads a transaction" do use_cassette "transaction_importer_download_transaction" do