Allow ImportTransaction worker to scrape receipts for prefetched transactions

pull/42/head
Doc Ritezel 7 years ago
parent b08b445122
commit 17129ef302
  1. 6
      lib/explorer/importers/transaction_importer.ex
  2. 8
      lib/explorer/workers/import_transaction.ex
  3. 16
      test/explorer/workers/import_transaction_test.exs

@ -21,13 +21,13 @@ defmodule Explorer.TransactionImporter do
end
def persist_transaction(raw_transaction) do
changes = extract_attrs(raw_transaction)
found_transaction = changes.hash |> find()
found_transaction = raw_transaction["hash"] |> find()
transaction = case is_nil(found_transaction.id) do
false -> found_transaction
true ->
changes = extract_attrs(raw_transaction)
found_transaction |> Transaction.changeset(changes) |> Repo.insert!
false -> found_transaction
end
to_address = raw_transaction["to"] || raw_transaction["creates"]

@ -7,11 +7,17 @@ defmodule Explorer.Workers.ImportTransaction do
alias Explorer.Workers.ImportReceipt
@dialyzer {:nowarn_function, perform: 1}
def perform(hash) do
def perform(hash) when is_binary(hash) do
TransactionImporter.import(hash)
ImportReceipt.perform_later(hash)
end
@dialyzer {:nowarn_function, perform: 1}
def perform(raw_transaction) when is_map(raw_transaction) do
TransactionImporter.import(raw_transaction)
ImportReceipt.perform_later(raw_transaction["hash"])
end
def perform_later(hash) do
Exq.enqueue(Exq.Enqueuer, "transactions", __MODULE__, [hash])
end

@ -40,6 +40,22 @@ defmodule Explorer.Workers.ImportTransactionTest do
end
end
end
test "imports the receipt in another queue when a map is supplied" do
transaction = insert(:transaction, hash: "0xf9a0959d5ccde33ec5221ddba1c6d7eaf9580a8d3512c7a1a60301362a98f926")
use_cassette "import_transaction_perform_1" do
with_mock Exq, [enqueue: fn (_, _, _, _) -> insert(:receipt, transaction: transaction) end] do
ImportTransaction.perform(%{
"hash" => "0xf9a0959d5ccde33ec5221ddba1c6d7eaf9580a8d3512c7a1a60301362a98f926",
"to" => "0xc001",
"from" => "0xbead5",
"blockHash" => "0xcafe",
})
receipt = Repo.one(Receipt)
refute is_nil(receipt)
end
end
end
end
describe "perform_later/1" do

Loading…
Cancel
Save