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

Commit

Permalink
Merge pull request #677 from helium/rg/hip0017
Browse files Browse the repository at this point in the history
Add support for hip17
  • Loading branch information
evanmcc authored Dec 10, 2020
2 parents e434162 + 9032342 commit 7258701
Show file tree
Hide file tree
Showing 18 changed files with 3,070 additions and 175 deletions.
54 changes: 54 additions & 0 deletions eqc/h3dex_eqc.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
-module(h3dex_eqc).

-include_lib("eqc/include/eqc.hrl").
-include_lib("eunit/include/eunit.hrl").

-define(INDICES, lists:flatten([ h3:children(I, 5) || I <- h3:get_res0_indexes()])).

-export([prop_h3dex_check/0]).

prop_h3dex_check() ->
?FORALL({Hex1, Hex2, Resolution}, ?SUCHTHAT({X1, X2, _}, {gen_h3(), gen_h3(), choose(6, 6)}, X1 /= 0 andalso X2 /= 0 andalso X1 /= X2),
begin
Children1 = h3:children(Hex1, Resolution) -- [0],
Children2 = h3:children(Hex2, Resolution) -- [0],

End = h3_to_key(Hex1),
Start = find_lower_bound_hex(Hex1),
?WHENFAIL(begin
io:format("Hex 1: ~p (~w -> ~w), Hex 2: ~p (~w -> ~w), Resolution ~p~n", [Hex1, find_lower_bound_hex(Hex1), h3_to_key(Hex1), Hex2, find_lower_bound_hex(Hex2), h3_to_key(Hex2), Resolution])
end,
noshrink(conjunction(
[{all_children_in_range, eqc:equals([], lists:filter(fun(X) -> X < Start orelse X > End end, lists:sort(lists:map(fun h3_to_key/1, Children1)))) },
{all_non_children_out_of_range, eqc:equals([], lists:filter(fun(E) -> X = h3_to_key(E), X > Start andalso X =< End end, Children2)) },
{unparse_works, eqc:equals(Hex1, key_to_h3(h3_to_key(Hex1)))}
]
)
)
)
end).

gen_h3() ->
elements(?INDICES).

h3_to_key(H3) ->
%% both reserved fields must be 0 and Mode must be 1 for this to be a h3 cell
<<0:1/integer-unsigned-big, 1:4/integer-unsigned-big, 0:3/integer-unsigned-big, Resolution:4/integer-unsigned-big, BaseCell:7/integer-unsigned-big, Digits:45/integer-unsigned-big>> = <<H3:64/integer-unsigned-big>>,
%% store the resolution inverted (15 - Resolution) so it sorts later
<<BaseCell:7/integer-unsigned-big, Digits:45/integer-unsigned-big, (15 - Resolution):4/integer-unsigned-big>>.

key_to_h3(Key) ->
<<BaseCell:7/integer-unsigned-big, Digits:45/integer-unsigned-big, InverseResolution:4/integer-unsigned-big>> = Key,
<<H3:64/integer-unsigned-big>> = <<0:1, 1:4/integer-unsigned-big, 0:3, (15 - InverseResolution):4/integer-unsigned-big, BaseCell:7/integer-unsigned-big, Digits:45/integer-unsigned-big>>,
H3.

find_lower_bound_hex(Hex) ->
%% both reserved fields must be 0 and Mode must be 1 for this to be a h3 cell
<<0:1, 1:4/integer-unsigned-big, 0:3, Resolution:4/integer-unsigned-big, BaseCell:7/integer-unsigned-big, Digits/bitstring>> = <<Hex:64/integer-unsigned-big>>,
ActualDigitCount = Resolution * 3,
%% pull out the actual digits used and dump the rest
<<ActualDigits:ActualDigitCount/integer-unsigned-big, _/bitstring>> = Digits,
Padding = 45 - ActualDigitCount,
%% store the resolution inverted (15 - 15) = 0 so it sorts earlier
%% pad the actual digits used with 0s on the end
<<BaseCell:7/integer-unsigned-big, ActualDigits:ActualDigitCount/integer-unsigned-big, 0:Padding, 0:4/integer-unsigned-big>>.
2 changes: 1 addition & 1 deletion include/blockchain.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
% Misc
-define(EVT_MGR, blockchain_event_mgr).

-define(BC_UPGRADE_NAMES, [<<"gateway_v2">>, <<"hex_targets">>, <<"gateway_oui">>]).
-define(BC_UPGRADE_NAMES, [<<"gateway_v2">>, <<"hex_targets">>, <<"gateway_oui">>, <<"h3dex">>]).
29 changes: 29 additions & 0 deletions include/blockchain_vars.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,32 @@
%% POC challenge for X blocks would be considered stale for the purposes
%% of a hotspot transfer. (We do not allow stale hotspots to be transferred.)
-define(transfer_hotspot_stale_poc_blocks, transfer_hotspot_stale_poc_blocks).

%% ------------------------------------------------------------------
%% HIP 17 vars
%%
%% For every possible h3 resolution, we will define:
%% - number of siblings
%% - density_tgt
%% - density_max
%%
%% So hip17_res_0 value could be: 2, 10000, 10000 for example;
%% where num_siblings=2, density_tgt=10000, density_max=10000
%%
%% We'd specify any of the below variables like so: <<"2,10000,10000">>
%% We expect the value of any of these variables to be in format: <<"int,int,int">>
-define(hip17_res_0, hip17_res_0).
-define(hip17_res_1, hip17_res_1).
-define(hip17_res_2, hip17_res_2).
-define(hip17_res_3, hip17_res_3).
-define(hip17_res_4, hip17_res_4).
-define(hip17_res_5, hip17_res_5).
-define(hip17_res_6, hip17_res_6).
-define(hip17_res_7, hip17_res_7).
-define(hip17_res_8, hip17_res_8).
-define(hip17_res_9, hip17_res_9).
-define(hip17_res_10, hip17_res_10).
-define(hip17_res_11, hip17_res_11).
-define(hip17_res_12, hip17_res_12).
-define(density_tgt_res, density_tgt_res).
-define(hip17_interactivity_blocks, hip17_interactivity_blocks).
2 changes: 1 addition & 1 deletion rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
{<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1},
{<<"h3">>,
{git,"https://github.com/helium/erlang-h3.git",
{ref,"a92737698d45c97b7b9b6694513b48c29522a42e"}},
{ref,"8541da45596549e36bdbf82dcb77f19c8608e9d4"}},
0},
{<<"helium_proto">>,
{git,"https://github.com/helium/proto.git",
Expand Down
17 changes: 15 additions & 2 deletions src/blockchain.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
-include("blockchain_vars.hrl").

-ifdef(TEST).
-export([bootstrap_hexes/1, can_add_block/2, get_plausible_blocks/1]).
-export([bootstrap_hexes/1, can_add_block/2, get_plausible_blocks/1, bootstrap_h3dex/1]).
%% export a macro so we can interpose block saving to test failure
-define(save_block(Block, Chain), ?MODULE:save_block(Block, Chain)).
-include_lib("eunit/include/eunit.hrl").
Expand Down Expand Up @@ -94,7 +94,8 @@

-define(BC_UPGRADE_FUNS, [fun upgrade_gateways_v2/1,
fun bootstrap_hexes/1,
fun upgrade_gateways_oui/1]).
fun upgrade_gateways_oui/1,
fun bootstrap_h3dex/1]).

-type blocks() :: #{blockchain_block:hash() => blockchain_block:block()}.
-type blockchain() :: #blockchain{}.
Expand Down Expand Up @@ -262,6 +263,18 @@ upgrade_gateways_oui_(Ledger) ->
end, Gateways),
ok.

-spec bootstrap_h3dex(blockchain_ledger_v1:ledger()) -> ok.
%% @doc Bootstrap the H3Dex for both the active and delayed ledgers
bootstrap_h3dex(Ledger) ->
ok = do_bootstrap_h3dex(Ledger),
Ledger1 = blockchain_ledger_v1:mode(delayed, Ledger),
Ledger2 = blockchain_ledger_v1:new_context(Ledger1),
ok = do_bootstrap_h3dex(Ledger2),
blockchain_ledger_v1:commit_context(Ledger2).

do_bootstrap_h3dex(Ledger) ->
blockchain_ledger_v1:bootstrap_h3dex(Ledger).

%%--------------------------------------------------------------------
%% @doc
%% @end
Expand Down
Loading

0 comments on commit 7258701

Please sign in to comment.