Monkeypatch net crash on open

This commit is contained in:
Craig Everett 2025-12-30 16:27:29 +09:00
parent 8a8bee0bbd
commit 52994a12cb

View File

@ -19,6 +19,7 @@
deploy/3, deploy/3,
make_key/6, recover_key/1, mnemonic/1, rename_key/2, drop_key/1, list_keys/0, make_key/6, recover_key/1, mnemonic/1, rename_key/2, drop_key/1, list_keys/0,
add_node/1, set_sole_node/1]). add_node/1, set_sole_node/1]).
-export([tic/1, update_balance/2]).
-export([encrypt/2, decrypt/2]). -export([encrypt/2, decrypt/2]).
-export([save/2]). -export([save/2]).
-export([start_link/0, stop/0]). -export([start_link/0, stop/0]).
@ -41,7 +42,7 @@
-record(s, -record(s,
{version = 1 :: integer(), {version = 1 :: integer(),
window = none :: none | wx:wx_object(), window = none :: none | wx:wx_object(),
timer = none :: none | reference(), timer = none :: none | {Timer :: reference(), MS :: pos_integer()},
tasks = [] :: [#ui{}], tasks = [] :: [#ui{}],
selected = 0 :: non_neg_integer(), selected = 0 :: non_neg_integer(),
wallet = none :: none | #wallet{}, wallet = none :: none | #wallet{},
@ -73,7 +74,7 @@ show_ui(Name) ->
Result :: ok | {error, Reason :: term()}. Result :: ok | {error, Reason :: term()}.
open_wallet(Path, Phrase) -> open_wallet(Path, Phrase) ->
gen_server:call(?MODULE, {open_wallet, Path, Phrase}). gen_server:call(?MODULE, {open_wallet, Path, Phrase}, infinity).
-spec close_wallet() -> ok. -spec close_wallet() -> ok.
@ -278,6 +279,8 @@ list_keys() ->
gen_server:call(?MODULE, list_keys). gen_server:call(?MODULE, list_keys).
%%% Network functions
-spec add_node(New) -> ok -spec add_node(New) -> ok
when New :: #node{}. when New :: #node{}.
@ -292,6 +295,25 @@ set_sole_node(TheOneTrueNode) ->
gen_server:cast(?MODULE, {set_sole_node, TheOneTrueNode}). gen_server:cast(?MODULE, {set_sole_node, TheOneTrueNode}).
-spec tic(Interval) -> ok
when Interval :: pos_integer() | stop.
tic(stop) ->
gen_server:cast(?MODULE, {tic, stop});
tic(0) ->
gen_server:cast(?MODULE, {tic, stop});
tic(Interval) when Interval ->
gen_server:cast(?MODULE, {tic, Interval}).
-spec update_balance(AccountID, Pucks) -> ok
when AccountID :: gajudesk:id(),
Pucks :: non_neg_integer().
update_balance(AccountID, Pucks) ->
gen_server:cast(?MODULE, {update_balance, AccountID, Pucks}).
%%% Lifecycle functions %%% Lifecycle functions
-spec stop() -> ok. -spec stop() -> ok.
@ -334,8 +356,9 @@ init(none) ->
GUI_Prefs = maps:get(gd_gui, Prefs, #{}), GUI_Prefs = maps:get(gd_gui, Prefs, #{}),
Window = gd_gui:start_link(GUI_Prefs), Window = gd_gui:start_link(GUI_Prefs),
Wallets = get_prefs(wallets, Prefs, []), Wallets = get_prefs(wallets, Prefs, []),
T = erlang:send_after(tic(), self(), tic), MS = default_tic(),
State = #s{window = Window, timer = T, wallets = Wallets, prefs = Prefs}, T = erlang:send_after(MS, self(), tic),
State = #s{window = Window, timer = {T, MS}, wallets = Wallets, prefs = Prefs},
NewState = do_show_ui(gd_v_wallman, State), NewState = do_show_ui(gd_v_wallman, State),
ok = ok =
case FirstRun of case FirstRun of
@ -352,7 +375,7 @@ read_prefs() ->
end. end.
tic() -> default_tic() ->
6000. 6000.
@ -476,6 +499,9 @@ handle_cast({add_node, New}, State) ->
handle_cast({set_sole_node, TheOneTrueNode}, State) -> handle_cast({set_sole_node, TheOneTrueNode}, State) ->
NewState = do_set_sole_node(TheOneTrueNode, State), NewState = do_set_sole_node(TheOneTrueNode, State),
{noreply, NewState}; {noreply, NewState};
handle_cast({tic, Interval}, State) ->
NewState = do_tic(Interval, State),
{noreply, NewState};
handle_cast(stop, State) -> handle_cast(stop, State) ->
NewState = do_stop(State), NewState = do_stop(State),
{noreply, NewState}; {noreply, NewState};
@ -493,7 +519,7 @@ handle_cast(Unexpected, State) ->
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_info-2 %% See: http://erlang.org/doc/man/gen_server.html#Module:handle_info-2
handle_info(tic, State) -> handle_info(tic, State) ->
NewState = do_tic(State), NewState = handle_tic(State),
{noreply, NewState}; {noreply, NewState};
handle_info({show_ui, Name}, State) -> handle_info({show_ui, Name}, State) ->
NewState = do_show_ui(Name, State), NewState = do_show_ui(Name, State),
@ -627,7 +653,9 @@ check_balance(ChainID) ->
end. end.
ensure_hz_set(Node = #node{ip = IP, external = Port}) -> ensure_hz_set(Node = #node{ip = IP, external = Port}) ->
tell("Ensuring hz is set..."),
case hz:chain_nodes() of case hz:chain_nodes() of
[{IP, Port}] -> [{IP, Port}] ->
case hz:status() of case hz:status() of
@ -854,7 +882,7 @@ do_network(#s{wallet = #wallet{chain_id = ChainID}}) ->
%%% State Operations %%% Stateless Operations
encrypt(Pass, Binary) -> encrypt(Pass, Binary) ->
Flags = [{encrypt, true}, {padding, pkcs_padding}], Flags = [{encrypt, true}, {padding, pkcs_padding}],
@ -1053,11 +1081,12 @@ do_open_wallet(Path, Phrase, State) ->
{ok, Recovered = #wallet{name = Name, poas = POAs, endpoint = Node}} -> {ok, Recovered = #wallet{name = Name, poas = POAs, endpoint = Node}} ->
ok = gd_gui:show(POAs), ok = gd_gui:show(POAs),
ok = gd_gui:wallet(Name), ok = gd_gui:wallet(Name),
ok = % TODO: set_hz/1 should dispatch async to the gd_netman.
case ensure_hz_set(Node) of % ok = set_hz(Node),
{ok, ChainID} -> gd_gui:chain(ChainID, Node); % case ensure_hz_set(Node) of
Error -> gd_gui:trouble(Error) % {ok, ChainID} -> gd_gui:chain(ChainID, Node);
end, % Error -> gd_gui:trouble(Error)
% end,
{ok, State#s{pass = Pass, wallet = Recovered}}; {ok, State#s{pass = Pass, wallet = Recovered}};
Error -> Error ->
{Error, State} {Error, State}
@ -1298,7 +1327,24 @@ read3(T) ->
end. end.
do_tic(State = #s{wallet = This = #wallet{poas = POAs, endpoint = Node}, selected = Selected}) when Selected > 0 -> do_tic(stop, State = #s{timer = none}) ->
State;
do_tic(stop, State = #s{timer = {T, _}}) ->
ok = erlang:cancel_timer(T, [{async, true}]),
State#s{timer = none};
do_tic(MS, State = #s{timer = none}) ->
T = erlang:send_after(MS, self(), tic),
State#s{timer = {T, MS}};
do_tic(MS, State = #s{timer = {T, _}}) ->
ok = erlang:cancel_timer(T, [{async, true}]),
T = erlang:send_after(MS, self(), tic),
State#s{timer = {T, MS}}.
handle_tic(State = #s{wallet = This = #wallet{poas = POAs, endpoint = Node},
timer = {T, MS},
selected = Selected})
when Selected > 0 ->
NewState = NewState =
case ensure_hz_set(Node) of case ensure_hz_set(Node) of
{ok, ChainID} -> {ok, ChainID} ->
@ -1312,11 +1358,15 @@ do_tic(State = #s{wallet = This = #wallet{poas = POAs, endpoint = Node}, selecte
ok = log(info, "Balance update on tic failed with: ~p", [Error]), ok = log(info, "Balance update on tic failed with: ~p", [Error]),
State State
end, end,
T = erlang:send_after(tic(), self(), tic), ok = erlang:cancel_timer(T, [{async, true}]),
NewState#s{timer = T}; NewT = erlang:send_after(MS, self(), tic),
do_tic(State) -> NewState#s{timer = {NewT, MS}};
T = erlang:send_after(tic(), self(), tic), handle_tic(State = #s{timer = {T, MS}}) ->
State#s{timer = T}. ok = erlang:cancel_timer(T, [{async, true}]),
NewT = erlang:send_after(MS, self(), tic),
State#s{timer = {NewT, MS}};
handle_tic(State) ->
State.