From 1abbf61404c8df2306db12483982feb367a948ac Mon Sep 17 00:00:00 2001 From: Doc Ritezel Date: Thu, 15 Feb 2018 22:29:40 -0800 Subject: [PATCH] Allow the latest block worker to re-enqueue an actual block number --- lib/explorer/workers/import_block.ex | 19 ++++++++---- test/explorer/workers/import_block_test.exs | 12 +++++++- .../import_block_perform_1_latest.json | 30 +++++++++++++++++++ 3 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 test/support/fixture/vcr_cassettes/import_block_perform_1_latest.json diff --git a/lib/explorer/workers/import_block.ex b/lib/explorer/workers/import_block.ex index 2019727b66..e515975a3b 100644 --- a/lib/explorer/workers/import_block.ex +++ b/lib/explorer/workers/import_block.ex @@ -1,16 +1,23 @@ defmodule Explorer.Workers.ImportBlock do + @moduledoc "Imports blocks by web3 conventions." + + import Ethereumex.HttpClient, only: [eth_block_number: 0] + alias Explorer.BlockImporter - @moduledoc "Imports blocks by web3 conventions." + @dialyzer {:nowarn_function, perform: 1} + def perform("latest") do + case eth_block_number() do + {:ok, number} -> perform_later(number) + _ -> nil + end + end @dialyzer {:nowarn_function, perform: 1} def perform(number), do: BlockImporter.import("#{number}") - @dialyzer {:nowarn_function, perform: 0} - def perform, do: perform("latest") - - def perform_later("latest") do - Exq.enqueue(Exq.Enqueuer, "blocks", __MODULE__, ["latest"], max_retries: 0) + def perform_later("0x" <> number) when is_binary(number) do + number |> String.to_integer(16) |> perform_later() end def perform_later(number) do diff --git a/test/explorer/workers/import_block_test.exs b/test/explorer/workers/import_block_test.exs index 674925babc..4d77e8d039 100644 --- a/test/explorer/workers/import_block_test.exs +++ b/test/explorer/workers/import_block_test.exs @@ -32,6 +32,16 @@ defmodule Explorer.Workers.ImportBlockTest do end end + test "imports the latest block" do + use_cassette "import_block_perform_1_latest" do + with_mock Exq, [enqueue: fn (_, _, _, [number]) -> insert(:block, number: number) end] do + ImportBlock.perform("latest") + last_block = Block |> order_by(asc: :number) |> Repo.one + assert last_block.number > 0 + end + end + end + test "when there is already a block with the requested hash" do use_cassette "import_block_perform_1_duplicate" do insert(:block, hash: "0x52c867bc0a91e573dc39300143c3bead7408d09d45bdb686749f02684ece72f3") @@ -45,7 +55,7 @@ defmodule Explorer.Workers.ImportBlockTest do describe "perform_later/1" do test "does not retry fetching the latest block" do use_cassette "import_block_perform_later_1_latest" do - with_mock Exq, [enqueue: fn (_, _, _, _, max_retries: 0) -> insert(:block, number: 1) end] do + with_mock Exq, [enqueue: fn (_, _, _, _) -> insert(:block, number: 1) end] do ImportBlock.perform_later("latest") last_block = Block |> order_by(asc: :number) |> limit(1) |> Repo.one assert last_block.number == 1 diff --git a/test/support/fixture/vcr_cassettes/import_block_perform_1_latest.json b/test/support/fixture/vcr_cassettes/import_block_perform_1_latest.json new file mode 100644 index 0000000000..5f24eb3c4e --- /dev/null +++ b/test/support/fixture/vcr_cassettes/import_block_perform_1_latest.json @@ -0,0 +1,30 @@ +[ + { + "request": { + "body": "{\"params\":[],\"method\":\"eth_blockNumber\",\"jsonrpc\":\"2.0\",\"id\":4}", + "headers": { + "Content-Type": "application/json" + }, + "method": "post", + "options": [], + "request_body": "", + "url": "https://sokol.poa.network" + }, + "response": { + "binary": false, + "body": "{\"jsonrpc\":\"2.0\",\"result\":\"0xead0d\",\"id\":4}\n", + "headers": { + "Date": "Fri, 16 Feb 2018 06:27:57 GMT", + "Content-Type": "application/json", + "Transfer-Encoding": "chunked", + "Connection": "keep-alive", + "Set-Cookie": "__cfduid=de29d41d050c846681bac8973c50f5aa51518762476; expires=Sat, 16-Feb-19 06:27:56 GMT; path=/; domain=.poa.network; HttpOnly; Secure", + "Expect-CT": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"", + "Server": "cloudflare", + "CF-RAY": "3ede71a76b58962b-SJC" + }, + "status_code": 200, + "type": "ok" + } + } +] \ No newline at end of file