From 23bd2b00499c7d5496bc828a616bf6a6ed7d8a17 Mon Sep 17 00:00:00 2001 From: William Sanches Date: Mon, 10 Dec 2018 18:03:57 -0200 Subject: [PATCH] Rename block_rewards to emission_rewards --- .../api/rpc/address_controller_test.exs | 12 ++++----- .../api/rpc/block_controller_test.exs | 4 +-- apps/explorer/lib/explorer/chain.ex | 10 +++---- .../explorer/chain/block/emission_reward.ex | 27 +++++++++++++++++++ .../lib/explorer/chain/block/reward.ex | 8 +++--- apps/explorer/lib/explorer/etherscan.ex | 4 +-- ...name_block_rewards_to_emission_rewards.exs | 7 +++++ apps/explorer/test/explorer/chain_test.exs | 12 ++++----- .../explorer/test/explorer/etherscan_test.exs | 24 ++++++++--------- apps/explorer/test/support/factory.ex | 6 ++--- 10 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 apps/explorer/lib/explorer/chain/block/emission_reward.ex create mode 100644 apps/explorer/priv/repo/migrations/20181206200140_rename_block_rewards_to_emission_rewards.exs diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs index bc5892a152..4fc27546ee 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs @@ -2053,7 +2053,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do end test "returns all the required fields", %{conn: conn} do - %{block_range: range} = block_reward = insert(:block_reward) + %{block_range: range} = emission_reward = insert(:emission_reward) block = insert(:block, number: Enum.random(Range.new(range.from, range.to))) @@ -2062,7 +2062,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do |> with_block(block, gas_used: 1) expected_reward = - block_reward.reward + emission_reward.reward |> Wei.to(:wei) |> Decimal.add(Decimal.new(1)) |> Wei.from(:wei) @@ -2092,7 +2092,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do end test "with a block with one transaction", %{conn: conn} do - %{block_range: range} = block_reward = insert(:block_reward) + %{block_range: range} = emission_reward = insert(:emission_reward) block = insert(:block, number: Enum.random(Range.new(range.from, range.to))) @@ -2101,7 +2101,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do |> with_block(block, gas_used: 1) expected_reward = - block_reward.reward + emission_reward.reward |> Wei.to(:wei) |> Decimal.add(Decimal.new(1)) |> Wei.from(:wei) @@ -2131,7 +2131,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do end test "with pagination options", %{conn: conn} do - %{block_range: range} = block_reward = insert(:block_reward) + %{block_range: range} = emission_reward = insert(:emission_reward) block_numbers = Range.new(range.from, range.to) @@ -2147,7 +2147,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do |> with_block(block2, gas_used: 2) expected_reward = - block_reward.reward + emission_reward.reward |> Wei.to(:wei) |> Decimal.add(Decimal.new(4)) |> Wei.from(:wei) diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/block_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/block_controller_test.exs index cf4027f54d..bda89fe49a 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/block_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/block_controller_test.exs @@ -41,7 +41,7 @@ defmodule BlockScoutWeb.API.RPC.BlockControllerTest do end test "with a valid block", %{conn: conn} do - %{block_range: range} = block_reward = insert(:block_reward) + %{block_range: range} = emission_reward = insert(:emission_reward) block = insert(:block, number: Enum.random(Range.new(range.from, range.to))) :transaction @@ -49,7 +49,7 @@ defmodule BlockScoutWeb.API.RPC.BlockControllerTest do |> with_block(block, gas_used: 1) expected_reward = - block_reward.reward + emission_reward.reward |> Wei.to(:wei) |> Decimal.add(Decimal.new(1)) |> Decimal.to_string(:normal) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 19374d9f5d..5b5aab9867 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -38,7 +38,7 @@ defmodule Explorer.Chain do Wei } - alias Explorer.Chain.Block.Reward + alias Explorer.Chain.Block.EmissionReward alias Explorer.{PagingOptions, Repo} alias Explorer.Counters.{ @@ -366,15 +366,15 @@ defmodule Explorer.Chain do from( block in Block, left_join: transaction in assoc(block, :transactions), - inner_join: block_reward in Reward, - on: fragment("? <@ ?", block.number, block_reward.block_range), + inner_join: emission_reward in EmissionReward, + on: fragment("? <@ ?", block.number, emission_reward.block_range), where: block.number == ^block_number, - group_by: block_reward.reward, + group_by: emission_reward.reward, select: %{ transaction_reward: %Wei{ value: default_if_empty(sum_of_products(transaction.gas_used, transaction.gas_price), 0) }, - static_reward: block_reward.reward + static_reward: emission_reward.reward } ) diff --git a/apps/explorer/lib/explorer/chain/block/emission_reward.ex b/apps/explorer/lib/explorer/chain/block/emission_reward.ex new file mode 100644 index 0000000000..82ab015ac9 --- /dev/null +++ b/apps/explorer/lib/explorer/chain/block/emission_reward.ex @@ -0,0 +1,27 @@ +defmodule Explorer.Chain.Block.EmissionReward do + @moduledoc """ + Represents the static reward given to the miner of a block in a range of block numbers. + """ + + use Ecto.Schema + + alias Explorer.Chain.Block.{EmissionReward, Range} + alias Explorer.Chain.Wei + + @typedoc """ + The static reward given to the miner of a block. + + * `:block_range` - Range of block numbers + * `:reward` - Reward given in Wei + """ + @type t :: %EmissionReward{ + block_range: Range.t(), + reward: Wei.t() + } + + @primary_key false + schema "emission_rewards" do + field(:block_range, Range) + field(:reward, Wei) + end +end diff --git a/apps/explorer/lib/explorer/chain/block/reward.ex b/apps/explorer/lib/explorer/chain/block/reward.ex index 0ec65dba80..82ab015ac9 100644 --- a/apps/explorer/lib/explorer/chain/block/reward.ex +++ b/apps/explorer/lib/explorer/chain/block/reward.ex @@ -1,11 +1,11 @@ -defmodule Explorer.Chain.Block.Reward do +defmodule Explorer.Chain.Block.EmissionReward do @moduledoc """ Represents the static reward given to the miner of a block in a range of block numbers. """ use Ecto.Schema - alias Explorer.Chain.Block.{Range, Reward} + alias Explorer.Chain.Block.{EmissionReward, Range} alias Explorer.Chain.Wei @typedoc """ @@ -14,13 +14,13 @@ defmodule Explorer.Chain.Block.Reward do * `:block_range` - Range of block numbers * `:reward` - Reward given in Wei """ - @type t :: %Reward{ + @type t :: %EmissionReward{ block_range: Range.t(), reward: Wei.t() } @primary_key false - schema "block_rewards" do + schema "emission_rewards" do field(:block_range, Range) field(:reward, Wei) end diff --git a/apps/explorer/lib/explorer/etherscan.ex b/apps/explorer/lib/explorer/etherscan.ex index f89dc6cad7..a4d4649d97 100644 --- a/apps/explorer/lib/explorer/etherscan.ex +++ b/apps/explorer/lib/explorer/etherscan.ex @@ -9,7 +9,7 @@ defmodule Explorer.Etherscan do alias Explorer.{Chain, Repo} alias Explorer.Chain.Address.TokenBalance alias Explorer.Chain.{Block, Hash, InternalTransaction, Transaction, Wei} - alias Explorer.Chain.Block.Reward + alias Explorer.Chain.Block.EmissionReward @default_options %{ order_by_direction: :asc, @@ -186,7 +186,7 @@ defmodule Explorer.Etherscan do from( b in Block, left_join: t in assoc(b, :transactions), - inner_join: r in Reward, + inner_join: r in EmissionReward, on: fragment("? <@ ?", b.number, r.block_range), where: b.miner_hash == ^address_hash, order_by: [desc: b.number], diff --git a/apps/explorer/priv/repo/migrations/20181206200140_rename_block_rewards_to_emission_rewards.exs b/apps/explorer/priv/repo/migrations/20181206200140_rename_block_rewards_to_emission_rewards.exs new file mode 100644 index 0000000000..6ec5a8c466 --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20181206200140_rename_block_rewards_to_emission_rewards.exs @@ -0,0 +1,7 @@ +defmodule Explorer.Repo.Migrations.RenameBlockRewardsToEmissionRewards do + use Ecto.Migration + + def change do + rename(table(:block_rewards), to: table(:emission_rewards)) + end +end diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 62c4052aec..c3cfcb2e42 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -2159,15 +2159,15 @@ defmodule Explorer.ChainTest do describe "block_reward/1" do setup do - %{block_range: range} = block_reward = insert(:block_reward) + %{block_range: range} = emission_reward = insert(:emission_reward) block = insert(:block, number: Enum.random(Range.new(range.from, range.to))) insert(:transaction) - {:ok, block: block, block_reward: block_reward} + {:ok, block: block, emission_reward: emission_reward} end - test "with block containing transactions", %{block: block, block_reward: block_reward} do + test "with block containing transactions", %{block: block, emission_reward: emission_reward} do :transaction |> insert(gas_price: 1) |> with_block(block, gas_used: 1) @@ -2177,7 +2177,7 @@ defmodule Explorer.ChainTest do |> with_block(block, gas_used: 2) expected = - block_reward.reward + emission_reward.reward |> Wei.to(:wei) |> Decimal.add(Decimal.new(3)) |> Wei.from(:wei) @@ -2185,8 +2185,8 @@ defmodule Explorer.ChainTest do assert expected == Chain.block_reward(block) end - test "with block without transactions", %{block: block, block_reward: block_reward} do - assert block_reward.reward == Chain.block_reward(block) + test "with block without transactions", %{block: block, emission_reward: emission_reward} do + assert emission_reward.reward == Chain.block_reward(block) end end diff --git a/apps/explorer/test/explorer/etherscan_test.exs b/apps/explorer/test/explorer/etherscan_test.exs index c100edabe3..cb6ff93005 100644 --- a/apps/explorer/test/explorer/etherscan_test.exs +++ b/apps/explorer/test/explorer/etherscan_test.exs @@ -1168,7 +1168,7 @@ defmodule Explorer.EtherscanTest do describe "list_blocks/1" do test "it returns all required fields" do - %{block_range: range} = block_reward = insert(:block_reward) + %{block_range: range} = emission_reward = insert(:emission_reward) block = insert(:block, number: Enum.random(Range.new(range.from, range.to))) @@ -1180,7 +1180,7 @@ defmodule Explorer.EtherscanTest do |> with_block(block, gas_used: 1) expected_reward = - block_reward.reward + emission_reward.reward |> Wei.to(:wei) |> Decimal.add(Decimal.new(1)) |> Wei.from(:wei) @@ -1197,7 +1197,7 @@ defmodule Explorer.EtherscanTest do end test "with block containing multiple transactions" do - %{block_range: range} = block_reward = insert(:block_reward) + %{block_range: range} = emission_reward = insert(:emission_reward) block = insert(:block, number: Enum.random(Range.new(range.from, range.to))) @@ -1213,7 +1213,7 @@ defmodule Explorer.EtherscanTest do |> with_block(block, gas_used: 2) expected_reward = - block_reward.reward + emission_reward.reward |> Wei.to(:wei) |> Decimal.add(Decimal.new(3)) |> Wei.from(:wei) @@ -1230,7 +1230,7 @@ defmodule Explorer.EtherscanTest do end test "with block without transactions" do - %{block_range: range} = block_reward = insert(:block_reward) + %{block_range: range} = emission_reward = insert(:emission_reward) block = insert(:block, number: Enum.random(Range.new(range.from, range.to))) @@ -1241,7 +1241,7 @@ defmodule Explorer.EtherscanTest do %{ number: block.number, timestamp: block.timestamp, - reward: block_reward.reward + reward: emission_reward.reward } ] @@ -1249,7 +1249,7 @@ defmodule Explorer.EtherscanTest do end test "with multiple blocks" do - %{block_range: range} = block_reward = insert(:block_reward) + %{block_range: range} = emission_reward = insert(:emission_reward) block_numbers = Range.new(range.from, range.to) @@ -1280,13 +1280,13 @@ defmodule Explorer.EtherscanTest do |> with_block(block2, gas_used: 3) expected_reward_block1 = - block_reward.reward + emission_reward.reward |> Wei.to(:wei) |> Decimal.add(Decimal.new(8)) |> Wei.from(:wei) expected_reward_block2 = - block_reward.reward + emission_reward.reward |> Wei.to(:wei) |> Decimal.add(Decimal.new(18)) |> Wei.from(:wei) @@ -1308,7 +1308,7 @@ defmodule Explorer.EtherscanTest do end test "with pagination options" do - %{block_range: range} = block_reward = insert(:block_reward) + %{block_range: range} = emission_reward = insert(:emission_reward) block_numbers = Range.new(range.from, range.to) @@ -1324,7 +1324,7 @@ defmodule Explorer.EtherscanTest do |> with_block(block1, gas_used: 2) expected_reward = - block_reward.reward + emission_reward.reward |> Wei.to(:wei) |> Decimal.add(Decimal.new(4)) |> Wei.from(:wei) @@ -1333,7 +1333,7 @@ defmodule Explorer.EtherscanTest do %{ number: block2.number, timestamp: block2.timestamp, - reward: block_reward.reward + reward: emission_reward.reward } ] diff --git a/apps/explorer/test/support/factory.ex b/apps/explorer/test/support/factory.ex index e1aac30fd8..5781ca16c5 100644 --- a/apps/explorer/test/support/factory.ex +++ b/apps/explorer/test/support/factory.ex @@ -9,7 +9,7 @@ defmodule Explorer.Factory do alias Comeonin.Bcrypt alias Explorer.Accounts.{User, UserContact} alias Explorer.Admin.Administrator - alias Explorer.Chain.Block.{Range, Reward} + alias Explorer.Chain.Block.{EmissionReward, Range} alias Explorer.Chain.{ Address, @@ -404,7 +404,7 @@ defmodule Explorer.Factory do } end - def block_reward_factory do + def emission_reward_factory do # Generate ranges like 1 - 10,000; 10,001 - 20,000, 20,001 - 30,000; etc x = sequence("block_range", & &1) lower = x * Kernel.+(10_000, 1) @@ -419,7 +419,7 @@ defmodule Explorer.Factory do reward = Decimal.mult(reward_multiplier, wei_per_ether) - %Reward{ + %EmissionReward{ block_range: %Range{from: lower, to: upper}, reward: reward }