Allow the latest block worker to re-enqueue an actual block number

pull/42/head
Doc Ritezel 7 years ago
parent 10595c0ea2
commit 1abbf61404
  1. 19
      lib/explorer/workers/import_block.ex
  2. 12
      test/explorer/workers/import_block_test.exs
  3. 30
      test/support/fixture/vcr_cassettes/import_block_perform_1_latest.json

@ -1,16 +1,23 @@
defmodule Explorer.Workers.ImportBlock do defmodule Explorer.Workers.ImportBlock do
@moduledoc "Imports blocks by web3 conventions."
import Ethereumex.HttpClient, only: [eth_block_number: 0]
alias Explorer.BlockImporter 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} @dialyzer {:nowarn_function, perform: 1}
def perform(number), do: BlockImporter.import("#{number}") def perform(number), do: BlockImporter.import("#{number}")
@dialyzer {:nowarn_function, perform: 0} def perform_later("0x" <> number) when is_binary(number) do
def perform, do: perform("latest") number |> String.to_integer(16) |> perform_later()
def perform_later("latest") do
Exq.enqueue(Exq.Enqueuer, "blocks", __MODULE__, ["latest"], max_retries: 0)
end end
def perform_later(number) do def perform_later(number) do

@ -32,6 +32,16 @@ defmodule Explorer.Workers.ImportBlockTest do
end end
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 test "when there is already a block with the requested hash" do
use_cassette "import_block_perform_1_duplicate" do use_cassette "import_block_perform_1_duplicate" do
insert(:block, hash: "0x52c867bc0a91e573dc39300143c3bead7408d09d45bdb686749f02684ece72f3") insert(:block, hash: "0x52c867bc0a91e573dc39300143c3bead7408d09d45bdb686749f02684ece72f3")
@ -45,7 +55,7 @@ defmodule Explorer.Workers.ImportBlockTest do
describe "perform_later/1" do describe "perform_later/1" do
test "does not retry fetching the latest block" do test "does not retry fetching the latest block" do
use_cassette "import_block_perform_later_1_latest" 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") ImportBlock.perform_later("latest")
last_block = Block |> order_by(asc: :number) |> limit(1) |> Repo.one last_block = Block |> order_by(asc: :number) |> limit(1) |> Repo.one
assert last_block.number == 1 assert last_block.number == 1

@ -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"
}
}
]
Loading…
Cancel
Save