Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Commit

Permalink
include attestation in witness and receipt reports
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Mar 11, 2022
1 parent 7f6ac16 commit ee841f0
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 84 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ deps:

compile:
REBAR_CONFIG="config/grpc_client_gen_local.config" $(REBAR) grpc gen
REBAR_CONFIG="config/grpc_client_gen.config" $(REBAR) grpc gen
$(MAKE) external_svcs
$(REBAR) compile

Expand Down Expand Up @@ -72,7 +73,7 @@ devrelease:
grpc:
@echo "generating miner grpc services"
REBAR_CONFIG="config/grpc_client_gen_local.config" $(REBAR) grpc gen
REBAR_CONFIG="config/grpc_client_gen.config" $(REBAR) grpc gen
REBAR_CONFIG="config/grpc_client_gen.config" $(REBAR) grpc gen

$(GRPC_SERVICE_DIR):
@echo "miner grpc service directory $(directory) does not exist"
Expand Down
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

{deps, [
{blockchain, {git, "https://github.com/helium/blockchain-core.git",
{branch, "andymck/poc-grpc-v2"}}},
{branch, "andymck/poc-grpc-v2-report-attestation"}}},
{sibyl, {git, "https://github.com/helium/sibyl.git",
{branch, "andymck/poc-grpc"}}},
{branch, "andymck/poc-grpc-report-attestation"}}},
{hbbft, {git, "https://github.com/helium/erlang-hbbft.git",
{branch, "master"}}},
{dkg, {git, "https://github.com/helium/erlang-dkg.git", {branch, "master"}}},
Expand Down
6 changes: 3 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{<<"base64url">>,{pkg,<<"base64url">>,<<"1.0.1">>},1},
{<<"blockchain">>,
{git,"https://github.com/helium/blockchain-core.git",
{ref,"fcc35a3de11b083cbe6a527a4e614d07dc2d2f9d"}},
{ref,"1a07ed7ba57bf48f73405f18f8f2060cb1d2162d"}},
0},
{<<"chatterbox">>,
{git,"https://github.com/andymck/chatterbox",
Expand Down Expand Up @@ -105,7 +105,7 @@
0},
{<<"helium_proto">>,
{git,"https://github.com/helium/proto.git",
{ref,"f743a80e534bdc78805e3c5438cb466bec3c0b6f"}},
{ref,"9643884f68c7553f73a5b2de4b8c9ce10874e100"}},
1},
{<<"hpack">>,{pkg,<<"hpack_erl">>,<<"0.2.3">>},3},
{<<"http2_client">>,
Expand Down Expand Up @@ -170,7 +170,7 @@
3},
{<<"sibyl">>,
{git,"https://github.com/helium/sibyl.git",
{ref,"9c68561f1e9689ce3d0bc02c40f3af548b2a51e4"}},
{ref,"e3a594e7e7054d0ec7c335a11438e1797260b63f"}},
0},
{<<"sidejob">>,{pkg,<<"sidejob">>,<<"2.1.0">>},2},
{<<"small_ints">>,{pkg,<<"small_ints">>,<<"0.1.0">>},4},
Expand Down
25 changes: 24 additions & 1 deletion src/miner_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@
true_predicate/1,
has_valid_local_capability/2,
hbbft_perf/0,
mk_rescue_block/3
mk_rescue_block/3,
get_attestation/1
]).

-include("src/grpc/autogen/client/gateway_miner_client_pb.hrl").
-include_lib("helium_proto/include/blockchain_poc_core_v1_pb.hrl").
-include_lib("blockchain/include/blockchain_vars.hrl").
-include_lib("blockchain/include/blockchain.hrl").


-type attestation() :: #attestation_pb{}.
-export_type([attestation/0]).

%% get the firmware release data from a hotspot
-define(LSB_FILE, "/etc/lsb_release").
-define(RELEASE_CMD, "cat " ++ ?LSB_FILE ++ " | grep RELEASE | cut -d'=' -f2").
Expand Down Expand Up @@ -270,3 +277,19 @@ mk_rescue_block(Vars, Addrs, KeyStr) ->
RescueSig = RescueSigFun(EncodedBlock),

blockchain_block_v1:set_signatures(RescueBlock, [], RescueSig).

-spec get_attestation(#gateway_resp_v1_pb{}) -> attestation().
get_attestation(Msg) ->
#gateway_resp_v1_pb{
address = AttAddress,
height = AttHeight,
block_time = AttBlockTime,
block_age = AttBlockAge,
signature = AttSig} = Msg,
#attestation_pb{
height = AttHeight,
block_time = AttBlockTime,
block_age = AttBlockAge,
address = AttAddress,
signature = AttSig
}.
47 changes: 20 additions & 27 deletions src/poc/miner_onion_server_light.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
%% ------------------------------------------------------------------
-export([
start_link/1,
decrypt_p2p/1,
decrypt_grpc/2,
decrypt_radio/7,
retry_decrypt/11,
send_receipt/11,
send_receipt/12,
send_witness/9,
region_params_update/2,
region_params/0
Expand Down Expand Up @@ -72,16 +71,13 @@
start_link(Args) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, Args, []).

-spec decrypt_p2p(binary()) -> ok.
decrypt_p2p(Onion) ->
gen_server:cast(?MODULE, {decrypt_p2p, Onion}).
-spec decrypt_grpc(binary(), miner_util:attestation()) -> ok.
decrypt_grpc(Onion, Attestation) ->
gen_server:cast(?MODULE, {decrypt_grpc, Onion, Attestation}).

decrypt_radio(Packet, RSSI, SNR, Timestamp, Freq, Channel, Spreading) ->
gen_server:cast(?MODULE, {decrypt_radio, Packet, RSSI, SNR, Timestamp, Freq, Channel, Spreading}).

retry_decrypt(Type, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channel, DataRate, Stream) ->
gen_server:cast(?MODULE, {retry_decrypt, Type, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channel, DataRate, Stream}).

-spec region_params_update(atom(), [blockchain_region_param_v1:region_param_v1()]) -> ok.
region_params_update(Region, RegionParams) ->
gen_server:cast(?MODULE, {region_params_update, Region, RegionParams}).
Expand All @@ -92,16 +88,17 @@ region_params() ->

-spec send_receipt(Data :: binary(),
OnionCompactKey :: libp2p_crypto:pubkey_bin(),
Type :: radio | p2p,
Type :: radio | p2p | grpc,
Time :: pos_integer(),
RSSI :: integer(),
SNR :: float(),
Frequency :: float(),
Channel :: non_neg_integer(),
DataRate :: binary(),
Power :: non_neg_integer(),
Attestation :: miner_util:attestation(),
State :: state()) -> ok | {error, any()}.
send_receipt(Data, OnionCompactKey, Type, Time, RSSI, SNR, Frequency, Channel, DataRate, Power, _State) ->
send_receipt(Data, OnionCompactKey, Type, Time, RSSI, SNR, Frequency, Channel, DataRate, Power, Attestation, _State) ->
case miner_lora_light:location_ok() of
true ->
lager:md([{poc_id, blockchain_utils:poc_id(OnionCompactKey)}]),
Expand All @@ -113,7 +110,7 @@ send_receipt(Data, OnionCompactKey, Type, Time, RSSI, SNR, Frequency, Channel, D
2 ->
blockchain_poc_receipt_v1:new(Address, Time, RSSI, Data, Type, SNR, Frequency, Channel, DataRate);
V when V >= 3 ->
R0 = blockchain_poc_receipt_v1:new(Address, Time, RSSI, Data, Type, SNR, Frequency, Channel, DataRate),
R0 = blockchain_poc_receipt_v1:new(Address, Time, RSSI, Data, Type, SNR, Frequency, Channel, DataRate, Attestation),
blockchain_poc_receipt_v1:tx_power(R0, Power);
_ ->
blockchain_poc_receipt_v1:new(Address, Time, RSSI, Data, Type)
Expand Down Expand Up @@ -181,16 +178,16 @@ handle_call(_Msg, _From, State) ->
handle_cast({region_params_update, Region, RegionParams}, State) ->
lager:info("updating region params. Region: ~p, Params: ~p", [Region, RegionParams]),
{noreply, State#state{region = Region, region_params = RegionParams}};
handle_cast({decrypt_p2p, _Payload}, #state{region_params = undefined} = State) ->
lager:warning("dropping p2p challenge packet as no region params data", []),
handle_cast({decrypt_grpc, _Payload, _Attestation}, #state{region_params = undefined} = State) ->
lager:warning("dropping grpc challenge packet as no region params data", []),
{noreply, State};
handle_cast({decrypt_p2p, <<IV:2/binary,
handle_cast({decrypt_grpc, <<IV:2/binary,
OnionCompactKey:33/binary,
Tag:4/binary,
CipherText/binary>>}, State) ->
CipherText/binary>>, Attestation}, State) ->
%%TODO - rssi, freq, snr, channel and datarate were originally undefined
%% but this breaks the in use PB encoder, so defaulted to values below
NewState = decrypt(p2p, IV, OnionCompactKey, Tag, CipherText, 0, 0.0, 0.0, 0, [12], State),
NewState = decrypt(grpc, IV, OnionCompactKey, Tag, CipherText, 0, 0.0, 0.0, 0, [12], Attestation, State),
{noreply, NewState};
handle_cast({decrypt_radio, _Payload}, #state{region_params = undefined} = State) ->
lager:warning("dropping radio challenge packet as no region params data", []),
Expand All @@ -200,13 +197,7 @@ handle_cast({decrypt_radio, <<IV:2/binary,
Tag:4/binary,
CipherText/binary>>,
RSSI, SNR, _Timestamp, Frequency, Channel, DataRate}, State) ->
NewState = decrypt(radio, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channel, DataRate, State),
{noreply, NewState};
handle_cast({retry_decrypt, Type, _IV, _OnionCompactKey, _Tag, _CipherText, _RSSI, _SNR, _Frequency, _Channel, _DataRate}, #state{region_params = undefined} = State) ->
lager:warning("dropping retry ~p challenge packet as no region params data", [Type]),
{noreply, State};
handle_cast({retry_decrypt, Type, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channel, DataRate}, State) ->
NewState = decrypt(Type, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channel, DataRate, State),
NewState = decrypt(radio, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channel, DataRate, undefined, State),
{noreply, NewState};
handle_cast(_Msg, State) ->
{noreply, State}.
Expand All @@ -218,7 +209,9 @@ handle_info(_Msg, State) ->
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
decrypt(Type, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channel, DataRate, #state{ecdh_fun=ECDHFun, region_params = RegionParams, region = Region}=State) ->
decrypt(Type, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channel,
DataRate, Attestation,
#state{ecdh_fun=ECDHFun, region_params = RegionParams, region = Region} = State) ->
POCID = blockchain_utils:poc_id(OnionCompactKey),
OnionKeyHash = crypto:hash(sha256, OnionCompactKey),
lager:info("attempting decrypt of type ~p for onion key hash ~p", [Type, OnionKeyHash]),
Expand Down Expand Up @@ -283,14 +276,14 @@ decrypt(Type, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channe
ok ->
lager:info("sending receipt with observed power: ~p with radio power ~p", [EffectiveTxPower, TxPower]),
?MODULE:send_receipt(Data, OnionCompactKey, Type, os:system_time(nanosecond),
RSSI, SNR, Frequency, Channel, DataRate, EffectiveTxPower, State);
RSSI, SNR, Frequency, Channel, DataRate, EffectiveTxPower, Attestation, State);
{warning, {tx_power_corrected, CorrectedPower}} ->
%% Corrected power never takes into account antenna gain config in pkt forwarder so we
%% always add it back here
lager:warning("tx_power_corrected! original_power: ~p, corrected_power: ~p, with gain ~p; sending receipt with power ~p",
[TxPower, CorrectedPower, AssertedGain, CorrectedPower + AssertedGain]),
?MODULE:send_receipt(Data, OnionCompactKey, Type, os:system_time(nanosecond),
RSSI, SNR, Frequency, Channel, DataRate, CorrectedPower + AssertedGain, State);
RSSI, SNR, Frequency, Channel, DataRate, CorrectedPower + AssertedGain, Attestation, State);
{warning, {unknown, Other}} ->
%% This should not happen
lager:warning("What is this? ~p", [Other]),
Expand Down
17 changes: 9 additions & 8 deletions src/poc/miner_poc_grpc_client_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
-module(miner_poc_grpc_client_handler).

-include("src/grpc/autogen/client/gateway_miner_client_pb.hrl").
-include_lib("helium_proto/include/blockchain_poc_core_v1_pb.hrl").

%% ------------------------------------------------------------------
%% Stream Exports
Expand Down Expand Up @@ -128,8 +129,8 @@ handle_msg({data, #gateway_resp_v1_pb{msg = {poc_challenge_resp, ChallengeNotifi
TargetRes = miner_poc_grpc_client_statem:check_target(binary_to_list(URI), PubKeyBin, OnionKeyHash, BlockHash, NotificationHeight, ChallengerSig),
lager:info("check target result for key ~p: ~p",[OnionKeyHash, TargetRes]),
case TargetRes of
{ok, Result, _Details} ->
handle_check_target_resp(Result);
{ok, Result, Attestation} ->
handle_check_target_resp(Result, Attestation);
{error, <<"queued_poc">>} ->
erlang:send_after(5000, Self, {retry_check_target, 1, Msg});
{error, _Reason, _Details} ->
Expand Down Expand Up @@ -165,8 +166,8 @@ handle_info({retry_check_target, Attempt, Msg}, StreamState) when Attempt =< 3
TargetRes = miner_poc_grpc_client_statem:check_target(binary_to_list(URI), PubKeyBin, OnionKeyHash, BlockHash, NotificationHeight, ChallengerSig),
lager:info("check target result retry ~p for key ~p: ~p",[Attempt, OnionKeyHash, TargetRes]),
case TargetRes of
{ok, Result, _Details} ->
handle_check_target_resp(Result);
{ok, Result, Attestation} ->
handle_check_target_resp(Result, Attestation);
{error, <<"queued_poc">>} ->
erlang:send_after(5000, Self, {retry_check_target, Attempt +1, Msg});
{error, _Reason, _Details} ->
Expand All @@ -184,10 +185,10 @@ handle_info(_Msg, StreamState) ->
%% ------------------------------------------------------------------
%% Internal functions
%% ------------------------------------------------------------------
-spec handle_check_target_resp(#gateway_poc_check_challenge_target_resp_v1_pb{})-> ok.
handle_check_target_resp(#gateway_poc_check_challenge_target_resp_v1_pb{target = true, onion = Onion} = _ChallengeResp) ->
ok = miner_onion_server_light:decrypt_p2p(Onion);
handle_check_target_resp(#gateway_poc_check_challenge_target_resp_v1_pb{target = false} = _ChallengeResp) ->
-spec handle_check_target_resp(#gateway_poc_check_challenge_target_resp_v1_pb{}, miner_util:attestation())-> ok.
handle_check_target_resp(#gateway_poc_check_challenge_target_resp_v1_pb{target = true, onion = Onion} = _ChallengeResp, Attestation) ->
ok = miner_onion_server_light:decrypt_grpc(Onion, Attestation);
handle_check_target_resp(#gateway_poc_check_challenge_target_resp_v1_pb{target = false} = _ChallengeResp, _Attestation) ->
ok.

-ifdef(TEST).
Expand Down
Loading

0 comments on commit ee841f0

Please sign in to comment.