|
|
|
@ -2,6 +2,7 @@ defmodule EthereumJSONRPCTest do |
|
|
|
|
use EthereumJSONRPC.Case, async: true |
|
|
|
|
|
|
|
|
|
import EthereumJSONRPC.Case |
|
|
|
|
import EthereumJSONRPC, only: [quantity_to_integer: 1] |
|
|
|
|
import Mox |
|
|
|
|
|
|
|
|
|
alias EthereumJSONRPC.{Blocks, FetchedBalances, FetchedBeneficiaries, FetchedCodes, Subscription} |
|
|
|
@ -543,191 +544,32 @@ defmodule EthereumJSONRPCTest do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "fetch_blocks_by_tag/2" do |
|
|
|
|
@tag capture_log: false |
|
|
|
|
test "with earliest", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn [%{id: id}], _options -> |
|
|
|
|
block_number = "0x0" |
|
|
|
|
block_hash = "0x29c850324e357f3c0c836d79860c5af55f7b651e5d7ee253c1af1b14908af49c" |
|
|
|
|
transaction_hash = "0xa2e81bb56b55ba3dab2daf76501b50dfaad240cccb905dbf89d65c7a84a4a48e" |
|
|
|
|
|
|
|
|
|
{:ok, |
|
|
|
|
[ |
|
|
|
|
%{ |
|
|
|
|
id: id, |
|
|
|
|
result: %{ |
|
|
|
|
"difficulty" => "0x0", |
|
|
|
|
"gasLimit" => "0x0", |
|
|
|
|
"gasUsed" => "0x0", |
|
|
|
|
"hash" => block_hash, |
|
|
|
|
"extraData" => "0x0", |
|
|
|
|
"logsBloom" => "0x0", |
|
|
|
|
"miner" => "0x0", |
|
|
|
|
"number" => block_number, |
|
|
|
|
"parentHash" => "0x0", |
|
|
|
|
"receiptsRoot" => "0x0", |
|
|
|
|
"size" => "0x0", |
|
|
|
|
"sha3Uncles" => "0x0", |
|
|
|
|
"stateRoot" => "0x0", |
|
|
|
|
"timestamp" => "0x0", |
|
|
|
|
"totalDifficulty" => "0x0", |
|
|
|
|
"transactions" => [ |
|
|
|
|
%{ |
|
|
|
|
"blockHash" => block_hash, |
|
|
|
|
"blockNumber" => block_number, |
|
|
|
|
"from" => "0x0", |
|
|
|
|
"gas" => "0x0", |
|
|
|
|
"gasPrice" => "0x0", |
|
|
|
|
"hash" => transaction_hash, |
|
|
|
|
"input" => "0x", |
|
|
|
|
"nonce" => "0x0", |
|
|
|
|
"r" => "0x0", |
|
|
|
|
"s" => "0x0", |
|
|
|
|
"to" => "0x0", |
|
|
|
|
"transactionIndex" => "0x0", |
|
|
|
|
"v" => "0x0", |
|
|
|
|
"value" => "0x0" |
|
|
|
|
} |
|
|
|
|
], |
|
|
|
|
"transactionsRoot" => "0x0", |
|
|
|
|
"uncles" => [] |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
]} |
|
|
|
|
end) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
log_bad_gateway( |
|
|
|
|
fn -> EthereumJSONRPC.fetch_blocks_by_tag("earliest", json_rpc_named_arguments) end, |
|
|
|
|
fn result -> |
|
|
|
|
assert {:ok, %Blocks{blocks_params: [_ | _], transactions_params: [_ | _]}} = result |
|
|
|
|
end |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
describe "fetch_block_by_tag/2" do |
|
|
|
|
@supported_tags ~w(earliest latest pending) |
|
|
|
|
|
|
|
|
|
@tag capture_log: false |
|
|
|
|
test "with latest", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn [%{id: id}], _options -> |
|
|
|
|
block_number = "0x1" |
|
|
|
|
block_hash = "0x29c850324e357f3c0c836d79860c5af55f7b651e5d7ee253c1af1b14908af49c" |
|
|
|
|
transaction_hash = "0xa2e81bb56b55ba3dab2daf76501b50dfaad240cccb905dbf89d65c7a84a4a48e" |
|
|
|
|
|
|
|
|
|
{:ok, |
|
|
|
|
[ |
|
|
|
|
%{ |
|
|
|
|
id: id, |
|
|
|
|
result: %{ |
|
|
|
|
"difficulty" => "0x0", |
|
|
|
|
"gasLimit" => "0x0", |
|
|
|
|
"gasUsed" => "0x0", |
|
|
|
|
"hash" => block_hash, |
|
|
|
|
"extraData" => "0x0", |
|
|
|
|
"logsBloom" => "0x0", |
|
|
|
|
"miner" => "0x0", |
|
|
|
|
"number" => block_number, |
|
|
|
|
"parentHash" => "0x0", |
|
|
|
|
"receiptsRoot" => "0x0", |
|
|
|
|
"size" => "0x0", |
|
|
|
|
"sha3Uncles" => "0x0", |
|
|
|
|
"stateRoot" => "0x0", |
|
|
|
|
"timestamp" => "0x0", |
|
|
|
|
"totalDifficulty" => "0x0", |
|
|
|
|
"transactions" => [ |
|
|
|
|
%{ |
|
|
|
|
"blockHash" => block_hash, |
|
|
|
|
"blockNumber" => block_number, |
|
|
|
|
"from" => "0x0", |
|
|
|
|
"gas" => "0x0", |
|
|
|
|
"gasPrice" => "0x0", |
|
|
|
|
"hash" => transaction_hash, |
|
|
|
|
"input" => "0x", |
|
|
|
|
"nonce" => "0x0", |
|
|
|
|
"r" => "0x0", |
|
|
|
|
"s" => "0x0", |
|
|
|
|
"to" => "0x0", |
|
|
|
|
"transactionIndex" => "0x0", |
|
|
|
|
"v" => "0x0", |
|
|
|
|
"value" => "0x0" |
|
|
|
|
} |
|
|
|
|
], |
|
|
|
|
"transactionsRoot" => "0x0", |
|
|
|
|
"uncles" => [] |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
]} |
|
|
|
|
end) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
log_bad_gateway( |
|
|
|
|
fn -> EthereumJSONRPC.fetch_blocks_by_tag("latest", json_rpc_named_arguments) end, |
|
|
|
|
fn result -> |
|
|
|
|
{:ok, %Blocks{blocks_params: [_ | _], transactions_params: [_ | _]}} = result |
|
|
|
|
test "with all supported tags", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
|
for tag <- @supported_tags do |
|
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn [ |
|
|
|
|
%{ |
|
|
|
|
id: id, |
|
|
|
|
method: "eth_getBlockByNumber", |
|
|
|
|
params: [^tag, false] |
|
|
|
|
} |
|
|
|
|
], |
|
|
|
|
_options -> |
|
|
|
|
block_response(id, tag == "pending", "0x1") |
|
|
|
|
end) |
|
|
|
|
end |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@tag capture_log: false |
|
|
|
|
test "with pending", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn [%{id: id}], _options -> |
|
|
|
|
block_number = "0x1" |
|
|
|
|
block_hash = "0x29c850324e357f3c0c836d79860c5af55f7b651e5d7ee253c1af1b14908af49c" |
|
|
|
|
transaction_hash = "0xa2e81bb56b55ba3dab2daf76501b50dfaad240cccb905dbf89d65c7a84a4a48e" |
|
|
|
|
|
|
|
|
|
{:ok, |
|
|
|
|
[ |
|
|
|
|
%{ |
|
|
|
|
id: id, |
|
|
|
|
result: %{ |
|
|
|
|
"difficulty" => "0x0", |
|
|
|
|
"gasLimit" => "0x0", |
|
|
|
|
"gasUsed" => "0x0", |
|
|
|
|
"hash" => block_hash, |
|
|
|
|
"extraData" => "0x0", |
|
|
|
|
"logsBloom" => "0x0", |
|
|
|
|
"miner" => "0x0", |
|
|
|
|
"number" => block_number, |
|
|
|
|
"parentHash" => "0x0", |
|
|
|
|
"receiptsRoot" => "0x0", |
|
|
|
|
"size" => "0x0", |
|
|
|
|
"sha3Uncles" => "0x0", |
|
|
|
|
"stateRoot" => "0x0", |
|
|
|
|
"timestamp" => "0x0", |
|
|
|
|
"totalDifficulty" => "0x0", |
|
|
|
|
"transactions" => [ |
|
|
|
|
%{ |
|
|
|
|
"blockHash" => block_hash, |
|
|
|
|
"blockNumber" => block_number, |
|
|
|
|
"from" => "0x0", |
|
|
|
|
"gas" => "0x0", |
|
|
|
|
"gasPrice" => "0x0", |
|
|
|
|
"hash" => transaction_hash, |
|
|
|
|
"input" => "0x", |
|
|
|
|
"nonce" => "0x0", |
|
|
|
|
"r" => "0x0", |
|
|
|
|
"s" => "0x0", |
|
|
|
|
"to" => "0x0", |
|
|
|
|
"transactionIndex" => "0x0", |
|
|
|
|
"v" => "0x0", |
|
|
|
|
"value" => "0x0" |
|
|
|
|
} |
|
|
|
|
], |
|
|
|
|
"transactionsRoot" => "0x0", |
|
|
|
|
"uncles" => [] |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
]} |
|
|
|
|
end) |
|
|
|
|
log_bad_gateway( |
|
|
|
|
fn -> EthereumJSONRPC.fetch_block_by_tag(tag, json_rpc_named_arguments) end, |
|
|
|
|
fn result -> |
|
|
|
|
{:ok, %Blocks{blocks_params: [_ | _], transactions_params: []}} = result |
|
|
|
|
end |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
log_bad_gateway( |
|
|
|
|
fn -> EthereumJSONRPC.fetch_blocks_by_tag("pending", json_rpc_named_arguments) end, |
|
|
|
|
fn result -> |
|
|
|
|
{:ok, %Blocks{blocks_params: [_ | _], transactions_params: [_ | _]}} = result |
|
|
|
|
end |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
test "unknown errors are returned", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
@ -740,66 +582,54 @@ defmodule EthereumJSONRPCTest do |
|
|
|
|
{:error, unknown_error} |
|
|
|
|
end) |
|
|
|
|
|
|
|
|
|
assert {:error, ^unknown_error} = |
|
|
|
|
EthereumJSONRPC.fetch_block_number_by_tag("latest", moxed_json_rpc_named_arguments) |
|
|
|
|
assert {:error, ^unknown_error} = EthereumJSONRPC.fetch_block_by_tag("latest", moxed_json_rpc_named_arguments) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe "fetch_block_number_by_tag" do |
|
|
|
|
@tag capture_log: false |
|
|
|
|
test "with earliest", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn _json, _options -> |
|
|
|
|
{:ok, %{"number" => "0x0"}} |
|
|
|
|
end) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
log_bad_gateway( |
|
|
|
|
fn -> EthereumJSONRPC.fetch_block_number_by_tag("earliest", json_rpc_named_arguments) end, |
|
|
|
|
fn result -> |
|
|
|
|
assert {:ok, 0} = result |
|
|
|
|
end |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
@supported_tags %{"earliest" => "0x0", "latest" => "0x1", "pending" => nil} |
|
|
|
|
|
|
|
|
|
@tag capture_log: false |
|
|
|
|
test "with latest", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn _json, _options -> |
|
|
|
|
{:ok, %{"number" => "0x1"}} |
|
|
|
|
end) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
log_bad_gateway( |
|
|
|
|
fn -> EthereumJSONRPC.fetch_block_number_by_tag("latest", json_rpc_named_arguments) end, |
|
|
|
|
fn result -> |
|
|
|
|
assert {:ok, number} = result |
|
|
|
|
assert number > 0 |
|
|
|
|
test "with all supported tags", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
|
for {tag, expected_result} <- @supported_tags do |
|
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn [ |
|
|
|
|
%{ |
|
|
|
|
id: id, |
|
|
|
|
method: "eth_getBlockByNumber", |
|
|
|
|
params: [^tag, false] |
|
|
|
|
} |
|
|
|
|
], |
|
|
|
|
_options -> |
|
|
|
|
if tag == "pending" do |
|
|
|
|
{:ok, [%{id: id, result: nil}]} |
|
|
|
|
else |
|
|
|
|
block_response(id, false, expected_result) |
|
|
|
|
end |
|
|
|
|
end) |
|
|
|
|
end |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@tag capture_log: false |
|
|
|
|
test "with pending", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn _json, _options -> |
|
|
|
|
{:ok, nil} |
|
|
|
|
end) |
|
|
|
|
log_bad_gateway( |
|
|
|
|
fn -> EthereumJSONRPC.fetch_block_number_by_tag(tag, json_rpc_named_arguments) end, |
|
|
|
|
if tag == "pending" do |
|
|
|
|
fn |
|
|
|
|
# Parity after https://github.com/paritytech/parity-ethereum/pull/8281 and anything spec-compliant |
|
|
|
|
{:error, reason} -> |
|
|
|
|
assert reason == :not_found |
|
|
|
|
|
|
|
|
|
# Parity before https://github.com/paritytech/parity-ethereum/pull/8281 |
|
|
|
|
{:ok, number} -> |
|
|
|
|
assert is_integer(number) |
|
|
|
|
assert number > 0 |
|
|
|
|
end |
|
|
|
|
else |
|
|
|
|
fn result -> |
|
|
|
|
integer_result = expected_result && quantity_to_integer(expected_result) |
|
|
|
|
assert {:ok, ^integer_result} = result |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
log_bad_gateway( |
|
|
|
|
fn -> EthereumJSONRPC.fetch_block_number_by_tag("pending", json_rpc_named_arguments) end, |
|
|
|
|
fn |
|
|
|
|
# Parity after https://github.com/paritytech/parity-ethereum/pull/8281 and anything spec-compliant |
|
|
|
|
{:error, reason} -> |
|
|
|
|
assert reason == :not_found |
|
|
|
|
|
|
|
|
|
# Parity before https://github.com/paritytech/parity-ethereum/pull/8281 |
|
|
|
|
{:ok, number} -> |
|
|
|
|
assert is_integer(number) |
|
|
|
|
assert number > 0 |
|
|
|
|
end |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
test "unknown errors are returned", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
@ -1114,12 +944,43 @@ defmodule EthereumJSONRPCTest do |
|
|
|
|
:ok |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp block_response(id, pending, block_number) do |
|
|
|
|
block_hash = "0x29c850324e357f3c0c836d79860c5af55f7b651e5d7ee253c1af1b14908af49c" |
|
|
|
|
transaction_hash = "0xa2e81bb56b55ba3dab2daf76501b50dfaad240cccb905dbf89d65c7a84a4a48e" |
|
|
|
|
|
|
|
|
|
{:ok, |
|
|
|
|
[ |
|
|
|
|
%{ |
|
|
|
|
id: id, |
|
|
|
|
result: %{ |
|
|
|
|
"difficulty" => "0x0", |
|
|
|
|
"gasLimit" => "0x0", |
|
|
|
|
"gasUsed" => "0x0", |
|
|
|
|
"hash" => if(pending, do: nil, else: block_hash), |
|
|
|
|
"extraData" => "0x0", |
|
|
|
|
"logsBloom" => "0x0", |
|
|
|
|
"miner" => "0x0", |
|
|
|
|
"number" => block_number, |
|
|
|
|
"parentHash" => "0x0", |
|
|
|
|
"receiptsRoot" => "0x0", |
|
|
|
|
"size" => "0x0", |
|
|
|
|
"sha3Uncles" => "0x0", |
|
|
|
|
"stateRoot" => "0x0", |
|
|
|
|
"timestamp" => "0x0", |
|
|
|
|
"totalDifficulty" => "0x0", |
|
|
|
|
"transactions" => [transaction_hash], |
|
|
|
|
"transactionsRoot" => "0x0", |
|
|
|
|
"uncles" => [] |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
]} |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defmodule EthereumJSONRPCSyncTest do |
|
|
|
|
use EthereumJSONRPC.Case, async: false |
|
|
|
|
|
|
|
|
|
import EthereumJSONRPC.Case |
|
|
|
|
import Mox |
|
|
|
|
|
|
|
|
|
alias EthereumJSONRPC.FetchedBalances |
|
|
|
|