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

Commit 01fe359

Browse files
committed
dont reactivate gw if already active
1 parent 0d330f7 commit 01fe359

File tree

8 files changed

+68
-59
lines changed

8 files changed

+68
-59
lines changed

include/blockchain_vars.hrl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@
181181
%% when determining if a GW is active or inactive: boolean
182182
-define(harmonize_activity_on_hip17_interactivity_blocks, harmonize_activity_on_hip17_interactivity_blocks).
183183

184-
-define(poc_hardcode_poc_selection_count, poc_hardcode_poc_selection_count).
185-
186184
%% Number of blocks to wait before a hotspot can be eligible to participate in a poc
187185
%% challenge. This would avoid new hotspots getting challenged before they sync to an
188186
%% acceptable height.

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 :: 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/ledger/v1/blockchain_ledger_v1.erl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,22 +2132,16 @@ process_poc_proposals(BlockHeight, BlockHash, Ledger) ->
21322132
%% Mark the selected POCs as active on ledger
21332133
case blockchain:config(?poc_challenge_rate, Ledger) of
21342134
{ok, K} ->
2135-
2136-
K2 =
2137-
case blockchain:config(?poc_hardcode_poc_selection_count, Ledger) of
2138-
{ok, V} -> V;
2139-
_ -> K
2140-
end,
21412135
ProposalGCWindowCheck =
21422136
case blockchain:config(?poc_proposal_gc_window_check, Ledger) of
2143-
{ok, V2} -> V2;
2137+
{ok, V} -> V;
21442138
_ -> false
21452139
end,
21462140
{ok, POCValKeyProposalTimeout} = blockchain:config(?poc_validator_ephemeral_key_timeout, Ledger),
21472141
RandState = blockchain_utils:rand_state(BlockHash),
21482142
{Name, DB, CF} = proposed_pocs_cf(Ledger),
21492143
{ok, Itr} = rocksdb:iterator(DB, CF, []),
2150-
POCSubset = promote_proposals(K2, BlockHash, BlockHeight, POCValKeyProposalTimeout,
2144+
POCSubset = promote_proposals(K, BlockHash, BlockHeight, POCValKeyProposalTimeout,
21512145
ProposalGCWindowCheck, RandState, Ledger, Name, Itr, []),
21522146
catch rocksdb:iterator_close(Itr),
21532147
lager:debug("Selected POCs ~p", [POCSubset]),

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: 9 additions & 17 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+
Height :: pos_integer(),
207208
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.
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: 9 additions & 17 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+
Height :: pos_integer(),
250251
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.
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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,22 @@ 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};
277282
{ok, GW0} ->
278-
blockchain_ledger_v1:reactivate_gateway(Height, GW0, GWAddr, Ledger)
283+
LastActivity = blockchain_ledger_gateway_v2:last_poc_challenge(GW0),
284+
case blockchain_utils:is_gw_active(CurHeight, LastActivity, Ledger) of
285+
false -> blockchain_ledger_v1:reactivate_gateway(Height, GW0, GWAddr, Ledger);
286+
true -> ok
287+
end
279288
end
280289
end, GWAddrs).
281290

src/transactions/v1/blockchain_txn_vars_v1.erl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,8 +1132,6 @@ validate_var(?poc_targeting_version, Value) ->
11321132
_ ->
11331133
throw({error, {invalid_poc_targeting_version, Value}})
11341134
end;
1135-
validate_var(?poc_hardcode_poc_selection_count, Value) ->
1136-
validate_int(Value, "poc_hardcode_poc_selection_count", 1, 500, false);
11371135

11381136
validate_var(?poc_hexing_type, Value) ->
11391137
case Value of

0 commit comments

Comments
 (0)