From dab3734eeff8692091edcab5fb8c83b62d5bb7d9 Mon Sep 17 00:00:00 2001 From: Andrew McKenzie Date: Mon, 23 May 2022 14:36:15 +0100 Subject: [PATCH] dont reactivate gw if already active --- src/blockchain_utils.erl | 37 ++++++++++++++++++- src/poc/blockchain_poc_path_v4.erl | 11 +----- src/poc/blockchain_poc_target_v5.erl | 28 +++++--------- src/poc/blockchain_poc_target_v6.erl | 28 +++++--------- .../blockchain_txn_validator_heartbeat_v1.erl | 15 +++++++- 5 files changed, 70 insertions(+), 49 deletions(-) diff --git a/src/blockchain_utils.erl b/src/blockchain_utils.erl index f4a9f88dc4..759f17e636 100644 --- a/src/blockchain_utils.erl +++ b/src/blockchain_utils.erl @@ -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 ]). @@ -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). diff --git a/src/poc/blockchain_poc_path_v4.erl b/src/poc/blockchain_poc_path_v4.erl index b43cd8525c..1f0087a2dd 100644 --- a/src/poc/blockchain_poc_path_v4.erl +++ b/src/poc/blockchain_poc_path_v4.erl @@ -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(). @@ -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). diff --git a/src/poc/blockchain_poc_target_v5.erl b/src/poc/blockchain_poc_target_v5.erl index a15e143353..b1868147da 100644 --- a/src/poc/blockchain_poc_target_v5.erl +++ b/src/poc/blockchain_poc_target_v5.erl @@ -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, @@ -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). diff --git a/src/poc/blockchain_poc_target_v6.erl b/src/poc/blockchain_poc_target_v6.erl index 771cff2b7f..4f70494927 100644 --- a/src/poc/blockchain_poc_target_v6.erl +++ b/src/poc/blockchain_poc_target_v6.erl @@ -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, @@ -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). diff --git a/src/transactions/v1/blockchain_txn_validator_heartbeat_v1.erl b/src/transactions/v1/blockchain_txn_validator_heartbeat_v1.erl index 7174d016d5..99e2ec4662 100644 --- a/src/transactions/v1/blockchain_txn_validator_heartbeat_v1.erl +++ b/src/transactions/v1/blockchain_txn_validator_heartbeat_v1.erl @@ -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).