Commit 513baca8 authored by 陈涛's avatar 陈涛

判断节点用正则

parent f8f73509
import re
from typing import List from typing import List
from motor.core import AgnosticCollection from motor.core import AgnosticCollection
import dependencies import dependencies
from exception.api import APIError from exception.api import APIError
from exception.db import ExistDataError, NotFundError from exception.db import ExistDataError, NotFundError
...@@ -19,6 +18,14 @@ from tools.jwt_tools import User ...@@ -19,6 +18,14 @@ from tools.jwt_tools import User
router = APIRouter() router = APIRouter()
def str_not_numbers(string_with_numbers: str) -> bool:
flag = True
regex_pattern = r"^\d+$"
if re.match(regex_pattern, string_with_numbers):
flag = False
return flag
@router.post('/', @router.post('/',
response_model=BaseResponse, response_model=BaseResponse,
summary='绑定节点', summary='绑定节点',
...@@ -29,25 +36,22 @@ async def subscribe( ...@@ -29,25 +36,22 @@ async def subscribe(
fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect), fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect),
beacon_service: BeaconChaService = Depends(BeaconChaService), beacon_service: BeaconChaService = Depends(BeaconChaService),
): ):
try: assert str_not_numbers(bind_node.pub_key), RequestInvalidParamsError()
int(bind_node.pub_key) node_detail = await beacon_service.get_validator(index_or_pubkey=bind_node.pub_key)
raise RequestInvalidParamsError() db_data = BaseNode(**bind_node.dict(), index=node_detail.validator_index)
except Exception as e: # 限制staking基金才可绑定节点
node_detail = await beacon_service.get_validator(index_or_pubkey=bind_node.pub_key)
db_data = BaseNode(**bind_node.dict(), index=node_detail.validator_index) query = {'id': bind_node.fund_id, 'user_id': user.id, 'fund_type': FundType.staking,
# 限制staking基金才可绑定节点 f"nodes.{bind_node.pub_key}": {'$exists': False}}
query = {'id': bind_node.fund_id, 'user_id': user.id, 'fund_type': FundType.staking, update = {"$set": {f"nodes.{bind_node.pub_key}": db_data.dict()}}
f"nodes.{bind_node.pub_key}": {'$exists': False}} res = await fund_collect.update_one(
query,
update = {"$set": {f"nodes.{bind_node.pub_key}": db_data.dict()}} update
res = await fund_collect.update_one( )
query, if res.raw_result['nModified'] == 0:
update raise ExistDataError(message='节点已存在')
) return BaseResponse(data='绑定成功')
if res.raw_result['nModified'] == 0:
raise ExistDataError(message='节点已存在')
return BaseResponse(data='绑定成功')
@router.delete('/', response_model=BaseResponse, summary='解绑节点', description='解绑节点') @router.delete('/', response_model=BaseResponse, summary='解绑节点', description='解绑节点')
...@@ -76,16 +80,13 @@ async def get_node_info( ...@@ -76,16 +80,13 @@ async def get_node_info(
beacon_service: BeaconChaService = Depends(BeaconChaService), beacon_service: BeaconChaService = Depends(BeaconChaService),
fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect) fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect)
): ):
try: assert str_not_numbers(pub_key), RequestInvalidParamsError()
int(pub_key) query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
raise RequestInvalidParamsError() fund = await fund_collect.find_one(query)
except Exception as e: assert fund, NotFundError('未绑定该节点')
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}} validator_detail = await beacon_service.get_validator(pub_key)
fund = await fund_collect.find_one(query) response = Response[Validator](data=validator_detail)
assert fund, NotFundError('未绑定该节点') return response
validator_detail = await beacon_service.get_validator(pub_key)
response = Response[Validator](data=validator_detail)
return response
@router.get( @router.get(
...@@ -99,16 +100,13 @@ async def get_node_deposit( ...@@ -99,16 +100,13 @@ async def get_node_deposit(
beacon_service: BeaconChaService = Depends(BeaconChaService), beacon_service: BeaconChaService = Depends(BeaconChaService),
fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect) fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect)
): ):
try: assert str_not_numbers(pub_key), RequestInvalidParamsError()
int(pub_key) query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
raise RequestInvalidParamsError() fund = await fund_collect.find_one(query)
except Exception as e: assert fund, NotFundError('未绑定该节点')
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}} validator_deposit_list = await beacon_service.get_validator_deposit(pub_key)
fund = await fund_collect.find_one(query) response = Response[List[ValidatorDeposit]](data=validator_deposit_list)
assert fund, NotFundError('未绑定该节点') return response
validator_deposit_list = await beacon_service.get_validator_deposit(pub_key)
response = Response[List[ValidatorDeposit]](data=validator_deposit_list)
return response
@router.get( @router.get(
...@@ -123,20 +121,17 @@ async def get_node_blocks( ...@@ -123,20 +121,17 @@ async def get_node_blocks(
beacon_service: BeaconChaService = Depends(BeaconChaService), beacon_service: BeaconChaService = Depends(BeaconChaService),
fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect) fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect)
): ):
try: assert str_not_numbers(pub_key), RequestInvalidParamsError()
int(pub_key) start = (page.page - 1) * page.page_size
raise RequestInvalidParamsError() query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
except Exception as e: fund = await fund_collect.find_one(query)
start = (page.page - 1) * page.page_size assert fund, NotFundError('未绑定该节点')
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}} validator_blocks = await beacon_service.get_validator_blocks(
fund = await fund_collect.find_one(query) index=fund["nodes"][pub_key]["index"],
assert fund, NotFundError('未绑定该节点') start=start,
validator_blocks = await beacon_service.get_validator_blocks( length=page.page_size
index=fund["nodes"][pub_key]["index"], )
start=start, return PageResponse[ValidatorBlock](data=validator_blocks.data, **page.dict(), total=validator_blocks.total)
length=page.page_size
)
return PageResponse[ValidatorBlock](data=validator_blocks.data, **page.dict(), total=validator_blocks.total)
@router.get( @router.get(
...@@ -150,17 +145,14 @@ async def get_node_income( ...@@ -150,17 +145,14 @@ async def get_node_income(
beacon_service: BeaconChaService = Depends(BeaconChaService), beacon_service: BeaconChaService = Depends(BeaconChaService),
fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect) fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect)
): ):
try: assert str_not_numbers(pub_key), RequestInvalidParamsError()
int(pub_key) query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
raise RequestInvalidParamsError() fund = await fund_collect.find_one(query)
except Exception as e: assert fund, NotFundError('未绑定该节点')
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}} validator_income_list = await beacon_service.get_validator_income(index_or_pubkey=pub_key)
fund = await fund_collect.find_one(query) assert validator_income_list, NotFundError()
assert fund, NotFundError('未绑定该节点') response = Response[ValidatorIncome](data=validator_income_list[0])
validator_income_list = await beacon_service.get_validator_income(index_or_pubkey=pub_key) return response
assert validator_income_list, NotFundError()
response = Response[ValidatorIncome](data=validator_income_list[0])
return response
@router.get("/epoch/{epoch}/", response_model=Response[Epoch], summary="查询epoch", description="") @router.get("/epoch/{epoch}/", response_model=Response[Epoch], summary="查询epoch", description="")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment