From 29fbfb3c877d39de94b900e5c6c41d6329b17d05 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Tue, 20 May 2025 23:40:54 +0200 Subject: [PATCH] Fix zomp's app file, config from cmd line, add network config --- ebin/gmhive_client.app | 100 +++---------------------------------- src/gmhc_config.erl | 54 +++++++++----------- src/gmhc_config_schema.erl | 18 +++++-- src/gmhc_eureka.erl | 28 ++++++++--- zomp.meta | 6 ++- 5 files changed, 70 insertions(+), 136 deletions(-) diff --git a/ebin/gmhive_client.app b/ebin/gmhive_client.app index 5eb2af3..f200782 100644 --- a/ebin/gmhive_client.app +++ b/ebin/gmhive_client.app @@ -1,102 +1,16 @@ {application,gmhive_client, - [{description,"Gajumaru Hive client"}, - {vsn,"0.2.1"}, + [{description,"Gajumaru Hive Client"}, + {vsn,"0.1.0"}, {registered,[]}, - {applications,[kernel,stdlib,sasl,gproc,enoise,gmconfig, - gmhive_protocol,gmhive_worker]}, - {mod,{gmhive_client,[]}}, + {applications,[kernel,stdlib,sasl,gproc,inets,ssl,enoise, + gmconfig,gmhive_protocol,gmhive_worker]}, + {mod,{gmhc_app,[]}}, {start_phases,[{connect_to_primary,[]}]}, {env,[]}, - {modules,[base58,eblake2,eblake2_tests,timing,enacl_eqc, - enacl_ext_eqc,enacl,enacl_ext,enacl_nif,enacl_SUITE, - enoise,enoise_cipher_state,enoise_connection, - enoise_crypto,enoise_hs_state,enoise_keypair, - enoise_protocol,enoise_sym_state, - enoise_bad_data_tests,enoise_chiper_state_tests, - enoise_crypto_tests,enoise_hs_state_tests, - enoise_protocol_tests,enoise_sym_state_tests, - enoise_tests,enoise_utils,test_utils,gmconfig, - gmconfig_schema_helpers,gmconfig_schema_utils, - gmcuckoo,gmhc_app,gmhc_config,gmhc_config_schema, + {modules,[gmhc_app,gmhc_config,gmhc_config_schema, gmhc_connector,gmhc_connectors_sup,gmhc_counters, gmhc_eureka,gmhc_events,gmhc_handler,gmhc_server, - gmhc_sup,gmhc_workers,gmhive_client,gmhp_msgs, - gmhw_blake2b_256,gmhw_pow,gmhw_pow_cuckoo, - gmhw_siphash24,gmcuckoo_SUITE,gmhw_pow_cuckoo_tests, - gmhw_pow_tests,gmser_api_encoder,gmser_chain_objects, - gmser_contract_code,gmser_delegation,gmser_dyn, - gmser_id,gmser_rlp,gmserialization, - gmser_api_encoder_tests,gmser_chain_objects_tests, - gmser_contract_code_tests,gmser_delegation_tests, - gmser_rlp_tests,gproc,gproc_app,gproc_bcast, - gproc_dist,gproc_info,gproc_init,gproc_lib, - gproc_monitor,gproc_pool,gproc_ps,gproc_pt,gproc_sup, - setup,setup_app,setup_file,setup_file_io_server, - setup_gen,setup_lib,setup_srv,setup_sup,setup_zomp, - testapp_app,testapp_sup,testapp_app,testapp_sup, - hex_api,hex_api_auth,hex_api_key,hex_api_organization, - hex_api_organization_member,hex_api_package, - hex_api_package_owner,hex_api_release,hex_api_user, - hex_core,hex_erl_tar,hex_filename,hex_http, - hex_http_httpc,hex_licenses,hex_pb_names, - hex_pb_package,hex_pb_signed,hex_pb_versions, - hex_registry,hex_repo,hex_tarball,safe_erl_term,pc, - pc_compilation,pc_port_env,pc_port_specs,pc_prv_clean, - pc_prv_compile,pc_util,rebar3_hex,rebar3_hex_app, - rebar3_hex_build,rebar3_hex_client,rebar3_hex_config, - rebar3_hex_cut,rebar3_hex_error,rebar3_hex_file, - rebar3_hex_httpc_adapter,rebar3_hex_io,rebar3_hex_key, - rebar3_hex_organization,rebar3_hex_owner, - rebar3_hex_publish,rebar3_hex_results, - rebar3_hex_retire,rebar3_hex_search,rebar3_hex_user, - rebar3_hex_version, - rebar_aecuckooprebuilt_app_with_priv_from_git_resource, - rebar_aecuckooprebuilt_dep,verl,verl_parser,base58, - eblake2,eblake2_tests,timing,enacl_eqc,enacl_ext_eqc, - enacl,enacl_ext,enacl_nif,enacl_SUITE,enoise, - enoise_cipher_state,enoise_connection,enoise_crypto, - enoise_hs_state,enoise_keypair,enoise_protocol, - enoise_sym_state,enoise_bad_data_tests, - enoise_chiper_state_tests,enoise_crypto_tests, - enoise_hs_state_tests,enoise_protocol_tests, - enoise_sym_state_tests,enoise_tests,enoise_utils, - test_utils,gmconfig,gmconfig_schema_helpers, - gmconfig_schema_utils,gmcuckoo,gmhp_msgs, - gmhw_blake2b_256,gmhw_pow,gmhw_pow_cuckoo, - gmhw_siphash24,gmcuckoo_SUITE,gmhw_pow_cuckoo_tests, - gmhw_pow_tests,gmser_api_encoder,gmser_chain_objects, - gmser_contract_code,gmser_delegation,gmser_dyn, - gmser_id,gmser_rlp,gmserialization, - gmser_api_encoder_tests,gmser_chain_objects_tests, - gmser_contract_code_tests,gmser_delegation_tests, - gmser_rlp_tests,gproc,gproc_app,gproc_bcast, - gproc_dist,gproc_info,gproc_init,gproc_lib, - gproc_monitor,gproc_pool,gproc_ps,gproc_pt,gproc_sup, - setup,setup_app,setup_file,setup_file_io_server, - setup_gen,setup_lib,setup_srv,setup_sup,setup_zomp, - testapp_app,testapp_sup,testapp_app,testapp_sup, - hex_api,hex_api_auth,hex_api_key,hex_api_organization, - hex_api_organization_member,hex_api_package, - hex_api_package_owner,hex_api_release,hex_api_user, - hex_core,hex_erl_tar,hex_filename,hex_http, - hex_http_httpc,hex_licenses,hex_pb_names, - hex_pb_package,hex_pb_signed,hex_pb_versions, - hex_registry,hex_repo,hex_tarball,safe_erl_term,pc, - pc_compilation,pc_port_env,pc_port_specs,pc_prv_clean, - pc_prv_compile,pc_util,rebar3_hex,rebar3_hex_app, - rebar3_hex_build,rebar3_hex_client,rebar3_hex_config, - rebar3_hex_cut,rebar3_hex_error,rebar3_hex_file, - rebar3_hex_httpc_adapter,rebar3_hex_io,rebar3_hex_key, - rebar3_hex_organization,rebar3_hex_owner, - rebar3_hex_publish,rebar3_hex_results, - rebar3_hex_retire,rebar3_hex_search,rebar3_hex_user, - rebar3_hex_version, - rebar_aecuckooprebuilt_app_with_priv_from_git_resource, - rebar_aecuckooprebuilt_dep,verl,verl_parser,gmhc_app, - gmhc_config,gmhc_config_schema,gmhc_connector, - gmhc_connectors_sup,gmhc_counters,gmhc_eureka, - gmhc_events,gmhc_handler,gmhc_server,gmhc_sup, - gmhc_workers,gmhive_client]}, + gmhc_sup,gmhc_workers,gmhive_client]}, {maintainers,["QPQ IaaS AG"]}, {licensens,["ISC"]}, {links,[{"gitea","https://git.qpq.swiss/gmhive_client"}]}]}. diff --git a/src/gmhc_config.erl b/src/gmhc_config.erl index 174b55a..55fd08d 100644 --- a/src/gmhc_config.erl +++ b/src/gmhc_config.erl @@ -2,7 +2,6 @@ -export([ load_config/0 , get_config/1 - , verify_cfg_props/1 ]). -include_lib("kernel/include/logger.hrl"). @@ -13,6 +12,7 @@ load_config() -> gmconfig:load_user_config(report), gmconfig:apply_os_env(), gmconfig:process_plain_args(), + check_application_env(), ok. instrument_gmconfig() -> @@ -31,37 +31,31 @@ gmconfig_env() -> get_config(Path) -> gmconfig:get_config(Path, cpath(Path)). -cpath([<<"pubkey">>]) -> [user_config, env(pubkey ) ]; -cpath([<<"extra_pubkeys">>]) -> [user_config, env(extra_pubkeys ), schema_default]; -cpath([<<"pool_admin">>, <<"url">>]) -> [user_config, env(pool_admin_url), schema_default]; -cpath([<<"workers">>]) -> [user_config, env(workers ), schema_default]; -cpath(_) -> [user_config, schema_default]. - -env2cpath(pubkey ) -> [<<"pubkey">>]; -env2cpath(extra_pubkeys ) -> [<<"extra_pubkeys">>]; -env2cpath(pool_admin_url) -> [<<"pool_admin">>, <<"url">>]; -env2cpath(workers ) -> [<<"workers">>]. - -verify_cfg_props(PropList) -> - Cfg = lists:foldl( - fun({K,V}, M) -> - CfgK = env2cpath(K), - gmconfig:merge_config_maps(M, to_cfg_map(CfgK, V)) - end, #{}, PropList), - case gmconfig_schema_utils:valid(Cfg) of - Ok when is_map(Ok) -> +check_application_env() -> + case lists:foldl(fun appl_env/2, #{}, env_keys()) of + C when map_size(C) == 0 -> ok; - Other -> - error({invalid_config, Other}) + C -> + gmconfig:update_config(C) + end. +appl_env(K, C) -> + case application:get_env(gmhive_client, K) of + {ok, V} -> appl_env(K, V, C); + undefined -> C end. -to_cfg_map(K, V) -> - to_cfg_map(K, V, #{}). +appl_env(pubkey , K , C) -> C#{<<"pubkey">> => K}; +appl_env(extra_pubkeys , Ks, C) -> C#{<<"extra_pubkeys">> => Ks}; +appl_env(pool_admin_url, U , C) -> merge(C, #{<<"pool_admin">> => #{<<"url">> => U}}); +appl_env(workers , Ws, C) -> C#{<<"workers">> => Ws}. -to_cfg_map([H], V, M) -> - M#{H => V}; -to_cfg_map([H|T], V, M) -> - M#{H => to_cfg_map(T, V, M)}. +env_keys() -> [pubkey, extra_pubkeys, pool_admin_url, workers]. -env(K) -> - {env, gmhive_client, K}. +cpath([<<"pubkey">>]) -> [user_config ]; +cpath([<<"extra_pubkeys">>]) -> [user_config, schema_default]; +cpath([<<"pool_admin">>, <<"url">>]) -> [user_config, schema_default]; +cpath([<<"workers">>]) -> [user_config, schema_default]; +cpath(_) -> [user_config, schema_default]. + +merge(A, B) -> + gmconfig:merge_config_maps(A, B). diff --git a/src/gmhc_config_schema.erl b/src/gmhc_config_schema.erl index b605b94..9bbecb8 100644 --- a/src/gmhc_config_schema.erl +++ b/src/gmhc_config_schema.erl @@ -24,7 +24,8 @@ to_json() -> schema() -> obj(schema_init(), #{ - pubkey => str(#{pattern => ?ACCOUNT_PATTERN}, + network => str(#{ default => <<"mainnet">> }) + , pubkey => str(#{pattern => ?ACCOUNT_PATTERN}, <<"Primary client pubkey">>) , extra_pubkeys => array(#{ description => <<"Additional worker pubkeys, sharing rewards">> @@ -52,12 +53,20 @@ pool() -> pool_admin() -> obj(#{ url => str(#{ default => <<"https://test.gajumining.com/api/workers/{CLIENT_ID}">> - , description => <<"URL of Eureka worker api">> }) + , description => <<"URL of Eureka worker api">> }), + default_per_network => + obj(#{ + testnet => + str(#{default => <<"https://test.gajumining.com/api/workers/{CLIENT_ID}">>}) + , mainnet => + str(#{default => <<"https://gajumining.com/api/workers/{CLIENT_ID}">>}) + }) }). workers() -> array( - #{default => [#{executable => <<"mean29-generic">>}], + #{default => [#{executable => <<"mean29-generic">>, + executable_group => <<"aecuckoo">>}], description => <<"Definitions of workers' configurations. If no worker are configured one worker " "is used as default, i.e. 'mean29-generic' executable without any extra args.">>}, @@ -74,7 +83,8 @@ workers() -> "from faster CPU supporting AVX2 instructions).">>}), executable_group => str(#{description => <<"Group of executable binaries of the worker.">>, - enum => [ <<"aecuckoo">>, <<"aecuckooprebuilt">>, <<"gmcuckoo">>, <<"cuda">>, <<"gajumine">> ], + enum => [ <<"aecuckoo">>, <<"aecuckooprebuilt">>, + <<"gmcuckoo">>, <<"cuda">>, <<"gajumine">> ], default => <<"aecuckoo">>}), extra_args => str(#{description => <<"Extra arguments to pass to the worker executable binary. " diff --git a/src/gmhc_eureka.erl b/src/gmhc_eureka.erl index a37300c..c271f6c 100644 --- a/src/gmhc_eureka.erl +++ b/src/gmhc_eureka.erl @@ -6,14 +6,26 @@ -include("gmhc_events.hrl"). get_pool_address() -> - URL0 = gmhc_config:get_config([<<"pool_admin">>, <<"url">>]), - case expand_url(URL0) of - <<"local">> -> - #{<<"address">> => <<"127.0.0.1">>, - <<"port">> => gmconfig:get_config([<<"pool">>, <<"port">>], [schema_default]), - <<"pool_id">> => gmhc_config:get_config([<<"pool">>, <<"id">>]) }; - URL -> - ?LOG_INFO("Trying to connect to ~p", [URL]), + case gmconfig:find_config([<<"pool_admin">>, <<"url">>], [user_config]) of + {ok, URL0} -> + case expand_url(URL0) of + <<"local">> -> + #{<<"address">> => <<"127.0.0.1">>, + <<"port">> => gmconfig:get_config( + [<<"pool">>, <<"port">>], [schema_default]), + <<"pool_id">> => gmhc_config:get_config([<<"pool">>, <<"id">>]) }; + URL -> + ?LOG_INFO("Trying to connect to ~p", [URL]), + connect1(URL) + end; + undefined -> + Network = gmconfig:get_config([<<"network">>]), + URL0 = gmconfig:get_config([ <<"pool_admin">> + , <<"default_per_network">> + , Network ], + [schema_default]), + URL = expand_url(URL0), + ?LOG_INFO("Using default for ~p: ~p", [Network, URL]), connect1(URL) end. diff --git a/zomp.meta b/zomp.meta index f98b81b..b5cb09d 100644 --- a/zomp.meta +++ b/zomp.meta @@ -5,7 +5,11 @@ {desc,"Gajumaru Hive Client"}. {author,"Ulf Wiger, QPQ AG"}. {package_id,{"uwiger","gmhive_client",{0,2,1}}}. -{deps,[{"uwiger","gmhive_worker",{0,1,1}}, +{deps,[{"otpr","eblake2",{1,0,1}}, + {"uwiger","gmcuckoo",{1,1,1}}, + {"otpr","base58",{0,1,1}}, + {"otpr","gmserialization",{0,1,3}}, + {"uwiger","gmhive_worker",{0,1,1}}, {"uwiger","gmhive_protocol",{0,1,1}}, {"uwiger","setup",{2,2,4}}, {"uwiger","gproc",{1,0,1}},