Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: endpoint to get the current block number #365

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/model/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
SPDX-License-Identifier: Apache-2.0
"""
from .index import (
E2EEResponse
E2EEResponse,
BlockNumberResponse
)
from .account import (
AccountCreateKeyRequest,
Expand Down
5 changes: 5 additions & 0 deletions app/model/schema/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@
class E2EEResponse(BaseModel):
"""E2EE schema (Response)"""
public_key: Optional[str]


class BlockNumberResponse(BaseModel):
"""Block Number schema (Response)"""
block_number: int
21 changes: 20 additions & 1 deletion app/routers/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@
from app.model.db import Node
from app.utils.e2ee_utils import E2EEUtils
from app.utils.docs_utils import get_routers_responses
from app.model.schema import E2EEResponse
from app.utils.web3_utils import Web3Wrapper
from app.model.schema import (
E2EEResponse,
BlockNumberResponse
)
from app.exceptions import ServiceUnavailableError
from app import log

web3 = Web3Wrapper()

LOG = log.get_logger()

router = APIRouter(tags=["index"])
Expand Down Expand Up @@ -88,3 +94,16 @@ def __check_ethereum(errors: list, db: Session):
msg = "Ethereum node's block synchronization is down"
LOG.error(msg)
errors.append(msg)


# GET: /block_number
@router.get(
"/block_number",
response_model=BlockNumberResponse,
responses=get_routers_responses(ServiceUnavailableError)
)
def get_block_number():
"""Get Block Number in current"""
block_number = web3.eth.block_number

return {"block_number": block_number}
67 changes: 67 additions & 0 deletions tests/test_app_routers_block_number_GET.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
Copyright BOOSTRY Co., Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache-2.0
"""
from datetime import datetime

from unittest import mock
from unittest.mock import MagicMock
from app.exceptions import ServiceUnavailableError

from app.model.db import Node


class TestAppRoutersBlockNumberGET:
# target API endpoint
apiurl = "/block_number"

###########################################################################
# Normal Case
###########################################################################

# <Normal_1>
@mock.patch("web3.eth.Eth.block_number", 100)
def test_normal_1(self, client, db):
# request target api
resp = client.get(self.apiurl)

# assertion
assert resp.status_code == 200
assert resp.json() == {
"block_number": 100
}

###########################################################################
# Error Case
###########################################################################

# <Error_1>
# Unable to connect ibet
@mock.patch("web3.eth.BaseEth.get_block_number", MagicMock(side_effect=ServiceUnavailableError("")))
def test_error_1(self, client, db):
# request target api
resp = client.get(self.apiurl)

# assertion
assert resp.status_code == 503
assert resp.json() == {
"meta": {
"code": 1,
"title": "ServiceUnavailableError"
},
"detail": ""
}