Skip to content

Commit

Permalink
Merge pull request #126 from HathorNetwork/dev
Browse files Browse the repository at this point in the history
Merge v0.1.18
  • Loading branch information
luislhl authored Feb 10, 2022
2 parents 81236de + dee02f7 commit 869eba6
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 108 deletions.
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ AWS_SUBNET_ID_3=
API_PORT=3001
LAMBDA_INVOKE_URL=http://localhost:3002/
DATA_AGGREGATOR_LAMBDA_NAME=hathor-explorer-service-dev-node_data_aggregator_handler
HATHOR_CORE_DOMAIN=node1.foxtrot.testnet.hathor.network
HATHOR_NODES=node1.foxtrot.testnet.hathor.network
HATHOR_CORE_DOMAIN=node1.golf.testnet.hathor.network
HATHOR_NODES=node1.golf.testnet.hathor.network
REDIS_KEY_PREFIX=hathor-explorer-service-dev
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0
S3_ENDPOINT=http://localhost:4569/
METADATA_BUCKET=metadata-testnet
CORS_ALLOWED_ORIGIN=""
CORS_ALLOWED_REGEX=https?
NODE_CACHE_TTL=30
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
REDIS_PORT: 6379
REDIS_DB: 0
METADATA_BUCKET: hathor-explorer-metadata-testnet
CORS_ALLOWED_ORIGIN: https://explorer.testnet.hathor.network
CORS_ALLOWED_REGEX: https?://([a-z0-9]*\.){0,5}hathor\.network
NODE_CACHE_TTL: 30
- name: Deploy Lambdas Mainnet
if: ${{ needs.init.outputs.environment == 'mainnet' }}
Expand All @@ -96,7 +96,7 @@ jobs:
REDIS_PORT: 6379
REDIS_DB: 0
METADATA_BUCKET: hathor-explorer-metadata-mainnet
CORS_ALLOWED_ORIGIN: https://explorer.hathor.network
CORS_ALLOWED_REGEX: https?://([a-z0-9]*\.){0,5}hathor\.network
NODE_CACHE_TTL: 30
- name: Deploy Daemons Testnet
if: ${{ needs.init.outputs.environment == 'testnet' }}
Expand Down
2 changes: 1 addition & 1 deletion common/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ def default(cls) -> 'LogRenderer':

METADATA_BUCKET = config('METADATA_BUCKET', default=None)

CORS_ALLOWED_ORIGIN = config('CORS_ALLOWED_ORIGIN', default=None)
CORS_ALLOWED_REGEX = config('CORS_ALLOWED_REGEX', default=r'https?://([a-z0-9]*\.){0,5}hathor\.network')

LOG_RENDERER = LogRenderer(config('LOG_RENDERER', default=LogRenderer.default().value))
16 changes: 6 additions & 10 deletions gateways/clients/hathor_core_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ async def get(self, path: str, callback: Callable[[dict], None], params: Optiona
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as response:
self.log.info(
"hathor_core_response",
path=path,
status=response.status,
body=await response.text())
if response.status > 299:
self.log.warning(
"hathor_core_error",
path=path,
status=response.status,
body=await response.text())
callback(await response.json())
except Exception as e:
self.log.error("hathor_core_error", path=path, error=repr(e))
Expand Down Expand Up @@ -95,11 +96,6 @@ def get_text(self, path: str, params: Optional[dict] = None, **kwargs: Any) -> O
status=response.status_code,
body=response.text)
return None
self.log.info(
"hathor_core_response",
path=path,
status=response.status_code,
body=response.text)

return response.text
except requests.ReadTimeout:
Expand Down
5 changes: 0 additions & 5 deletions gateways/node_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,3 @@ def get_token(self, id: str) -> Optional[dict]:
"""Retrieve token by id
"""
return self.hathor_core_client.get(TOKEN_ENDPOINT, params={'id': id})

def list_tokens(self) -> Optional[dict]:
"""Retrieve list of tokens
"""
return self.hathor_core_client.get(TOKEN_ENDPOINT)
21 changes: 0 additions & 21 deletions handlers/node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,27 +274,6 @@ def get_token(
}


@ApiGateway()
def list_tokens(
event: ApiGatewayEvent,
_context: LambdaContext,
node_api: Optional[NodeApi] = None
) -> dict:
"""Get a list of tokens with details.
*IMPORTANT: Any changes on the parameters should be reflected on the `cacheKeyParameters` for this method.
"""
node_api = node_api or NodeApi()
response = node_api.list_tokens()
return {
"statusCode": 200,
"body": json.dumps(response or {}),
"headers": {
"Content-Type": "application/json"
}
}


@ApiGateway()
def decode_tx(
event: ApiGatewayEvent,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hathor-explorer-service",
"version": "0.1.17",
"version": "0.1.18",
"description": "Hathor Explorer Service Serverless deps",
"dependencies": {
"serverless": "^2.44.0",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hathor-explorer-service"
version = "0.1.17"
version = "0.1.18"
description = ""
authors = ["Hathor Labs <[email protected]>"]
license = "MIT"
Expand Down
19 changes: 1 addition & 18 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ provider:
REDIS_PORT: ${env:REDIS_PORT}
REDIS_DB: ${env:REDIS_DB}
METADATA_BUCKET: ${env:METADATA_BUCKET}
CORS_ALLOWED_ORIGIN: ${env:CORS_ALLOWED_ORIGIN}
CORS_ALLOWED_REGEX: ${env:CORS_ALLOWED_REGEX}
NODE_CACHE_TTL: ${env:NODE_CACHE_TTL}

plugins:
Expand Down Expand Up @@ -337,23 +337,6 @@ functions:
cacheKeyParameters:
- name: request.querystring.id

node_api_list_tokens:
handler: handlers/node_api.list_tokens
maximumRetryAttempts: 0
package:
patterns:
- 'handlers/node_api.py'
layers:
- { Ref: PythonRequirementsLambdaLayer }
events:
- http:
path: node_api/tokens
method: get
cors: true
caching:
enabled: true
ttlInSeconds: 3600

node_api_decode_tx:
handler: handlers/node_api.decode_tx
maximumRetryAttempts: 0
Expand Down
10 changes: 0 additions & 10 deletions tests/unit/gateways/test_node_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,6 @@ def test_get_token(self, hathor_client):
assert result
assert sorted(result) == sorted(obj)

@patch('gateways.node_api_gateway.TOKEN_ENDPOINT', 'mock-endpoint')
def test_list_tokens(self, hathor_client):
obj = {'foo': 'bar'}
hathor_client.get = MagicMock(return_value=obj)
gateway = NodeApiGateway(hathor_core_client=hathor_client)
result = gateway.list_tokens()
hathor_client.get.assert_called_once_with('mock-endpoint')
assert result
assert sorted(result) == sorted(obj)

@patch('gateways.node_api_gateway.DECODE_TX_ENDPOINT', 'mock-endpoint')
def test_decode_tx(self, hathor_client):
obj = {'foo': 'bar'}
Expand Down
9 changes: 0 additions & 9 deletions tests/unit/usecases/test_node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,6 @@ def test_get_token(self, node_api_gateway):
assert result
assert sorted(result) == sorted(obj)

def test_list_tokens(self, node_api_gateway):
obj = {"foo": "bar"}
node_api_gateway.list_tokens = MagicMock(return_value=obj)
node_api = NodeApi(node_api_gateway)
result = node_api.list_tokens()
node_api_gateway.list_tokens.assert_called_once()
assert result
assert sorted(result) == sorted(obj)

def test_decode_tx(self, node_api_gateway):
obj = {"foo": "bar"}
node_api_gateway.decode_tx = MagicMock(return_value=obj)
Expand Down
99 changes: 84 additions & 15 deletions tests/unit/utils/wrappers/aws/test_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,68 +88,135 @@ def test_parse_json_fail_proof(self):
function.assert_called()
assert result['statusCode'] == 200

@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_ORIGIN', 'explorer.hathor.network')
@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_REGEX', r'https?://([a-z0-9]*\.){0,5}hathor\.network')
def test_returning_cors_headers(self):
function = MagicMock(return_value={'statusCode': 200, 'headers': {}})
api_gateway = ApiGateway()

event = {
'body': '{}',
'headers': {}
'headers': {
'origin': 'https://explorer.hathor.network'
}
}

context = LambdaContext()

result = api_gateway.__call__(function)(event, context)

function.assert_called()
assert result['headers']['Access-Control-Allow-Origin'] == 'https://explorer.hathor.network'
assert result['headers']['Access-Control-Allow-Credentials'] is True

@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_REGEX', r'https?://([a-z0-9]*\.){0,5}hathor\.network')
def test_returning_cors_headers_testnet(self):
function = MagicMock(return_value={'statusCode': 200, 'headers': {}})
api_gateway = ApiGateway()

event = {
'body': '{}',
'headers': {
'origin': 'https://explorer.testnet.hathor.network'
}
}

context = LambdaContext()

result = api_gateway.__call__(function)(event, context)

function.assert_called()
assert result['headers']['Access-Control-Allow-Origin'] == r'https://explorer.testnet.hathor.network'
assert result['headers']['Access-Control-Allow-Credentials'] is True

@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_REGEX', r'https?://([a-z0-9]*\.){0,5}hathor\.network')
def test_returning_cors_headers_golf(self):
function = MagicMock(return_value={'statusCode': 200, 'headers': {}})
api_gateway = ApiGateway()

event = {
'body': '{}',
'headers': {
'origin': 'https://explorer.golf.hathor.network'
}
}

context = LambdaContext()

result = api_gateway.__call__(function)(event, context)

function.assert_called()
assert result['headers']['Access-Control-Allow-Origin'] == 'https://explorer.golf.hathor.network'
assert result['headers']['Access-Control-Allow-Credentials'] is True

@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_REGEX', r'https?://([a-z0-9]*\.){0,5}hathor\.network')
def test_cors_fail_regex_not_matching(self):
function = MagicMock(return_value={'statusCode': 200, 'headers': {}})
api_gateway = ApiGateway()

event = {
'body': '{}',
'headers': {
'origin': 'https://explorer.anydomain.network'
}
}

context = LambdaContext()

result = api_gateway.__call__(function)(event, context)

function.assert_called()
assert result['headers']['Access-Control-Allow-Origin']
assert result['headers']['Access-Control-Allow-Credentials']
assert result['headers'] == {}

@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_ORIGIN', 'explorer.hathor.network')
@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_REGEX', r'https?://([a-z0-9]*\.){0,5}hathor\.network')
def test_returning_cors_on_apierror(self):
function = MagicMock(side_effect=ApiError('Boom!'))
api_gateway = ApiGateway()

event = {
'body': '{}',
'headers': {}
'headers': {
'origin': 'https://explorer.hathor.network'
}
}

context = LambdaContext()

result = api_gateway.__call__(function)(event, context)

function.assert_called()
assert result['headers']['Access-Control-Allow-Origin']
assert result['headers']['Access-Control-Allow-Credentials']
assert result['headers']['Access-Control-Allow-Origin'] == 'https://explorer.hathor.network'
assert result['headers']['Access-Control-Allow-Credentials'] is True

@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_ORIGIN', 'explorer.hathor.network')
@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_REGEX', r'https?://([a-z0-9]*\.){0,5}hathor\.network')
def test_returning_cors_on_error(self):
function = MagicMock(side_effect=Exception('Boom!'))
api_gateway = ApiGateway()

event = {
'body': '{}',
'headers': {}
'headers': {
'origin': 'https://explorer.hathor.network'
}
}

context = LambdaContext()

result = api_gateway.__call__(function)(event, context)

function.assert_called()
assert result['headers']['Access-Control-Allow-Origin']
assert result['headers']['Access-Control-Allow-Credentials']
assert result['headers']['Access-Control-Allow-Origin'] == 'https://explorer.hathor.network'
assert result['headers']['Access-Control-Allow-Credentials'] is True

@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_ORIGIN', 'explorer.hathor.network')
@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_REGEX', r'https?://([a-z0-9]*\.){0,5}hathor\.network')
def test_works_with_no_headers_from_function(self):
function = MagicMock(return_value={'statusCode': 200})
api_gateway = ApiGateway()

event = {
'body': '{}',
'headers': {}
'headers': {
'origin': 'https://explorer.hathor.network'
}
}

context = LambdaContext()
Expand All @@ -158,8 +225,10 @@ def test_works_with_no_headers_from_function(self):

function.assert_called()
assert result['headers']
assert result['headers']['Access-Control-Allow-Origin'] == 'https://explorer.hathor.network'
assert result['headers']['Access-Control-Allow-Credentials'] is True

@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_ORIGIN', None)
@patch('utils.wrappers.aws.api_gateway.CORS_ALLOWED_REGEX', None)
def test_no_headers_with_no_config(self):
function = MagicMock(return_value={'statusCode': 200})
api_gateway = ApiGateway()
Expand Down
3 changes: 0 additions & 3 deletions usecases/node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,3 @@ def list_transactions(

def get_token(self, id: str) -> Optional[dict]:
return self.node_api_gateway.get_token(id)

def list_tokens(self) -> Optional[dict]:
return self.node_api_gateway.list_tokens()
Loading

0 comments on commit 869eba6

Please sign in to comment.