From 44bade02f4bf169c7a99b05cf3a180a0b4157265 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 5 Aug 2019 13:53:48 +0300 Subject: [PATCH] parse genesis balances --- .../explorer/chain_spec/parity/importer.ex | 26 +++++++++++++++++-- .../chain_spec/parity/importer_test.exs | 19 +++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/apps/explorer/lib/explorer/chain_spec/parity/importer.ex b/apps/explorer/lib/explorer/chain_spec/parity/importer.ex index 92b63f4c42..2c478f663c 100644 --- a/apps/explorer/lib/explorer/chain_spec/parity/importer.ex +++ b/apps/explorer/lib/explorer/chain_spec/parity/importer.ex @@ -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) diff --git a/apps/explorer/test/explorer/chain_spec/parity/importer_test.exs b/apps/explorer/test/explorer/chain_spec/parity/importer_test.exs index 2fb98f64dc..2f9be087af 100644 --- a/apps/explorer/test/explorer/chain_spec/parity/importer_test.exs +++ b/apps/explorer/test/explorer/chain_spec/parity/importer_test.exs @@ -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