Skip to content

Commit

Permalink
Drop checkpoints from #nonce_limiter_update
Browse files Browse the repository at this point in the history
  • Loading branch information
Lev Berman committed May 23, 2024
1 parent cb5f2a8 commit 2338807
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
1 change: 0 additions & 1 deletion apps/arweave/include/ar.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@
-record(nonce_limiter_update, {
session_key,
session,
checkpoints,
is_partial = true
}).

Expand Down
19 changes: 10 additions & 9 deletions apps/arweave/src/ar_nonce_limiter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,8 @@ handle_cast({validated_steps, Args}, State) ->
{step_number, StepNumber}]),
{_, Steps2} =
get_step_range(Steps, StepNumber, CurrentStepNumber + 1, StepNumber),
update_session(Session, StepNumber, LastStepCheckpoints, Steps2, #{});
update_session(Session, StepNumber,
#{ StepNumber => LastStepCheckpoints }, Steps2);
false ->
Session
end,
Expand Down Expand Up @@ -718,7 +719,8 @@ handle_info({computed, Args}, State) ->
{output, ar_util:encode(Output)}]),
{noreply, State};
true ->
Session2 = update_session(Session, StepNumber, Checkpoints, [Output], #{}),
Session2 = update_session(Session, StepNumber,
#{ StepNumber => Checkpoints }, [Output]),
State2 = cache_session(State, CurrentSessionKey, Session2),
?LOG_DEBUG([{event, new_vdf_step}, {source, computed},
{session_key, encode_session_key(CurrentSessionKey)}, {step_number, StepNumber}]),
Expand Down Expand Up @@ -747,11 +749,10 @@ session_key(NextSeed, StepNumber, NextVDFDifficulty) ->
get_session(SessionKey, #state{ session_by_key = SessionByKey }) ->
maps:get(SessionKey, SessionByKey, not_found).

update_session(Session, StepNumber, Checkpoints, Steps, StepCheckpointsMap) ->
update_session(Session, StepNumber, StepCheckpointsMap, Steps) ->
#vdf_session{ step_checkpoints_map = Map } = Session,
Map2 = maps:put(StepNumber, Checkpoints, Map),
Map3 = maps:merge(StepCheckpointsMap, Map2),
update_session(Session#vdf_session{ step_checkpoints_map = Map3 }, StepNumber, Steps).
Map2 = maps:merge(StepCheckpointsMap, Map),
update_session(Session#vdf_session{ step_checkpoints_map = Map2 }, StepNumber, Steps).

update_session(Session, StepNumber, Steps) ->
#vdf_session{ steps = CurrentSteps } = Session,
Expand Down Expand Up @@ -1075,7 +1076,7 @@ apply_external_update2(Update, State) ->
session = #vdf_session{
step_checkpoints_map = StepCheckpointsMap,
prev_session_key = PrevSessionKey, step_number = StepNumber } = Session,
checkpoints = Checkpoints, is_partial = IsPartial } = Update,
is_partial = IsPartial } = Update,
{_SessionSeed, SessionInterval, _SessionVDFDifficulty} = SessionKey,
case get_session(SessionKey, State) of
not_found ->
Expand Down Expand Up @@ -1114,8 +1115,8 @@ apply_external_update2(Update, State) ->
case CurrentStepNumber + 1 == StepNumber of
true ->
[Output | _] = Session#vdf_session.steps,
CurrentSession2 = update_session(CurrentSession, StepNumber, Checkpoints,
[Output], StepCheckpointsMap),
CurrentSession2 = update_session(CurrentSession, StepNumber,
StepCheckpointsMap, [Output]),
State2 = apply_external_update3(State, SessionKey, CurrentSession2, [Output]),
{reply, ok, State2};
false ->
Expand Down
3 changes: 1 addition & 2 deletions apps/arweave/src/ar_nonce_limiter_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,12 @@ make_nonce_limiter_update(_SessionKey, not_found, _IsPartial) ->
not_found;
make_nonce_limiter_update(SessionKey, Session, IsPartial) ->
StepNumber = Session#vdf_session.step_number,
Checkpoints = maps:get(StepNumber, Session#vdf_session.step_checkpoints_map, []),
StepCheckpointsMap = Session#vdf_session.step_checkpoints_map,
%% Clear the step_checkpoints_map to cut down on the amount of data pushed to each client.
RecentStepNumbers = get_recent_step_numbers(StepNumber),
StepCheckpointsMap2 = maps:with(RecentStepNumbers, StepCheckpointsMap),
#nonce_limiter_update{ session_key = SessionKey,
is_partial = IsPartial, checkpoints = Checkpoints,
is_partial = IsPartial,
session = Session#vdf_session{ step_checkpoints_map = StepCheckpointsMap2 } }.

get_recent_step_numbers(StepNumber) ->
Expand Down
7 changes: 4 additions & 3 deletions apps/arweave/src/ar_serialize.erl
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,9 @@ binary_to_block_time_history(_Rest, _BlockTimeHistory) ->
%% by nodes that compute their own VDF and never need to be shared from VDF server to VDF client.
nonce_limiter_update_to_binary(2 = _Format, #nonce_limiter_update{
session_key = {NextSeed, Interval, NextVDFDifficulty},
session = Session, checkpoints = Checkpoints, is_partial = IsPartial }) ->
session = Session, is_partial = IsPartial }) ->
#vdf_session{ step_number = StepNumber, step_checkpoints_map = Map } = Session,
Checkpoints = maps:get(StepNumber, Map, []),
IsPartialBin = case IsPartial of true -> << 1:8 >>; _ -> << 0:8 >> end,
CheckpointLen = length(Checkpoints),
<< NextSeed:48/binary, (ar_serialize:encode_int(NextVDFDifficulty, 8))/binary,
Expand Down Expand Up @@ -432,9 +434,9 @@ binary_to_nonce_limiter_update(2, % Format
when UpperBoundSize > 0, StepsLen > 0, CheckpointLen == ?VDF_CHECKPOINT_COUNT_IN_STEP ->
NextUpperBound2 = case NextUpperBoundSize of 0 -> undefined; _ -> NextUpperBound end,
Update = #nonce_limiter_update{ session_key = {NextSeed, Interval, NextVDFDifficulty},
checkpoints = parse_32b_list(Checkpoints),
is_partial = case IsPartial of 0 -> false; _ -> true end,
session = Session = #vdf_session{ step_number = StepNumber, seed = Seed,
step_checkpoints_map = #{ StepNumber => parse_32b_list(Checkpoints) },
upper_bound = UpperBound, next_upper_bound = NextUpperBound2,
steps = parse_32b_list(Steps) } },
case decode_session_key(PrevSessionKeyBin) of
Expand Down Expand Up @@ -462,7 +464,6 @@ binary_to_nonce_limiter_update(3, % Format = 3.
NextUpperBound2 = case NextUpperBoundSize of 0 -> undefined; _ -> NextUpperBound end,
StepCheckpointsMap = decode_step_checkpoints_map(CheckpointsMapBin, #{}),
Update = #nonce_limiter_update{ session_key = {NextSeed, Interval, NextVDFDifficulty},
checkpoints = maps:get(StepNumber, StepCheckpointsMap),
is_partial = case IsPartial of 0 -> false; _ -> true end,
session = Session = #vdf_session{ step_number = StepNumber, seed = Seed,
upper_bound = UpperBound, next_upper_bound = NextUpperBound2,
Expand Down

0 comments on commit 2338807

Please sign in to comment.