iface #16

Merged
zxq9 merged 7 commits from iface into master 2025-11-20 16:02:09 +09:00
2 changed files with 123 additions and 48 deletions
Showing only changes of commit 54a70d4b0b - Show all commits

View File

@ -23,15 +23,16 @@
-record(s,
{version = 1 :: integer(),
window = none :: none | wx:wx_object(),
network = <<"mainnet">> :: binary(), % <<"testnet">> | <<"mainnet">>
acc = none :: none | binary(),
keys = [] :: [],
max_cores = 2 :: pos_integer(),
max_mem = 3550722201 :: pos_integer(),
prefs = #{} :: #{},
gmc_conf = none :: none | reference()}).
{version = 1 :: integer(),
window = none :: none | wx:wx_object(),
network = <<"mainnet">> :: binary(), % <<"testnet">> | <<"mainnet">>
acc = none :: none | binary(),
keys = [] :: [],
max_cores = 2 :: pos_integer(),
max_mem = 3550722201 :: pos_integer(),
prefs = #{} :: #{},
gmc_conf = none :: none | reference(),
toggle = {stop, 0} :: {start | stop, integer()}}).
-type state() :: #s{}.
@ -102,15 +103,15 @@ start_link() ->
init(none) ->
ok = log(info, "Starting"),
{AProcs, AMem} = proc_mem(),
TwoMaps = default_spec(mem) * 2,
TwoGraphs = default_spec(mem) * 2,
RProcs =
case AProcs >= 2 of
true -> 2;
false -> 1
end,
RMem =
case AMem >= TwoMaps of
true -> TwoMaps;
case AMem >= TwoGraphs of
true -> TwoGraphs;
false -> default_spec(mem)
end,
{Acc, Keys, Network, MaxCores, MaxMem} =
@ -125,6 +126,14 @@ init(none) ->
{none, [], <<"mainnet">>, RProcs, RMem}
end,
State = #s{network = Network, acc = Acc, keys = Keys, max_cores = MaxCores, max_mem = MaxMem},
Events =
[pool_notification,
connected,
puzzle,
result,
error,
disconnected],
ok = lists:foreach(fun gmhc_events:ensure_subscribed/1, Events),
{ok, start_gui(State)}.
@ -155,8 +164,8 @@ handle_call(Unexpected, From, State) ->
handle_cast(start_stop, State) ->
ok = do_start_stop(State),
{noreply, State};
NewState = do_start_stop(State),
{noreply, NewState};
handle_cast(gajudesk, State) ->
ok = do_gajudesk(),
{noreply, State};
@ -176,7 +185,7 @@ handle_cast(Unexpected, State) ->
handle_info({gproc_ps_event, Event, Data}, State) ->
ok = gmc_gui:message({Event, Data}),
ok = gproc_ps_event({Event, Data}),
{noreply, State};
handle_info({'DOWN', Mon, process, PID, Info}, State = #s{gmc_conf = Mon}) ->
ok = log(info, "gmc_conf ~p closed with ~p", [PID, Info]),
@ -187,6 +196,15 @@ handle_info(Unexpected, State) ->
{noreply, State}.
gproc_ps_event(Info = {disconnect, #{info := #{reconnecting := false, error := #{code := Code, message := Message}}}}) ->
ok = log(error, "Error terminal event received. Code: ~p. Message: ~p", [Code, Message]),
Outcome = application:stop(gmhive_client),
ok = log(info, "application:stop(gmhivie_client) -> ~p", [Outcome]),
gmc_gui:message(Info);
gproc_ps_event(Info) ->
gmc_gui:message(Info).
code_change(_, State, _) ->
{ok, State}.
@ -209,7 +227,20 @@ do_stop() ->
%%% Doers
do_start_stop(#s{acc = PubKey, keys = Keys, network = Network, max_cores = MaxCores, max_mem = MaxMem}) ->
do_start_stop(State = #s{toggle = {Last, TS}}) ->
Now = erlang:system_time(second),
Clicklimit = Now - 5,
case TS < Clicklimit of
true ->
case Last of
stop -> do_start(State#s{toggle = {start, Now}});
start -> do_stop(State#s{toggle = {stop, Now}})
end;
false -> State
end.
do_start(State = #s{acc = PubKey, keys = Keys, network = Network, max_cores = MaxCores, max_mem = MaxMem}) ->
% smrt guy stuff happens here
{Bits, Eureka} =
case Network of
@ -251,29 +282,31 @@ do_start_stop(#s{acc = PubKey, keys = Keys, network = Network, max_cores = MaxCo
Started = io_lib:format("Apps started:~n ~p", [Apps]),
ok = log(info, Started),
ok = gmc_gui:message({notice, Started}),
Events =
[pool_notification,
connected,
puzzle,
result,
error,
disconnected],
lists:foreach(fun gmhc_events:ensure_subscribed/1, Events).
State.
do_stop(State) ->
ok =
case application:stop(gmhive_client) of
ok -> ok;
Error -> log(warning, "application:stop(gmhive_client) returned: ~p", [Error])
end,
State.
optimize_count(MaxC, MaxM) ->
MapSize = 3550722201,
GraphSize = 3550722201,
MaxCores = max(1, MaxC),
MaxMem = max(MapSize, MaxM),
MaxMem = max(GraphSize, MaxM),
{Procs, Memory} = proc_mem(),
MeanMaps = min(MaxMem, Memory) div MapSize,
Recommended = min(MaxCores, min(Procs, MeanMaps)),
MeanGraphs = min(MaxMem, Memory) div GraphSize,
Recommended = min(MaxCores, min(Procs, MeanGraphs)),
Notice = fun(F, A) -> gmc_gui:message({notice, io_lib:format(F, A)}) end,
ok = Notice("Physical Processors: ~p", [Procs]),
ok = Notice("Physical Memory: ~p", [Memory]),
ok = Notice("Max Processor Commit: ~p", [MaxCores]),
ok = Notice("Max Memory Commit: ~p", [MaxMem]),
ok = Notice("29-bit Mean Map Space: ~p", [MeanMaps]),
ok = Notice("29-bit Mean Graph Space: ~p", [MeanGraphs]),
ok = Notice("Workers: ~p", [Recommended]),
Recommended.

View File

@ -23,21 +23,23 @@
wx = none :: none | wx:wx_object()}).
-record(s,
{wx = none :: none | wx:wx_object(),
frame = none :: none | wx:wx_object(),
lang = en :: en | jp,
j = none :: none | fun(),
id = none :: none | wx:wx_object(),
diff = none :: none | wx:wx_object(),
perf = none :: none | wx:wx_object(),
hist = {ts(), 0} :: {LastTS :: integer(), Average :: integer()},
candy = none :: none | wx:wx_object(),
height = none :: none | wx:wx_object(),
block = none :: none | wx:wx_object(),
% solved = 0 :: non_neg_integer(), % TODO: Add a widget to show this. Maybe
mess = none :: none | wx:wx_object(),
buff = new_buff() :: buff(),
buttons = [] :: [#w{}]}).
{wx = none :: none | wx:wx_object(),
frame = none :: none | wx:wx_object(),
lang = en :: en | jp,
j = none :: none | fun(),
id = none :: none | wx:wx_object(),
diff = none :: none | wx:wx_object(),
perf = none :: none | wx:wx_object(),
hist = {ts(), 0} :: {LastTS :: integer(), Average :: integer()},
candy = none :: none | wx:wx_object(),
height = none :: none | wx:wx_object(),
block = none :: none | wx:wx_object(),
% solved = 0 :: non_neg_integer(), % TODO: Add a widget to show this. Maybe
mess = none :: none | wx:wx_object(),
buff = new_buff() :: buff(),
buttons = [] :: [#w{}],
toggle = start :: stop | start,
toggle_t = none :: none | reference()}).
ts() ->
erlang:system_time(microsecond).
@ -203,6 +205,9 @@ handle_cast(Unexpected, State) ->
{noreply, State}.
handle_info(toggle, State) ->
NewState = toggle(State),
{noreply, NewState};
handle_info(Unexpected, State) ->
ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]),
{noreply, State}.
@ -295,10 +300,29 @@ do_message({pool_notification, #{info := #{msg := MSG}}}, State = #s{height = He
do_message2(Entry, State)
end;
do_message({connected, _}, State) ->
Entry = "\nConnected!",
Entry = "\nConnected",
do_message2(Entry, State);
do_message({disconnected, #{info := #{error := #{code := Code, message := Message}}}}, State = #s{}) ->
% -32000 -> % mining_disabled
% -32001 -> % nyi
% -32002 -> % pool_not_found
% -32003 -> % pool_exists
% -32004 -> % unknown_contract
% -32005 -> % invalid_prefix
% -32006 -> % invalid_encoding
% -32007 -> % outdated
% -32008 -> % solution_mismatch
% -32009 -> % invalid_input
% -32010 -> % session_limit
% -32011 -> % accounts_limit
% -32012 -> % duplicate_accounts
% -32601 -> % unknown_method
% -32603 -> % internal error
Format = "~nAn issue has been reported by the Hive Leader.~nError code: ~p '~s'~nStopping.",
Entry = io_lib:format(Format, [Code, Message]),
do_message2(Entry, State);
do_message({disconnected, _}, State) ->
Entry = "\nDisconnected!",
Entry = "\nDisconnected",
do_message2(Entry, State);
do_message({error, #{info := #{info := #{error := get_failed, data := {error, #{code := 404}}, url := URL}}}}, State) ->
ID =
@ -372,11 +396,29 @@ add_message2(Entry, {OMax, 0, IMax, 0, []}) ->
{append, {OMax, 1, IMax, 1, [[Entry]]}}.
start_stop(State = #s{buttons = Buttons}) ->
start_stop(State = #s{buttons = Buttons, toggle = Last, toggle_t = none}) ->
#w{wx = SSB} = lists:keyfind(start_stop, #w.name, Buttons),
_ = wxButton:disable(SSB),
ok = gmc_con:start_stop(),
State.
Timer = erlang:send_after(5000, self(), toggle),
Next =
case Last of
stop -> start;
start -> stop
end,
State#s{toggle = Next, toggle_t = Timer}.
toggle(State = #s{buttons = Buttons, toggle = Next, j = J}) ->
#w{wx = SSB} = lists:keyfind(start_stop, #w.name, Buttons),
Label =
case Next of
start -> J("Start");
stop -> J("Stop")
end,
ok = wxButton:setLabel(SSB, Label),
_ = wxButton:enable(SSB),
State#s{toggle_t = none}.
gajudesk(State) ->