Skip to content

Commit

Permalink
Merge pull request #2613 from poanetwork/ab-fix-getminedblocks
Browse files Browse the repository at this point in the history
remove rewards from getminedblocks rpc response
  • Loading branch information
ayrat555 authored Aug 26, 2019
2 parents 9aef894 + 4b19c55 commit 0c6bd68
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 129 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [#2497](https://github.com/poanetwork/blockscout/pull/2497) - Add generic Ordered Cache behaviour and implementation

### Fixes
- [#2613](https://github.com/poanetwork/blockscout/pull/2613) - fix getminedblocks rpc endpoint
- [#2592](https://github.com/poanetwork/blockscout/pull/2592) - process new metadata format for whisper
- [#2572](https://github.com/poanetwork/blockscout/pull/2572) - Ease non-critical css
- [#2570](https://github.com/poanetwork/blockscout/pull/2570) - Network icons preload
Expand Down
5 changes: 0 additions & 5 deletions apps/block_scout_web/lib/block_scout_web/etherscan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,6 @@ defmodule BlockScoutWeb.Etherscan do
type: "timestamp",
definition: "When the block was collated.",
example: ~s("1480072029")
},
blockReward: %{
type: "block reward",
definition: "The reward given to the miner of a block.",
example: ~s("5003251945421042780")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ defmodule BlockScoutWeb.API.RPC.AddressView do
defp prepare_block(block) do
%{
"blockNumber" => to_string(block.number),
"timeStamp" => to_string(block.timestamp),
"blockReward" => to_string(block.reward.value)
"timeStamp" => to_string(block.timestamp)
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2266,25 +2266,18 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do
end

test "returns all the required fields", %{conn: conn} do
%{block_range: range} = emission_reward = insert(:emission_reward)
%{block_range: range} = insert(:emission_reward)

block = insert(:block, number: Enum.random(Range.new(range.from, range.to)))

:transaction
|> insert(gas_price: 1)
|> with_block(block, gas_used: 1)

expected_reward =
emission_reward.reward
|> Wei.to(:wei)
|> Decimal.add(Decimal.new(1))
|> Wei.from(:wei)

expected_result = [
%{
"blockNumber" => to_string(block.number),
"timeStamp" => to_string(block.timestamp),
"blockReward" => to_string(expected_reward.value)
"timeStamp" => to_string(block.timestamp)
}
]

Expand All @@ -2306,20 +2299,14 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do
end

test "with a block with one transaction", %{conn: conn} do
%{block_range: range} = emission_reward = insert(:emission_reward)
%{block_range: range} = insert(:emission_reward)

block = insert(:block, number: Enum.random(Range.new(range.from, range.to)))

:transaction
|> insert(gas_price: 1)
|> with_block(block, gas_used: 1)

expected_reward =
emission_reward.reward
|> Wei.to(:wei)
|> Decimal.add(Decimal.new(1))
|> Wei.from(:wei)

params = %{
"module" => "account",
"action" => "getminedblocks",
Expand All @@ -2329,8 +2316,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do
expected_result = [
%{
"blockNumber" => to_string(block.number),
"timeStamp" => to_string(block.timestamp),
"blockReward" => to_string(expected_reward.value)
"timeStamp" => to_string(block.timestamp)
}
]

Expand All @@ -2346,7 +2332,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do
end

test "with pagination options", %{conn: conn} do
%{block_range: range} = emission_reward = insert(:emission_reward)
%{block_range: range} = insert(:emission_reward)

block_numbers = Range.new(range.from, range.to)

Expand All @@ -2361,12 +2347,6 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do
|> insert(gas_price: 2)
|> with_block(block2, gas_used: 2)

expected_reward =
emission_reward.reward
|> Wei.to(:wei)
|> Decimal.add(Decimal.new(4))
|> Wei.from(:wei)

params = %{
"module" => "account",
"action" => "getminedblocks",
Expand All @@ -2380,8 +2360,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do
expected_result = [
%{
"blockNumber" => to_string(block2.number),
"timeStamp" => to_string(block2.timestamp),
"blockReward" => to_string(expected_reward.value)
"timeStamp" => to_string(block2.timestamp)
}
]

Expand Down Expand Up @@ -2683,7 +2662,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do
})
end

defp resolve_schema(result \\ %{}) do
defp resolve_schema(result) do
%{
"type" => "object",
"properties" => %{
Expand Down
12 changes: 2 additions & 10 deletions apps/explorer/lib/explorer/etherscan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ defmodule Explorer.Etherscan do
alias Explorer.Etherscan.Logs
alias Explorer.{Chain, Repo}
alias Explorer.Chain.Address.TokenBalance
alias Explorer.Chain.{Block, Hash, InternalTransaction, Transaction, Wei}
alias Explorer.Chain.Block.EmissionReward
alias Explorer.Chain.{Block, Hash, InternalTransaction, Transaction}

@default_options %{
order_by_direction: :asc,
Expand Down Expand Up @@ -187,22 +186,15 @@ defmodule Explorer.Etherscan do
query =
from(
b in Block,
left_join: t in assoc(b, :transactions),
inner_join: r in EmissionReward,
on: fragment("? <@ ?", b.number, r.block_range),
where: b.miner_hash == ^address_hash,
order_by: [desc: b.number],
group_by: b.number,
group_by: b.timestamp,
group_by: r.reward,
limit: ^merged_options.page_size,
offset: ^offset(merged_options),
select: %{
number: b.number,
timestamp: b.timestamp,
reward: %Wei{
value: fragment("coalesce(sum(? * ?), 0) + ?", t.gas_used, t.gas_price, r.reward)
}
timestamp: b.timestamp
}
)

Expand Down
97 changes: 13 additions & 84 deletions apps/explorer/test/explorer/etherscan_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Explorer.EtherscanTest do
import Explorer.Factory

alias Explorer.{Etherscan, Chain}
alias Explorer.Chain.{Transaction, Wei}
alias Explorer.Chain.Transaction

describe "list_transactions/2" do
test "with empty db" do
Expand Down Expand Up @@ -1170,7 +1170,7 @@ defmodule Explorer.EtherscanTest do

describe "list_blocks/1" do
test "it returns all required fields" do
%{block_range: range} = emission_reward = insert(:emission_reward)
%{block_range: range} = insert(:emission_reward)

block = insert(:block, number: Enum.random(Range.new(range.from, range.to)))

Expand All @@ -1181,58 +1181,33 @@ defmodule Explorer.EtherscanTest do
|> insert(gas_price: 1)
|> with_block(block, gas_used: 1)

expected_reward =
emission_reward.reward
|> Wei.to(:wei)
|> Decimal.add(Decimal.new(1))
|> Wei.from(:wei)

expected = [
%{
number: block.number,
timestamp: block.timestamp,
reward: expected_reward
timestamp: block.timestamp
}
]

assert Etherscan.list_blocks(block.miner_hash) == expected
end

test "with block containing multiple transactions" do
%{block_range: range} = emission_reward = insert(:emission_reward)
%{block_range: range} = insert(:emission_reward)

block = insert(:block, number: Enum.random(Range.new(range.from, range.to)))

# irrelevant transaction
insert(:transaction)

:transaction
|> insert(gas_price: 1)
|> with_block(block, gas_used: 1)

:transaction
|> insert(gas_price: 1)
|> with_block(block, gas_used: 2)

expected_reward =
emission_reward.reward
|> Wei.to(:wei)
|> Decimal.add(Decimal.new(3))
|> Wei.from(:wei)

expected = [
%{
number: block.number,
timestamp: block.timestamp,
reward: expected_reward
timestamp: block.timestamp
}
]

assert Etherscan.list_blocks(block.miner_hash) == expected
end

test "with block without transactions" do
%{block_range: range} = emission_reward = insert(:emission_reward)
%{block_range: range} = insert(:emission_reward)

block = insert(:block, number: Enum.random(Range.new(range.from, range.to)))

Expand All @@ -1242,16 +1217,15 @@ defmodule Explorer.EtherscanTest do
expected = [
%{
number: block.number,
timestamp: block.timestamp,
reward: emission_reward.reward
timestamp: block.timestamp
}
]

assert Etherscan.list_blocks(block.miner_hash) == expected
end

test "with multiple blocks" do
%{block_range: range} = emission_reward = insert(:emission_reward)
%{block_range: range} = insert(:emission_reward)

block_numbers = Range.new(range.from, range.to)

Expand All @@ -1262,55 +1236,22 @@ defmodule Explorer.EtherscanTest do
block1 = insert(:block, number: block_number1, miner: address)
block2 = insert(:block, number: block_number2, miner: address)

# irrelevant transaction
insert(:transaction)

:transaction
|> insert(gas_price: 2)
|> with_block(block1, gas_used: 2)

:transaction
|> insert(gas_price: 2)
|> with_block(block1, gas_used: 2)

:transaction
|> insert(gas_price: 3)
|> with_block(block2, gas_used: 3)

:transaction
|> insert(gas_price: 3)
|> with_block(block2, gas_used: 3)

expected_reward_block1 =
emission_reward.reward
|> Wei.to(:wei)
|> Decimal.add(Decimal.new(8))
|> Wei.from(:wei)

expected_reward_block2 =
emission_reward.reward
|> Wei.to(:wei)
|> Decimal.add(Decimal.new(18))
|> Wei.from(:wei)

expected = [
%{
number: block2.number,
timestamp: block2.timestamp,
reward: expected_reward_block2
timestamp: block2.timestamp
},
%{
number: block1.number,
timestamp: block1.timestamp,
reward: expected_reward_block1
timestamp: block1.timestamp
}
]

assert Etherscan.list_blocks(address.hash) == expected
end

test "with pagination options" do
%{block_range: range} = emission_reward = insert(:emission_reward)
%{block_range: range} = insert(:emission_reward)

block_numbers = Range.new(range.from, range.to)

Expand All @@ -1321,29 +1262,17 @@ defmodule Explorer.EtherscanTest do
block1 = insert(:block, number: block_number1, miner: address)
block2 = insert(:block, number: block_number2, miner: address)

:transaction
|> insert(gas_price: 2)
|> with_block(block1, gas_used: 2)

expected_reward =
emission_reward.reward
|> Wei.to(:wei)
|> Decimal.add(Decimal.new(4))
|> Wei.from(:wei)

expected1 = [
%{
number: block2.number,
timestamp: block2.timestamp,
reward: emission_reward.reward
timestamp: block2.timestamp
}
]

expected2 = [
%{
number: block1.number,
timestamp: block1.timestamp,
reward: expected_reward
timestamp: block1.timestamp
}
]

Expand Down

0 comments on commit 0c6bd68

Please sign in to comment.