64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
from sanic import response
|
|
from base58 import b58encode, b58decode
|
|
from app.core._secrets import hot_pubkey, service_wallet, hot_privkey
|
|
from app.core._blockchain.ton.platform import platform
|
|
from datetime import datetime, timedelta
|
|
from app.core._crypto.signer import Signer
|
|
import subprocess
|
|
import json
|
|
|
|
|
|
def get_git_info():
|
|
branch_name = subprocess.check_output(["git", "branch", "--show-current"]).decode('utf-8').strip()
|
|
commit_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode('utf-8').strip()
|
|
return branch_name, commit_hash
|
|
|
|
|
|
async def s_api_system(request): # /api/node
|
|
return response.json({
|
|
'id': b58encode(hot_pubkey).decode(),
|
|
'node_address': service_wallet.address.to_string(1, 1, 1),
|
|
'master_address': platform.address.to_string(1, 1, 1),
|
|
'indexer_height': 0,
|
|
'services': {
|
|
service_key: {
|
|
'status': service['status'],
|
|
'delay': int((datetime.now() - service['timestamp']).total_seconds()) if service['timestamp'] else -1,
|
|
}
|
|
for service_key, service in request.app.ctx.memory.known_states.items()
|
|
}
|
|
})
|
|
|
|
|
|
async def s_api_system_send_status(request):
|
|
if not request.json:
|
|
return response.json({'error': 'No data'}, status=400)
|
|
|
|
message = request.json.get('message', '')
|
|
signature = request.json.get('signature', '')
|
|
if not message or not signature:
|
|
return response.json({'error': 'No message or signature'}, status=400)
|
|
|
|
signer = Signer(hot_privkey)
|
|
if not signer.verify(message, signature):
|
|
return response.json({'error': 'Invalid signature'}, status=400)
|
|
|
|
message = b58decode(message)
|
|
message = json.loads(message)
|
|
request.app.ctx.memory.known_states[
|
|
message['service']
|
|
] = {
|
|
'status': message['status'],
|
|
'timestamp': datetime.now(),
|
|
}
|
|
|
|
return response.json({'message': 'Status received'})
|
|
|
|
|
|
async def s_api_system_version(request):
|
|
branch_name, commit_hash = get_git_info()
|
|
return response.json({
|
|
"codebase_hash": commit_hash,
|
|
"codebase_branch": branch_name,
|
|
})
|