From 570f31ab3c189af00268d9e0bb52e7dfe546eb57 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Mon, 13 Oct 2025 13:57:34 +0200 Subject: [PATCH] Network specific cache filename + more sanity checks --- README.md | 14 ++++++++++ ebin/gmhive_client.app | 2 +- src/gmhc_eureka.erl | 59 ++++++++++++++++++++++++++++++------------ zomp.meta | 2 +- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 7df0c93..1f61351 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,20 @@ only does this inform the client of the network for which the client should mine, but it also changes what URL the client will use to find the hive server. (For `"testnet"` this will be `"test.gajumining.com"`.) +### Caching of connection data + +The client retrieves the Hive Server connection info from gajumining.com. +This information is account-specific, and may change over time. + +To speed up the reconnecting process, should the Hive Server restart, +this connection information is cached locally for up to 24 hours. +The normal place for the cached data would be +`$ZOMP_DIR//var/uwiger/gmhive_client/Vsn/setup.data/mainnet/gmhc_eureka.XXXXXXXX.cache`, +where `Vsn` is the current version of `gmhive_client`, and `XXXXXXXX` is a fragment +of the current account ID. The data location can be changed with the command-line +option `-setup data_dir Dir`. + + ## JSON Schema ```json diff --git a/ebin/gmhive_client.app b/ebin/gmhive_client.app index 4af5114..8b15bbc 100644 --- a/ebin/gmhive_client.app +++ b/ebin/gmhive_client.app @@ -1,6 +1,6 @@ {application,gmhive_client, [{description,"Gajumaru Hive Client"}, - {vsn,"0.8.0"}, + {vsn,"0.8.1"}, {registered,[]}, {applications,[kernel,stdlib,sasl,gproc,inets,ssl,enoise, gmconfig,gmhive_protocol,gmhive_worker]}, diff --git a/src/gmhc_eureka.erl b/src/gmhc_eureka.erl index a9ee860..dafdf17 100644 --- a/src/gmhc_eureka.erl +++ b/src/gmhc_eureka.erl @@ -16,7 +16,8 @@ get_pool_address() -> end. cached_address() -> - CacheF = cache_filename(), + I0 = cache_info(), + CacheF = cache_filename(I0), ?LOG_DEBUG("Eureka cache filename: ~p", [CacheF]), case file:read_file(CacheF) of {ok, Bin} -> @@ -24,9 +25,12 @@ cached_address() -> OldestTS = NowTS - 24*60*60, try binary_to_term(Bin) of #{ ts := TS + , network := N + , pubkey := PK , host := _ , port := _ - , pool_id := _} = Map -> + , pool_id := _} = Map when N == map_get(network, I0), + PK == map_get(pubkey, I0) -> if TS >= OldestTS -> Result = maps:remove(ts, Map), ?LOG_DEBUG("Cached eureka info: ~p", [Result]), @@ -44,20 +48,25 @@ cached_address() -> Err end. -cache_good_address(#{host := Addr, - port := Port, - pool_id := PoolId}) -> - CacheF = cache_filename(), - ToCache = #{ host => unicode:characters_to_binary(Addr) - , port => Port - , pool_id => unicode:characters_to_binary(PoolId) - , ts => erlang:system_time(seconds)}, - case file:write_file(CacheF, term_to_binary(ToCache)) of +cache_good_address(#{host := _, + port := _, + pool_id := _} = I0) -> + CacheInfo = cache_info(I0), + CacheF = cache_filename(CacheInfo), + + ToCache = CacheInfo#{ts => erlang:system_time(seconds)}, + case filelib:ensure_dir(CacheF) of ok -> - ?LOG_DEBUG("Cached eureka info in: ~p", [CacheF]), - {ok, ToCache}; + case file:write_file(CacheF, term_to_binary(ToCache)) of + ok -> + ?LOG_DEBUG("Cached eureka info in: ~p", [CacheF]), + {ok, ToCache}; + {error, _} = Err -> + ?LOG_DEBUG("Couldn't cache eureka in ~p: ~p", [CacheF, Err]), + Err + end; {error, _} = Err -> - ?LOG_DEBUG("Couldn't cache eureka in ~p: ~p", [CacheF, Err]), + ?LOG_ERROR("Cannot save cached info to ~s", [CacheF]), Err end. @@ -72,9 +81,27 @@ invalidate_cache() -> Err end. +cache_info(#{ host := Addr + , port := Port + , pool_id := PoolId }) -> + I0 = cache_info(), + I0#{ host => unicode:characters_to_binary(Addr) + , port => Port + , pool_id => unicode:characters_to_binary(PoolId)}. + +cache_info() -> + Pubkey = gmhc_config:get_config([<<"pubkey">>]), + Network = gmhc_config:get_config([<<"network">>]), + #{ pubkey => Pubkey + , network => Network }. + cache_filename() -> - <<"ak_", PKShort:8/binary, _/binary>> = gmhc_config:get_config([<<"pubkey">>]), - filename:join(setup:data_dir(), "gmhc_eureka." ++ binary_to_list(PKShort) ++ ".cache"). + cache_filename(cache_info()). + +cache_filename(#{network := Network, pubkey := Pubkey}) -> + Path = filename:join(setup:data_dir(), Network), + <<"ak_", PKShort:8/binary, _/binary>> = Pubkey, + filename:join(Path, "gmhc_eureka." ++ binary_to_list(PKShort) ++ ".cache"). get_pool_address_() -> case gmconfig:find_config([<<"pool_admin">>, <<"url">>], [user_config]) of diff --git a/zomp.meta b/zomp.meta index 5a7c7d4..4fe9103 100644 --- a/zomp.meta +++ b/zomp.meta @@ -4,7 +4,7 @@ {prefix,"gmhc"}. {desc,"Gajumaru Hive Client"}. {author,"Ulf Wiger, QPQ AG"}. -{package_id,{"uwiger","gmhive_client",{0,8,0}}}. +{package_id,{"uwiger","gmhive_client",{0,8,1}}}. {deps,[{"uwiger","gmhive_protocol",{0,2,0}}, {"uwiger","gmhive_worker",{0,5,1}}, {"uwiger","gmcuckoo",{1,2,4}},