Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit dab3734

Browse files
committed
dont reactivate gw if already active
1 parent ce5f544 commit dab3734

File tree

5 files changed

+70
-49
lines changed

5 files changed

+70
-49
lines changed

src/blockchain_utils.erl

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
var_cache_stats/0,
5959
teardown_var_cache/0,
6060
init_var_cache/0,
61-
target_v_to_mod/1
61+
target_v_to_mod/1,
62+
is_gw_active/3,
63+
max_activity_age/1
6264

6365
]).
6466

@@ -728,7 +730,38 @@ target_v_to_mod({ok, V}) when V =< 5 ->
728730
target_v_to_mod({ok, 6}) ->
729731
blockchain_poc_target_v6.
730732

731-
%% ------------------------------------------------------------------
733+
734+
-spec is_gw_active(
735+
CurHeight :: pos_integer(),
736+
LastActivity :: undefined | pos_integer(),
737+
MaxActivityAge :: pos_integer()
738+
) -> boolean().
739+
is_gw_active(_CurHeight, undefined, _MaxActivityAge) ->
740+
%% no activity, default to in active
741+
false;
742+
is_gw_active(CurHeight, LastActivity, MaxActivityAge) ->
743+
(CurHeight - LastActivity) < MaxActivityAge.
744+
745+
-spec max_activity_age(Vars :: map() | blockchain_ledger_v1:ledger()) -> pos_integer().
746+
%% need to cater for deriving max activity age from ledger directly
747+
%% or from a supplied map containing required vars
748+
max_activity_age(Vars) when is_map(Vars)->
749+
case maps:get(harmonize_activity_on_hip17_interactivity_blocks, Vars, false) of
750+
true -> maps:get(hip17_interactivity_blocks, Vars);
751+
false -> maps:get(poc_v4_target_challenge_age, Vars)
752+
end;
753+
max_activity_age(Ledger) ->
754+
{ok, MaxActivityAge} =
755+
case
756+
blockchain:config(
757+
?harmonize_activity_on_hip17_interactivity_blocks, Ledger
758+
)
759+
of
760+
{ok, true} -> blockchain:config(?hip17_interactivity_blocks, Ledger);
761+
_ -> blockchain:config(?poc_v4_target_challenge_age, Ledger)
762+
end,
763+
MaxActivityAge.
764+
%% ------------------------------------------------------------------
732765
%% EUNIT Tests
733766
%% ------------------------------------------------------------------
734767
-ifdef(TEST).

src/poc/blockchain_poc_path_v4.erl

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ is_witness_stale(GatewayAddr, Height, Vars, Ledger) ->
415415
%% No POC challenge, don't include
416416
true;
417417
{ok, C} ->
418-
%% Check challenge age is recent depending on the set chain var
419-
(Height - C) >= challenge_age(Vars)
418+
%% Check activity age is recent depending on the set chain var
419+
(Height - C) >= blockchain_utils:max_activity_age(Vars)
420420
end.
421421

422422
-spec rssi_weight(Vars :: map()) -> float().
@@ -467,13 +467,6 @@ centrality_wt(Vars) ->
467467
poc_version(Vars) ->
468468
maps:get(poc_version, Vars).
469469

470-
-spec challenge_age(Vars :: map()) -> pos_integer().
471-
challenge_age(Vars) ->
472-
case maps:get(harmonize_activity_on_hip17_interactivity_blocks, Vars, false) of
473-
true -> maps:get(hip17_interactivity_blocks, Vars);
474-
false -> maps:get(poc_v4_target_challenge_age, Vars)
475-
end.
476-
477470
-spec poc_good_bucket_low(Vars :: map()) -> integer().
478471
poc_good_bucket_low(Vars) ->
479472
maps:get(poc_good_bucket_low, Vars).

src/poc/blockchain_poc_target_v5.erl

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ filter(AddrList, Ledger, Height, Vars) ->
152152
{ok, V} -> V;
153153
_ -> false
154154
end,
155-
MaxActivityAge = max_activity_age(Vars),
155+
MaxActivityAge = blockchain_utils:max_activity_age(Vars),
156156
lists:filter(
157157
fun(A) ->
158158
{ok, Gateway} = blockchain_ledger_v1:find_gateway_info(A, Ledger),
159159
Mode = blockchain_ledger_gateway_v2:mode(Gateway),
160160
LastActivity = blockchain_ledger_gateway_v2:last_poc_challenge(Gateway),
161-
is_active(ActivityFilterEnabled, LastActivity, MaxActivityAge, Height) andalso
161+
is_active(ActivityFilterEnabled, Height, LastActivity, MaxActivityAge) andalso
162162
blockchain_ledger_gateway_v2:is_valid_capability(
163163
Mode,
164164
?GW_CAPABILITY_POC_CHALLENGEE,
@@ -204,19 +204,11 @@ limit_addrs(_Vars, RandState, Witnesses) ->
204204
{RandState, Witnesses}.
205205

206206
-spec is_active(ActivityFilterEnabled :: boolean(),
207-
LastActivity :: pos_integer(),
208-
MaxActivityAge :: pos_integer(),
209-
Height :: pos_integer()) -> boolean().
210-
is_active(true, undefined, _MaxActivityAge, _Height) ->
211-
false;
212-
is_active(true, LastActivity, MaxActivityAge, Height) ->
213-
(Height - LastActivity) < MaxActivityAge;
214-
is_active(_ActivityFilterEnabled, _Gateway, _Height, _Vars) ->
215-
true.
216-
217-
-spec max_activity_age(Vars :: map()) -> pos_integer().
218-
max_activity_age(Vars) ->
219-
case maps:get(harmonize_activity_on_hip17_interactivity_blocks, Vars, false) of
220-
true -> maps:get(hip17_interactivity_blocks, Vars);
221-
false -> maps:get(poc_v4_target_challenge_age, Vars)
222-
end.
207+
Height :: pos_integer(),
208+
LastActivity :: undefined | pos_integer(),
209+
MaxActivityAge :: pos_integer()
210+
) -> boolean().
211+
is_active(false, _Height, _LastActivity, _MaxActivityAge) ->
212+
true;
213+
is_active(true, Height, LastActivity, MaxActivityAge) ->
214+
blockchain_utils:is_gw_active(Height, LastActivity, MaxActivityAge).

src/poc/blockchain_poc_target_v6.erl

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ filter(AddrList, Height, Ledger, Vars) ->
183183
{ok, V} -> V;
184184
_ -> false
185185
end,
186-
MaxActivityAge = max_activity_age(Vars),
186+
MaxActivityAge = blockchain_utils:max_activity_age(Vars),
187187
lists:filter(
188188
fun(A) ->
189189
{ok, Gateway} = blockchain_ledger_v1:find_gateway_info(A, Ledger),
190190
Mode = blockchain_ledger_gateway_v2:mode(Gateway),
191191
LastActivity = blockchain_ledger_gateway_v2:last_poc_challenge(Gateway),
192-
is_active(ActivityFilterEnabled, LastActivity, MaxActivityAge, Height) andalso
192+
is_active(ActivityFilterEnabled, Height, LastActivity, MaxActivityAge) andalso
193193
blockchain_ledger_gateway_v2:is_valid_capability(
194194
Mode,
195195
?GW_CAPABILITY_POC_CHALLENGEE,
@@ -247,19 +247,11 @@ limit_addrs(_Vars, RandState, Witnesses) ->
247247
{RandState, Witnesses}.
248248

249249
-spec is_active(ActivityFilterEnabled :: boolean(),
250-
LastActivity :: pos_integer(),
251-
MaxActivityAge :: pos_integer(),
252-
Height :: pos_integer()) -> boolean().
253-
is_active(true, undefined, _MaxActivityAge, _Height) ->
254-
false;
255-
is_active(true, LastActivity, MaxActivityAge, Height) ->
256-
(Height - LastActivity) < MaxActivityAge;
257-
is_active(_ActivityFilterEnabled, _Gateway, _Height, _Vars) ->
258-
true.
259-
260-
-spec max_activity_age(Vars :: map()) -> pos_integer().
261-
max_activity_age(Vars) ->
262-
case maps:get(harmonize_activity_on_hip17_interactivity_blocks, Vars, false) of
263-
true -> maps:get(hip17_interactivity_blocks, Vars);
264-
false -> maps:get(poc_v4_target_challenge_age, Vars)
265-
end.
250+
Height :: pos_integer(),
251+
LastActivity :: undefined | pos_integer(),
252+
MaxActivityAge :: pos_integer()
253+
) -> boolean().
254+
is_active(false, _Height, _LastActivity, _MaxActivityAge) ->
255+
true;
256+
is_active(true, Height, LastActivity, MaxActivityAge) ->
257+
blockchain_utils:is_gw_active(Height, LastActivity, MaxActivityAge).

src/transactions/v1/blockchain_txn_validator_heartbeat_v1.erl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,24 @@ maybe_reactivate_gateways(Txn, Ledger) ->
269269
end.
270270

271271
reactivate_gws(GWAddrs, Height, Ledger) ->
272+
%% when deciding if we need to update a GWs last activity
273+
%% use the ledger height as opposed to the txn height
274+
%% the GW could have been updated via another HB or POC activity
275+
%% prior to this txn being handled
276+
CurHeight = blockchain_ledger_v1:current_height(Ledger),
272277
lists:foreach(
273278
fun(GWAddr) ->
274-
case blockchain_ledger_v1:find_gateway_info(GWAddr, Ledger) of
279+
case blockchain_ledger_v1:find_gateway_location(GWAddr, Ledger) of
275280
{error, _} ->
276281
{error, no_active_gateway};
282+
{ok, undefined} ->
283+
{error, gw_not_asserted};
277284
{ok, GW0} ->
278-
blockchain_ledger_v1:reactivate_gateway(Height, GW0, GWAddr, Ledger)
285+
LastActivity = blockchain_ledger_gateway_v2:last_poc_challenge(GW0),
286+
case blockchain_utils:is_gw_active(CurHeight, LastActivity, Ledger) of
287+
false -> blockchain_ledger_v1:reactivate_gateway(Height, GW0, GWAddr, Ledger);
288+
true -> ok
289+
end
279290
end
280291
end, GWAddrs).
281292

0 commit comments

Comments
 (0)