gmhive_client/src/gmhc_workers.erl
2025-05-21 22:40:40 +02:00

75 lines
3.2 KiB
Erlang

%%%-------------------------------------------------------------------
%%% @copyright (C) 2025, QPQ AG
%%% @copyright (C) 2017, Aeternity Anstalt
%%%
%%% @doc Hive worker configuration logic, based on aec_mining.erl
%%% in the Gajumaru platform (https://git.qpq.swiss/QPQ-AG/gajumaru)
%%% @end
%%%-------------------------------------------------------------------
-module(gmhc_workers).
-export([
get_worker_configs/0
, generate_from_hash/5
]).
-include_lib("kernel/include/logger.hrl").
-define(DEFAULT_EXECUTABLE_GROUP , <<"cpu">>).
-define(DEFAULT_EXTRA_ARGS , <<>>).
-define(DEFAULT_HEX_ENCODED_HEADER , false).
-define(DEFAULT_REPEATS , 1).
-define(DEFAULT_EDGE_BITS , 29).
%%------------------------------------------------------------------------------
%% Read and parse worker configs.
%%
%% Workers defined in gajumaru.{json,yaml} user config file take precedence.
%% If there are no workers defined in the user config, sys.config cuckoo
%% workers are read. If there are neither user config nor sys.config workers
%% ?DEFAULT_CUCKOO_ENV is used as the last resort option (i.e. mean29-generic
%% without any extra args).
%%------------------------------------------------------------------------------
-spec get_worker_configs() -> [gmhw_pow_cuckoo:config()].
get_worker_configs() ->
ConfigMaps = worker_config_map(),
?LOG_DEBUG("ConfigMaps = ~p", [ConfigMaps]),
lists:foldl(
fun(Cfg, Configs) ->
[build_worker_config(Cfg) | Configs]
end, [], ConfigMaps).
-spec generate_from_hash(gmhw_pow_cuckoo:hash(), gmhw_pow:sci_target(),
gmhw_pow:nonce(), gmhw_pow_cuckoo:config(),
gmhw_pow:instance() | undefined) ->
{ok, [{gmhw_pow:nonce(), gmhw_pow_cuckoo:solution()}]} | {error, term()}.
generate_from_hash(Hash, Target, Nonce, Config, WorkerInstance) ->
gmhw_pow_cuckoo:generate_from_hash(Hash, Target, Nonce, Config, WorkerInstance, true).
%% Internal functions.
%%------------------------------------------------------------------------------
%% Config handling
%%------------------------------------------------------------------------------
build_worker_config(Config) when is_map(Config) ->
Exec = maps:get(<<"executable">>, Config),
ExecGroup = maps:get(<<"executable_group">>, Config, ?DEFAULT_EXECUTABLE_GROUP),
ExtraArgs = maps:get(<<"extra_args">>, Config, ?DEFAULT_EXTRA_ARGS),
HexEncHdr = maps:get(<<"hex_encoded_header">>, Config,
hex_encoding_default(ExecGroup, Exec)),
Repeats = maps:get(<<"repeats">>, Config, ?DEFAULT_REPEATS),
Instances = maps:get(<<"addressed_instances">>, Config, undefined),
EdgeBits = ?DEFAULT_EDGE_BITS,
gmhw_pow_cuckoo:config(Exec, ExecGroup, ExtraArgs, HexEncHdr, Repeats, EdgeBits, Instances);
build_worker_config({Exec, ExtraArgs, HexEncHdr, Repeats, Instances, ExecGroup}) ->
EdgeBits = ?DEFAULT_EDGE_BITS,
gmhw_pow_cuckoo:config(Exec, ExecGroup, ExtraArgs, HexEncHdr, Repeats, EdgeBits, Instances).
worker_config_map() ->
gmhc_config:get_config([<<"workers">>]).
hex_encoding_default(_, <<"cuda29">>) -> true;
hex_encoding_default(_, _) -> ?DEFAULT_HEX_ENCODED_HEADER.