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 0d330f7 commit 7d8f7fb
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 62 deletions.
2 changes: 0 additions & 2 deletions include/blockchain_vars.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@
%% when determining if a GW is active or inactive: boolean
-define(harmonize_activity_on_hip17_interactivity_blocks, harmonize_activity_on_hip17_interactivity_blocks).

-define(poc_hardcode_poc_selection_count, poc_hardcode_poc_selection_count).

%% Number of blocks to wait before a hotspot can be eligible to participate in a poc
%% challenge. This would avoid new hotspots getting challenged before they sync to an
%% acceptable height.
Expand Down
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
10 changes: 2 additions & 8 deletions src/ledger/v1/blockchain_ledger_v1.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2132,22 +2132,16 @@ process_poc_proposals(BlockHeight, BlockHash, Ledger) ->
%% Mark the selected POCs as active on ledger
case blockchain:config(?poc_challenge_rate, Ledger) of
{ok, K} ->

K2 =
case blockchain:config(?poc_hardcode_poc_selection_count, Ledger) of
{ok, V} -> V;
_ -> K
end,
ProposalGCWindowCheck =
case blockchain:config(?poc_proposal_gc_window_check, Ledger) of
{ok, V2} -> V2;
{ok, V} -> V;
_ -> false
end,
{ok, POCValKeyProposalTimeout} = blockchain:config(?poc_validator_ephemeral_key_timeout, Ledger),
RandState = blockchain_utils:rand_state(BlockHash),
{Name, DB, CF} = proposed_pocs_cf(Ledger),
{ok, Itr} = rocksdb:iterator(DB, CF, []),
POCSubset = promote_proposals(K2, BlockHash, BlockHeight, POCValKeyProposalTimeout,
POCSubset = promote_proposals(K, BlockHash, BlockHeight, POCValKeyProposalTimeout,
ProposalGCWindowCheck, RandState, Ledger, Name, Itr, []),
catch rocksdb:iterator_close(Itr),
lager:debug("Selected POCs ~p", [POCSubset]),
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
3 changes: 0 additions & 3 deletions src/transactions/v1/blockchain_txn_vars_v1.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1132,9 +1132,6 @@ validate_var(?poc_targeting_version, Value) ->
_ ->
throw({error, {invalid_poc_targeting_version, Value}})
end;
validate_var(?poc_hardcode_poc_selection_count, Value) ->
validate_int(Value, "poc_hardcode_poc_selection_count", 1, 500, false);

validate_var(?poc_hexing_type, Value) ->
case Value of
hex_h3dex -> ok;
Expand Down

0 comments on commit 7d8f7fb

Please sign in to comment.