parse genesis balances

pull/2499/head
Ayrat Badykov 5 years ago
parent 6e0a1a649b
commit 44bade02f4
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 26
      apps/explorer/lib/explorer/chain_spec/parity/importer.ex
  2. 19
      apps/explorer/test/explorer/chain_spec/parity/importer_test.exs

@ -3,9 +3,10 @@ defmodule Explorer.ChainSpec.Parity.Importer do
Imports data from parity chain spec.
"""
alias Explorer.Repo
alias Explorer.Chain
alias Explorer.Chain.Block.{EmissionReward, Range}
alias Explorer.Chain.Wei
alias Explorer.Chain.Hash.Address, as: AddressHash
alias Explorer.Chain.{Repo, Wei}
@max_block_number :infinity
@ -16,6 +17,13 @@ defmodule Explorer.ChainSpec.Parity.Importer do
{_, nil} = Repo.insert_all(EmissionReward, rewards)
end
def genesis_coin_balances(chain_spec) do
accounts = chain_spec["accounts"]
parse_accounts(accounts)
end
def emission_rewards(chain_spec) do
rewards = chain_spec["engine"]["Ethash"]["params"]["blockReward"]
@ -24,6 +32,20 @@ defmodule Explorer.ChainSpec.Parity.Importer do
|> format_ranges()
end
defp parse_accounts(accounts) do
accounts
|> Stream.filter(fn {_address, map} ->
!is_nil(map["balance"])
end)
|> Stream.map(fn {address, %{"balance" => value}} ->
{:ok, address_hash} = AddressHash.cast(address)
balance = parse_hex_number(value)
%{address_hash: address_hash, value: balance}
end)
|> Enum.to_list()
end
defp format_ranges(block_number_reward_pairs) do
block_number_reward_pairs
|> Enum.chunk_every(2, 1)

@ -3,7 +3,7 @@ defmodule Explorer.ChainSpec.Parity.ImporterTest do
alias Explorer.Chain.Block.EmissionReward
alias Explorer.Chain.Block.Range
alias Explorer.Chain.Wei
alias Explorer.Chain.{Hash, Wei}
alias Explorer.ChainSpec.Parity.Importer
alias Explorer.Repo
@ -78,4 +78,21 @@ defmodule Explorer.ChainSpec.Parity.ImporterTest do
assert new_third.block_range == %Range{from: 7_280_001, to: :infinity}
end
end
describe "genesis_coin_balances/1" do
test "parses coin balance" do
coin_balances = Importer.genesis_coin_balances(@chain_spec)
assert Enum.count(coin_balances) == 403
assert %{
address_hash: %Hash{
byte_count: 20,
bytes: <<121, 174, 179, 69, 102, 185, 116, 195, 90, 88, 129, 222, 192, 32, 146, 125, 167, 223, 93, 37>>
},
balance: 2_000_000_000_000_000_000_000
} ==
List.first(coin_balances)
end
end
end

Loading…
Cancel
Save