Fork gen_server

This commit is contained in:
2026-05-18 21:23:16 +09:00
parent 9b2fe74e83
commit 0dde228e6b
2 changed files with 1578 additions and 55 deletions
+49 -55
View File
@@ -8,7 +8,7 @@
-copyright("QPQ AG <info@qpq.swiss>"). -copyright("QPQ AG <info@qpq.swiss>").
-license("GPL-3.0-or-later"). -license("GPL-3.0-or-later").
-behavior(gen_server). % -behavior(gd_server).
-export([show_ui/1, -export([show_ui/1,
open_wallet/2, close_wallet/0, new_wallet/4, import_wallet/3, drop_wallet/2, open_wallet/2, close_wallet/0, new_wallet/4, import_wallet/3, drop_wallet/2,
selected/1, network/0, selected/1, network/0,
@@ -25,6 +25,7 @@
-export([save/2]). -export([save/2]).
-export([start_link/0, stop/0]). -export([start_link/0, stop/0]).
-export([init/1, terminate/2, code_change/3, -export([init/1, terminate/2, code_change/3,
system_get_state/1, format_status/1,
handle_call/3, handle_cast/2, handle_info/2]). handle_call/3, handle_cast/2, handle_info/2]).
-include("$zx_include/zx_logger.hrl"). -include("$zx_include/zx_logger.hrl").
-include("gd.hrl"). -include("gd.hrl").
@@ -66,7 +67,7 @@
when Name :: ui_name(). when Name :: ui_name().
show_ui(Name) -> show_ui(Name) ->
gen_server:call(?MODULE, {show_ui, Name}). gd_server:call(?MODULE, {show_ui, Name}).
-spec open_wallet(Path, Phrase) -> Result -spec open_wallet(Path, Phrase) -> Result
@@ -75,13 +76,13 @@ 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}, infinity). gd_server:call(?MODULE, {open_wallet, Path, Phrase}, infinity).
-spec close_wallet() -> ok. -spec close_wallet() -> ok.
close_wallet() -> close_wallet() ->
gen_server:cast(?MODULE, close_wallet). gd_server:cast(?MODULE, close_wallet).
-spec new_wallet(Net, Name, Path, Password) -> ok -spec new_wallet(Net, Name, Path, Password) -> ok
@@ -91,7 +92,7 @@ close_wallet() ->
Password :: string(). Password :: string().
new_wallet(Net, Name, Path, Password) -> new_wallet(Net, Name, Path, Password) ->
gen_server:cast(?MODULE, {new_wallet, Net, Name, Path, Password}). gd_server:cast(?MODULE, {new_wallet, Net, Name, Path, Password}).
-spec import_wallet(Name, Path, Password) -> ok -spec import_wallet(Name, Path, Password) -> ok
@@ -100,7 +101,7 @@ new_wallet(Net, Name, Path, Password) ->
Password :: string(). Password :: string().
import_wallet(Name, Path, Password) -> import_wallet(Name, Path, Password) ->
gen_server:cast(?MODULE, {import_wallet, Name, Path, Password}). gd_server:cast(?MODULE, {import_wallet, Name, Path, Password}).
-spec drop_wallet(Path, Delete) -> ok -spec drop_wallet(Path, Delete) -> ok
@@ -108,21 +109,21 @@ import_wallet(Name, Path, Password) ->
Delete :: boolean(). Delete :: boolean().
drop_wallet(Path, Delete) -> drop_wallet(Path, Delete) ->
gen_server:cast(?MODULE, {drop_wallet, Path, Delete}). gd_server:cast(?MODULE, {drop_wallet, Path, Delete}).
-spec selected(Index) -> ok -spec selected(Index) -> ok
when Index :: pos_integer() | none. when Index :: pos_integer() | none.
selected(Index) -> selected(Index) ->
gen_server:cast(?MODULE, {selected, Index}). gd_server:cast(?MODULE, {selected, Index}).
-spec network() -> {ok, NetworkID} | none -spec network() -> {ok, NetworkID} | none
when NetworkID :: binary(). when NetworkID :: binary().
network() -> network() ->
gen_server:call(?MODULE, network). gd_server:call(?MODULE, network).
-spec password(Old, New) -> ok -spec password(Old, New) -> ok
@@ -130,13 +131,13 @@ network() ->
New :: none | string(). New :: none | string().
password(Old, New) -> password(Old, New) ->
gen_server:cast(?MODULE, {password, Old, New}). gd_server:cast(?MODULE, {password, Old, New}).
-spec refresh() -> ok. -spec refresh() -> ok.
refresh() -> refresh() ->
gen_server:cast(?MODULE, refresh). gd_server:cast(?MODULE, refresh).
-spec nonce(ID) -> {ok, Nonce} | {error, Reason} -spec nonce(ID) -> {ok, Nonce} | {error, Reason}
@@ -145,48 +146,48 @@ refresh() ->
Reason :: term(). % FIXME Reason :: term(). % FIXME
nonce(ID) -> nonce(ID) ->
gen_server:call(?MODULE, {nonce, ID}). gd_server:call(?MODULE, {nonce, ID}).
-spec spend(TX) -> ok -spec spend(TX) -> ok
when TX :: #spend_tx{}. when TX :: #spend_tx{}.
spend(TX) -> spend(TX) ->
gen_server:cast(?MODULE, {spend, TX}). gd_server:cast(?MODULE, {spend, TX}).
-spec chain_id() -> {ok, ID} -spec chain_id() -> {ok, ID}
when ID :: binary(). when ID :: binary().
chain_id() -> chain_id() ->
gen_server:call(?MODULE, chain_id). gd_server:call(?MODULE, chain_id).
-spec grids(string()) -> ok. -spec grids(string()) -> ok.
grids(String) -> grids(String) ->
gen_server:cast(?MODULE, {grids, String}). gd_server:cast(?MODULE, {grids, String}).
-spec sign_mess(Request) -> ok -spec sign_mess(Request) -> ok
when Request :: map(). when Request :: map().
sign_mess(Request) -> sign_mess(Request) ->
gen_server:cast(?MODULE, {sign_mess, Request}). gd_server:cast(?MODULE, {sign_mess, Request}).
-spec sign_binary(Request) -> ok -spec sign_binary(Request) -> ok
when Request :: map(). when Request :: map().
sign_binary(Request) -> sign_binary(Request) ->
gen_server:cast(?MODULE, {sign_binary, Request}). gd_server:cast(?MODULE, {sign_binary, Request}).
-spec sign_tx(Request) -> ok -spec sign_tx(Request) -> ok
when Request :: map(). when Request :: map().
sign_tx(Request) -> sign_tx(Request) ->
gen_server:cast(?MODULE, {sign_tx, Request}). gd_server:cast(?MODULE, {sign_tx, Request}).
-spec sign_call(ChainID, PubKey, TX) -> Result -spec sign_call(ChainID, PubKey, TX) -> Result
@@ -197,14 +198,14 @@ sign_tx(Request) ->
| {error, Reason :: term()}. | {error, Reason :: term()}.
sign_call(ChainID, PubKey, TX) -> sign_call(ChainID, PubKey, TX) ->
gen_server:call(?MODULE, {sign_call, ChainID, PubKey, TX}). gd_server:call(?MODULE, {sign_call, ChainID, PubKey, TX}).
-spec deploy(Build) -> ok -spec deploy(Build) -> ok
when Build :: map(). when Build :: map().
deploy(Build) -> deploy(Build) ->
gen_server:cast(?MODULE, {deploy, Build}). gd_server:cast(?MODULE, {deploy, Build}).
-spec prompt_call(FunDef, ConID, Build) -> ok -spec prompt_call(FunDef, ConID, Build) -> ok
@@ -215,7 +216,7 @@ deploy(Build) ->
Build :: map(). % Fixme Build :: map(). % Fixme
prompt_call(FunDef, ConID, Build) -> prompt_call(FunDef, ConID, Build) ->
gen_server:cast(?MODULE, {prompt_call, FunDef, ConID, Build}). gd_server:cast(?MODULE, {prompt_call, FunDef, ConID, Build}).
-spec list_calls() -> Calls -spec list_calls() -> Calls
@@ -227,7 +228,7 @@ prompt_call(FunDef, ConID, Build) ->
%% List any active contract call tasks. %% List any active contract call tasks.
list_calls() -> list_calls() ->
gen_server:call(?MODULE, list_calls). gd_server:call(?MODULE, list_calls).
-spec open_contract(ConID) -> ok -spec open_contract(ConID) -> ok
@@ -247,7 +248,7 @@ open_contract(ConID) ->
%% The controller will start the doomweaver if it isn't already on. %% The controller will start the doomweaver if it isn't already on.
open_contract(ConID, DevmanToFront) when is_binary(ConID) -> open_contract(ConID, DevmanToFront) when is_binary(ConID) ->
gen_server:cast(?MODULE, {open_contract, ConID, DevmanToFront}); gd_server:cast(?MODULE, {open_contract, ConID, DevmanToFront});
open_contract(ConID, DevmanToFront) when is_list(ConID) -> open_contract(ConID, DevmanToFront) when is_list(ConID) ->
open_contract(list_to_binary(ConID), DevmanToFront). open_contract(list_to_binary(ConID), DevmanToFront).
@@ -272,7 +273,7 @@ show_call(ConID, Info) ->
%% Starts the doomweaver if it isn't already running. %% Starts the doomweaver if it isn't already running.
show_call(ConID, Info, DevmanToFront) when is_binary(ConID) -> show_call(ConID, Info, DevmanToFront) when is_binary(ConID) ->
gen_server:cast(?MODULE, {show_call, ConID, Info, DevmanToFront}); gd_server:cast(?MODULE, {show_call, ConID, Info, DevmanToFront});
show_call(ConID, Info, DevmanToFront) when is_list(ConID) -> show_call(ConID, Info, DevmanToFront) when is_list(ConID) ->
show_call(list_to_binary(ConID), Info, DevmanToFront). show_call(list_to_binary(ConID), Info, DevmanToFront).
@@ -293,14 +294,14 @@ show_call(ConID, Info, DevmanToFront) when is_list(ConID) ->
%% system this will change quite a lot. %% system this will change quite a lot.
make_key({eddsa, ed25519}, 256, Name, Seed, Encoding, Transform) -> make_key({eddsa, ed25519}, 256, Name, Seed, Encoding, Transform) ->
gen_server:cast(?MODULE, {make_key, Name, Seed, Encoding, Transform}). gd_server:cast(?MODULE, {make_key, Name, Seed, Encoding, Transform}).
-spec recover_key(Mnemonic) -> ok -spec recover_key(Mnemonic) -> ok
when Mnemonic :: string(). when Mnemonic :: string().
recover_key(Mnemonic) -> recover_key(Mnemonic) ->
gen_server:cast(?MODULE, {recover_key, Mnemonic}). gd_server:cast(?MODULE, {recover_key, Mnemonic}).
-spec mnemonic(ID) -> {ok, Mnemonic} | error -spec mnemonic(ID) -> {ok, Mnemonic} | error
@@ -308,7 +309,7 @@ recover_key(Mnemonic) ->
Mnemonic :: string(). Mnemonic :: string().
mnemonic(ID) -> mnemonic(ID) ->
gen_server:call(?MODULE, {mnemonic, ID}). gd_server:call(?MODULE, {mnemonic, ID}).
-spec rename_key(ID, NewName) -> ok -spec rename_key(ID, NewName) -> ok
@@ -316,14 +317,14 @@ mnemonic(ID) ->
NewName :: string(). NewName :: string().
rename_key(ID, NewName) -> rename_key(ID, NewName) ->
gen_server:cast(?MODULE, {rename_key, ID, NewName}). gd_server:cast(?MODULE, {rename_key, ID, NewName}).
-spec drop_key(ID) -> ok -spec drop_key(ID) -> ok
when ID :: gajudesk:id(). when ID :: gajudesk:id().
drop_key(ID) -> drop_key(ID) ->
gen_server:cast(?MODULE, {drop_key, ID}). gd_server:cast(?MODULE, {drop_key, ID}).
-spec list_keys() -> Result -spec list_keys() -> Result
@@ -331,7 +332,7 @@ drop_key(ID) ->
| error. | error.
list_keys() -> list_keys() ->
gen_server:call(?MODULE, list_keys). gd_server:call(?MODULE, list_keys).
%%% Network functions %%% Network functions
@@ -340,25 +341,25 @@ list_keys() ->
when New :: #node{}. when New :: #node{}.
add_node(New) -> add_node(New) ->
gen_server:cast(?MODULE, {add_node, New}). gd_server:cast(?MODULE, {add_node, New}).
-spec set_sole_node(TheOneTrueNode) -> ok -spec set_sole_node(TheOneTrueNode) -> ok
when TheOneTrueNode :: #node{}. when TheOneTrueNode :: #node{}.
set_sole_node(TheOneTrueNode) -> set_sole_node(TheOneTrueNode) ->
gen_server:cast(?MODULE, {set_sole_node, TheOneTrueNode}). gd_server:cast(?MODULE, {set_sole_node, TheOneTrueNode}).
-spec tic(Interval) -> ok -spec tic(Interval) -> ok
when Interval :: pos_integer() | stop. when Interval :: pos_integer() | stop.
tic(stop) -> tic(stop) ->
gen_server:cast(?MODULE, {tic, stop}); gd_server:cast(?MODULE, {tic, stop});
tic(0) -> tic(0) ->
gen_server:cast(?MODULE, {tic, stop}); gd_server:cast(?MODULE, {tic, stop});
tic(Interval) when Interval -> tic(Interval) when Interval ->
gen_server:cast(?MODULE, {tic, Interval}). gd_server:cast(?MODULE, {tic, Interval}).
-spec update_balance(AccountID, Pucks) -> ok -spec update_balance(AccountID, Pucks) -> ok
@@ -366,14 +367,14 @@ tic(Interval) when Interval ->
Pucks :: non_neg_integer(). Pucks :: non_neg_integer().
update_balance(AccountID, Pucks) -> update_balance(AccountID, Pucks) ->
gen_server:cast(?MODULE, {update_balance, AccountID, Pucks}). gd_server:cast(?MODULE, {update_balance, AccountID, Pucks}).
%%% Lifecycle functions %%% Lifecycle functions
-spec stop() -> ok. -spec stop() -> ok.
stop() -> stop() ->
gen_server:cast(?MODULE, stop). gd_server:cast(?MODULE, stop).
-spec save(Module, Prefs) -> ok | {error, Reason} -spec save(Module, Prefs) -> ok | {error, Reason}
@@ -382,7 +383,7 @@ stop() ->
Reason :: file:posix(). Reason :: file:posix().
save(Module, Prefs) -> save(Module, Prefs) ->
gen_server:call(?MODULE, {save, Module, Prefs}). gd_server:call(?MODULE, {save, Module, Prefs}).
@@ -399,14 +400,14 @@ save(Module, Prefs) ->
%% Called by gd_sup. %% Called by gd_sup.
start_link() -> start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []). gd_server:start_link({local, ?MODULE}, ?MODULE, none, []).
-spec init(none) -> {ok, state()}. -spec init(none) -> {ok, state()}.
init(none) -> init(none) ->
ok = log(info, "Starting"), ok = log(info, "Starting"),
process_flag(sensitive, true), _ = process_flag(sensitive, true),
{FirstRun, Prefs} = read_prefs(), {FirstRun, Prefs} = read_prefs(),
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),
@@ -434,9 +435,6 @@ default_tic() ->
%%% gen_server Message Handling Callbacks
-spec handle_call(Message, From, State) -> Result -spec handle_call(Message, From, State) -> Result
when Message :: term(), when Message :: term(),
From :: {pid(), reference()}, From :: {pid(), reference()},
@@ -446,9 +444,6 @@ default_tic() ->
Response :: ok Response :: ok
| {error, {listening, inet:port_number()}}, | {error, {listening, inet:port_number()}},
NewState :: state(). NewState :: state().
%% @private
%% The gen_server:handle_call/3 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_call-3
handle_call(list_keys, _, State) -> handle_call(list_keys, _, State) ->
Response = do_list_keys(State), Response = do_list_keys(State),
@@ -486,9 +481,6 @@ handle_call(Unexpected, From, State) ->
when Message :: term(), when Message :: term(),
State :: state(), State :: state(),
NewState :: state(). NewState :: state().
%% @private
%% The gen_server:handle_cast/2 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_cast-2
handle_cast(close_wallet, State) -> handle_cast(close_wallet, State) ->
NextState = do_close_wallet(State), NextState = do_close_wallet(State),
@@ -574,9 +566,6 @@ handle_cast(Unexpected, State) ->
when Message :: term(), when Message :: term(),
State :: state(), State :: state(),
NewState :: state(). NewState :: state().
%% @private
%% The gen_server:handle_info/2 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_info-2
handle_info(tic, State) -> handle_info(tic, State) ->
NewState = handle_tic(State), NewState = handle_tic(State),
@@ -603,10 +592,15 @@ handle_down(Mon, PID, Info, State = #s{tasks = Tasks}) ->
end. end.
system_get_state(State) ->
{ok, sanitize(State)}.
format_status(Status = #{state := State}) ->
Status#{state := sanitize(State)}.
sanitize(State) ->
State#s{wallet = nope, pass = nope, wallets = []}.
%% @private
%% gen_server callback to handle state transformations necessary for hot
%% code updates. This template performs no transformation.
code_change(_, State, _) -> code_change(_, State, _) ->
{ok, State}. {ok, State}.
+1529
View File
File diff suppressed because it is too large Load Diff