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

Commit 0e3b006

Browse files
committed
reject poc requests txns if chainvar set
1 parent 5b74177 commit 0e3b006

File tree

3 files changed

+73
-56
lines changed

3 files changed

+73
-56
lines changed

include/blockchain_vars.hrl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@
173173
%% from POC targets :: boolean()
174174
-define(poc_activity_filter_enabled, poc_activity_filter_enabled).
175175

176+
%% If set to true, all incoming poc request txns will be rejected : boolean
177+
-define(poc_reject_requests, poc_reject_requests).
178+
176179
%% Number of blocks to wait before a hotspot can be eligible to participate in a poc
177180
%% challenge. This would avoid new hotspots getting challenged before they sync to an
178181
%% acceptable height.

src/transactions/v1/blockchain_txn_poc_request_v1.erl

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -153,66 +153,76 @@ is_valid(Txn, Chain) ->
153153
PubKey = libp2p_crypto:bin_to_pubkey(Challenger),
154154
BaseTxn = Txn#blockchain_txn_poc_request_v1_pb{signature = <<>>},
155155
EncodedTxn = blockchain_txn_poc_request_v1_pb:encode_msg(BaseTxn),
156-
157-
case blockchain_txn:validate_fields([{{secret_hash, ?MODULE:secret_hash(Txn)}, {binary, 32}},
158-
{{onion_key_hash, ?MODULE:secret_hash(Txn)}, {binary, 32}},
159-
{{block_hash, ?MODULE:secret_hash(Txn)}, {binary, 32}}]) of
160-
ok ->
161-
case libp2p_crypto:verify(EncodedTxn, ChallengerSignature, PubKey) of
162-
false ->
163-
{error, bad_signature};
164-
true ->
165-
StartFind = maybe_start_duration(),
166-
case blockchain_ledger_v1:find_gateway_info(Challenger, Ledger) of
167-
{error, not_found} ->
168-
{error, missing_gateway};
169-
{error, _Reason}=Error ->
170-
Error;
171-
{ok, Info} ->
172-
StartCap = maybe_log_duration(fetch_gw, StartFind),
173-
%% check the gateway mode to determine if its allowed to issue POC requests
174-
Mode = blockchain_ledger_gateway_v2:mode(Info),
175-
case blockchain_ledger_gateway_v2:is_valid_capability(Mode, ?GW_CAPABILITY_POC_CHALLENGER, Ledger) of
176-
false -> {error, {gateway_not_allowed, blockchain_ledger_gateway_v2:mode(Info)}};
177-
true ->
178-
StartRest = maybe_log_duration(check_cap, StartCap),
179-
case blockchain_ledger_gateway_v2:location(Info) of
180-
undefined ->
181-
lager:info("no loc for challenger: ~p ~p", [Challenger, Info]),
182-
{error, no_gateway_location};
183-
_Location ->
184-
{ok, Height} = blockchain_ledger_v1:current_height(Ledger),
185-
LastChallenge = blockchain_ledger_gateway_v2:last_poc_challenge(Info),
186-
PoCInterval = blockchain_utils:challenge_interval(Ledger),
187-
case LastChallenge == undefined orelse LastChallenge =< (Height+1 - PoCInterval) of
188-
false ->
189-
{error, too_many_challenges};
190-
true ->
191-
BlockHash = ?MODULE:block_hash(Txn),
192-
case blockchain:get_block_height(BlockHash, Chain) of
193-
{error, not_found} ->
194-
{error, missing_challenge_block_hash};
195-
{error, _}=Error ->
196-
Error;
197-
{ok, BlockHeight} ->
198-
case (BlockHeight + PoCInterval) > (Height+1) of
199-
false ->
200-
{error, replaying_request};
201-
true ->
202-
Fee = ?MODULE:fee(Txn),
203-
Owner = blockchain_ledger_gateway_v2:owner_address(Info),
204-
R = blockchain_ledger_v1:check_dc_balance(Owner, Fee, Ledger),
205-
maybe_log_duration(rest, StartRest),
206-
R
156+
MaybeRejectPocRequests =
157+
case blockchain:config(?poc_reject_requests, Ledger) of
158+
{ok, V} -> V;
159+
_ -> false
160+
end,
161+
%% doh sorry for adding another case to this crazy nest but these txns are going away....
162+
case MaybeRejectPocRequests of
163+
false ->
164+
case blockchain_txn:validate_fields([{{secret_hash, ?MODULE:secret_hash(Txn)}, {binary, 32}},
165+
{{onion_key_hash, ?MODULE:secret_hash(Txn)}, {binary, 32}},
166+
{{block_hash, ?MODULE:secret_hash(Txn)}, {binary, 32}}]) of
167+
ok ->
168+
case libp2p_crypto:verify(EncodedTxn, ChallengerSignature, PubKey) of
169+
false ->
170+
{error, bad_signature};
171+
true ->
172+
StartFind = maybe_start_duration(),
173+
case blockchain_ledger_v1:find_gateway_info(Challenger, Ledger) of
174+
{error, not_found} ->
175+
{error, missing_gateway};
176+
{error, _Reason}=Error ->
177+
Error;
178+
{ok, Info} ->
179+
StartCap = maybe_log_duration(fetch_gw, StartFind),
180+
%% check the gateway mode to determine if its allowed to issue POC requests
181+
Mode = blockchain_ledger_gateway_v2:mode(Info),
182+
case blockchain_ledger_gateway_v2:is_valid_capability(Mode, ?GW_CAPABILITY_POC_CHALLENGER, Ledger) of
183+
false -> {error, {gateway_not_allowed, blockchain_ledger_gateway_v2:mode(Info)}};
184+
true ->
185+
StartRest = maybe_log_duration(check_cap, StartCap),
186+
case blockchain_ledger_gateway_v2:location(Info) of
187+
undefined ->
188+
lager:info("no loc for challenger: ~p ~p", [Challenger, Info]),
189+
{error, no_gateway_location};
190+
_Location ->
191+
{ok, Height} = blockchain_ledger_v1:current_height(Ledger),
192+
LastChallenge = blockchain_ledger_gateway_v2:last_poc_challenge(Info),
193+
PoCInterval = blockchain_utils:challenge_interval(Ledger),
194+
case LastChallenge == undefined orelse LastChallenge =< (Height+1 - PoCInterval) of
195+
false ->
196+
{error, too_many_challenges};
197+
true ->
198+
BlockHash = ?MODULE:block_hash(Txn),
199+
case blockchain:get_block_height(BlockHash, Chain) of
200+
{error, not_found} ->
201+
{error, missing_challenge_block_hash};
202+
{error, _}=Error ->
203+
Error;
204+
{ok, BlockHeight} ->
205+
case (BlockHeight + PoCInterval) > (Height+1) of
206+
false ->
207+
{error, replaying_request};
208+
true ->
209+
Fee = ?MODULE:fee(Txn),
210+
Owner = blockchain_ledger_gateway_v2:owner_address(Info),
211+
R = blockchain_ledger_v1:check_dc_balance(Owner, Fee, Ledger),
212+
maybe_log_duration(rest, StartRest),
213+
R
214+
end
207215
end
208216
end
209217
end
210218
end
211219
end
212-
end
220+
end;
221+
Error -> Error
213222
end;
214-
Error -> Error
215-
end.
223+
true ->
224+
{error, poc_requests_not_accepted}
225+
end.
216226
%%--------------------------------------------------------------------
217227
%% @doc
218228
%% @end
@@ -274,7 +284,6 @@ maybe_log_duration(Type, Start) ->
274284
_ -> ok
275285
end.
276286

277-
278287
%% ------------------------------------------------------------------
279288
%% EUNIT Tests
280289
%% ------------------------------------------------------------------

src/transactions/v1/blockchain_txn_vars_v1.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,12 @@ validate_var(?poc_proposal_gc_window_check, Value) ->
10361036
false -> ok;
10371037
_ -> throw({error, {poc_proposal_gc_window_check, Value}})
10381038
end;
1039-
1039+
validate_var(?poc_reject_requests, Value) ->
1040+
case Value of
1041+
true -> ok;
1042+
false -> ok;
1043+
_ -> throw({error, {poc_reject_requests, Value}})
1044+
end;
10401045
validate_var(?poc_challenge_sync_interval, Value) ->
10411046
validate_int(Value, "poc_challenge_sync_interval", 10, 1440, false);
10421047
validate_var(?poc_path_limit, undefined) ->

0 commit comments

Comments
 (0)