From d290936e117d14f07df854cc880290a4735498e5 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 5 Aug 2019 12:25:29 +0300 Subject: [PATCH] rewrite emission rewards records --- .../explorer/chain_spec/parity/importer.ex | 3 +- .../chain_spec/parity/importer_test.exs | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain_spec/parity/importer.ex b/apps/explorer/lib/explorer/chain_spec/parity/importer.ex index df4a6a0f68..92b63f4c42 100644 --- a/apps/explorer/lib/explorer/chain_spec/parity/importer.ex +++ b/apps/explorer/lib/explorer/chain_spec/parity/importer.ex @@ -12,7 +12,8 @@ defmodule Explorer.ChainSpec.Parity.Importer do def import_emission_rewards(chain_spec) do rewards = emission_rewards(chain_spec) - Repo.insert_all(EmissionReward, rewards) + {_, nil} = Repo.delete_all(EmissionReward) + {_, nil} = Repo.insert_all(EmissionReward, rewards) end def emission_rewards(chain_spec) do 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 60be2c688d..2fb98f64dc 100644 --- a/apps/explorer/test/explorer/chain_spec/parity/importer_test.exs +++ b/apps/explorer/test/explorer/chain_spec/parity/importer_test.exs @@ -1,9 +1,11 @@ defmodule Explorer.ChainSpec.Parity.ImporterTest do use Explorer.DataCase + alias Explorer.Chain.Block.EmissionReward alias Explorer.Chain.Block.Range alias Explorer.Chain.Wei alias Explorer.ChainSpec.Parity.Importer + alias Explorer.Repo @chain_spec "#{File.cwd!()}/test/support/fixture/chain_spec/foundation.json" |> File.read!() @@ -32,5 +34,48 @@ defmodule Explorer.ChainSpec.Parity.ImporterTest do test "inserts emission rewards from chain spec" do assert {3, nil} = Importer.import_emission_rewards(@chain_spec) 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