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

Commit

Permalink
dont reactivate gw if already active
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed May 24, 2022
1 parent ce5f544 commit dab3734
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 49 deletions.
37 changes: 35 additions & 2 deletions src/blockchain_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
var_cache_stats/0,
teardown_var_cache/0,
init_var_cache/0,
target_v_to_mod/1
target_v_to_mod/1,
is_gw_active/3,
max_activity_age/1

]).

Expand Down Expand Up @@ -728,7 +730,38 @@ target_v_to_mod({ok, V}) when V =< 5 ->
target_v_to_mod({ok, 6}) ->
blockchain_poc_target_v6.

%% ------------------------------------------------------------------

-spec is_gw_active(
CurHeight :: pos_integer(),
LastActivity :: undefined | pos_integer(),
MaxActivityAge :: pos_integer()
) -> boolean().
is_gw_active(_CurHeight, undefined, _MaxActivityAge) ->
%% no activity, default to in active
false;
is_gw_active(CurHeight, LastActivity, MaxActivityAge) ->
(CurHeight - LastActivity) < MaxActivityAge.

-spec max_activity_age(Vars :: map() | blockchain_ledger_v1:ledger()) -> pos_integer().
%% need to cater for deriving max activity age from ledger directly
%% or from a supplied map containing required vars
max_activity_age(Vars) when is_map(Vars)->
case maps:get(harmonize_activity_on_hip17_interactivity_blocks, Vars, false) of
true -> maps:get(hip17_interactivity_blocks, Vars);
false -> maps:get(poc_v4_target_challenge_age, Vars)
end;
max_activity_age(Ledger) ->
{ok, MaxActivityAge} =
case
blockchain:config(
?harmonize_activity_on_hip17_interactivity_blocks, Ledger
)
of
{ok, true} -> blockchain:config(?hip17_interactivity_blocks, Ledger);
_ -> blockchain:config(?poc_v4_target_challenge_age, Ledger)
end,
MaxActivityAge.
%% ------------------------------------------------------------------
%% EUNIT Tests
%% ------------------------------------------------------------------
-ifdef(TEST).
Expand Down
11 changes: 2 additions & 9 deletions src/poc/blockchain_poc_path_v4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ is_witness_stale(GatewayAddr, Height, Vars, Ledger) ->
%% No POC challenge, don't include
true;
{ok, C} ->
%% Check challenge age is recent depending on the set chain var
(Height - C) >= challenge_age(Vars)
%% Check activity age is recent depending on the set chain var
(Height - C) >= blockchain_utils:max_activity_age(Vars)
end.

-spec rssi_weight(Vars :: map()) -> float().
Expand Down Expand Up @@ -467,13 +467,6 @@ centrality_wt(Vars) ->
poc_version(Vars) ->
maps:get(poc_version, Vars).

-spec challenge_age(Vars :: map()) -> pos_integer().
challenge_age(Vars) ->
case maps:get(harmonize_activity_on_hip17_interactivity_blocks, Vars, false) of
true -> maps:get(hip17_interactivity_blocks, Vars);
false -> maps:get(poc_v4_target_challenge_age, Vars)
end.

-spec poc_good_bucket_low(Vars :: map()) -> integer().
poc_good_bucket_low(Vars) ->
maps:get(poc_good_bucket_low, Vars).
Expand Down
28 changes: 10 additions & 18 deletions src/poc/blockchain_poc_target_v5.erl
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ filter(AddrList, Ledger, Height, Vars) ->
{ok, V} -> V;
_ -> false
end,
MaxActivityAge = max_activity_age(Vars),
MaxActivityAge = blockchain_utils:max_activity_age(Vars),
lists:filter(
fun(A) ->
{ok, Gateway} = blockchain_ledger_v1:find_gateway_info(A, Ledger),
Mode = blockchain_ledger_gateway_v2:mode(Gateway),
LastActivity = blockchain_ledger_gateway_v2:last_poc_challenge(Gateway),
is_active(ActivityFilterEnabled, LastActivity, MaxActivityAge, Height) andalso
is_active(ActivityFilterEnabled, Height, LastActivity, MaxActivityAge) andalso
blockchain_ledger_gateway_v2:is_valid_capability(
Mode,
?GW_CAPABILITY_POC_CHALLENGEE,
Expand Down Expand Up @@ -204,19 +204,11 @@ limit_addrs(_Vars, RandState, Witnesses) ->
{RandState, Witnesses}.

-spec is_active(ActivityFilterEnabled :: boolean(),
LastActivity :: pos_integer(),
MaxActivityAge :: pos_integer(),
Height :: pos_integer()) -> boolean().
is_active(true, undefined, _MaxActivityAge, _Height) ->
false;
is_active(true, LastActivity, MaxActivityAge, Height) ->
(Height - LastActivity) < MaxActivityAge;
is_active(_ActivityFilterEnabled, _Gateway, _Height, _Vars) ->
true.

-spec max_activity_age(Vars :: map()) -> pos_integer().
max_activity_age(Vars) ->
case maps:get(harmonize_activity_on_hip17_interactivity_blocks, Vars, false) of
true -> maps:get(hip17_interactivity_blocks, Vars);
false -> maps:get(poc_v4_target_challenge_age, Vars)
end.
Height :: pos_integer(),
LastActivity :: undefined | pos_integer(),
MaxActivityAge :: pos_integer()
) -> boolean().
is_active(false, _Height, _LastActivity, _MaxActivityAge) ->
true;
is_active(true, Height, LastActivity, MaxActivityAge) ->
blockchain_utils:is_gw_active(Height, LastActivity, MaxActivityAge).
28 changes: 10 additions & 18 deletions src/poc/blockchain_poc_target_v6.erl
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ filter(AddrList, Height, Ledger, Vars) ->
{ok, V} -> V;
_ -> false
end,
MaxActivityAge = max_activity_age(Vars),
MaxActivityAge = blockchain_utils:max_activity_age(Vars),
lists:filter(
fun(A) ->
{ok, Gateway} = blockchain_ledger_v1:find_gateway_info(A, Ledger),
Mode = blockchain_ledger_gateway_v2:mode(Gateway),
LastActivity = blockchain_ledger_gateway_v2:last_poc_challenge(Gateway),
is_active(ActivityFilterEnabled, LastActivity, MaxActivityAge, Height) andalso
is_active(ActivityFilterEnabled, Height, LastActivity, MaxActivityAge) andalso
blockchain_ledger_gateway_v2:is_valid_capability(
Mode,
?GW_CAPABILITY_POC_CHALLENGEE,
Expand Down Expand Up @@ -247,19 +247,11 @@ limit_addrs(_Vars, RandState, Witnesses) ->
{RandState, Witnesses}.

-spec is_active(ActivityFilterEnabled :: boolean(),
LastActivity :: pos_integer(),
MaxActivityAge :: pos_integer(),
Height :: pos_integer()) -> boolean().
is_active(true, undefined, _MaxActivityAge, _Height) ->
false;
is_active(true, LastActivity, MaxActivityAge, Height) ->
(Height - LastActivity) < MaxActivityAge;
is_active(_ActivityFilterEnabled, _Gateway, _Height, _Vars) ->
true.

-spec max_activity_age(Vars :: map()) -> pos_integer().
max_activity_age(Vars) ->
case maps:get(harmonize_activity_on_hip17_interactivity_blocks, Vars, false) of
true -> maps:get(hip17_interactivity_blocks, Vars);
false -> maps:get(poc_v4_target_challenge_age, Vars)
end.
Height :: pos_integer(),
LastActivity :: undefined | pos_integer(),
MaxActivityAge :: pos_integer()
) -> boolean().
is_active(false, _Height, _LastActivity, _MaxActivityAge) ->
true;
is_active(true, Height, LastActivity, MaxActivityAge) ->
blockchain_utils:is_gw_active(Height, LastActivity, MaxActivityAge).
15 changes: 13 additions & 2 deletions src/transactions/v1/blockchain_txn_validator_heartbeat_v1.erl
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,24 @@ maybe_reactivate_gateways(Txn, Ledger) ->
end.

reactivate_gws(GWAddrs, Height, Ledger) ->
%% when deciding if we need to update a GWs last activity
%% use the ledger height as opposed to the txn height
%% the GW could have been updated via another HB or POC activity
%% prior to this txn being handled
CurHeight = blockchain_ledger_v1:current_height(Ledger),
lists:foreach(
fun(GWAddr) ->
case blockchain_ledger_v1:find_gateway_info(GWAddr, Ledger) of
case blockchain_ledger_v1:find_gateway_location(GWAddr, Ledger) of
{error, _} ->
{error, no_active_gateway};
{ok, undefined} ->
{error, gw_not_asserted};
{ok, GW0} ->
blockchain_ledger_v1:reactivate_gateway(Height, GW0, GWAddr, Ledger)
LastActivity = blockchain_ledger_gateway_v2:last_poc_challenge(GW0),
case blockchain_utils:is_gw_active(CurHeight, LastActivity, Ledger) of
false -> blockchain_ledger_v1:reactivate_gateway(Height, GW0, GWAddr, Ledger);
true -> ok
end
end
end, GWAddrs).

Expand Down

0 comments on commit dab3734

Please sign in to comment.