From a31de31341b3138bc2b00cb6af99a70eba18571f Mon Sep 17 00:00:00 2001 From: Andrew McKenzie Date: Mon, 10 Oct 2022 16:48:04 +0100 Subject: [PATCH] add additional params to gateway resp --- rebar.lock | 2 +- src/helium_follower_service.erl | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/rebar.lock b/rebar.lock index 5491bab..f81a694 100644 --- a/rebar.lock +++ b/rebar.lock @@ -69,7 +69,7 @@ {<<"hackney">>,{pkg,<<"hackney">>,<<"1.18.1">>},1}, {<<"helium_proto">>, {git,"https://github.com/helium/proto.git", - {ref,"81fe0a4de38aad4e1a9044623769724419725389"}}, + {ref,"1a1a4ae82a898620d96cb486ed76d3857d3ed8f2"}}, 0}, {<<"hpack">>,{pkg,<<"hpack_erl">>,<<"0.2.3">>},3}, {<<"idna">>,{pkg,<<"idna">>,<<"6.1.1">>},2}, diff --git a/src/helium_follower_service.erl b/src/helium_follower_service.erl index decd4f0..0853563 100644 --- a/src/helium_follower_service.erl +++ b/src/helium_follower_service.erl @@ -131,15 +131,24 @@ find_gateway(Chain, Ctx, Req) -> {ok, Height} = blockchain_ledger_v1:current_height(Ledger), case blockchain_ledger_v1:find_gateway_info(PubKeyBin, Ledger) of {ok, GwInfo} -> - Location = case blockchain_ledger_gateway_v2:location(GwInfo) of - undefined -> <<>>; - H3 -> h3:to_string(H3) - end, - {ok, #follower_gateway_resp_v1_pb{height=Height, address = PubKeyBin, location=Location, - owner = blockchain_ledger_gateway_v2:owner_address(GwInfo)}, Ctx}; - _ -> - {ok, #follower_gateway_resp_v1_pb{height=Height, address = PubKeyBin, location = <<>>, - owner = <<>>}, Ctx} + {Location, Region} = + case blockchain_ledger_gateway_v2:location(GwInfo) of + undefined -> {<<>>, undefined}; + H3 -> + case blockchain_region_v1:h3_to_region(H3, Ledger) of + {ok, R} -> {h3:to_string(H3), normalize_region(R)}; + _ -> {h3:to_string(H3), undefined} + end + end, + {ok, #follower_gateway_resp_v1_pb{ + height=Height, + address = PubKeyBin, + location=Location, + owner = blockchain_ledger_gateway_v2:owner_address(GwInfo), + staking_mode = blockchain_ledger_gateway_v2:mode(GwInfo), + gain = blockchain_ledger_gateway_v2:gain(GwInfo), + region = Region + }, Ctx} end. -spec subnetwork_last_reward_height(blockchain:chain() | undefined, ctx:ctx(), follower_pb:follower_subnetwork_last_reward_height_req_v1_pb()) -> @@ -253,3 +262,9 @@ validate_txn_filters(TxnFilters0) -> is_blockchain_txn(Module) -> ModInfo = Module:module_info(attributes), lists:any(fun({behavior, [blockchain_txn]}) -> true; (_) -> false end, ModInfo). + +%% blockchain_region_v1 returns region as an atom with a 'region_' prefix, ie +%% 'region_us915' etc, we need it without the prefix and capitalised to +%% be compatible with the proto +normalize_region(V) -> + list_to_atom(string:to_upper(string:slice(atom_to_list(V), 7))).