Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/vdf client incomplete prev session #570

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/arweave/src/ar_http_iface_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ get_vdf_update(Peer) ->
{ok, {{<<"200">>, _}, _, Bin, _, _}} ->
ar_serialize:binary_to_nonce_limiter_update(Bin);
{ok, {{<<"404">>, _}, _, _, _, _}} ->
not_found;
{error, not_found};
{ok, {{Status, _}, _, ResponseBody, _, _}} ->
{error, {Status, ResponseBody}};
Reply ->
Expand All @@ -535,7 +535,7 @@ get_vdf_session(Peer) ->
{ok, {{<<"200">>, _}, _, Bin, _, _}} ->
ar_serialize:binary_to_nonce_limiter_update(Bin);
{ok, {{<<"404">>, _}, _, _, _, _}} ->
not_found;
{error, not_found};
{ok, {{Status, _}, _, ResponseBody, _, _}} ->
{error, {Status, ResponseBody}};
Reply ->
Expand All @@ -548,7 +548,7 @@ get_previous_vdf_session(Peer) ->
{ok, {{<<"200">>, _}, _, Bin, _, _}} ->
ar_serialize:binary_to_nonce_limiter_update(Bin);
{ok, {{<<"404">>, _}, _, _, _, _}} ->
not_found;
{error, not_found};
{ok, {{Status, _}, _, ResponseBody, _, _}} ->
{error, {Status, ResponseBody}};
Reply ->
Expand Down
1 change: 1 addition & 0 deletions apps/arweave/src/ar_nonce_limiter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ request_validation(H, #nonce_limiter_info{ output = Output,
true ->
%% Wait for our VDF server(s) to validate the remaining steps.
%% Alternatively, the network may abandon this block.
ar_nonce_limiter_client:request_sessions(),
spawn(fun() -> ar_events:send(nonce_limiter, {refuse_validation, H}) end);
false ->
%% Validate the remaining steps.
Expand Down
34 changes: 26 additions & 8 deletions apps/arweave/src/ar_nonce_limiter_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-behaviour(gen_server).

-export([start_link/0]).
-export([start_link/0, request_sessions/0]).

-export([init/1, handle_cast/2, handle_call/3, handle_info/2, terminate/2]).

Expand All @@ -21,6 +21,11 @@
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

%% @doc Pick a server from the list of configured VDF servers and request complete
%% VDF sessions from them. Failures to resolve a domain of make a request are not retried.
JamesPiechota marked this conversation as resolved.
Show resolved Hide resolved
request_sessions() ->
gen_server:cast(?MODULE, request_sessions).

%%%===================================================================
%%% Generic server callbacks.
%%%===================================================================
Expand Down Expand Up @@ -80,7 +85,7 @@ handle_cast(pull, State) ->
{noreply, State2}
end
end;
not_found ->
{error, not_found} ->
%% The server might be restarting. Try another one, if there are any.
{noreply, State#state{ remote_servers = queue:in(RawPeer, Q2) }};
{error, Reason} ->
Expand All @@ -92,6 +97,23 @@ handle_cast(pull, State) ->
end
end;

handle_cast(request_sessions, State) ->
#state{ remote_servers = Q } = State,
{{value, RawPeer}, Q2} = queue:out(Q),
State2 = State#state{ remote_servers = queue:in(RawPeer, Q2) },
case resolve_peer(RawPeer) of
error ->
%% Push the peer to the back of the queue.
{noreply, State2};
{ok, Peer} ->
case fetch_and_apply_session_and_previous_session(Peer) of
ok ->
{noreply, State};
JamesPiechota marked this conversation as resolved.
Show resolved Hide resolved
_ ->
{noreply, State2}
end
end;

handle_cast(Cast, State) ->
?LOG_WARNING([{event, unhandled_cast}, {module, ?MODULE}, {cast, Cast}]),
{noreply, State}.
Expand Down Expand Up @@ -127,8 +149,7 @@ fetch_and_apply_session_and_previous_session(Peer) ->
case ar_http_iface_client:get_previous_vdf_session(Peer) of
{ok, #nonce_limiter_update{ session_key = PrevSessionKey } = Update2} ->
ar_nonce_limiter:apply_external_update(Update2, Peer),
ar_nonce_limiter:apply_external_update(Update, Peer),
ok;
ar_nonce_limiter:apply_external_update(Update, Peer);
{ok, _} ->
%% The session should have just changed, retry.
fetch_and_apply_session_and_previous_session(Peer);
Expand All @@ -148,10 +169,7 @@ fetch_and_apply_session_and_previous_session(Peer) ->
fetch_and_apply_session(Peer) ->
case ar_http_iface_client:get_vdf_session(Peer) of
{ok, Update} ->
ar_nonce_limiter:apply_external_update(Update, Peer),
ok;
not_found ->
not_found;
ar_nonce_limiter:apply_external_update(Update, Peer);
{error, Reason} = Error ->
?LOG_WARNING([{event, failed_to_fetch_vdf_session},
{peer, ar_util:format_peer(Peer)},
Expand Down
17 changes: 13 additions & 4 deletions apps/arweave/src/ar_nonce_limiter_server_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,15 @@ push_update(SessionKey, StepNumber, Output, Peer, Format, State) ->
%% Client requested the full session
PrevSessionKey = Session#vdf_session.prev_session_key,
PrevSession = ar_nonce_limiter:get_session(PrevSessionKey),
push_session(PrevSessionKey, PrevSession, Peer, Format),
push_session(SessionKey, Session, Peer, Format),
case push_session(PrevSessionKey, PrevSession, Peer, Format) of
ok ->
%% Do not push the new session until the previous
%% session is in line with our view (i.e., has steps
%% at least up to StepNumber where the new session begins).
push_session(SessionKey, Session, Peer, Format);
fail ->
ok
end,
State;
{true, true, true, false} ->
%% Client requested missing steps
Expand Down Expand Up @@ -158,9 +165,11 @@ push_session(SessionKey, Session, Peer, Format) ->
session_found = ReportedSessionFound }} ->
log_failure(Peer, SessionKey, Update, behind_client,
[{client_step_number, ClientStepNumber},
{session_found, ReportedSessionFound}]);
{session_found, ReportedSessionFound}]),
fail;
{error, Error} ->
log_failure(Peer, SessionKey, Update, Error, [])
log_failure(Peer, SessionKey, Update, Error, []),
fail
end
end.

Expand Down
11 changes: 9 additions & 2 deletions apps/arweave/src/ar_storage_module.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,15 @@ packing_label(Packing) ->
label_by_id("default") ->
"default";
label_by_id(StoreID) ->
M = get_by_id(StoreID),
label(M).
case get_by_id(StoreID) of
not_found ->
%% Occurs in tests on application shutdown.
?LOG_WARNING([{event, store_id_for_label_not_found},
{store_id, StoreID}]),
"error";
M ->
label(M)
end.

%% @doc Return the storage module with the given identifier or not_found.
%% Search across both attached modules and repacked in-place modules.
Expand Down
Loading