From 2da61212ca37d85eef663edcd1c5af4baa1f1ac3 Mon Sep 17 00:00:00 2001 From: guoshijiang Date: Wed, 27 Mar 2024 21:36:23 +0800 Subject: [PATCH] l3staking grpc server development --- l3staking/l3staking_server.py | 45 ++++ l3staking/management/__init__.py | 0 l3staking/management/commands/__init__.py | 0 .../management/commands/l3staking_server.py | 20 ++ .../0003_stakingstrategy_address.py | 18 ++ l3staking/models.py | 6 + services/savour_rpc/l3staking_grpc.py | 40 +++ services/savour_rpc/l3staking_pb2.py | 228 ++++++++++++++++++ services/savour_rpc/l3staking_pb2_grpc.py | 66 +++++ 9 files changed, 423 insertions(+) create mode 100644 l3staking/l3staking_server.py create mode 100644 l3staking/management/__init__.py create mode 100644 l3staking/management/commands/__init__.py create mode 100644 l3staking/management/commands/l3staking_server.py create mode 100644 l3staking/migrations/0003_stakingstrategy_address.py create mode 100644 services/savour_rpc/l3staking_grpc.py create mode 100644 services/savour_rpc/l3staking_pb2.py create mode 100644 services/savour_rpc/l3staking_pb2_grpc.py diff --git a/l3staking/l3staking_server.py b/l3staking/l3staking_server.py new file mode 100644 index 0000000..7c3e2ed --- /dev/null +++ b/l3staking/l3staking_server.py @@ -0,0 +1,45 @@ +# encoding=utf-8 + +import pytz +from l3staking.models import ( + Node +) +from services.savour_rpc import l3staking_pb2_grpc, l3staking_pb2 +from django.conf import settings + +tz = pytz.timezone(settings.TIME_ZONE) + + +class L3StakingServer(l3staking_pb2_grpc.L3StakingServiceServicer): + def updateStakingNodeIncome(self, request, context) -> l3staking_pb2.StakingNodeRep: + address = str(request.address) + chain_id = str(request.chain_id) + strategy = str(request.strategy) + eth_income = str(request.eth_income) + eth_income_rate = str(request.eth_income_rate) + dp_income = str(request.dp_income) + dp_income_rate = str(request.dp_income_rate) + eth_evil = str(request.eth_evil) + eth_evil_rate = str(request.eth_evil_rate) + dp_evil = str(request.dp_evil) + dp_evil_rate = str(request.dp_evil_rate) + tvl = str(request.tvl) + Node.objects.filter( + address=address, + chain__chain_id=chain_id, + strategy__address=strategy + ).update( + eth_income=eth_income, + eth_income_rate=eth_income_rate, + dp_income=dp_income, + dp_income_rate=dp_income_rate, + eth_evil=eth_evil, + eth_evil_rate=eth_evil_rate, + dp_evil=dp_evil, + dp_evil_rate=dp_evil_rate, + tvl=tvl + ) + return l3staking_pb2.StakingNodeRep( + code="200", + msg="submit stake node income success", + ) diff --git a/l3staking/management/__init__.py b/l3staking/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/l3staking/management/commands/__init__.py b/l3staking/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/l3staking/management/commands/l3staking_server.py b/l3staking/management/commands/l3staking_server.py new file mode 100644 index 0000000..8d711f1 --- /dev/null +++ b/l3staking/management/commands/l3staking_server.py @@ -0,0 +1,20 @@ +#encoding=utf-8 + +import grpc +from django.core.management.base import BaseCommand +from concurrent import futures +from services.savour_rpc import l3staking_pb2_grpc +from l3staking.l3staking_server import L3StakingServer + + +class Command(BaseCommand): + def handle(self, *args, **options): + server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) + l3staking_pb2_grpc.add_L3StakingServiceServicer_to_server( + L3StakingServer(), + server + ) + server.add_insecure_port('[::]:50252') + server.start() + print("l3 staking rpc server start") + server.wait_for_termination() \ No newline at end of file diff --git a/l3staking/migrations/0003_stakingstrategy_address.py b/l3staking/migrations/0003_stakingstrategy_address.py new file mode 100644 index 0000000..22d84f7 --- /dev/null +++ b/l3staking/migrations/0003_stakingstrategy_address.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.1 on 2024-03-27 13:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('l3staking', '0002_stakingstrategy_chain'), + ] + + operations = [ + migrations.AddField( + model_name='stakingstrategy', + name='address', + field=models.CharField(default='', max_length=100, verbose_name='策略地址'), + ), + ] diff --git a/l3staking/models.py b/l3staking/models.py index 80ef3c1..06b4cb4 100644 --- a/l3staking/models.py +++ b/l3staking/models.py @@ -60,6 +60,12 @@ class StakingStrategy(BaseModel): unique=False, verbose_name='质押模块名称' ) + address = models.CharField( + default="", + max_length=100, + unique=False, + verbose_name='策略地址' + ) class Meta: verbose_name = 'StakingStrategy' diff --git a/services/savour_rpc/l3staking_grpc.py b/services/savour_rpc/l3staking_grpc.py new file mode 100644 index 0000000..d4abc28 --- /dev/null +++ b/services/savour_rpc/l3staking_grpc.py @@ -0,0 +1,40 @@ +# Generated by the Protocol Buffers compiler. DO NOT EDIT! +# source: savour_rpc/l3staking.proto +# plugin: grpclib.plugin.main +import abc +import typing + +import grpclib.const +import grpclib.client +if typing.TYPE_CHECKING: + import grpclib.server + +from services.savour_rpc import l3staking_pb2 + + +class L3StakingServiceBase(abc.ABC): + + @abc.abstractmethod + async def updateStakingNodeIncome(self, stream: 'grpclib.server.Stream[l3staking_pb2.StakingNodeReq, l3staking_pb2.StakingNodeRep]') -> None: + pass + + def __mapping__(self) -> typing.Dict[str, grpclib.const.Handler]: + return { + '/services.l3staking.L3StakingService/updateStakingNodeIncome': grpclib.const.Handler( + self.updateStakingNodeIncome, + grpclib.const.Cardinality.UNARY_UNARY, + l3staking_pb2.StakingNodeReq, + l3staking_pb2.StakingNodeRep, + ), + } + + +class L3StakingServiceStub: + + def __init__(self, channel: grpclib.client.Channel) -> None: + self.updateStakingNodeIncome = grpclib.client.UnaryUnaryMethod( + channel, + '/services.l3staking.L3StakingService/updateStakingNodeIncome', + l3staking_pb2.StakingNodeReq, + l3staking_pb2.StakingNodeRep, + ) diff --git a/services/savour_rpc/l3staking_pb2.py b/services/savour_rpc/l3staking_pb2.py new file mode 100644 index 0000000..c59872f --- /dev/null +++ b/services/savour_rpc/l3staking_pb2.py @@ -0,0 +1,228 @@ +# -*- coding: utf-8 -*- +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: savour_rpc/l3staking.proto +"""Generated protocol buffer code.""" +from google.protobuf import descriptor as _descriptor +from google.protobuf import message as _message +from google.protobuf import reflection as _reflection +from google.protobuf import symbol_database as _symbol_database +# @@protoc_insertion_point(imports) + +_sym_db = _symbol_database.Default() + + + + +DESCRIPTOR = _descriptor.FileDescriptor( + name='savour_rpc/l3staking.proto', + package='services.savour_rpc.l3staking', + syntax='proto3', + serialized_options=b'\n\026group.savour.l3stakingZ\021./proto/l3staking', + create_key=_descriptor._internal_create_key, + serialized_pb=b'\n\x1asavour_rpc/l3staking.proto\x12\x1dservices.savour_rpc.l3staking\"\x92\x02\n\x0eStakingNodeReq\x12\x16\n\x0e\x63onsumer_token\x18\x01 \x01(\t\x12\x10\n\x08\x63hain_id\x18\x02 \x01(\t\x12\x10\n\x08strategy\x18\x03 \x01(\t\x12\x0f\n\x07\x61\x64\x64ress\x18\x04 \x01(\t\x12\x12\n\neth_income\x18\x05 \x01(\t\x12\x17\n\x0f\x65th_income_rate\x18\x06 \x01(\t\x12\x11\n\tdp_income\x18\x07 \x01(\t\x12\x16\n\x0e\x64p_income_rate\x18\x08 \x01(\t\x12\x10\n\x08\x65th_evil\x18\t \x01(\t\x12\x15\n\reth_evil_rate\x18\n \x01(\t\x12\x0f\n\x07\x64p_evil\x18\x0b \x01(\t\x12\x14\n\x0c\x64p_evil_rate\x18\x0c \x01(\t\x12\x0b\n\x03tvl\x18\r \x01(\t\"+\n\x0eStakingNodeRep\x12\x0c\n\x04\x63ode\x18\x01 \x01(\t\x12\x0b\n\x03msg\x18\x02 \x01(\t2\x8d\x01\n\x10L3StakingService\x12y\n\x17updateStakingNodeIncome\x12-.services.savour_rpc.l3staking.StakingNodeReq\x1a-.services.savour_rpc.l3staking.StakingNodeRep\"\x00\x42+\n\x16group.savour.l3stakingZ\x11./proto/l3stakingb\x06proto3' +) + + + + +_STAKINGNODEREQ = _descriptor.Descriptor( + name='StakingNodeReq', + full_name='services.savour_rpc.l3staking.StakingNodeReq', + filename=None, + file=DESCRIPTOR, + containing_type=None, + create_key=_descriptor._internal_create_key, + fields=[ + _descriptor.FieldDescriptor( + name='consumer_token', full_name='services.savour_rpc.l3staking.StakingNodeReq.consumer_token', index=0, + number=1, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='chain_id', full_name='services.savour_rpc.l3staking.StakingNodeReq.chain_id', index=1, + number=2, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='strategy', full_name='services.savour_rpc.l3staking.StakingNodeReq.strategy', index=2, + number=3, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='address', full_name='services.savour_rpc.l3staking.StakingNodeReq.address', index=3, + number=4, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='eth_income', full_name='services.savour_rpc.l3staking.StakingNodeReq.eth_income', index=4, + number=5, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='eth_income_rate', full_name='services.savour_rpc.l3staking.StakingNodeReq.eth_income_rate', index=5, + number=6, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='dp_income', full_name='services.savour_rpc.l3staking.StakingNodeReq.dp_income', index=6, + number=7, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='dp_income_rate', full_name='services.savour_rpc.l3staking.StakingNodeReq.dp_income_rate', index=7, + number=8, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='eth_evil', full_name='services.savour_rpc.l3staking.StakingNodeReq.eth_evil', index=8, + number=9, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='eth_evil_rate', full_name='services.savour_rpc.l3staking.StakingNodeReq.eth_evil_rate', index=9, + number=10, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='dp_evil', full_name='services.savour_rpc.l3staking.StakingNodeReq.dp_evil', index=10, + number=11, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='dp_evil_rate', full_name='services.savour_rpc.l3staking.StakingNodeReq.dp_evil_rate', index=11, + number=12, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='tvl', full_name='services.savour_rpc.l3staking.StakingNodeReq.tvl', index=12, + number=13, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + ], + extensions=[ + ], + nested_types=[], + enum_types=[ + ], + serialized_options=None, + is_extendable=False, + syntax='proto3', + extension_ranges=[], + oneofs=[ + ], + serialized_start=62, + serialized_end=336, +) + + +_STAKINGNODEREP = _descriptor.Descriptor( + name='StakingNodeRep', + full_name='services.savour_rpc.l3staking.StakingNodeRep', + filename=None, + file=DESCRIPTOR, + containing_type=None, + create_key=_descriptor._internal_create_key, + fields=[ + _descriptor.FieldDescriptor( + name='code', full_name='services.savour_rpc.l3staking.StakingNodeRep.code', index=0, + number=1, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='msg', full_name='services.savour_rpc.l3staking.StakingNodeRep.msg', index=1, + number=2, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=b"".decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + ], + extensions=[ + ], + nested_types=[], + enum_types=[ + ], + serialized_options=None, + is_extendable=False, + syntax='proto3', + extension_ranges=[], + oneofs=[ + ], + serialized_start=338, + serialized_end=381, +) + +DESCRIPTOR.message_types_by_name['StakingNodeReq'] = _STAKINGNODEREQ +DESCRIPTOR.message_types_by_name['StakingNodeRep'] = _STAKINGNODEREP +_sym_db.RegisterFileDescriptor(DESCRIPTOR) + +StakingNodeReq = _reflection.GeneratedProtocolMessageType('StakingNodeReq', (_message.Message,), { + 'DESCRIPTOR' : _STAKINGNODEREQ, + '__module__' : 'savour_rpc.l3staking_pb2' + # @@protoc_insertion_point(class_scope:services.savour_rpc.l3staking.StakingNodeReq) + }) +_sym_db.RegisterMessage(StakingNodeReq) + +StakingNodeRep = _reflection.GeneratedProtocolMessageType('StakingNodeRep', (_message.Message,), { + 'DESCRIPTOR' : _STAKINGNODEREP, + '__module__' : 'savour_rpc.l3staking_pb2' + # @@protoc_insertion_point(class_scope:services.savour_rpc.l3staking.StakingNodeRep) + }) +_sym_db.RegisterMessage(StakingNodeRep) + + +DESCRIPTOR._options = None + +_L3STAKINGSERVICE = _descriptor.ServiceDescriptor( + name='L3StakingService', + full_name='services.savour_rpc.l3staking.L3StakingService', + file=DESCRIPTOR, + index=0, + serialized_options=None, + create_key=_descriptor._internal_create_key, + serialized_start=384, + serialized_end=525, + methods=[ + _descriptor.MethodDescriptor( + name='updateStakingNodeIncome', + full_name='services.savour_rpc.l3staking.L3StakingService.updateStakingNodeIncome', + index=0, + containing_service=None, + input_type=_STAKINGNODEREQ, + output_type=_STAKINGNODEREP, + serialized_options=None, + create_key=_descriptor._internal_create_key, + ), +]) +_sym_db.RegisterServiceDescriptor(_L3STAKINGSERVICE) + +DESCRIPTOR.services_by_name['L3StakingService'] = _L3STAKINGSERVICE + +# @@protoc_insertion_point(module_scope) diff --git a/services/savour_rpc/l3staking_pb2_grpc.py b/services/savour_rpc/l3staking_pb2_grpc.py new file mode 100644 index 0000000..6aec84f --- /dev/null +++ b/services/savour_rpc/l3staking_pb2_grpc.py @@ -0,0 +1,66 @@ +# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! +"""Client and server classes corresponding to protobuf-defined services.""" +import grpc + +from services.savour_rpc import l3staking_pb2 as savour__rpc_dot_l3staking__pb2 + + +class L3StakingServiceStub(object): + """Missing associated documentation comment in .proto file.""" + + def __init__(self, channel): + """Constructor. + + Args: + channel: A grpc.Channel. + """ + self.updateStakingNodeIncome = channel.unary_unary( + '/services.savour_rpc.l3staking.L3StakingService/updateStakingNodeIncome', + request_serializer=savour__rpc_dot_l3staking__pb2.StakingNodeReq.SerializeToString, + response_deserializer=savour__rpc_dot_l3staking__pb2.StakingNodeRep.FromString, + ) + + +class L3StakingServiceServicer(object): + """Missing associated documentation comment in .proto file.""" + + def updateStakingNodeIncome(self, request, context): + """Missing associated documentation comment in .proto file.""" + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details('Method not implemented!') + raise NotImplementedError('Method not implemented!') + + +def add_L3StakingServiceServicer_to_server(servicer, server): + rpc_method_handlers = { + 'updateStakingNodeIncome': grpc.unary_unary_rpc_method_handler( + servicer.updateStakingNodeIncome, + request_deserializer=savour__rpc_dot_l3staking__pb2.StakingNodeReq.FromString, + response_serializer=savour__rpc_dot_l3staking__pb2.StakingNodeRep.SerializeToString, + ), + } + generic_handler = grpc.method_handlers_generic_handler( + 'services.savour_rpc.l3staking.L3StakingService', rpc_method_handlers) + server.add_generic_rpc_handlers((generic_handler,)) + + + # This class is part of an EXPERIMENTAL API. +class L3StakingService(object): + """Missing associated documentation comment in .proto file.""" + + @staticmethod + def updateStakingNodeIncome(request, + target, + options=(), + channel_credentials=None, + call_credentials=None, + insecure=False, + compression=None, + wait_for_ready=None, + timeout=None, + metadata=None): + return grpc.experimental.unary_unary(request, target, '/services.savour_rpc.l3staking.L3StakingService/updateStakingNodeIncome', + savour__rpc_dot_l3staking__pb2.StakingNodeReq.SerializeToString, + savour__rpc_dot_l3staking__pb2.StakingNodeRep.FromString, + options, channel_credentials, + insecure, call_credentials, compression, wait_for_ready, timeout, metadata)