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 Jul 4, 2022
1 parent 198883d commit 5443e79
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 106 deletions.
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, "master"}}},
{branch, "andymck/poc-grpc-v2-report-attestation"}}},
{sibyl, {git, "https://github.com/helium/sibyl.git",
{branch, "master"}}},
{branch, "andymck/poc-grpc-v2-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
8 changes: 6 additions & 2 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{<<"base64url">>,{pkg,<<"base64url">>,<<"1.0.1">>},1},
{<<"blockchain">>,
{git,"https://github.com/helium/blockchain-core.git",
{ref,"2418468a75bbed961ac2ff244694de859cbb0756"}},
{ref,"02558491319997e7e13120de0f5c49a9a3f67597"}},
0},
{<<"certifi">>,{pkg,<<"certifi">>,<<"2.8.0">>},2},
{<<"chatterbox">>,
Expand Down Expand Up @@ -110,7 +110,7 @@
0},
{<<"helium_proto">>,
{git,"https://github.com/helium/proto.git",
{ref,"d1694d5ddb3801f70461921a33407b04d9df66f3"}},
{ref,"068e4fc448ffbec1807936e84467af61d67f5d9c"}},
1},
{<<"hpack">>,{pkg,<<"hpack_erl">>,<<"0.2.3">>},3},
{<<"http2_client">>,
Expand Down Expand Up @@ -147,6 +147,10 @@
{git,"https://github.com/helium/longfi-erlang",
{ref,"08669ccdd7847b584cbcbcb5cf756ed253581a13"}},
0},
{<<"lorawan">>,
{git,"https://github.com/helium/erlang-lorawan.git",
{ref,"49925a083347e91e74daee3516a2105e82ef6c07"}},
1},
{<<"merkerl">>,
{git,"https://github.com/helium/merkerl.git",
{ref,"26ddcaf7f3c2c76eebf6f9258822f923ce69cb75"}},
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,16 +18,23 @@
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("blockchain/include/blockchain_vars.hrl").
-include_lib("blockchain/include/blockchain.hrl").
-include_lib("helium_proto/include/blockchain_poc_core_v1_pb.hrl").

%% 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").

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

%%-----------------------------------------------------------------------------
%% @doc Count the number of occurrences of each element in the list.
%% @end
Expand Down Expand Up @@ -285,3 +292,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: 22 additions & 25 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_p2p/2,
decrypt_radio/7,
retry_decrypt/11,
send_receipt/11,
send_receipt/12,
send_witness/9,
region_params_update/3,
region_params/0
Expand Down Expand Up @@ -73,16 +72,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_p2p(binary(), miner_util:attestation()) -> ok.
decrypt_p2p(Onion, Attestation) ->
gen_server:cast(?MODULE, {decrypt_p2p, 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()], integer()) -> ok.
region_params_update(Region, RegionParams, Gain) ->
gen_server:cast(?MODULE, {region_params_update, Region, RegionParams, Gain}).
Expand All @@ -101,8 +97,9 @@ region_params() ->
Channel :: non_neg_integer(),
DataRate :: list(),
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 @@ -114,7 +111,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 @@ -184,16 +181,16 @@ handle_cast({region_params_update, Region, RegionParams, Gain}, State) ->
region = Region,
region_params = RegionParams,
gain = Gain}};
handle_cast({decrypt_p2p, _Payload}, #state{region_params = undefined} = State) ->
handle_cast({decrypt_p2p, _Payload, _Attestation}, #state{region_params = undefined} = State) ->
lager:warning("dropping p2p challenge packet as no region params data", []),
{noreply, State};
handle_cast({decrypt_p2p, <<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(p2p, 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 @@ -203,13 +200,11 @@ 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),
%% NOTE: attestation is defaulted to undefined for radio
%% attestation is only applicable to receipts and we never
%% get a receipt over the radio
%% it always comes in over p2p ( rather grpc but we are reusing the p2p function here )
NewState = decrypt(radio, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channel, DataRate, undefined, State),
{noreply, NewState};
handle_cast(_Msg, State) ->
{noreply, State}.
Expand All @@ -221,7 +216,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:debug("attempting decrypt of type ~p for onion key hash ~p", [Type, OnionKeyHash]),
Expand Down Expand Up @@ -264,7 +261,7 @@ decrypt(Type, IV, OnionCompactKey, Tag, CipherText, RSSI, SNR, Frequency, Channe
TxPower = tx_power(Region),
erlang:spawn(fun() -> miner_lora_light:send_poc(Packet, immediate, ChannelSelectorFun, Spreading, TxPower) end),
erlang:spawn(fun() -> ?MODULE:send_receipt(Data, OnionCompactKey, Type, os:system_time(nanosecond),
RSSI, SNR, Frequency, Channel, DataRate, TxPower, State) end);
RSSI, SNR, Frequency, Channel, DataRate, TxPower, Attestation, State) end);
_ ->
case blockchain_region_params_v1:get_spreading(RegionParams, erlang:byte_size(Packet)) of
{error, Why} ->
Expand All @@ -286,14 +283,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, DR, EffectiveTxPower, State);
RSSI, SNR, Frequency, Channel, DR, 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, DR, CorrectedPower + AssertedGain, State);
RSSI, SNR, Frequency, Channel, DR, CorrectedPower + AssertedGain, Attestation, State);
{warning, {unknown, Other}} ->
%% This should not happen
lager:warning("What is this? ~p", [Other]),
Expand Down
Loading

0 comments on commit 5443e79

Please sign in to comment.