Add persistent (in cache dir) session_id

This commit is contained in:
Ulf Wiger
2026-08-14 13:37:53 +02:00
parent df438b3871
commit c0442c117e
4 changed files with 72 additions and 2 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
{enoise, {git, "https://git.qpq.swiss/QPQ-AG/enoise.git", {ref, "029292817e"}}},
{gmhive_protocol,
{git, "https://git.qpq.swiss/QPQ-AG/gmhive_protocol.git",
{ref, "8d4652a"}}},
{ref, "9eec871"}}},
{gmhive_worker, {git, "https://git.qpq.swiss/QPQ-AG/gmhive_worker", {ref, "cabd104114"}}},
{gmconfig, {git, "https://git.qpq.swiss/QPQ-AG/gmconfig.git",
{ref, "38620ff9e2"}}},
+1 -1
View File
@@ -25,7 +25,7 @@
1},
{<<"gmhive_protocol">>,
{git,"https://git.qpq.swiss/QPQ-AG/gmhive_protocol.git",
{ref,"8d4652a79a2ad8f51e1fe560c15c698d4c92485c"}},
{ref,"9eec871e954b729cc5c9249da81abb91de03ad39"}},
0},
{<<"gmhive_worker">>,
{git,"https://git.qpq.swiss/QPQ-AG/gmhive_worker",
+1
View File
@@ -456,6 +456,7 @@ protocol_connect(Opts, #st{econn = EConn} = S) ->
, client => Client
, type => Type
, nonces => gmhc_server:total_nonces()
, session_id => gmhc_session:session_id()
, signature => ""},
?LOG_DEBUG("ConnectReq = ~p", [ConnectReq]),
try gmhp_msgs:encode_connect(ConnectReq, RId) of
+69
View File
@@ -0,0 +1,69 @@
-module(gmhc_session).
-vsn("0.8.3").
%% Persistent mining session id, stored separately from the Eureka
%% connection-info cache. `gmhc_eureka:invalidate_cache/0` must not
%% delete this file.
-export([ session_id/0
, filename/0
]).
-include_lib("kernel/include/logger.hrl").
session_id() ->
File = filename(),
case read_session_id(File) of
{ok, Id} ->
Id;
{error, _} ->
Id = new_session_id(),
_ = write_session_id(File, Id),
Id
end.
filename() ->
Pubkey = gmhc_config:get_config([<<"pubkey">>]),
Network = gmhc_config:get_config([<<"network">>]),
Path = filename:join(gmhc_eureka:cache_dir(), Network),
<<"ak_", PKShort:8/binary, _/binary>> = Pubkey,
filename:join(Path, "gmhc_session." ++ binary_to_list(PKShort) ++ ".id").
new_session_id() ->
gmser_api_encoder:encode(bytearray, crypto:strong_rand_bytes(32)).
read_session_id(File) ->
Pubkey = gmhc_config:get_config([<<"pubkey">>]),
Network = gmhc_config:get_config([<<"network">>]),
case file:read_file(File) of
{ok, Bin} ->
try binary_to_term(Bin) of
#{ session_id := Id
, network := N
, pubkey := PK } when N == Network, PK == Pubkey,
is_binary(Id) ->
case gmser_api_encoder:safe_decode(bytearray, Id) of
{ok, _} -> {ok, Id};
_ -> {error, invalid_session_id}
end;
_ ->
{error, invalid_session_term}
catch
error:E ->
{error, {invalid_session_data, E}}
end;
{error, _} = Err ->
Err
end.
write_session_id(File, Id) ->
Term = #{ session_id => Id
, pubkey => gmhc_config:get_config([<<"pubkey">>])
, network => gmhc_config:get_config([<<"network">>]) },
case filelib:ensure_dir(File) of
ok ->
file:write_file(File, term_to_binary(Term));
{error, _} = Err ->
?LOG_ERROR("Cannot save session id to ~s: ~p", [File, Err]),
Err
end.