Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: use exexec from upstream #1743

Merged
merged 13 commits into from
Oct 1, 2020
2 changes: 1 addition & 1 deletion apps/omg_eth/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule OMG.Eth.MixProject do
{:omg_utils, in_umbrella: true},
{:omg_db, in_umbrella: true},
# TEST ONLY
{:exexec, git: "https://github.com/pthomalla/exexec.git", branch: "add_streams", only: [:dev, :test]},
{:exexec, "~> 0.2.0", only: [:dev, :test]},
{:briefly, "~> 0.3.0", only: [:dev, :test]}
]
end
Expand Down
80 changes: 58 additions & 22 deletions apps/omg_eth/test/support/dev_geth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

defmodule OMG.Eth.DevGeth do
use GenServer

@moduledoc """
Helper module for deployment of contracts to dev geth.
"""
Expand All @@ -36,54 +38,88 @@ defmodule OMG.Eth.DevGeth do
geth = ~s(geth --miner.gastarget 7500000 \
--nodiscover \
--maxpeers 0 \
--miner.gasprice \"10\" \
--datadir /data/ \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second datadir is on line 50

--miner.gasprice "10" \
--syncmode 'full' \
--networkid 1337 \
--gasprice '1' \
--keystore #{keystore} \
--password /tmp/geth-blank-password \
--unlock \"0,1\" \
--unlock "0,1" \
--rpc --rpcapi personal,web3,eth,net --rpcaddr 0.0.0.0 --rpcvhosts='*' --rpcport=8545 \
--ws --wsaddr 0.0.0.0 --wsorigins='*' \
--allow-insecure-unlock \
--mine --datadir #{datadir} 2>&1)
geth_pid = launch(geth)
_pid = launch(geth)

{:ok, :ready} = WaitFor.eth_rpc(20_000)

on_exit = fn -> stop(geth_pid) end
on_exit = fn ->
Exexec.run("pkill -9 geth")
end

{:ok, on_exit}
end

# PRIVATE
@impl true
def init(cmd) do
_ = Logger.debug("Starting geth")

defp stop(pid) do
# NOTE: monitor is required to stop_and_wait, don't know why? `monitor: true` on run doesn't work
_ = Process.monitor(pid)
{:ok, geth_proc, os_proc} = Exexec.run(cmd, stdout: true)

{:exit_status, 35_072} = Exexec.stop_and_wait(pid)
:ok
{:ok, %{geth_proc: geth_proc, os_proc: os_proc, ready?: false}}
end

@impl true
def handle_info({:stdout, pid, stdout}, %{os_proc: pid} = state) do
new_state =
if String.contains?(stdout, "IPC endpoint opened") do
Map.put(state, :ready?, true)
else
state
end

if Application.get_env(:omg_eth, :node_logging_in_debug) do
ayrat555 marked this conversation as resolved.
Show resolved Hide resolved
_ = Logger.debug("eth node: " <> stdout)
end

{:noreply, new_state}
end

@impl true
def handle_call(:ready?, _from, state) do
{:reply, state.ready?, state}
end

# PRIVATE

defp launch(cmd) do
_ = Logger.debug("Starting geth")
{:ok, pid} = start_link(cmd)

{:ok, geth_proc, _ref, [{:stream, geth_out, _stream_server}]} =
Exexec.run(cmd, stdout: :stream, kill_command: "pkill -9 geth")
waiting_task_function = fn ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think function part in var name in unnecessary. We can take a look on the right side of the assignment and we know it's a function.

wait_for_rpc(pid)
end

wait_for_geth_start(geth_out)
waiting_task_function
|> Task.async()
|> Task.await(15_000)

_ =
if Application.get_env(:omg_eth, :node_logging_in_debug) do
%Task{} = Task.async(fn -> Enum.each(geth_out, &Support.DevNode.default_logger/1) end)
end
pid
end

defp wait_for_rpc(pid) do
if ready?(pid) do
:ok
else
Process.sleep(1_000)
ready?(pid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be wait_for_rpc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right

end
end

geth_proc
defp start_link(cmd) do
GenServer.start_link(__MODULE__, cmd)
end

defp wait_for_geth_start(geth_out) do
Support.DevNode.wait_for_start(geth_out, "IPC endpoint opened", 15_000)
defp ready?(pid) do
GenServer.call(pid, :ready?)
end
end
21 changes: 0 additions & 21 deletions apps/omg_eth/test/support/dev_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,4 @@ defmodule Support.DevNode do
def start() do
OMG.Eth.DevGeth.start()
end

def wait_for_start(outstream, look_for, timeout, logger_fn \\ &default_logger/1) do
# Monitors the stdout coming out of a process for signal of successful startup
waiting_task_function = fn ->
outstream
|> Stream.map(logger_fn)
|> Stream.take_while(fn line -> not String.contains?(line, look_for) end)
|> Enum.to_list()
end

waiting_task_function
|> Task.async()
|> Task.await(timeout)

:ok
end

def default_logger(line) do
_ = Logger.debug("eth node: " <> line)
line
end
end
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"ex_rlp": {:hex, :ex_rlp, "0.5.3", "9055bddade545ee3e734aaad62c4b4d08211834da3beb43ae269b75785909e5e", [:mix], [], "hexpm", "a755a5f8f9f66079f3ecbe021536b949077fac0df963d9e59a20321bab28722d"},
"ex_unit_fixtures": {:git, "https://github.com/omisego/ex_unit_fixtures.git", "4a099c621dc70e0d65cb9619b38192e31ec5f504", [branch: "feature/require_files_not_load"]},
"excoveralls": {:hex, :excoveralls, "0.12.3", "2142be7cb978a3ae78385487edda6d1aff0e482ffc6123877bb7270a8ffbcfe0", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "568a3e616c264283f5dea5b020783ae40eef3f7ee2163f7a67cbd7b35bcadada"},
"exexec": {:git, "https://github.com/pthomalla/exexec.git", "213658a4ccb96d7a84ffddc642b2596c86acc5d0", [branch: "add_streams"]},
"exexec": {:hex, :exexec, "0.2.0", "a6ffc48cba3ac9420891b847e4dc7120692fb8c08c9e82220ebddc0bb8d96103", [:mix], [{:erlexec, "~> 1.10", [hex: :erlexec, repo: "hexpm", optional: false]}], "hexpm", "312cd1c9befba9e078e57f3541e4f4257eabda6eb9c348154fe899d6ac633299"},
"exth_crypto": {:hex, :exth_crypto, "0.1.6", "8e636a9bcb75d8e32451be96e547a495121ed2178d078db294edb0f81f7cf2e8", [:mix], [{:binary, "~> 0.0.4", [hex: :binary, repo: "hexpm", optional: false]}, {:keccakf1600, "~> 2.0.0", [hex: :keccakf1600_orig, repo: "hexpm", optional: false]}, {:libsecp256k1, "~> 0.1.9", [hex: :libsecp256k1, repo: "hexpm", optional: false]}], "hexpm", "45d6faf4b889f8fc526deba059e0c7947423784ab1e7fa85be8db4c46cf4416b"},
"fake_server": {:hex, :fake_server, "2.1.0", "aefed08a587e2498fdb39ac9de6f9eabbe7bd83da9801d08d3574d61b7eb03d5", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm", "3200d57a523b27d2c8ebfc1a80b76697b3c8a06bf9d678d82114f5f98d350c75"},
"hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "3bf0bebbd5d3092a3543b783bf065165fa5d3ad4b899b836810e513064134e18"},
Expand Down