From c0442c117e8ad1883bd7567283ecee04c1ee5545 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Fri, 14 Aug 2026 13:37:53 +0200 Subject: [PATCH] Add persistent (in cache dir) session_id --- rebar.config | 2 +- rebar.lock | 2 +- src/gmhc_connector.erl | 1 + src/gmhc_session.erl | 69 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/gmhc_session.erl diff --git a/rebar.config b/rebar.config index 9dde3e3..8ee56cc 100644 --- a/rebar.config +++ b/rebar.config @@ -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"}}}, diff --git a/rebar.lock b/rebar.lock index 56f1326..fa1d758 100644 --- a/rebar.lock +++ b/rebar.lock @@ -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", diff --git a/src/gmhc_connector.erl b/src/gmhc_connector.erl index 66b4105..dfcf33a 100644 --- a/src/gmhc_connector.erl +++ b/src/gmhc_connector.erl @@ -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 diff --git a/src/gmhc_session.erl b/src/gmhc_session.erl new file mode 100644 index 0000000..7f83719 --- /dev/null +++ b/src/gmhc_session.erl @@ -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.