method_to_url config

Instead of making up symbolic names for the different types of URLs we
need, have the configuration be a base `url` for a fallback and then
entries under `method_to_url` which maps a JSONRPC method name to a the
URL to use.  This allows the devops running the JSONRPC nodes to say
which nodes support which methods specifically, which is easier than
knowing which symbolic name uses which methods.  It also means we don't
need separate variant specific symbolic names since the variants will
be able to use their own method names in this new format for the config.
pull/335/head
Luke Imhoff 7 years ago
parent 88bb8a3b55
commit 72373f7770
  1. 1
      apps/ethereum_jsonrpc/config/geth.exs
  2. 5
      apps/ethereum_jsonrpc/config/parity.exs
  3. 29
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex
  4. 6
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity.ex
  5. 2
      coveralls.json

@ -1,6 +1,5 @@
use Mix.Config use Mix.Config
config :ethereum_jsonrpc, config :ethereum_jsonrpc,
trace_url: "https://mainnet.infura.io/mew",
url: "https://mainnet.infura.io/mew", url: "https://mainnet.infura.io/mew",
variant: EthereumJSONRPC.Geth variant: EthereumJSONRPC.Geth

@ -1,6 +1,9 @@
use Mix.Config use Mix.Config
config :ethereum_jsonrpc, config :ethereum_jsonrpc,
trace_url: "https://sokol-trace.poa.network",
url: "https://sokol.poa.network", url: "https://sokol.poa.network",
method_to_url: [
eth_getBalance: "https://sokol-trace.poa.network",
trace_replayTransaction: "https://sokol-trace.poa.network"
],
variant: EthereumJSONRPC.Parity variant: EthereumJSONRPC.Parity

@ -89,6 +89,27 @@ defmodule EthereumJSONRPC do
Application.fetch_env!(:ethereum_jsonrpc, key) Application.fetch_env!(:ethereum_jsonrpc, key)
end end
@doc """
Fetches the configured url for the specific `method` or the fallback `url`
Configuration for a specific `method` can be set in `method_to_url` `config`
config :ethereum_jsonrpc,
method_to_url: [
eth_getBalance: "method_to_url"
]
The fallback 'url' MUST we set if not all methods have a url set.
config :ethereum_jsonrpc,
url:
"""
def method_to_url(method) when is_atom(method) do
:ethereum_jsonrpc
|> Application.get_env(:method_to_url, [])
|> Keyword.get_lazy(method, fn -> config(:url) end)
end
@doc """ @doc """
Fetches balance for each address `hash` at the `block_number` Fetches balance for each address `hash` at the `block_number`
""" """
@ -108,7 +129,7 @@ defmodule EthereumJSONRPC do
with {:ok, responses} <- with {:ok, responses} <-
id_to_params id_to_params
|> get_balance_requests() |> get_balance_requests()
|> json_rpc(config(:trace_url)) do |> json_rpc(method_to_url(:eth_getBalance)) do
get_balance_responses_to_addresses_params(responses, id_to_params) get_balance_responses_to_addresses_params(responses, id_to_params)
end end
end end
@ -121,7 +142,7 @@ defmodule EthereumJSONRPC do
def fetch_blocks_by_hash(block_hashes) do def fetch_blocks_by_hash(block_hashes) do
block_hashes block_hashes
|> get_block_by_hash_requests() |> get_block_by_hash_requests()
|> json_rpc(config(:url)) |> json_rpc(method_to_url(:eth_getBlockByHash))
|> handle_get_blocks() |> handle_get_blocks()
|> case do |> case do
{:ok, _next, results} -> {:ok, results} {:ok, _next, results} -> {:ok, results}
@ -135,7 +156,7 @@ defmodule EthereumJSONRPC do
def fetch_blocks_by_range(_first.._last = range) do def fetch_blocks_by_range(_first.._last = range) do
range range
|> get_block_by_number_requests() |> get_block_by_number_requests()
|> json_rpc(config(:url)) |> json_rpc(method_to_url(:eth_getBlockByNumber))
|> handle_get_blocks() |> handle_get_blocks()
end end
@ -158,7 +179,7 @@ defmodule EthereumJSONRPC do
def fetch_block_number_by_tag(tag) when tag in ~w(earliest latest pending) do def fetch_block_number_by_tag(tag) when tag in ~w(earliest latest pending) do
tag tag
|> get_block_by_tag_request() |> get_block_by_tag_request()
|> json_rpc(config(:url)) |> json_rpc(method_to_url(:eth_getBlockByNumber))
|> handle_get_block_by_tag() |> handle_get_block_by_tag()
end end

@ -3,7 +3,7 @@ defmodule EthereumJSONRPC.Parity do
Ethereum JSONRPC methods that are only supported by [Parity](https://wiki.parity.io/). Ethereum JSONRPC methods that are only supported by [Parity](https://wiki.parity.io/).
""" """
import EthereumJSONRPC, only: [config: 1, id_to_params: 1, json_rpc: 2, request: 1] import EthereumJSONRPC, only: [id_to_params: 1, method_to_url: 1, json_rpc: 2, request: 1]
alias EthereumJSONRPC.Parity.Traces alias EthereumJSONRPC.Parity.Traces
alias EthereumJSONRPC.{Transaction, Transactions} alias EthereumJSONRPC.{Transaction, Transactions}
@ -19,7 +19,7 @@ defmodule EthereumJSONRPC.Parity do
with {:ok, responses} <- with {:ok, responses} <-
id_to_params id_to_params
|> trace_replay_transaction_requests() |> trace_replay_transaction_requests()
|> json_rpc(config(:trace_url)) do |> json_rpc(method_to_url(:trace_replayTransaction)) do
trace_replay_transaction_responses_to_internal_transactions_params(responses, id_to_params) trace_replay_transaction_responses_to_internal_transactions_params(responses, id_to_params)
end end
end end
@ -35,7 +35,7 @@ defmodule EthereumJSONRPC.Parity do
with {:ok, transactions} <- with {:ok, transactions} <-
%{id: 1, method: "parity_pendingTransactions", params: []} %{id: 1, method: "parity_pendingTransactions", params: []}
|> request() |> request()
|> json_rpc(config(:url)) do |> json_rpc(method_to_url(:parity_pendingTransactions)) do
transactions_params = transactions_params =
transactions transactions
|> Transactions.to_elixir() |> Transactions.to_elixir()

@ -1,7 +1,7 @@
{ {
"coverage_options": { "coverage_options": {
"treat_no_relevant_lines_as_covered": true, "treat_no_relevant_lines_as_covered": true,
"minimum_coverage": 94.4 "minimum_coverage": 94.5
}, },
"terminal_options": { "terminal_options": {
"file_column_width": 120 "file_column_width": 120

Loading…
Cancel
Save