Add entrypoints for returning all solutions found
This commit is contained in:
parent
1430f43a63
commit
532d7b4471
@ -27,11 +27,14 @@
|
|||||||
|
|
||||||
-export([hash_data/1,
|
-export([hash_data/1,
|
||||||
generate/5,
|
generate/5,
|
||||||
|
generate_all/5,
|
||||||
generate_from_hash/5,
|
generate_from_hash/5,
|
||||||
|
generate_from_hash/6,
|
||||||
verify/5,
|
verify/5,
|
||||||
verify_proof/4,
|
verify_proof/4,
|
||||||
verify_proof_from_hash/4,
|
verify_proof_from_hash/4,
|
||||||
get_target/2
|
get_target/2,
|
||||||
|
test_target/3
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-export([ set_edge_bits/2 ]).
|
-export([ set_edge_bits/2 ]).
|
||||||
@ -88,6 +91,7 @@
|
|||||||
| undefined.
|
| undefined.
|
||||||
|
|
||||||
-type solution() :: [integer()].
|
-type solution() :: [integer()].
|
||||||
|
-type solutions() :: [{nonce(), solution()}].
|
||||||
|
|
||||||
-type output_parser_fun() :: fun(([string()], state()) ->
|
-type output_parser_fun() :: fun(([string()], state()) ->
|
||||||
{ok, term(), term()} | {error, term()}).
|
{ok, term(), term()} | {error, term()}).
|
||||||
@ -105,12 +109,14 @@
|
|||||||
-opaque config() :: #config{}.
|
-opaque config() :: #config{}.
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
os_pid :: integer() | undefined,
|
os_pid :: integer() | undefined,
|
||||||
port :: port() | undefined,
|
port :: port() | undefined,
|
||||||
buffer = [] :: string(),
|
buffer = [] :: string(),
|
||||||
target :: sci_target() | undefined,
|
solutions = [] :: solutions(),
|
||||||
edge_bits :: edge_bits(),
|
keep_going = false :: boolean(),
|
||||||
parser :: output_parser_fun()
|
target :: sci_target() | undefined,
|
||||||
|
edge_bits :: edge_bits(),
|
||||||
|
parser :: output_parser_fun()
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-type state() :: #state{}.
|
-type state() :: #state{}.
|
||||||
@ -190,15 +196,30 @@ generate(Data, Target, Nonce, Config, Instance) when
|
|||||||
Hash = gmminer_blake2b_256:hash(Data),
|
Hash = gmminer_blake2b_256:hash(Data),
|
||||||
generate_from_hash(Hash, Target, Nonce, Config, Instance).
|
generate_from_hash(Hash, Target, Nonce, Config, Instance).
|
||||||
|
|
||||||
|
-spec generate_all(hashable(), sci_target(), nonce(), config(), instance()) ->
|
||||||
|
{ok, solutions()} | {error, no_solution} | {error, {runtime, term()}}.
|
||||||
|
generate_all(Data, Target, Nonce, Config, Instance) when
|
||||||
|
Nonce >= 0, Nonce =< ?MAX_NONCE ->
|
||||||
|
Hash = gmminer_blake2b_256:hash(Data),
|
||||||
|
generate_from_hash(Hash, Target, Nonce, Config, Instance, true).
|
||||||
|
|
||||||
-spec generate_from_hash(hash(), sci_target(), nonce(), config(), instance()) ->
|
-spec generate_from_hash(hash(), sci_target(), nonce(), config(), instance()) ->
|
||||||
{ok, {nonce(), solution()}} | {error, no_solution} | {error, {runtime, term()}}.
|
{ok, {nonce(), solution()}} | {error, no_solution} | {error, {runtime, term()}}.
|
||||||
generate_from_hash(Hash, Target, Nonce, Config, Instance) ->
|
generate_from_hash(Hash, Target, Nonce, Config, Instance) ->
|
||||||
|
generate_from_hash(Hash, Target, Nonce, Config, Instance, false).
|
||||||
|
|
||||||
|
|
||||||
|
-spec generate_from_hash(hash(), sci_target(), nonce(), config(), instance(), boolean()) ->
|
||||||
|
{ok, {nonce(), solution()}} | {ok, solutions()} | {error, no_solution} | {error, {runtime, term()}}.
|
||||||
|
generate_from_hash(Hash, Target, Nonce, Config, Instance, KeepGoing) ->
|
||||||
Hash64 = base64:encode_to_string(Hash),
|
Hash64 = base64:encode_to_string(Hash),
|
||||||
?log(debug, "Generating solution for data hash ~p and nonce ~p with target ~p.",
|
?log(debug, "Generating solution for data hash ~p and nonce ~p with target ~p.",
|
||||||
[Hash, Nonce, Target]),
|
[Hash, Nonce, Target]),
|
||||||
case generate_int(Hash64, Nonce, Target, Config, Instance) of
|
case generate_int(Hash64, Nonce, Target, Config, Instance, KeepGoing) of
|
||||||
{ok, Nonce1, Soln} ->
|
{ok, Nonce1, Soln} ->
|
||||||
{ok, {Nonce1, Soln}};
|
{ok, {Nonce1, Soln}};
|
||||||
|
{ok, _} = Ok ->
|
||||||
|
Ok;
|
||||||
{error, no_value} ->
|
{error, no_value} ->
|
||||||
?log(debug, "No cuckoo solution found", []),
|
?log(debug, "No cuckoo solution found", []),
|
||||||
{error, no_solution};
|
{error, no_solution};
|
||||||
@ -251,7 +272,7 @@ get_target(Soln, EdgeBits) when is_list(Soln), length(Soln) =:= ?SOLUTION_SIZE -
|
|||||||
|
|
||||||
generate_int(Hash, Nonce, Target,
|
generate_int(Hash, Nonce, Target,
|
||||||
#config{exec = Exec0, extra_args = ExtraArgs0,
|
#config{exec = Exec0, extra_args = ExtraArgs0,
|
||||||
hex_enc_header = HexEncHdr} = Config, Instance) ->
|
hex_enc_header = HexEncHdr} = Config, Instance, KeepGoing) ->
|
||||||
ExtraArgs =
|
ExtraArgs =
|
||||||
case is_miner_instance_addressation_enabled(Config) of
|
case is_miner_instance_addressation_enabled(Config) of
|
||||||
true -> binary_to_list(ExtraArgs0) ++ " -d " ++ integer_to_list(Instance);
|
true -> binary_to_list(ExtraArgs0) ++ " -d " ++ integer_to_list(Instance);
|
||||||
@ -264,10 +285,10 @@ generate_int(Hash, Nonce, Target,
|
|||||||
end,
|
end,
|
||||||
ExecBinDir = exec_bin_dir(Config),
|
ExecBinDir = exec_bin_dir(Config),
|
||||||
Exec = binary_to_list(Exec0),
|
Exec = binary_to_list(Exec0),
|
||||||
generate_int(EncodedHash, Nonce, Target, ExecBinDir, Exec, ExtraArgs, Config).
|
generate_int(EncodedHash, Nonce, Target, ExecBinDir, Exec, ExtraArgs, Config, KeepGoing).
|
||||||
|
|
||||||
generate_int(Hash, Nonce, Target, MinerBinDir, MinerBin, MinerExtraArgs,
|
generate_int(Hash, Nonce, Target, MinerBinDir, MinerBin, MinerExtraArgs,
|
||||||
#config{repeats = Repeats0, edge_bits = EdgeBits}) ->
|
#config{repeats = Repeats0, edge_bits = EdgeBits}, KeepGoing) ->
|
||||||
Repeats = integer_to_list(Repeats0),
|
Repeats = integer_to_list(Repeats0),
|
||||||
Args = ["-h", Hash, "-n", integer_to_list(Nonce), "-r", Repeats | string:tokens(MinerExtraArgs, " ")],
|
Args = ["-h", Hash, "-n", integer_to_list(Nonce), "-r", Repeats | string:tokens(MinerExtraArgs, " ")],
|
||||||
?log(info, "Executing cmd '~s ~s'", [MinerBin, lists:concat(lists:join(" ", Args))]),
|
?log(info, "Executing cmd '~s ~s'", [MinerBin, lists:concat(lists:join(" ", Args))]),
|
||||||
@ -278,6 +299,7 @@ generate_int(Hash, Nonce, Target, MinerBinDir, MinerBin, MinerExtraArgs,
|
|||||||
port = Port,
|
port = Port,
|
||||||
buffer = [],
|
buffer = [],
|
||||||
target = Target,
|
target = Target,
|
||||||
|
keep_going = KeepGoing,
|
||||||
edge_bits = EdgeBits,
|
edge_bits = EdgeBits,
|
||||||
parser = fun parse_generation_result/2});
|
parser = fun parse_generation_result/2});
|
||||||
{error, _Rsn} = Err ->
|
{error, _Rsn} = Err ->
|
||||||
@ -453,7 +475,21 @@ wait_for_result(#state{os_pid = OsPid, port = Port, buffer = Buffer} = State) ->
|
|||||||
exit(shutdown);
|
exit(shutdown);
|
||||||
{'EXIT', Port, normal} ->
|
{'EXIT', Port, normal} ->
|
||||||
%% Process ended but no value found
|
%% Process ended but no value found
|
||||||
{error, no_value};
|
case State#state.solutions of
|
||||||
|
[] ->
|
||||||
|
{error, no_value};
|
||||||
|
|
||||||
|
[_|_] = Sols0 ->
|
||||||
|
Sols = lists:reverse(Sols0),
|
||||||
|
case State#state.keep_going of
|
||||||
|
false ->
|
||||||
|
%% For compatibility, pick the one first found
|
||||||
|
[{N, S}|_] = Sols,
|
||||||
|
{ok, N, S};
|
||||||
|
true ->
|
||||||
|
{ok, Sols}
|
||||||
|
end
|
||||||
|
end;
|
||||||
_Other ->
|
_Other ->
|
||||||
wait_for_result(State)
|
wait_for_result(State)
|
||||||
end.
|
end.
|
||||||
@ -495,16 +531,29 @@ handle_fragmented_lines(Str, Buffer) ->
|
|||||||
parse_generation_result([], State) ->
|
parse_generation_result([], State) ->
|
||||||
wait_for_result(State);
|
wait_for_result(State);
|
||||||
parse_generation_result(["Solution" ++ NonceValuesStr | Rest],
|
parse_generation_result(["Solution" ++ NonceValuesStr | Rest],
|
||||||
#state{os_pid = OsPid, edge_bits = EdgeBits, target = Target} = State) ->
|
#state{os_pid = OsPid, edge_bits = EdgeBits,
|
||||||
|
target = Target, keep_going = KeepGoing} = State) ->
|
||||||
[NonceStr | SolStrs] = string:tokens(NonceValuesStr, " "),
|
[NonceStr | SolStrs] = string:tokens(NonceValuesStr, " "),
|
||||||
Soln = [list_to_integer(string:trim(V, both, [$\r]), 16) || V <- SolStrs],
|
Soln = [list_to_integer(string:trim(V, both, [$\r]), 16) || V <- SolStrs],
|
||||||
case {length(Soln), test_target(Soln, Target, EdgeBits)} of
|
case {length(Soln), test_target(Soln, Target, EdgeBits)} of
|
||||||
{?SOLUTION_SIZE, true} ->
|
{?SOLUTION_SIZE, true} ->
|
||||||
stop_execution(OsPid),
|
?log(debug, "Solution found, KeepGoing = ~p", [KeepGoing]),
|
||||||
|
case KeepGoing of
|
||||||
|
true -> ok;
|
||||||
|
false ->
|
||||||
|
stop_execution(OsPid)
|
||||||
|
end,
|
||||||
case parse_nonce_str(NonceStr) of
|
case parse_nonce_str(NonceStr) of
|
||||||
{ok, Nonce} ->
|
{ok, Nonce} ->
|
||||||
?log(debug, "Solution found: ~p", [Soln]),
|
?log(debug, "Solution found: ~p", [Soln]),
|
||||||
{ok, Nonce, Soln};
|
case KeepGoing of
|
||||||
|
true ->
|
||||||
|
Sols = State#state.solutions,
|
||||||
|
State1 = State#state{solutions = [{Nonce, Soln}|Sols]},
|
||||||
|
parse_generation_result(Rest, State1);
|
||||||
|
false ->
|
||||||
|
{ok, Nonce, Soln}
|
||||||
|
end;
|
||||||
Err = {error, _} ->
|
Err = {error, _} ->
|
||||||
?log(debug, "Bad nonce: ~p", [Err]),
|
?log(debug, "Bad nonce: ~p", [Err]),
|
||||||
Err
|
Err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user