|
|
@ -2,6 +2,7 @@ defmodule EthereumJSONRPCTest do |
|
|
|
use EthereumJSONRPC.Case, async: true |
|
|
|
use EthereumJSONRPC.Case, async: true |
|
|
|
|
|
|
|
|
|
|
|
import EthereumJSONRPC.Case |
|
|
|
import EthereumJSONRPC.Case |
|
|
|
|
|
|
|
import EthereumJSONRPC, only: [quantity_to_integer: 1] |
|
|
|
import Mox |
|
|
|
import Mox |
|
|
|
|
|
|
|
|
|
|
|
alias EthereumJSONRPC.{Blocks, FetchedBalances, FetchedBeneficiaries, FetchedCodes, Subscription} |
|
|
|
alias EthereumJSONRPC.{Blocks, FetchedBalances, FetchedBeneficiaries, FetchedCodes, Subscription} |
|
|
@ -543,191 +544,32 @@ defmodule EthereumJSONRPCTest do |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
describe "fetch_blocks_by_tag/2" do |
|
|
|
describe "fetch_block_by_tag/2" do |
|
|
|
@tag capture_log: false |
|
|
|
@supported_tags ~w(earliest latest pending) |
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@tag capture_log: false |
|
|
|
@tag capture_log: false |
|
|
|
test "with latest", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
test "with all supported tags", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
for tag <- @supported_tags do |
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn [%{id: id}], _options -> |
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
block_number = "0x1" |
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn [ |
|
|
|
block_hash = "0x29c850324e357f3c0c836d79860c5af55f7b651e5d7ee253c1af1b14908af49c" |
|
|
|
%{ |
|
|
|
transaction_hash = "0xa2e81bb56b55ba3dab2daf76501b50dfaad240cccb905dbf89d65c7a84a4a48e" |
|
|
|
id: id, |
|
|
|
|
|
|
|
method: "eth_getBlockByNumber", |
|
|
|
{:ok, |
|
|
|
params: [^tag, false] |
|
|
|
[ |
|
|
|
} |
|
|
|
%{ |
|
|
|
], |
|
|
|
id: id, |
|
|
|
_options -> |
|
|
|
result: %{ |
|
|
|
block_response(id, tag == "pending", "0x1") |
|
|
|
"difficulty" => "0x0", |
|
|
|
end) |
|
|
|
"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 |
|
|
|
|
|
|
|
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, |
|
|
|
log_bad_gateway( |
|
|
|
[ |
|
|
|
fn -> EthereumJSONRPC.fetch_block_by_tag(tag, json_rpc_named_arguments) end, |
|
|
|
%{ |
|
|
|
fn result -> |
|
|
|
id: id, |
|
|
|
{:ok, %Blocks{blocks_params: [_ | _], transactions_params: []}} = result |
|
|
|
result: %{ |
|
|
|
end |
|
|
|
"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 |
|
|
|
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 |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "unknown errors are returned", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
test "unknown errors are returned", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
@ -740,66 +582,54 @@ defmodule EthereumJSONRPCTest do |
|
|
|
{:error, unknown_error} |
|
|
|
{:error, unknown_error} |
|
|
|
end) |
|
|
|
end) |
|
|
|
|
|
|
|
|
|
|
|
assert {:error, ^unknown_error} = |
|
|
|
assert {:error, ^unknown_error} = EthereumJSONRPC.fetch_block_by_tag("latest", moxed_json_rpc_named_arguments) |
|
|
|
EthereumJSONRPC.fetch_block_number_by_tag("latest", moxed_json_rpc_named_arguments) |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
describe "fetch_block_number_by_tag" do |
|
|
|
describe "fetch_block_number_by_tag" do |
|
|
|
@tag capture_log: false |
|
|
|
@supported_tags %{"earliest" => "0x0", "latest" => "0x1", "pending" => nil} |
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@tag capture_log: false |
|
|
|
@tag capture_log: false |
|
|
|
test "with latest", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
test "with all supported tags", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
for {tag, expected_result} <- @supported_tags do |
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn _json, _options -> |
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
{:ok, %{"number" => "0x1"}} |
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn [ |
|
|
|
end) |
|
|
|
%{ |
|
|
|
end |
|
|
|
id: id, |
|
|
|
|
|
|
|
method: "eth_getBlockByNumber", |
|
|
|
log_bad_gateway( |
|
|
|
params: [^tag, false] |
|
|
|
fn -> EthereumJSONRPC.fetch_block_number_by_tag("latest", json_rpc_named_arguments) end, |
|
|
|
} |
|
|
|
fn result -> |
|
|
|
], |
|
|
|
assert {:ok, number} = result |
|
|
|
_options -> |
|
|
|
assert number > 0 |
|
|
|
if tag == "pending" do |
|
|
|
|
|
|
|
{:ok, [%{id: id, result: nil}]} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
block_response(id, false, expected_result) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end) |
|
|
|
end |
|
|
|
end |
|
|
|
) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@tag capture_log: false |
|
|
|
log_bad_gateway( |
|
|
|
test "with pending", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
fn -> EthereumJSONRPC.fetch_block_number_by_tag(tag, json_rpc_named_arguments) end, |
|
|
|
if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do |
|
|
|
if tag == "pending" do |
|
|
|
expect(EthereumJSONRPC.Mox, :json_rpc, fn _json, _options -> |
|
|
|
fn |
|
|
|
{:ok, nil} |
|
|
|
# Parity after https://github.com/paritytech/parity-ethereum/pull/8281 and anything spec-compliant |
|
|
|
end) |
|
|
|
{: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 |
|
|
|
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 |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
test "unknown errors are returned", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
|
test "unknown errors are returned", %{json_rpc_named_arguments: json_rpc_named_arguments} do |
|
|
@ -1114,12 +944,43 @@ defmodule EthereumJSONRPCTest do |
|
|
|
:ok |
|
|
|
:ok |
|
|
|
end |
|
|
|
end |
|
|
|
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 |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
defmodule EthereumJSONRPCSyncTest do |
|
|
|
defmodule EthereumJSONRPCSyncTest do |
|
|
|
use EthereumJSONRPC.Case, async: false |
|
|
|
use EthereumJSONRPC.Case, async: false |
|
|
|
|
|
|
|
|
|
|
|
import EthereumJSONRPC.Case |
|
|
|
|
|
|
|
import Mox |
|
|
|
import Mox |
|
|
|
|
|
|
|
|
|
|
|
alias EthereumJSONRPC.FetchedBalances |
|
|
|
alias EthereumJSONRPC.FetchedBalances |
|
|
|