Use validation logic from Block.changeset in Fetcher

pull/2/head
Doc Ritezel 7 years ago
parent 7b2afeae34
commit 5035aac6eb
  1. 10
      lib/explorer/fetcher.ex
  2. 33
      test/explorer/fetcher_test.exs

@ -5,8 +5,8 @@ defmodule Explorer.Fetcher do
@moduledoc false
def fetch(block) do
block
def fetch(block_number) do
block_number
|> download_block
|> extract_block
|> validate_block
@ -19,7 +19,7 @@ defmodule Explorer.Fetcher do
end
def extract_block(block) do
%Block{
%{
hash: block["hash"],
number: block["number"] |> decode_integer_field,
gas_used: block["gasUsed"] |> decode_integer_field,
@ -34,8 +34,8 @@ defmodule Explorer.Fetcher do
}
end
def validate_block(struct) do
Block.changeset(struct, %{})
def validate_block(attrs) do
Block.changeset(%Block{}, attrs)
end
def decode_integer_field(hex) do

@ -30,23 +30,22 @@ defmodule Explorer.FetcherTest do
end
describe "extract_block/1" do
def raw_block(nonce \\ %{}) do
Map.merge(%{
"difficulty" => "0xfffffffffffffffffffffffffffffffe",
"gasLimit" => "0x02",
"gasUsed" => "0x19522",
"hash" => "bananas",
"miner" => "0xdb1207770e0a4258d7a4ce49ab037f92564fea85",
"number" => "0x7f2fb",
"parentHash" => "0x70029f66ea5a3b2b1ede95079d95a2ab74b649b5b17cdcf6f29b6317e7c7efa6",
"size" => "0x10",
"timestamp" => "0x12",
"totalDifficulty" => "0xff",
}, nonce)
end
@raw_block %{
"difficulty" => "0xfffffffffffffffffffffffffffffffe",
"gasLimit" => "0x02",
"gasUsed" => "0x19522",
"hash" => "bananas",
"miner" => "0xdb1207770e0a4258d7a4ce49ab037f92564fea85",
"number" => "0x7f2fb",
"parentHash" => "0x70029f66ea5a3b2b1ede95079d95a2ab74b649b5b17cdcf6f29b6317e7c7efa6",
"size" => "0x10",
"timestamp" => "0x12",
"totalDifficulty" => "0xff",
"nonce" => nil
}
test "returns the struct of a block" do
processed_block = %Block{
processed_block = %{
difficulty: 340282366920938463463374607431768211454,
gas_limit: 2,
gas_used: 103714,
@ -59,11 +58,11 @@ defmodule Explorer.FetcherTest do
timestamp: Timex.parse!("1970-01-01T00:00:18-00:00", "{ISO:Extended}"),
total_difficulty: 255,
}
assert Fetcher.extract_block(raw_block(%{"nonce" => "0xfb6e1a62d119228b"})) == processed_block
assert Fetcher.extract_block(%{@raw_block | "nonce" => "0xfb6e1a62d119228b"}) == processed_block
end
test "when there is no nonce" do
assert Fetcher.extract_block(raw_block()).nonce == "0"
assert Fetcher.extract_block(@raw_block).nonce == "0"
end
end

Loading…
Cancel
Save