iface #31

Merged
zxq9 merged 27 commits from iface into master 2025-12-30 22:32:33 +09:00
Showing only changes of commit 52994a12cb - Show all commits

View File

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