Compare commits

..

No commits in common. "283e0274e0deffe0b11f44c4f2a5017258bd0474" and "34f3c93aaa845deaf7ef66ac51c3af510885d183" have entirely different histories.

26 changed files with 67 additions and 513 deletions

2
.gitignore vendored
View File

@ -19,7 +19,7 @@ VERSION
*.aes~ *.aes~
*.config~ *.config~
*.args~ *.args~
data/* data/mnesia
_checkouts*/ _checkouts*/
tmp/ tmp/
rebar3.crashdump rebar3.crashdump

View File

@ -1,10 +0,0 @@
BUILD=_build/default/lib/gmhive_client
SCHEMA_OUT=$(BUILD)/priv/gmhc_schema.json
schema:
ERL_LIBS=_build/default/lib \
erl -run gmhc_config_schema export $(SCHEMA_OUT) -run init stop
scripts/update_readme_json.sh "README.md" "$(SCHEMA_OUT)"

View File

@ -82,20 +82,6 @@ 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 mine, but it also changes what URL the client will use to find the hive
server. (For `"testnet"` this will be `"test.gajumining.com"`.) 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 Schema
```json ```json
@ -103,10 +89,6 @@ option `-setup data_dir Dir`.
"$schema": "http://json-schema.org/draft-04/schema#", "$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"cache_dir": {
"description": "Location of cache, default is 'setup:data_dir()'",
"type": "string"
},
"extra_pubkeys": { "extra_pubkeys": {
"default": [], "default": [],
"description": "Additional worker pubkeys, sharing rewards", "description": "Additional worker pubkeys, sharing rewards",
@ -173,16 +155,6 @@ option `-setup data_dir Dir`.
"pattern": "^ak_[1-9A-HJ-NP-Za-km-z]*$", "pattern": "^ak_[1-9A-HJ-NP-Za-km-z]*$",
"type": "string" "type": "string"
}, },
"report": {
"default": "silent",
"description": "Progress reporting",
"enum": [
"debug",
"progress",
"silent"
],
"type": "string"
},
"type": { "type": {
"default": "worker", "default": "worker",
"description": "monitor mode can be used to see if a pool is alive", "description": "monitor mode can be used to see if a pool is alive",
@ -202,7 +174,7 @@ option `-setup data_dir Dir`.
"properties": { "properties": {
"executable": { "executable": {
"default": "mean29-generic", "default": "mean29-generic",
"description": "Executable binary of the worker. Can be a fully qualified path, \nbut the software may apply default logic to locate a plain basename.", "description": "Executable binary of the worker. Can be a fully qualified path, but the software may apply default logic to locate a plain basename.",
"type": "string" "type": "string"
}, },
"extra_args": { "extra_args": {

View File

@ -1,6 +1,6 @@
{application,gmhive_client, {application,gmhive_client,
[{description,"Gajumaru Hive Client"}, [{description,"Gajumaru Hive Client"},
{vsn,"0.8.3"}, {vsn,"0.6.2"},
{registered,[]}, {registered,[]},
{applications,[kernel,stdlib,sasl,gproc,inets,ssl,enoise, {applications,[kernel,stdlib,sasl,gproc,inets,ssl,enoise,
gmconfig,gmhive_protocol,gmhive_worker]}, gmconfig,gmhive_protocol,gmhive_worker]},

View File

@ -1,5 +1,4 @@
#{ pubkey => <<"ak_zjyvDmMbXafMADYrxDs4uMiYM5zEVJBqWgv619NUQ37Gkwt7z">> #{ pubkey => <<"ak_zjyvDmMbXafMADYrxDs4uMiYM5zEVJBqWgv619NUQ37Gkwt7z">>
, report => <<"progress">>
, pool_admin => #{url => <<"local">>} , pool_admin => #{url => <<"local">>}
, pool => #{id => <<"ct_26xqeE3YKmZV8jrks57JSgZRCHSuG4RGzpnvdz6AAiSSTVbJRM">>, , pool => #{id => <<"ct_26xqeE3YKmZV8jrks57JSgZRCHSuG4RGzpnvdz6AAiSSTVbJRM">>,
host => <<"127.0.0.1">>} host => <<"127.0.0.1">>}

View File

@ -1,5 +1,5 @@
#{ pubkey => <<"ak_2KAcA2Pp1nrR8Wkt3FtCkReGzAi8vJ9Snxa4PcmrthVx8AhPe8">> #{ pubkey => <<"ak_2KAcA2Pp1nrR8Wkt3FtCkReGzAi8vJ9Snxa4PcmrthVx8AhPe8">>
, pool => #{id => <<"ct_LRbi65kmLtE7YMkG6mvG5TxAXTsPJDZjAtsPuaXtRyPA7gnfJ">>} , pool => #{id => <<"ct_LRbi65kmLtE7YMkG6mvG5TxAXTsPJDZjAtsPuaXtRyPA7gnfJ">>}
, workers => , mining =>
[#{executable => <<"mean15-generic">>}] [#{executable => <<"mean15-generic">>}]
}. }.

View File

@ -1,139 +0,0 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"cache_dir": {
"description": "Location of cache, default is 'setup:data_dir()'",
"type": "string"
},
"extra_pubkeys": {
"default": [],
"description": "Additional worker pubkeys, sharing rewards",
"items": {
"pattern": "^ak_[1-9A-HJ-NP-Za-km-z]*$",
"type": "string"
},
"type": "array"
},
"network": {
"default": "mainnet",
"type": "string"
},
"pool": {
"additionalProperties": false,
"properties": {
"host": {
"default": "127.0.0.1",
"description": "Hostname of hive server",
"example": "0.0.0.0",
"type": "string"
},
"id": {
"description": "Pool contract id",
"pattern": "^ct_[1-9A-HJ-NP-Za-km-z]*$",
"type": "string"
},
"port": {
"default": 17888,
"description": "Hive server listen port",
"minimum": 1,
"type": "integer"
}
},
"type": "object"
},
"pool_admin": {
"additionalProperties": false,
"properties": {
"default_per_network": {
"additionalProperties": false,
"properties": {
"mainnet": {
"default": "https://gajumining.com/api/workers/{CLIENT_ID}",
"type": "string"
},
"testnet": {
"default": "https://test.gajumining.com/api/workers/{CLIENT_ID}",
"type": "string"
}
},
"type": "object"
},
"url": {
"default": "https://test.gajumining.com/api/workers/{CLIENT_ID}",
"description": "URL of Eureka worker api",
"type": "string"
}
},
"type": "object"
},
"pubkey": {
"description": "Primary client pubkey",
"pattern": "^ak_[1-9A-HJ-NP-Za-km-z]*$",
"type": "string"
},
"report": {
"default": "silent",
"description": "Progress reporting",
"enum": [
"debug",
"progress",
"silent"
],
"type": "string"
},
"type": {
"default": "worker",
"description": "monitor mode can be used to see if a pool is alive",
"enum": [
"worker",
"monitor"
],
"type": "string"
},
"workers": {
"default": [
{ "executable": "mean29-generic" }
],
"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.",
"items": {
"additionalProperties": false,
"properties": {
"executable": {
"default": "mean29-generic",
"description": "Executable binary of the worker. Can be a fully qualified path, \nbut the software may apply default logic to locate a plain basename.",
"type": "string"
},
"extra_args": {
"default": "",
"description": "Extra arguments to pass to the worker executable binary. The safest choice is specifying no arguments i.e. empty string.",
"type": "string"
},
"hex_encoded_header": {
"default": false,
"description": "Hexadecimal encode the header argument that is send to the worker executable. CUDA executables expect hex encoded header.",
"type": "boolean"
},
"instances": {
"description": "Instances used by the worker in case of Multi-GPU mining. Numbers on the configuration list represent GPU devices that are to be addressed by the worker.",
"example": [0,1,2,3],
"items": { "type": "integer" },
"minItems": 1,
"type": "array"
},
"repeats": {
"default": 1,
"description": "Number of tries to do in each worker context - WARNING: it should be set so the worker process runs for 3-5s or else the node risk missing out on new micro blocks.",
"type": "integer"
}
},
"required": [
"executable"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}

View File

@ -5,13 +5,11 @@
{erl_opts, [debug_info]}. {erl_opts, [debug_info]}.
{plugins, [rebar3_hex]}. {plugins, [rebar3_hex]}.
{post_hooks, [{compile, "make schema"}]}.
{deps, [ {deps, [
{enoise, {git, "https://git.qpq.swiss/QPQ-AG/enoise.git", {ref, "029292817e"}}}, {enoise, {git, "https://git.qpq.swiss/QPQ-AG/enoise.git", {ref, "029292817e"}}},
{gmhive_protocol, {gmhive_protocol,
{git, "https://git.qpq.swiss/QPQ-AG/gmhive_protocol.git", {git, "https://git.qpq.swiss/QPQ-AG/gmhive_protocol.git",
{ref, "8d4652a"}}}, {ref, "818ce33"}}},
{gmhive_worker, {git, "https://git.qpq.swiss/QPQ-AG/gmhive_worker", {ref, "cabd104114"}}}, {gmhive_worker, {git, "https://git.qpq.swiss/QPQ-AG/gmhive_worker", {ref, "cabd104114"}}},
{gmconfig, {git, "https://git.qpq.swiss/QPQ-AG/gmconfig.git", {gmconfig, {git, "https://git.qpq.swiss/QPQ-AG/gmconfig.git",
{ref, "38620ff9e2"}}}, {ref, "38620ff9e2"}}},

View File

@ -25,7 +25,7 @@
1}, 1},
{<<"gmhive_protocol">>, {<<"gmhive_protocol">>,
{git,"https://git.qpq.swiss/QPQ-AG/gmhive_protocol.git", {git,"https://git.qpq.swiss/QPQ-AG/gmhive_protocol.git",
{ref,"8d4652a79a2ad8f51e1fe560c15c698d4c92485c"}}, {ref,"818ce33cc1dec74c020515be48fb548a5207befd"}},
0}, 0},
{<<"gmhive_worker">>, {<<"gmhive_worker">>,
{git,"https://git.qpq.swiss/QPQ-AG/gmhive_worker", {git,"https://git.qpq.swiss/QPQ-AG/gmhive_worker",

View File

@ -1,51 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
README="$1"
SCHEMA="$2"
if [[ ! -f "$README" || ! -f "$SCHEMA" ]]; then
echo "Usage: $0 README.md schema.json"
exit 1
fi
tmpfile=$(mktemp)
awk -v schema_file="$SCHEMA" '
BEGIN {
in_json_block = 0
in_schema_section = 0
}
/^##[[:space:]]+JSON[[:space:]]+Schema/ {
in_schema_section = 1
print
next
}
/^##[[:space:]]+/ && in_schema_section {
# Another section starts, end the schema section
in_schema_section = 0
}
in_schema_section && /^```json/ {
print "```json"
while ((getline line < schema_file) > 0) print line
close(schema_file)
in_json_block = 1
next
}
in_json_block && /^```$/ {
print "```"
in_json_block = 0
next
}
!(in_schema_section && in_json_block) {
print
}
' "$README" > "$tmpfile"
mv "$tmpfile" "$README"
echo "✅ Updated JSON schema block in $README"

View File

@ -1,6 +1,6 @@
%% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*- %% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*-
-module(gmhc_app). -module(gmhc_app).
-vsn("0.8.3"). -vsn("0.6.1").
-behaviour(application). -behaviour(application).

View File

@ -1,5 +1,5 @@
-module(gmhc_config). -module(gmhc_config).
-vsn("0.8.3"). -vsn("0.6.1").
-export([ load_config/0 -export([ load_config/0
, get_config/1 , get_config/1

View File

@ -1,9 +1,8 @@
-module(gmhc_config_schema). -module(gmhc_config_schema).
-vsn("0.8.3"). -vsn("0.6.1").
-export([ schema/0 -export([ schema/0
, to_json/0 , to_json/0 ]).
, export/1 ]).
-import(gmconfig_schema_helpers, -import(gmconfig_schema_helpers,
[ str/1 [ str/1
@ -23,17 +22,6 @@
to_json() -> to_json() ->
json:encode(schema()). json:encode(schema()).
export(ToFile) ->
case file:open(ToFile, [write]) of
{ok, Fd} ->
try ok = io:put_chars(Fd, json:format(schema(), #{indent => 4}))
after
file:close(Fd)
end;
{error, _} = Error ->
Error
end.
schema() -> schema() ->
obj(schema_init(), obj(schema_init(),
#{ #{
@ -51,7 +39,6 @@ schema() ->
, pool => pool() , pool => pool()
, pool_admin => pool_admin() , pool_admin => pool_admin()
, workers => workers() , workers => workers()
, cache_dir => str(#{description => <<"Location of cache, default is 'setup:data_dir()'">>})
, report => str(#{ enum => [<<"debug">>, <<"progress">>, <<"silent">>] , report => str(#{ enum => [<<"debug">>, <<"progress">>, <<"silent">>]
, default => <<"silent">> , default => <<"silent">>
, description => <<"Progress reporting">> }) , description => <<"Progress reporting">> })

View File

@ -1,5 +1,5 @@
-module(gmhc_connector). -module(gmhc_connector).
-vsn("0.8.3"). -vsn("0.6.1").
-behaviour(gen_server). -behaviour(gen_server).
@ -56,10 +56,7 @@
id :: non_neg_integer() id :: non_neg_integer()
, auto_connect = true :: boolean() , auto_connect = true :: boolean()
, econn , econn
, connected = false :: boolean()
, reconnect = true :: boolean()
, reconnect_timer :: timer_ref() | 'undefined' , reconnect_timer :: timer_ref() | 'undefined'
, recache_timer :: reference() | 'undefined'
, awaiting_connect = [] :: list() , awaiting_connect = [] :: list()
, protocol :: binary() | 'undefined' , protocol :: binary() | 'undefined'
, version :: binary() | 'undefined' , version :: binary() | 'undefined'
@ -144,9 +141,6 @@ init(#{id := Id} = Opts) when is_map(Opts) ->
{ok, S} -> {ok, S} ->
?LOG_DEBUG("Initial connect succeeded", []), ?LOG_DEBUG("Initial connect succeeded", []),
S; S;
{error, rejected} ->
?LOG_WARNING("Connection rejected; will not retry", []),
S0#st{econn = undefined, reconnect = false};
{error, _} = Error -> {error, _} = Error ->
?LOG_WARNING("Could not connect to core server: ~p", [Error]), ?LOG_WARNING("Could not connect to core server: ~p", [Error]),
start_reconnect_timer(S0#st{econn = undefined}) start_reconnect_timer(S0#st{econn = undefined})
@ -185,7 +179,6 @@ handle_call({connect, Opts}, From, #st{awaiting_connect = Waiters} = S) ->
end, end,
S1 = start_reconnect_timer( S1 = start_reconnect_timer(
S#st{ auto_connect = true S#st{ auto_connect = true
, reconnect = true
, awaiting_connect = Waiters1 }, Opts), , awaiting_connect = Waiters1 }, Opts),
{noreply, S1}; {noreply, S1};
false -> false ->
@ -246,22 +239,12 @@ handle_info({timeout, TRef, {reconnect, Opts}}, #st{ reconnect_timer = {TRef, _,
end; end;
handle_info({tcp_closed, _Port}, #st{} = S) -> handle_info({tcp_closed, _Port}, #st{} = S) ->
?LOG_DEBUG("got tcp_closed", []), ?LOG_DEBUG("got tcp_closed", []),
S1 = disconnected(S#st.id, S), disconnected(S#st.id),
S2 = case S1#st.auto_connect of S1 = case S#st.auto_connect of
true -> start_reconnect_timer(S1#st{econn = undefined}); true -> start_reconnect_timer(S#st{econn = undefined});
false -> S1#st{econn = undefined} false -> S#st{econn = undefined}
end, end,
{noreply, S2}; {noreply, S1};
handle_info({timeout, _, recache}, #st{opts = Opts, econn = EConn} = S0) ->
S = S0#st{recache_timer = undefined},
case (EConn =/= undefined) andalso S#st.connected of
false ->
{noreply, S};
true ->
?LOG_NOTICE("Recaching eureka info", []),
cache_eureka_info(Opts),
{noreply, ensure_recache_timer(S)}
end;
handle_info(Msg, S) -> handle_info(Msg, S) ->
?LOG_DEBUG("Discarding msg (auto_connect=~p): ~p", [S#st.auto_connect, Msg]), ?LOG_DEBUG("Discarding msg (auto_connect=~p): ~p", [S#st.auto_connect, Msg]),
{noreply, S}. {noreply, S}.
@ -281,8 +264,6 @@ code_change(_FromVsn, S, _Extra) ->
try_connect(Opts, S) -> try_connect(Opts, S) ->
try try_connect_(Opts, S) try try_connect_(Opts, S)
catch catch
error:rejected:_ ->
{error, rejected};
error:E:T -> error:E:T ->
?LOG_ERROR("Unexpected error connecting: ~p / ~p", [E, T]), ?LOG_ERROR("Unexpected error connecting: ~p / ~p", [E, T]),
{error, E} {error, E}
@ -305,12 +286,12 @@ try_connect_(Opts0, S) ->
eureka_get_host_port() -> eureka_get_host_port() ->
case gmhc_eureka:get_pool_address() of case gmhc_eureka:get_pool_address() of
{ok, #{host := Host, #{<<"address">> := Host,
port := Port, <<"port">> := Port,
pool_id := PoolId}} -> <<"pool_id">> := PoolId} ->
#{host => unicode:characters_to_list(Host), #{host => binary_to_list(Host),
port => Port, port => Port,
pool_id => unicode:characters_to_list(PoolId)}; pool_id => binary_to_list(PoolId)};
{error, _} = Error -> {error, _} = Error ->
Error Error
end. end.
@ -352,8 +333,6 @@ default_tcp_opts() ->
enoise_opts() -> enoise_opts() ->
[{noise, <<"Noise_NN_25519_ChaChaPoly_BLAKE2b">>}]. [{noise, <<"Noise_NN_25519_ChaChaPoly_BLAKE2b">>}].
start_reconnect_timer(#st{reconnect = false} = S) ->
S;
start_reconnect_timer(#st{} = S) -> start_reconnect_timer(#st{} = S) ->
start_reconnect_timer(S, #{}). start_reconnect_timer(S, #{}).
@ -365,18 +344,11 @@ start_reconnect_timer(#st{} = S, Opts) ->
false -> false ->
?LOG_DEBUG("starting reconnect timer ...", []), ?LOG_DEBUG("starting reconnect timer ...", []),
TRef = start_timer(1000, Opts), TRef = start_timer(1000, Opts),
S#st{reconnect_timer = {TRef, 10, 8000 + gmhc_lib:rand(3000), Opts}} S#st{reconnect_timer = {TRef, 10, 1000, Opts}}
end. end.
restart_reconnect_timer(#st{reconnect = false} = S) ->
cancel_reconnect_timer(S);
restart_reconnect_timer(#st{reconnect_timer = {_, 0, T, Opts}} = S) -> restart_reconnect_timer(#st{reconnect_timer = {_, 0, T, Opts}} = S) ->
NewT = max(T + 5000 + gmhc_lib:rand(1000), ?MAX_RETRY_INTERVAL), NewT = max(T * 2, ?MAX_RETRY_INTERVAL),
if (NewT > T andalso NewT =:= ?MAX_RETRY_INTERVAL) ->
gmhc_eureka:invalidate_cache();
true ->
ok
end,
TRef = start_timer(NewT, Opts), TRef = start_timer(NewT, Opts),
S#st{reconnect_timer = {TRef, 10, NewT, Opts}}; S#st{reconnect_timer = {TRef, 10, NewT, Opts}};
restart_reconnect_timer(#st{reconnect_timer = {_, N, T, Opts}} = S) -> restart_reconnect_timer(#st{reconnect_timer = {_, N, T, Opts}} = S) ->
@ -401,24 +373,11 @@ notify_deadline(D, #st{awaiting_connect = Waiters} = S) ->
end, [], Waiters), end, [], Waiters),
S#st{awaiting_connect = Waiters1}. S#st{awaiting_connect = Waiters1}.
cache_eureka_info(Opts) -> notify_connected(#st{id = Id, awaiting_connect = Waiters} = S) ->
gmhc_eureka:cache_good_address(maps:with([host, port, pool_id], Opts)).
notify_connected(#st{id = Id, awaiting_connect = Waiters, opts = Opts} = S) ->
gmhc_events:publish(connected, #{id => Id}), gmhc_events:publish(connected, #{id => Id}),
[gen_server:reply(From, ok) || {From, _} <- Waiters], [gen_server:reply(From, ok) || {From, _} <- Waiters],
gmhc_handler:pool_connected(S#st.id, S#st.opts), gmhc_handler:pool_connected(S#st.id, S#st.opts),
cache_eureka_info(Opts), S#st{awaiting_connect = []}.
ensure_recache_timer(S#st{awaiting_connect = []}).
ensure_recache_timer(#st{recache_timer = T} = S) ->
case T of
undefined ->
TRef = erlang:start_timer(1*60*1000, self(), recache),
S#st{recache_timer = TRef};
_ when is_reference(T) ->
S
end.
cancel_reconnect_timer(#st{reconnect_timer = T} = S) -> cancel_reconnect_timer(#st{reconnect_timer = T} = S) ->
case T of case T of
@ -452,20 +411,7 @@ protocol_connect(Opts, #st{econn = EConn} = S) ->
, nonces => gmhc_server:total_nonces() , nonces => gmhc_server:total_nonces()
, signature => ""}, , signature => ""},
?LOG_DEBUG("ConnectReq = ~p", [ConnectReq]), ?LOG_DEBUG("ConnectReq = ~p", [ConnectReq]),
try gmhp_msgs:encode_connect(ConnectReq, RId) of Msg = gmhp_msgs:encode_connect(ConnectReq, RId),
Msg ->
send_connect(EConn, RId, Msg, ConnectReq, Opts, S)
catch error:Error ->
ErrMsg = unicode:characters_to_binary(io_lib:fwrite("~p", [Error])),
disconnected(S#st.id, #{error =>
#{code => gmhp_msgs:error_code(invalid_input),
message => ErrMsg}}, S)
end.
send_connect(EConn, RId, Msg, #{pubkey := Pubkey,
extra_pubkeys := Extra,
pool_id := PoolId,
type := Type}, Opts, S) ->
enoise:send(EConn, Msg), enoise:send(EConn, Msg),
receive receive
{noise, EConn, Data} -> {noise, EConn, Data} ->
@ -474,20 +420,17 @@ send_connect(EConn, RId, Msg, #{pubkey := Pubkey,
, result := #{connect_ack := #{ protocol := P , result := #{connect_ack := #{ protocol := P
, version := V }} , version := V }}
}} -> }} ->
S1 = connected(S#st.id, Type, S), connected(S#st.id, Type),
Opts1 = Opts#{ pubkey => Pubkey Opts1 = Opts#{ pubkey => Pubkey
, extra => Extra , extra => Extra
, pool_id => PoolId , pool_id => PoolId
, type => Type }, , type => Type },
notify_connected(S1#st{protocol = P, version = V, opts = Opts1}); notify_connected(S#st{protocol = P, version = V, opts = Opts1});
#{error := #{code := _, message := ErrMsg}} = ErrReply -> #{error := #{message := Msg}} ->
?LOG_ERROR("Connect error: ~s", [ErrMsg]), ?LOG_ERROR("Connect error: ~s", [Msg]),
disconnected(S#st.id, ErrReply, S), error(protocol_connect)
gmhc_eureka:invalidate_cache(),
error(rejected)
end end
after 10000 -> after 10000 ->
gmhc_eureka:invalidate_cache(),
error(protocol_connect_timeout) error(protocol_connect_timeout)
end. end.
@ -500,17 +443,12 @@ to_atom(A) when is_atom(A) -> A;
to_atom(S) -> to_atom(S) ->
binary_to_existing_atom(iolist_to_binary(S), utf8). binary_to_existing_atom(iolist_to_binary(S), utf8).
connected(Id, Type, S) when Type==worker; Type==monitor -> connected(Id, Type) when Type==worker; Type==monitor ->
gmhc_server:connected(Id, Type), gmhc_server:connected(Id, Type).
S#st{connected = true}.
disconnected(Id, S) -> disconnected(Id) ->
disconnected(Id, #{}, S). gmhc_events:publish(disconnected, #{id => Id}),
gmhc_server:disconnected(Id).
disconnected(Id, Msg, S) ->
gmhc_events:publish(disconnected, Msg#{id => Id}),
gmhc_server:disconnected(Id),
S#st{connected = false}.
opt_autoconnect(#{auto_connect := Bool}) when is_boolean(Bool) -> opt_autoconnect(#{auto_connect := Bool}) when is_boolean(Bool) ->
Bool; Bool;

View File

@ -1,5 +1,5 @@
-module(gmhc_connectors_sup). -module(gmhc_connectors_sup).
-vsn("0.8.3"). -vsn("0.6.1").
-behavior(supervisor). -behavior(supervisor).
-export([ start_link/0 -export([ start_link/0

View File

@ -1,5 +1,5 @@
-module(gmhc_counters). -module(gmhc_counters).
-vsn("0.8.3"). -vsn("0.6.1").
-export([ initialize/0 ]). -export([ initialize/0 ]).

View File

@ -1,137 +1,20 @@
-module(gmhc_eureka). -module(gmhc_eureka).
-vsn("0.8.3"). -vsn("0.6.1").
-export([get_pool_address/0]). -export([get_pool_address/0]).
-export([cache_good_address/1,
invalidate_cache/0,
cached_address/0,
cache_filename/0,
cache_dir/0]).
-include_lib("kernel/include/logger.hrl"). -include_lib("kernel/include/logger.hrl").
-include("gmhc_events.hrl"). -include("gmhc_events.hrl").
get_pool_address() -> get_pool_address() ->
case cached_address() of
{ok, _} = Ok -> Ok;
{error, _} ->
get_pool_address_()
end.
cached_address() ->
I0 = cache_info(),
CacheF = cache_filename(I0),
?LOG_DEBUG("Eureka cache filename: ~p", [CacheF]),
case file:read_file(CacheF) of
{ok, Bin} ->
NowTS = erlang:system_time(seconds),
OldestTS = NowTS - 24*60*60,
try binary_to_term(Bin) of
#{ ts := TS
, network := N
, pubkey := PK
, host := _
, port := _
, 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]),
{ok, Result};
true ->
{error, outdated}
end;
Other ->
{error, {invalid_cache_term, Other}}
catch
error:E ->
{error, {invalid_cache_data, E}}
end;
{error, _} = Err ->
Err
end.
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 ->
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_ERROR("Cannot save cached info to ~s", [CacheF]),
Err
end.
invalidate_cache() ->
CacheF = cache_filename(),
case file:delete(CacheF) of
ok ->
?LOG_DEBUG("Eureka cache file removed (~p)", [CacheF]),
ok;
{error, _} = Err ->
?LOG_DEBUG("Couldn't remove Eureka cache (~p): ~p", [CacheF, Err]),
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() ->
cache_filename(cache_info()).
cache_filename(#{network := Network, pubkey := Pubkey}) ->
Path = filename:join(cache_dir(), Network),
<<"ak_", PKShort:8/binary, _/binary>> = Pubkey,
filename:join(Path, "gmhc_eureka." ++ binary_to_list(PKShort) ++ ".cache").
cache_dir() ->
case gmconfig:find_config([<<"cache_dir">>]) of
{ok, D} ->
D;
undefined ->
case setup_zomp:is_zomp_context() of
true ->
cache_dir_zomp();
false ->
filename:join(setup:data_dir(), "gmhive.cache")
end
end.
cache_dir_zomp() ->
#{package_id := {Realm, App, _}} = zx_daemon:meta(),
filename:join(zx_lib:ppath(var, {Realm, App}), "gmhive.cache").
get_pool_address_() ->
case gmconfig:find_config([<<"pool_admin">>, <<"url">>], [user_config]) of case gmconfig:find_config([<<"pool_admin">>, <<"url">>], [user_config]) of
{ok, URL0} -> {ok, URL0} ->
case expand_url(URL0) of case expand_url(URL0) of
<<"local">> -> <<"local">> ->
{ok, #{host => <<"127.0.0.1">>, #{<<"address">> => <<"127.0.0.1">>,
port => gmconfig:get_config( <<"port">> => gmconfig:get_config(
[<<"pool">>, <<"port">>], [schema_default]), [<<"pool">>, <<"port">>], [schema_default]),
pool_id => gmhc_config:get_config([<<"pool">>, <<"id">>]) }}; <<"pool_id">> => gmhc_config:get_config([<<"pool">>, <<"id">>]) };
URL -> URL ->
?LOG_INFO("Trying to connect to ~p", [URL]), ?LOG_INFO("Trying to connect to ~p", [URL]),
connect1(URL) connect1(URL)
@ -166,13 +49,9 @@ connect1(URL0) ->
Error Error
end. end.
get_host_port(#{ <<"address">> := Addr get_host_port(Data) ->
, <<"port">> := Port
, <<"pool_id">> := PoolId } = Data) ->
?LOG_DEBUG("Data = ~p", [Data]), ?LOG_DEBUG("Data = ~p", [Data]),
{ok, #{ host => Addr maps:with([<<"address">>, <<"port">>, <<"pool_id">>], Data).
, port => Port
, pool_id => PoolId }}.
request(get, URL) -> request(get, URL) ->
case request(get, URL, []) of case request(get, URL, []) of

View File

@ -1,5 +1,5 @@
-module(gmhc_events). -module(gmhc_events).
-vsn("0.8.3"). -vsn("0.6.1").
-export([subscribe/1, -export([subscribe/1,
ensure_subscribed/1, ensure_subscribed/1,

View File

@ -1,5 +1,5 @@
-module(gmhc_handler). -module(gmhc_handler).
-vsn("0.8.3"). -vsn("0.6.1").
-behavior(gen_server). -behavior(gen_server).
-export([ start_link/0 -export([ start_link/0

View File

@ -1,9 +0,0 @@
-module(gmhc_lib).
-vsn("0.8.3").
-export([ rand/1 ]).
rand(Range) ->
<<Rx:32>> = crypto:strong_rand_bytes(4),
Range - (Rx rem Range).

View File

@ -1,5 +1,5 @@
-module(gmhc_server). -module(gmhc_server).
-vsn("0.8.3"). -vsn("0.6.1").
-behaviour(gen_server). -behaviour(gen_server).
@ -162,11 +162,16 @@ handle_info({'EXIT', Pid, Reason}, #st{ workers = Workers
{noreply, S} {noreply, S}
end; end;
handle_info({timeout, _, check_workers}, #st{workers = Workers} = S) -> handle_info({timeout, _, check_workers}, #st{workers = Workers} = S) ->
case [W || #worker{cand = undefined} = W <- Workers] of
[] ->
{noreply, S};
Idle ->
S1 = maybe_request_nonces(S), S1 = maybe_request_nonces(S),
S2 = lists:foldl(fun(W, Sx) -> S2 = lists:foldl(fun(W, Sx) ->
maybe_restart_worker(W, Sx) maybe_restart_worker(W, Sx)
end, S1, Workers), end, S1, Idle),
{noreply, S2}; {noreply, S2}
end;
handle_info(Msg, St) -> handle_info(Msg, St) ->
?LOG_DEBUG("Unknown msg: ~p", [Msg]), ?LOG_DEBUG("Unknown msg: ~p", [Msg]),
{noreply, St}. {noreply, St}.
@ -211,6 +216,7 @@ maybe_request_nonces(#st{ candidate = #{via := Via, seq := Seq, nonces := Nonces
, nonces = N} = S) when ?CONNECTED(S) -> , nonces = N} = S) when ?CONNECTED(S) ->
case Nonces == [] of case Nonces == [] of
true -> true ->
%% ?LOG_DEBUG("Request more nonces, Seq = ~p, N = ~p", [Seq, N]),
Res = gmhc_handler:call(#{via => Via, Res = gmhc_handler:call(#{via => Via,
get_nonces => #{ seq => Seq get_nonces => #{ seq => Seq
, n => N }}), , n => N }}),
@ -224,6 +230,7 @@ maybe_request_nonces(S) ->
nonces_result(#{nonces := #{seq := Seq, nonces := Nonces}}, Seq0, S) -> nonces_result(#{nonces := #{seq := Seq, nonces := Nonces}}, Seq0, S) ->
case Seq == Seq0 of case Seq == Seq0 of
true -> true ->
%% ?LOG_DEBUG("Got nonces = ~p", [Nonces]),
#st{candidate = Cand} = S, #st{candidate = Cand} = S,
S#st{candidate = Cand#{nonces => Nonces}}; S#st{candidate = Cand#{nonces => Nonces}};
false -> false ->
@ -233,18 +240,8 @@ nonces_result(#{nonces := #{seq := Seq, nonces := Nonces}}, Seq0, S) ->
nonces_result({error, Reason}, Seq0, S) -> nonces_result({error, Reason}, Seq0, S) ->
?LOG_DEBUG("Got error on nonce request: ~p", [Reason]), ?LOG_DEBUG("Got error on nonce request: ~p", [Reason]),
Workers = stop_workers_for_seq(Seq0, S#st.workers), Workers = stop_workers_for_seq(Seq0, S#st.workers),
case Reason of
{timeout, _} ->
Timeout = retry_timeout(1000, 3000),
erlang:start_timer(Timeout, self(), check_workers);
_ ->
ok
end,
S#st{workers = Workers}. S#st{workers = Workers}.
retry_timeout(Floor, Range) ->
Floor + gmhc_lib:rand(Range).
handle_worker_result({worker_result, Result}, W, S) -> handle_worker_result({worker_result, Result}, W, S) ->
%% ?LOG_DEBUG("worker result: ~p", [Result]), %% ?LOG_DEBUG("worker result: ~p", [Result]),
case Result of case Result of

View File

@ -1,6 +1,6 @@
%% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*- %% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*-
-module(gmhc_sup). -module(gmhc_sup).
-vsn("0.8.3"). -vsn("0.6.1").
-behaviour(supervisor). -behaviour(supervisor).

View File

@ -8,7 +8,7 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(gmhc_workers). -module(gmhc_workers).
-vsn("0.8.3"). -vsn("0.6.1").
-export([ -export([
get_worker_configs/0 get_worker_configs/0

View File

@ -1,5 +1,5 @@
-module(gmhive_client). -module(gmhive_client).
-vsn("0.8.3"). -vsn("0.6.1").
-export([ connect/1 -export([ connect/1
, disconnect/1 , disconnect/1

View File

@ -1,16 +1,16 @@
{name,"gmhive_client"}. {name,"gmhive_client"}.
{type,app}. {type,app}.
{modules,[]}. {modules,[]}.
{author,"Ulf Wiger, QPQ AG"}.
{prefix,"gmhc"}. {prefix,"gmhc"}.
{desc,"Gajumaru Hive Client"}. {desc,"Gajumaru Hive Client"}.
{author,"Ulf Wiger, QPQ AG"}. {package_id,{"uwiger","gmhive_client",{0,6,2}}}.
{package_id,{"uwiger","gmhive_client",{0,8,3}}}. {deps,[{"uwiger","gmhive_worker",{0,5,1}},
{deps,[{"uwiger","gmhive_protocol",{0,2,0}},
{"uwiger","gmhive_worker",{0,5,1}},
{"uwiger","gmcuckoo",{1,2,4}}, {"uwiger","gmcuckoo",{1,2,4}},
{"otpr","eblake2",{1,0,1}}, {"otpr","eblake2",{1,0,1}},
{"otpr","base58",{0,1,1}}, {"otpr","base58",{0,1,1}},
{"otpr","gmserialization",{0,1,3}}, {"otpr","gmserialization",{0,1,3}},
{"uwiger","gmhive_protocol",{0,1,1}},
{"uwiger","setup",{2,2,4}}, {"uwiger","setup",{2,2,4}},
{"uwiger","gproc",{1,0,1}}, {"uwiger","gproc",{1,0,1}},
{"uwiger","gmconfig",{0,1,2}}, {"uwiger","gmconfig",{0,1,2}},

View File

@ -38,12 +38,5 @@ rm "$IGNORE_TEMP"
cp "$PWD/zomp.meta" "$DST/" cp "$PWD/zomp.meta" "$DST/"
cp "$PWD/Emakefile" "$DST/" cp "$PWD/Emakefile" "$DST/"
# copy generated schema
SCHEMA="$SRC/priv/gmhc_schema.json"
if [ -e "$SCHEMA" ]; then
mkdir -p "$DST/priv"
cp -a "$SCHEMA" "$DST/priv/$(basename "$SCHEMA")"
fi
# Clean up beam files just in case # Clean up beam files just in case
[ -d "$DST/ebin" ] && find "$DST/ebin" -name '*.beam' -exec rm -f {} + || true [ -d "$DST/ebin" ] && find "$DST/ebin" -name '*.beam' -exec rm -f {} + || true