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

判断节点用正则

parent f8f73509
import re
from typing import List
from motor.core import AgnosticCollection
import dependencies
from exception.api import APIError
from exception.db import ExistDataError, NotFundError
......@@ -19,6 +18,14 @@ from tools.jwt_tools import User
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('/',
response_model=BaseResponse,
summary='绑定节点',
......@@ -29,25 +36,22 @@ async def subscribe(
fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect),
beacon_service: BeaconChaService = Depends(BeaconChaService),
):
try:
int(bind_node.pub_key)
raise RequestInvalidParamsError()
except Exception as e:
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)
# 限制staking基金才可绑定节点
query = {'id': bind_node.fund_id, 'user_id': user.id, 'fund_type': FundType.staking,
f"nodes.{bind_node.pub_key}": {'$exists': False}}
update = {"$set": {f"nodes.{bind_node.pub_key}": db_data.dict()}}
res = await fund_collect.update_one(
query,
update
)
if res.raw_result['nModified'] == 0:
raise ExistDataError(message='节点已存在')
return BaseResponse(data='绑定成功')
assert str_not_numbers(bind_node.pub_key), RequestInvalidParamsError()
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)
# 限制staking基金才可绑定节点
query = {'id': bind_node.fund_id, 'user_id': user.id, 'fund_type': FundType.staking,
f"nodes.{bind_node.pub_key}": {'$exists': False}}
update = {"$set": {f"nodes.{bind_node.pub_key}": db_data.dict()}}
res = await fund_collect.update_one(
query,
update
)
if res.raw_result['nModified'] == 0:
raise ExistDataError(message='节点已存在')
return BaseResponse(data='绑定成功')
@router.delete('/', response_model=BaseResponse, summary='解绑节点', description='解绑节点')
......@@ -76,16 +80,13 @@ async def get_node_info(
beacon_service: BeaconChaService = Depends(BeaconChaService),
fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect)
):
try:
int(pub_key)
raise RequestInvalidParamsError()
except Exception as e:
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
fund = await fund_collect.find_one(query)
assert fund, NotFundError('未绑定该节点')
validator_detail = await beacon_service.get_validator(pub_key)
response = Response[Validator](data=validator_detail)
return response
assert str_not_numbers(pub_key), RequestInvalidParamsError()
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
fund = await fund_collect.find_one(query)
assert fund, NotFundError('未绑定该节点')
validator_detail = await beacon_service.get_validator(pub_key)
response = Response[Validator](data=validator_detail)
return response
@router.get(
......@@ -99,16 +100,13 @@ async def get_node_deposit(
beacon_service: BeaconChaService = Depends(BeaconChaService),
fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect)
):
try:
int(pub_key)
raise RequestInvalidParamsError()
except Exception as e:
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
fund = await fund_collect.find_one(query)
assert fund, NotFundError('未绑定该节点')
validator_deposit_list = await beacon_service.get_validator_deposit(pub_key)
response = Response[List[ValidatorDeposit]](data=validator_deposit_list)
return response
assert str_not_numbers(pub_key), RequestInvalidParamsError()
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
fund = await fund_collect.find_one(query)
assert fund, NotFundError('未绑定该节点')
validator_deposit_list = await beacon_service.get_validator_deposit(pub_key)
response = Response[List[ValidatorDeposit]](data=validator_deposit_list)
return response
@router.get(
......@@ -123,20 +121,17 @@ async def get_node_blocks(
beacon_service: BeaconChaService = Depends(BeaconChaService),
fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect)
):
try:
int(pub_key)
raise RequestInvalidParamsError()
except Exception as e:
start = (page.page - 1) * page.page_size
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
fund = await fund_collect.find_one(query)
assert fund, NotFundError('未绑定该节点')
validator_blocks = await beacon_service.get_validator_blocks(
index=fund["nodes"][pub_key]["index"],
start=start,
length=page.page_size
)
return PageResponse[ValidatorBlock](data=validator_blocks.data, **page.dict(), total=validator_blocks.total)
assert str_not_numbers(pub_key), RequestInvalidParamsError()
start = (page.page - 1) * page.page_size
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
fund = await fund_collect.find_one(query)
assert fund, NotFundError('未绑定该节点')
validator_blocks = await beacon_service.get_validator_blocks(
index=fund["nodes"][pub_key]["index"],
start=start,
length=page.page_size
)
return PageResponse[ValidatorBlock](data=validator_blocks.data, **page.dict(), total=validator_blocks.total)
@router.get(
......@@ -150,17 +145,14 @@ async def get_node_income(
beacon_service: BeaconChaService = Depends(BeaconChaService),
fund_collect: AgnosticCollection = Depends(dependencies.get_fund_collect)
):
try:
int(pub_key)
raise RequestInvalidParamsError()
except Exception as e:
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
fund = await fund_collect.find_one(query)
assert fund, NotFundError('未绑定该节点')
validator_income_list = await beacon_service.get_validator_income(index_or_pubkey=pub_key)
assert validator_income_list, NotFundError()
response = Response[ValidatorIncome](data=validator_income_list[0])
return response
assert str_not_numbers(pub_key), RequestInvalidParamsError()
query = {'id': fund_id, 'user_id': user.id, 'fund_type': FundType.staking, f"nodes.{pub_key}": {"$exists": True}}
fund = await fund_collect.find_one(query)
assert fund, NotFundError('未绑定该节点')
validator_income_list = await beacon_service.get_validator_income(index_or_pubkey=pub_key)
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="")
......
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