parse emission reward ranges

pull/2499/head
Ayrat Badykov 5 years ago
parent 27f32f7985
commit cee1c66247
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 6
      apps/explorer/lib/explorer/chain/block/emission_reward.ex
  2. 52
      apps/explorer/lib/explorer/chain_spec/parity/importer.ex
  3. 19
      apps/explorer/test/explorer/chain_spec/parity/importer_test.exs
  4. 30636
      apps/explorer/test/support/fixture/chain_spec/foundation.json

@ -24,4 +24,10 @@ defmodule Explorer.Chain.Block.EmissionReward do
field(:block_range, Range)
field(:reward, Wei)
end
def changeset(%__MODULE__{} = emission_reward, attrs) do
emission_reward
|> cast(attr, [:block_range, :reward])
|> validate_required([:block_range, :reward])
end
end

@ -0,0 +1,52 @@
defmodule Explorer.ChainSpec.Parity.Importer do
@moduledoc """
Imports data from parity chain spec.
"""
@max_block_number 9_999_999_999_999_999_999
def emission_rewards(chain_spec) do
rewards = chain_spec["engine"]["Ethash"]["params"]["blockReward"]
rewards
|> parse_hex_numbers()
|> format_ranges()
end
defp format_ranges(block_number_reward_pairs) do
block_number_reward_pairs
|> Enum.chunk_every(2, 1)
|> Enum.map(fn values ->
create_range(values)
end)
end
defp create_range([{block_number1, reward}, {block_number2, _}]) do
%{
block_range: block_number1..block_number2,
reward: reward
}
end
defp create_range([{block_number, reward}]) do
%{
block_range: block_number..@max_block_number,
reward: reward
}
end
defp parse_hex_numbers(rewards) do
Enum.map(rewards, fn {hex_block_number, hex_reward} ->
block_number = parse_hex_number(hex_block_number)
reward = parse_hex_number(hex_reward)
{block_number, reward}
end)
end
defp parse_hex_number("0x" <> hex_number) do
{number, ""} = Integer.parse(hex_number, 16)
number
end
end

@ -0,0 +1,19 @@
defmodule Explorer.ChainSpec.Parity.ImporterTest do
use ExUnit.Case
alias Explorer.ChainSpec.Parity.Importer
@chain_spec "#{File.cwd!()}/test/support/fixture/chain_spec/foundation.json"
|> File.read!()
|> Jason.decode!()
describe "emission_rewards/1" do
test "fetches and formats reward ranges" do
assert Importer.emission_rewards(@chain_spec) == [
%{block_range: 0..4_370_000, reward: 5_000_000_000_000_000_000},
%{block_range: 4_370_000..7_280_000, reward: 3_000_000_000_000_000_000},
%{block_range: 7_280_000..9_999_999_999_999_999_999, reward: 2_000_000_000_000_000_000}
]
end
end
end

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