-
Notifications
You must be signed in to change notification settings - Fork 59
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
Changes from 9 commits
186a8df
e5028fe
36c68bc
2d9120d
4198b67
2542bf5
14bb243
d84eed5
66494cd
ab70132
cbffe74
bbddcb2
4830df2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
""" | ||
|
@@ -36,54 +38,88 @@ defmodule OMG.Eth.DevGeth do | |
geth = ~s(geth --miner.gastarget 7500000 \ | ||
--nodiscover \ | ||
--maxpeers 0 \ | ||
--miner.gasprice \"10\" \ | ||
--datadir /data/ \ | ||
--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 -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't it be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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