Fix import spec file to support accounts without 0x prefix and single block reward

pull/2990/head
Victor Baranov 5 years ago
parent c450a88a01
commit f9318c13ca
  1. 4
      CHANGELOG.md
  2. 11
      apps/explorer/lib/explorer/chain_spec/parity/importer.ex
  3. 22
      apps/explorer/test/explorer/chain_spec/parity/importer_test.exs
  4. 31157
      apps/explorer/test/support/fixture/chain_spec/classic.json

@ -3,8 +3,8 @@
### Features
### Fixes
- [#2897]( https://github.com/poanetwork/blockscout/pull/2897) - remove duplicate indexes
- [#2990](https://github.com/poanetwork/blockscout/pull/2990) - Fix import of Parity spec file
- [#2897](https://github.com/poanetwork/blockscout/pull/2897) - remove duplicate indexes
### Chore

@ -103,7 +103,8 @@ defmodule Explorer.ChainSpec.Parity.Importer do
!is_nil(map["balance"])
end)
|> Stream.map(fn {address, %{"balance" => value}} ->
{:ok, address_hash} = AddressHash.cast(address)
formatted_address = if String.starts_with?(address, "0x"), do: address, else: "0x" <> address
{:ok, address_hash} = AddressHash.cast(formatted_address)
balance = parse_number(value)
%{address_hash: address_hash, value: balance}
@ -135,7 +136,7 @@ defmodule Explorer.ChainSpec.Parity.Importer do
}
end
defp parse_hex_numbers(rewards) do
defp parse_hex_numbers(rewards) when is_map(rewards) do
Enum.map(rewards, fn {hex_block_number, hex_reward} ->
block_number = parse_number(hex_block_number)
{:ok, reward} = hex_reward |> parse_number() |> Wei.cast()
@ -144,6 +145,12 @@ defmodule Explorer.ChainSpec.Parity.Importer do
end)
end
defp parse_hex_numbers(reward) do
{:ok, reward} = reward |> parse_number() |> Wei.cast()
[{0, reward}]
end
defp parse_number("0x" <> hex_number) do
{number, ""} = Integer.parse(hex_number, 16)

@ -11,6 +11,10 @@ defmodule Explorer.ChainSpec.Parity.ImporterTest do
|> File.read!()
|> Jason.decode!()
@chain_classic_spec "#{File.cwd!()}/test/support/fixture/chain_spec/classic.json"
|> File.read!()
|> Jason.decode!()
describe "emission_rewards/1" do
test "fetches and formats reward ranges" do
assert Importer.emission_rewards(@chain_spec) == [
@ -28,6 +32,15 @@ defmodule Explorer.ChainSpec.Parity.ImporterTest do
}
]
end
test "fetches and formats a single reward" do
assert Importer.emission_rewards(@chain_classic_spec) == [
%{
block_range: %Range{from: 1, to: :infinity},
reward: %Wei{value: Decimal.new(5_000_000_000_000_000_000)}
}
]
end
end
describe "import_emission_rewards/1" do
@ -104,5 +117,14 @@ defmodule Explorer.ChainSpec.Parity.ImporterTest do
assert CoinBalance |> Repo.all() |> Enum.count() == 403
assert Address |> Repo.all() |> Enum.count() == 403
end
test "imports coin balances without 0x" do
{:ok, %{address_coin_balances: address_coin_balances}} =
Importer.import_genesis_coin_balances(@chain_classic_spec)
assert Enum.count(address_coin_balances) == 8894
assert CoinBalance |> Repo.all() |> Enum.count() == 8894
assert Address |> Repo.all() |> Enum.count() == 8894
end
end
end

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save