insert emission rewards

pull/2499/head
Ayrat Badykov 5 years ago
parent cee1c66247
commit af2a819547
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 4
      apps/explorer/lib/explorer/chain/block/emission_reward.ex
  2. 20
      apps/explorer/lib/explorer/chain_spec/parity/importer.ex
  3. 25
      apps/explorer/test/explorer/chain_spec/parity/importer_test.exs

@ -3,7 +3,7 @@ defmodule Explorer.Chain.Block.EmissionReward do
Represents the static reward given to the miner of a block in a range of block numbers.
"""
use Ecto.Schema
use Explorer.Schema
alias Explorer.Chain.Block.{EmissionReward, Range}
alias Explorer.Chain.Wei
@ -27,7 +27,7 @@ defmodule Explorer.Chain.Block.EmissionReward do
def changeset(%__MODULE__{} = emission_reward, attrs) do
emission_reward
|> cast(attr, [:block_range, :reward])
|> cast(attrs, [:block_range, :reward])
|> validate_required([:block_range, :reward])
end
end

@ -3,7 +3,17 @@ defmodule Explorer.ChainSpec.Parity.Importer do
Imports data from parity chain spec.
"""
@max_block_number 9_999_999_999_999_999_999
alias Explorer.Repo
alias Explorer.Chain.Block.{EmissionReward, Range}
alias Explorer.Chain.Wei
@max_block_number :infinity
def import_emission_rewards(chain_spec) do
rewards = emission_rewards(chain_spec)
Repo.insert_all(EmissionReward, rewards)
end
def emission_rewards(chain_spec) do
rewards = chain_spec["engine"]["Ethash"]["params"]["blockReward"]
@ -22,15 +32,17 @@ defmodule Explorer.ChainSpec.Parity.Importer do
end
defp create_range([{block_number1, reward}, {block_number2, _}]) do
block_number1 = if block_number1 != 0, do: block_number1 + 1, else: 0
%{
block_range: block_number1..block_number2,
block_range: %Range{from: block_number1, to: block_number2},
reward: reward
}
end
defp create_range([{block_number, reward}]) do
%{
block_range: block_number..@max_block_number,
block_range: %Range{from: block_number + 1, to: @max_block_number},
reward: reward
}
end
@ -38,7 +50,7 @@ defmodule Explorer.ChainSpec.Parity.Importer do
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)
{:ok, reward} = hex_reward |> parse_hex_number() |> Wei.cast()
{block_number, reward}
end)

@ -1,6 +1,8 @@
defmodule Explorer.ChainSpec.Parity.ImporterTest do
use ExUnit.Case
use Explorer.DataCase
alias Explorer.Chain.Block.Range
alias Explorer.Chain.Wei
alias Explorer.ChainSpec.Parity.Importer
@chain_spec "#{File.cwd!()}/test/support/fixture/chain_spec/foundation.json"
@ -10,10 +12,25 @@ defmodule Explorer.ChainSpec.Parity.ImporterTest do
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}
%{
block_range: %Range{from: 0, to: 4_370_000},
reward: %Wei{value: Decimal.new(5_000_000_000_000_000_000)}
},
%{
block_range: %Range{from: 4_370_001, to: 7_280_000},
reward: %Wei{value: Decimal.new(3_000_000_000_000_000_000)}
},
%{
block_range: %Range{from: 7_280_001, to: :infinity},
reward: %Wei{value: Decimal.new(2_000_000_000_000_000_000)}
}
]
end
end
describe "import_emission_rewards/1" do
test "inserts emission rewards from chain spec" do
assert {3, nil} = Importer.import_emission_rewards(@chain_spec)
end
end
end

Loading…
Cancel
Save