Skip to content

Commit

Permalink
fix: incorporate proto changes #NEBD-105 (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
pritamghanghas authored Apr 15, 2023
1 parent ccfb6c3 commit cc6e3a9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 72 deletions.
55 changes: 8 additions & 47 deletions hm_pyhelper/gateway_grpc/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import base58
import grpc
import subprocess
import json
from typing import Union

from hm_pyhelper.protos import blockchain_txn_add_gateway_v1_pb2, \
local_pb2_grpc, local_pb2, region_pb2, gateway_staking_mode_pb2
Expand Down Expand Up @@ -52,6 +49,9 @@ def __enter__(self):
def __exit__(self, _, _2, _3):
self._channel.close()

def api_stub(self):
return self.stub

def get_region_enum(self) -> int:
'''
Returns the current configured region of the gateway.
Expand All @@ -68,18 +68,6 @@ def get_region(self) -> str:
region_id = self.get_region_enum()
return region_pb2.region.Name(region_id)

def sign(self, data: bytes) -> bytes:
'''
Sign a message with the gateway private key
'''
return self.stub.sign(local_pb2.sign_req(data=data)).signature

def ecdh(self, address: bytes) -> bytes:
'''
Return shared secret using ECDH
'''
return self.stub.ecdh(local_pb2.ecdh_req(address=address)).secret

def get_pubkey(self) -> str:
'''
Returns decoded public key of the gateway
Expand All @@ -99,30 +87,13 @@ def get_summary(self) -> dict:
'''
return {
'region': self.get_region(),
'key': self.get_pubkey(),
'gateway_version': self.get_gateway_version(),
'key': self.get_pubkey()
}

def get_gateway_version(self) -> Union[str, None]:
'''
Returns the current version of the gateway package installed
'''
# NOTE:: there is a command line argument to helium-gateway
# but it is not exposed in the rpc, falling back to dpkg
try:
output = subprocess.check_output(['dpkg', '-s', 'helium_gateway'])
for line in output.decode().splitlines():
if line.strip().startswith('Version'):
# dpkg has version without v but github tags begin with v
return "v" + line.split(':')[1].strip()
return None
except subprocess.CalledProcessError:
return None

def create_add_gateway_txn(self, owner_address: str, payer_address: str,
staking_mode: gateway_staking_mode_pb2.gateway_staking_mode
= gateway_staking_mode_pb2.gateway_staking_mode.light,
gateway_address: str = "") -> dict:
= gateway_staking_mode_pb2.gateway_staking_mode.light
) -> bytes:
"""
Invokes the txn_add_gateway RPC endpoint on the gateway and returns
the same payload that the smartphone app traditionally expects.
Expand All @@ -136,23 +107,13 @@ def create_add_gateway_txn(self, owner_address: str, payer_address: str,
- staking_mode: The staking mode of the gateway.
ref:
https://github.com/helium/proto/blob/master/src/service/local.proto#L38
- gateway_address: The address of the miner itself. This is
an optional parameter because the miner
will always return it in the payload during
transaction generation. If the param is
provided, it will only be used as extra
validation.
"""
# NOTE:: this is unimplemented as of alpha23 release of the gateway
response = self.stub.add_gateway(local_pb2.add_gateway_req(
owner=owner_address.encode('utf-8'),
payer=payer_address.encode('utf-8'),
staking_mode=staking_mode
))
result = json.loads(response.decode())
if result["address"] != gateway_address:
raise MinerMalformedAddGatewayTxn
return result
return response.add_gateway_txn


def get_address_from_add_gateway_txn(add_gateway_txn:
Expand All @@ -163,7 +124,7 @@ def get_address_from_add_gateway_txn(add_gateway_txn:
Deserializes specified field in the blockchain_txn_add_gateway_v1_pb2
protobuf to a base58 Helium address.
Pararms:
Params:
- add_gateway_txn: The blockchain_txn_add_gateway_v1_pb2 to
inspect.
- address_type: 'owner', 'gateway', or 'payer'.
Expand Down
25 changes: 1 addition & 24 deletions hm_pyhelper/tests/test_gateway_grpc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
from unittest.mock import patch
import grpc
from concurrent import futures
from hm_pyhelper.gateway_grpc.client import GatewayClient
Expand All @@ -16,23 +15,9 @@ class TestData:
pubkey_decoded = "14RdqcZC2rbdTBwNaTsj5EVWYaM7BKGJ44ycq6wWJy9Hg7RKCii"
region_enum = 0
region_name = "US915"
dpkg_output = b"""Package: helium_gateway\n
Status: install ok installed\n
Priority: optional\n
Section: utility\n
Installed-Size: 3729\n
Maintainer: Marc Nijdam <[email protected]>\n
Architecture: amd64\n
Version: 1.0.0\n
Depends: curl\n
Conffiles:\n
/etc/helium_gateway/settings.toml 4d6fb434f97a50066b8163a371d5c208\n
Description: Helium Gateway for LoRa packet forwarders\n
The Helium Gateway to attach your LoRa gateway to the Helium Blockchain.\n"""
expected_summary = {
'region': region_name,
'key': pubkey_decoded,
'gateway_version': "v1.0.0"
'key': pubkey_decoded
}


Expand Down Expand Up @@ -82,17 +67,9 @@ def test_get_summary(self):
with GatewayClient(f'localhost:{TestData.server_port}') as client:
# summary when helium_gateway is not installed
test_summary_copy = TestData.expected_summary.copy()
test_summary_copy['gateway_version'] = None
self.assertIn(client.get_summary(),
[TestData.expected_summary, test_summary_copy])

@patch('subprocess.check_output', return_value=TestData.dpkg_output)
def test_get_gateway_version(self, mock_check_output):
mock_check_output.return_value = TestData.dpkg_output
with GatewayClient(f'localhost:{TestData.server_port}') as client:
self.assertIn(client.get_gateway_version(),
[TestData.expected_summary['gateway_version'], None])

def test_connection_failure(self):
with self.assertRaises(grpc.RpcError):
with GatewayClient('localhost:1234') as client:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='hm_pyhelper',
version='0.14.6',
version='0.14.7',
author="Nebra Ltd",
author_email="[email protected]",
description="Helium Python Helper",
Expand Down

0 comments on commit cc6e3a9

Please sign in to comment.