rewrite emission rewards records

pull/2499/head
Ayrat Badykov 5 years ago
parent 40e6fd76ca
commit d290936e11
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 3
      apps/explorer/lib/explorer/chain_spec/parity/importer.ex
  2. 45
      apps/explorer/test/explorer/chain_spec/parity/importer_test.exs

@ -12,7 +12,8 @@ defmodule Explorer.ChainSpec.Parity.Importer do
def import_emission_rewards(chain_spec) do def import_emission_rewards(chain_spec) do
rewards = emission_rewards(chain_spec) rewards = emission_rewards(chain_spec)
Repo.insert_all(EmissionReward, rewards) {_, nil} = Repo.delete_all(EmissionReward)
{_, nil} = Repo.insert_all(EmissionReward, rewards)
end end
def emission_rewards(chain_spec) do def emission_rewards(chain_spec) do

@ -1,9 +1,11 @@
defmodule Explorer.ChainSpec.Parity.ImporterTest do defmodule Explorer.ChainSpec.Parity.ImporterTest do
use Explorer.DataCase use Explorer.DataCase
alias Explorer.Chain.Block.EmissionReward
alias Explorer.Chain.Block.Range alias Explorer.Chain.Block.Range
alias Explorer.Chain.Wei alias Explorer.Chain.Wei
alias Explorer.ChainSpec.Parity.Importer alias Explorer.ChainSpec.Parity.Importer
alias Explorer.Repo
@chain_spec "#{File.cwd!()}/test/support/fixture/chain_spec/foundation.json" @chain_spec "#{File.cwd!()}/test/support/fixture/chain_spec/foundation.json"
|> File.read!() |> File.read!()
@ -32,5 +34,48 @@ defmodule Explorer.ChainSpec.Parity.ImporterTest do
test "inserts emission rewards from chain spec" do test "inserts emission rewards from chain spec" do
assert {3, nil} = Importer.import_emission_rewards(@chain_spec) assert {3, nil} = Importer.import_emission_rewards(@chain_spec)
end end
test "rewrites all recored" do
old_block_rewards = %{
"0x0" => "0x1bc16d674ec80000",
"0x42ae50" => "0x29a2241af62c0000",
"0x6f1580" => "0x4563918244f40000"
}
chain_spec = %{
@chain_spec
| "engine" => %{
@chain_spec["engine"]
| "Ethash" => %{
@chain_spec["engine"]["Ethash"]
| "params" => %{@chain_spec["engine"]["Ethash"]["params"] | "blockReward" => old_block_rewards}
}
}
}
assert {3, nil} = Importer.import_emission_rewards(chain_spec)
[first, second, third] = Repo.all(EmissionReward)
assert first.reward == %Wei{value: Decimal.new(2_000_000_000_000_000_000)}
assert first.block_range == %Range{from: 0, to: 4_370_000}
assert second.reward == %Wei{value: Decimal.new(3_000_000_000_000_000_000)}
assert second.block_range == %Range{from: 4_370_001, to: 7_280_000}
assert third.reward == %Wei{value: Decimal.new(5_000_000_000_000_000_000)}
assert third.block_range == %Range{from: 7_280_001, to: :infinity}
assert {3, nil} = Importer.import_emission_rewards(@chain_spec)
[new_first, new_second, new_third] = Repo.all(EmissionReward)
assert new_first.reward == %Wei{value: Decimal.new(5_000_000_000_000_000_000)}
assert new_first.block_range == %Range{from: 0, to: 4_370_000}
assert new_second.reward == %Wei{value: Decimal.new(3_000_000_000_000_000_000)}
assert new_second.block_range == %Range{from: 4_370_001, to: 7_280_000}
assert new_third.reward == %Wei{value: Decimal.new(2_000_000_000_000_000_000)}
assert new_third.block_range == %Range{from: 7_280_001, to: :infinity}
end
end end
end end

Loading…
Cancel
Save