diff --git a/dcache_py/dcache_pb2.py b/dcache_py/dcache_pb2.py index b81d9d5..80ac522 100644 --- a/dcache_py/dcache_pb2.py +++ b/dcache_py/dcache_pb2.py @@ -14,7 +14,7 @@ _sym_db = _symbol_database.Default() -DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0c\x64\x63\x61\x63he.proto\x12\x06\x64\x63\x61\x63he\"?\n\x05Level\x12\x1a\n\x11visitor_threshold\x18\xad\x02 \x01(\r\x12\x1a\n\x11\x64ifficulty_factor\x18\xae\x02 \x01(\r\")\n\x07\x44\x65\x66\x65nse\x12\x1e\n\x06levels\x18\x91\x03 \x03(\x0b\x32\r.dcache.Level\"@\n\x08MCaptcha\x12\x11\n\x08\x64uration\x18\xf6\x03 \x01(\x04\x12!\n\x07\x64\x65\x66\x65nse\x18\xf7\x03 \x01(\x0b\x32\x0f.dcache.Defense\"E\n\x11\x41\x64\x64\x43\x61ptchaRequest\x12\x0b\n\x02id\x18\xd9\x04 \x01(\t\x12#\n\x08mcaptcha\x18\xda\x04 \x01(\x0b\x32\x10.dcache.MCaptcha\"9\n\x14RenameCaptchaRequest\x12\r\n\x04name\x18\xbd\x05 \x01(\t\x12\x12\n\trename_to\x18\xbe\x05 \x01(\t\"_\n\x0f\x43\x61\x63hePowRequest\x12\x0f\n\x06string\x18\xa1\x06 \x01(\t\x12\x1a\n\x11\x64ifficulty_factor\x18\xa2\x06 \x01(\r\x12\x11\n\x08\x64uration\x18\xa3\x06 \x01(\x04\x12\x0c\n\x03key\x18\xa4\x06 \x01(\t\"E\n\x12\x43\x61\x63heResultRequest\x12\x0e\n\x05token\x18\xb1\x06 \x01(\t\x12\x0c\n\x03key\x18\xb2\x06 \x01(\t\x12\x11\n\x08\x64uration\x18\xb3\x06 \x01(\x04\",\n\x1a\x44\x65leteCaptchaResultRequest\x12\x0e\n\x05token\x18\xb5\x06 \x01(\t\"\x17\n\tCaptchaID\x12\n\n\x02id\x18\x01 \x01(\t\"\x12\n\x04PoID\x12\n\n\x02id\x18\x01 \x01(\t\"A\n\x10\x41\x64\x64VisitorResult\x12\x11\n\x08\x64uration\x18\x85\x07 \x01(\x04\x12\x1a\n\x11\x64ifficulty_factor\x18\x86\x07 \x01(\r\"S\n\x16OptionAddVisitorResult\x12.\n\x06result\x18\x8f\x07 \x01(\x0b\x32\x18.dcache.AddVisitorResultH\x00\x88\x01\x01\x42\t\n\x07_result\"\x1b\n\x0bRaftRequest\x12\x0c\n\x04\x64\x61ta\x18\x01 \x01(\t\"(\n\tRaftReply\x12\x0c\n\x04\x64\x61ta\x18\x01 \x01(\t\x12\r\n\x05\x65rror\x18\x02 \x01(\t\"#\n\x07Learner\x12\n\n\x02id\x18\x01 \x01(\x04\x12\x0c\n\x04\x61\x64\x64r\x18\x02 \x01(\t\"\xbd\x02\n\rDcacheRequest\x12/\n\naddCaptcha\x18\x01 \x01(\x0b\x32\x19.dcache.AddCaptchaRequestH\x00\x12\'\n\naddVisitor\x18\x02 \x01(\x0b\x32\x11.dcache.CaptchaIDH\x00\x12\x35\n\rrenameCaptcha\x18\x03 \x01(\x0b\x32\x1c.dcache.RenameCaptchaRequestH\x00\x12*\n\rremoveCaptcha\x18\x04 \x01(\x0b\x32\x11.dcache.CaptchaIDH\x00\x12+\n\x08\x63\x61\x63hePow\x18\x05 \x01(\x0b\x32\x17.dcache.CachePowRequestH\x00\x12\x31\n\x0b\x63\x61\x63heResult\x18\x06 \x01(\x0b\x32\x1a.dcache.CacheResultRequestH\x00\x42\x0f\n\rDcacheRequest\"\x8b\x01\n\x0e\x44\x63\x61\x63heResponse\x12\x43\n\x19option_add_visitor_result\x18\x01 \x01(\x0b\x32\x1e.dcache.OptionAddVisitorResultH\x00\x12\"\n\x05other\x18\x02 \x01(\x0b\x32\x11.dcache.RaftReplyH\x00\x42\x10\n\x0e\x44\x63\x61\x63heResponse\"=\n\x12\x44\x63\x61\x63heBatchRequest\x12\'\n\x08requests\x18\x01 \x03(\x0b\x32\x15.dcache.DcacheRequest\"@\n\x13\x44\x63\x61\x63heBatchResponse\x12)\n\tresponses\x18\x01 \x03(\x0b\x32\x16.dcache.DcacheResponse2\x97\x06\n\rDcacheService\x12<\n\nAddCaptcha\x12\x19.dcache.AddCaptchaRequest\x1a\x11.dcache.RaftReply\"\x00\x12\x41\n\nAddVisitor\x12\x11.dcache.CaptchaID\x1a\x1e.dcache.OptionAddVisitorResult\"\x00\x12\x42\n\rRenameCaptcha\x12\x1c.dcache.RenameCaptchaRequest\x1a\x11.dcache.RaftReply\"\x00\x12\x37\n\rRemoveCaptcha\x12\x11.dcache.CaptchaID\x1a\x11.dcache.RaftReply\"\x00\x12\x38\n\x08\x43\x61\x63hePow\x12\x17.dcache.CachePowRequest\x1a\x11.dcache.RaftReply\"\x00\x12>\n\x0b\x43\x61\x63heResult\x12\x1a.dcache.CacheResultRequest\x1a\x11.dcache.RaftReply\"\x00\x12N\n\x11PipelineDcacheOps\x12\x1a.dcache.DcacheBatchRequest\x1a\x1b.dcache.DcacheBatchResponse\"\x00\x12\x32\n\nAddLearner\x12\x0f.dcache.Learner\x1a\x11.dcache.RaftReply\"\x00\x12\x31\n\x05Write\x12\x13.dcache.RaftRequest\x1a\x11.dcache.RaftReply\"\x00\x12\x33\n\x07\x46orward\x12\x13.dcache.RaftRequest\x1a\x11.dcache.RaftReply\"\x00\x12\x37\n\rAppendEntries\x12\x13.dcache.RaftRequest\x1a\x11.dcache.RaftReply\x12\x39\n\x0fInstallSnapshot\x12\x13.dcache.RaftRequest\x1a\x11.dcache.RaftReply\x12.\n\x04vote\x12\x13.dcache.RaftRequest\x1a\x11.dcache.RaftReplyb\x06proto3') +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0c\x64\x63\x61\x63he.proto\x12\x06\x64\x63\x61\x63he\"?\n\x05Level\x12\x1a\n\x11visitor_threshold\x18\xad\x02 \x01(\r\x12\x1a\n\x11\x64ifficulty_factor\x18\xae\x02 \x01(\r\")\n\x07\x44\x65\x66\x65nse\x12\x1e\n\x06levels\x18\x91\x03 \x03(\x0b\x32\r.dcache.Level\"@\n\x08MCaptcha\x12\x11\n\x08\x64uration\x18\xf6\x03 \x01(\x04\x12!\n\x07\x64\x65\x66\x65nse\x18\xf7\x03 \x01(\x0b\x32\x0f.dcache.Defense\"E\n\x11\x41\x64\x64\x43\x61ptchaRequest\x12\x0b\n\x02id\x18\xd9\x04 \x01(\t\x12#\n\x08mcaptcha\x18\xda\x04 \x01(\x0b\x32\x10.dcache.MCaptcha\"9\n\x14RenameCaptchaRequest\x12\r\n\x04name\x18\xbd\x05 \x01(\t\x12\x12\n\trename_to\x18\xbe\x05 \x01(\t\"_\n\x0f\x43\x61\x63hePowRequest\x12\x0f\n\x06string\x18\xa1\x06 \x01(\t\x12\x1a\n\x11\x64ifficulty_factor\x18\xa2\x06 \x01(\r\x12\x11\n\x08\x64uration\x18\xa3\x06 \x01(\x04\x12\x0c\n\x03key\x18\xa4\x06 \x01(\t\"E\n\x12\x43\x61\x63heResultRequest\x12\x0e\n\x05token\x18\xb1\x06 \x01(\t\x12\x0c\n\x03key\x18\xb2\x06 \x01(\t\x12\x11\n\x08\x64uration\x18\xb3\x06 \x01(\x04\",\n\x1a\x44\x65leteCaptchaResultRequest\x12\x0e\n\x05token\x18\xb5\x06 \x01(\t\"\x17\n\tCaptchaID\x12\n\n\x02id\x18\x01 \x01(\t\"\x12\n\x04PoID\x12\n\n\x02id\x18\x01 \x01(\t\"A\n\x10\x41\x64\x64VisitorResult\x12\x11\n\x08\x64uration\x18\x85\x07 \x01(\x04\x12\x1a\n\x11\x64ifficulty_factor\x18\x86\x07 \x01(\r\"S\n\x16OptionAddVisitorResult\x12.\n\x06result\x18\x8f\x07 \x01(\x0b\x32\x18.dcache.AddVisitorResultH\x00\x88\x01\x01\x42\t\n\x07_result\"\x1b\n\x0bRaftRequest\x12\x0c\n\x04\x64\x61ta\x18\x01 \x01(\t\"(\n\tRaftReply\x12\x0c\n\x04\x64\x61ta\x18\x01 \x01(\t\x12\r\n\x05\x65rror\x18\x02 \x01(\t\"#\n\x07Learner\x12\n\n\x02id\x18\x01 \x01(\x04\x12\x0c\n\x04\x61\x64\x64r\x18\x02 \x01(\t\"\'\n\x15\x43\x61ptchaExistsResponse\x12\x0e\n\x06\x65xists\x18\x01 \x01(\x08\"+\n\x17GetVisitorCountResponse\x12\x10\n\x08visitors\x18\x01 \x01(\r\"`\n\x1dOptionGetVisitorCountResponse\x12\x34\n\x06result\x18\x01 \x01(\x0b\x32\x1f.dcache.GetVisitorCountResponseH\x00\x88\x01\x01\x42\t\n\x07_result\"\x97\x03\n\rDcacheRequest\x12/\n\naddCaptcha\x18\x01 \x01(\x0b\x32\x19.dcache.AddCaptchaRequestH\x00\x12\'\n\naddVisitor\x18\x02 \x01(\x0b\x32\x11.dcache.CaptchaIDH\x00\x12\x35\n\rrenameCaptcha\x18\x03 \x01(\x0b\x32\x1c.dcache.RenameCaptchaRequestH\x00\x12*\n\rremoveCaptcha\x18\x04 \x01(\x0b\x32\x11.dcache.CaptchaIDH\x00\x12+\n\x08\x63\x61\x63hePow\x18\x05 \x01(\x0b\x32\x17.dcache.CachePowRequestH\x00\x12\x31\n\x0b\x63\x61\x63heResult\x18\x06 \x01(\x0b\x32\x1a.dcache.CacheResultRequestH\x00\x12*\n\rcaptchaExists\x18\x07 \x01(\x0b\x32\x11.dcache.CaptchaIDH\x00\x12,\n\x0fgetVisitorCount\x18\x08 \x01(\x0b\x32\x11.dcache.CaptchaIDH\x00\x42\x0f\n\rDcacheRequest\"\x88\x02\n\x0e\x44\x63\x61\x63heResponse\x12\x43\n\x19option_add_visitor_result\x18\x01 \x01(\x0b\x32\x1e.dcache.OptionAddVisitorResultH\x00\x12\"\n\x05other\x18\x02 \x01(\x0b\x32\x11.dcache.RaftReplyH\x00\x12\x37\n\x0e\x63\x61ptcha_exists\x18\x03 \x01(\x0b\x32\x1d.dcache.CaptchaExistsResponseH\x00\x12\x42\n\x11get_visitor_count\x18\x04 \x01(\x0b\x32%.dcache.OptionGetVisitorCountResponseH\x00\x42\x10\n\x0e\x44\x63\x61\x63heResponse\"=\n\x12\x44\x63\x61\x63heBatchRequest\x12\'\n\x08requests\x18\x01 \x03(\x0b\x32\x15.dcache.DcacheRequest\"@\n\x13\x44\x63\x61\x63heBatchResponse\x12)\n\tresponses\x18\x01 \x03(\x0b\x32\x16.dcache.DcacheResponse\"0\n\x12RetrievePowRequest\x12\r\n\x05token\x18\x01 \x01(\t\x12\x0b\n\x03key\x18\x02 \x01(\t\"O\n\x13RetrievePowResponse\x12\x19\n\x11\x64ifficulty_factor\x18\x01 \x01(\r\x12\x10\n\x08\x64uration\x18\x02 \x01(\x04\x12\x0b\n\x03key\x18\x03 \x01(\t\")\n\x15\x43\x61ptchaResultVerified\x12\x10\n\x08verified\x18\x01 \x01(\x08\"\"\n\x10\x44\x65letePowRequest\x12\x0e\n\x06string\x18\x01 \x01(\t\"Z\n\x1bOptionalRetrievePoWResponse\x12\x30\n\x06result\x18\x01 \x01(\x0b\x32\x1b.dcache.RetrievePowResponseH\x00\x88\x01\x01\x42\t\n\x07_result2\xdd\t\n\rDcacheService\x12<\n\nAddCaptcha\x12\x19.dcache.AddCaptchaRequest\x1a\x11.dcache.RaftReply\"\x00\x12\x41\n\nAddVisitor\x12\x11.dcache.CaptchaID\x1a\x1e.dcache.OptionAddVisitorResult\"\x00\x12\x42\n\rRenameCaptcha\x12\x1c.dcache.RenameCaptchaRequest\x1a\x11.dcache.RaftReply\"\x00\x12\x37\n\rRemoveCaptcha\x12\x11.dcache.CaptchaID\x1a\x11.dcache.RaftReply\"\x00\x12\x38\n\x08\x43\x61\x63hePow\x12\x17.dcache.CachePowRequest\x1a\x11.dcache.RaftReply\"\x00\x12P\n\x0bRetrievePow\x12\x1a.dcache.RetrievePowRequest\x1a#.dcache.OptionalRetrievePoWResponse\"\x00\x12:\n\tDeletePow\x12\x18.dcache.DeletePowRequest\x1a\x11.dcache.RaftReply\"\x00\x12>\n\x0b\x43\x61\x63heResult\x12\x1a.dcache.CacheResultRequest\x1a\x11.dcache.RaftReply\"\x00\x12R\n\x13VerifyCaptchaResult\x12\x1a.dcache.RetrievePowRequest\x1a\x1d.dcache.CaptchaResultVerified\"\x00\x12N\n\x13\x44\x65leteCaptchaResult\x12\".dcache.DeleteCaptchaResultRequest\x1a\x11.dcache.RaftReply\"\x00\x12\x43\n\rCaptchaExists\x12\x11.dcache.CaptchaID\x1a\x1d.dcache.CaptchaExistsResponse\"\x00\x12M\n\x0fGetVisitorCount\x12\x11.dcache.CaptchaID\x1a%.dcache.OptionGetVisitorCountResponse\"\x00\x12N\n\x11PipelineDcacheOps\x12\x1a.dcache.DcacheBatchRequest\x1a\x1b.dcache.DcacheBatchResponse\"\x00\x12\x32\n\nAddLearner\x12\x0f.dcache.Learner\x1a\x11.dcache.RaftReply\"\x00\x12\x31\n\x05Write\x12\x13.dcache.RaftRequest\x1a\x11.dcache.RaftReply\"\x00\x12\x33\n\x07\x46orward\x12\x13.dcache.RaftRequest\x1a\x11.dcache.RaftReply\"\x00\x12\x37\n\rAppendEntries\x12\x13.dcache.RaftRequest\x1a\x11.dcache.RaftReply\x12\x39\n\x0fInstallSnapshot\x12\x13.dcache.RaftRequest\x1a\x11.dcache.RaftReply\x12.\n\x04vote\x12\x13.dcache.RaftRequest\x1a\x11.dcache.RaftReplyb\x06proto3') _globals = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) @@ -51,14 +51,30 @@ if _descriptor._USE_C_DESCRIPTORS == False: _globals['_RAFTREPLY']._serialized_end=808 _globals['_LEARNER']._serialized_start=810 _globals['_LEARNER']._serialized_end=845 - _globals['_DCACHEREQUEST']._serialized_start=848 - _globals['_DCACHEREQUEST']._serialized_end=1165 - _globals['_DCACHERESPONSE']._serialized_start=1168 - _globals['_DCACHERESPONSE']._serialized_end=1307 - _globals['_DCACHEBATCHREQUEST']._serialized_start=1309 - _globals['_DCACHEBATCHREQUEST']._serialized_end=1370 - _globals['_DCACHEBATCHRESPONSE']._serialized_start=1372 - _globals['_DCACHEBATCHRESPONSE']._serialized_end=1436 - _globals['_DCACHESERVICE']._serialized_start=1439 - _globals['_DCACHESERVICE']._serialized_end=2230 + _globals['_CAPTCHAEXISTSRESPONSE']._serialized_start=847 + _globals['_CAPTCHAEXISTSRESPONSE']._serialized_end=886 + _globals['_GETVISITORCOUNTRESPONSE']._serialized_start=888 + _globals['_GETVISITORCOUNTRESPONSE']._serialized_end=931 + _globals['_OPTIONGETVISITORCOUNTRESPONSE']._serialized_start=933 + _globals['_OPTIONGETVISITORCOUNTRESPONSE']._serialized_end=1029 + _globals['_DCACHEREQUEST']._serialized_start=1032 + _globals['_DCACHEREQUEST']._serialized_end=1439 + _globals['_DCACHERESPONSE']._serialized_start=1442 + _globals['_DCACHERESPONSE']._serialized_end=1706 + _globals['_DCACHEBATCHREQUEST']._serialized_start=1708 + _globals['_DCACHEBATCHREQUEST']._serialized_end=1769 + _globals['_DCACHEBATCHRESPONSE']._serialized_start=1771 + _globals['_DCACHEBATCHRESPONSE']._serialized_end=1835 + _globals['_RETRIEVEPOWREQUEST']._serialized_start=1837 + _globals['_RETRIEVEPOWREQUEST']._serialized_end=1885 + _globals['_RETRIEVEPOWRESPONSE']._serialized_start=1887 + _globals['_RETRIEVEPOWRESPONSE']._serialized_end=1966 + _globals['_CAPTCHARESULTVERIFIED']._serialized_start=1968 + _globals['_CAPTCHARESULTVERIFIED']._serialized_end=2009 + _globals['_DELETEPOWREQUEST']._serialized_start=2011 + _globals['_DELETEPOWREQUEST']._serialized_end=2045 + _globals['_OPTIONALRETRIEVEPOWRESPONSE']._serialized_start=2047 + _globals['_OPTIONALRETRIEVEPOWRESPONSE']._serialized_end=2137 + _globals['_DCACHESERVICE']._serialized_start=2140 + _globals['_DCACHESERVICE']._serialized_end=3385 # @@protoc_insertion_point(module_scope) diff --git a/dcache_py/dcache_pb2.pyi b/dcache_py/dcache_pb2.pyi index 4197ae5..562e3c4 100644 --- a/dcache_py/dcache_pb2.pyi +++ b/dcache_py/dcache_pb2.pyi @@ -119,29 +119,55 @@ class Learner(_message.Message): addr: str def __init__(self, id: _Optional[int] = ..., addr: _Optional[str] = ...) -> None: ... +class CaptchaExistsResponse(_message.Message): + __slots__ = ("exists",) + EXISTS_FIELD_NUMBER: _ClassVar[int] + exists: bool + def __init__(self, exists: bool = ...) -> None: ... + +class GetVisitorCountResponse(_message.Message): + __slots__ = ("visitors",) + VISITORS_FIELD_NUMBER: _ClassVar[int] + visitors: int + def __init__(self, visitors: _Optional[int] = ...) -> None: ... + +class OptionGetVisitorCountResponse(_message.Message): + __slots__ = ("result",) + RESULT_FIELD_NUMBER: _ClassVar[int] + result: GetVisitorCountResponse + def __init__(self, result: _Optional[_Union[GetVisitorCountResponse, _Mapping]] = ...) -> None: ... + class DcacheRequest(_message.Message): - __slots__ = ("addCaptcha", "addVisitor", "renameCaptcha", "removeCaptcha", "cachePow", "cacheResult") + __slots__ = ("addCaptcha", "addVisitor", "renameCaptcha", "removeCaptcha", "cachePow", "cacheResult", "captchaExists", "getVisitorCount") ADDCAPTCHA_FIELD_NUMBER: _ClassVar[int] ADDVISITOR_FIELD_NUMBER: _ClassVar[int] RENAMECAPTCHA_FIELD_NUMBER: _ClassVar[int] REMOVECAPTCHA_FIELD_NUMBER: _ClassVar[int] CACHEPOW_FIELD_NUMBER: _ClassVar[int] CACHERESULT_FIELD_NUMBER: _ClassVar[int] + CAPTCHAEXISTS_FIELD_NUMBER: _ClassVar[int] + GETVISITORCOUNT_FIELD_NUMBER: _ClassVar[int] addCaptcha: AddCaptchaRequest addVisitor: CaptchaID renameCaptcha: RenameCaptchaRequest removeCaptcha: CaptchaID cachePow: CachePowRequest cacheResult: CacheResultRequest - def __init__(self, addCaptcha: _Optional[_Union[AddCaptchaRequest, _Mapping]] = ..., addVisitor: _Optional[_Union[CaptchaID, _Mapping]] = ..., renameCaptcha: _Optional[_Union[RenameCaptchaRequest, _Mapping]] = ..., removeCaptcha: _Optional[_Union[CaptchaID, _Mapping]] = ..., cachePow: _Optional[_Union[CachePowRequest, _Mapping]] = ..., cacheResult: _Optional[_Union[CacheResultRequest, _Mapping]] = ...) -> None: ... + captchaExists: CaptchaID + getVisitorCount: CaptchaID + def __init__(self, addCaptcha: _Optional[_Union[AddCaptchaRequest, _Mapping]] = ..., addVisitor: _Optional[_Union[CaptchaID, _Mapping]] = ..., renameCaptcha: _Optional[_Union[RenameCaptchaRequest, _Mapping]] = ..., removeCaptcha: _Optional[_Union[CaptchaID, _Mapping]] = ..., cachePow: _Optional[_Union[CachePowRequest, _Mapping]] = ..., cacheResult: _Optional[_Union[CacheResultRequest, _Mapping]] = ..., captchaExists: _Optional[_Union[CaptchaID, _Mapping]] = ..., getVisitorCount: _Optional[_Union[CaptchaID, _Mapping]] = ...) -> None: ... class DcacheResponse(_message.Message): - __slots__ = ("option_add_visitor_result", "other") + __slots__ = ("option_add_visitor_result", "other", "captcha_exists", "get_visitor_count") OPTION_ADD_VISITOR_RESULT_FIELD_NUMBER: _ClassVar[int] OTHER_FIELD_NUMBER: _ClassVar[int] + CAPTCHA_EXISTS_FIELD_NUMBER: _ClassVar[int] + GET_VISITOR_COUNT_FIELD_NUMBER: _ClassVar[int] option_add_visitor_result: OptionAddVisitorResult other: RaftReply - def __init__(self, option_add_visitor_result: _Optional[_Union[OptionAddVisitorResult, _Mapping]] = ..., other: _Optional[_Union[RaftReply, _Mapping]] = ...) -> None: ... + captcha_exists: CaptchaExistsResponse + get_visitor_count: OptionGetVisitorCountResponse + def __init__(self, option_add_visitor_result: _Optional[_Union[OptionAddVisitorResult, _Mapping]] = ..., other: _Optional[_Union[RaftReply, _Mapping]] = ..., captcha_exists: _Optional[_Union[CaptchaExistsResponse, _Mapping]] = ..., get_visitor_count: _Optional[_Union[OptionGetVisitorCountResponse, _Mapping]] = ...) -> None: ... class DcacheBatchRequest(_message.Message): __slots__ = ("requests",) @@ -154,3 +180,39 @@ class DcacheBatchResponse(_message.Message): RESPONSES_FIELD_NUMBER: _ClassVar[int] responses: _containers.RepeatedCompositeFieldContainer[DcacheResponse] def __init__(self, responses: _Optional[_Iterable[_Union[DcacheResponse, _Mapping]]] = ...) -> None: ... + +class RetrievePowRequest(_message.Message): + __slots__ = ("token", "key") + TOKEN_FIELD_NUMBER: _ClassVar[int] + KEY_FIELD_NUMBER: _ClassVar[int] + token: str + key: str + def __init__(self, token: _Optional[str] = ..., key: _Optional[str] = ...) -> None: ... + +class RetrievePowResponse(_message.Message): + __slots__ = ("difficulty_factor", "duration", "key") + DIFFICULTY_FACTOR_FIELD_NUMBER: _ClassVar[int] + DURATION_FIELD_NUMBER: _ClassVar[int] + KEY_FIELD_NUMBER: _ClassVar[int] + difficulty_factor: int + duration: int + key: str + def __init__(self, difficulty_factor: _Optional[int] = ..., duration: _Optional[int] = ..., key: _Optional[str] = ...) -> None: ... + +class CaptchaResultVerified(_message.Message): + __slots__ = ("verified",) + VERIFIED_FIELD_NUMBER: _ClassVar[int] + verified: bool + def __init__(self, verified: bool = ...) -> None: ... + +class DeletePowRequest(_message.Message): + __slots__ = ("string",) + STRING_FIELD_NUMBER: _ClassVar[int] + string: str + def __init__(self, string: _Optional[str] = ...) -> None: ... + +class OptionalRetrievePoWResponse(_message.Message): + __slots__ = ("result",) + RESULT_FIELD_NUMBER: _ClassVar[int] + result: RetrievePowResponse + def __init__(self, result: _Optional[_Union[RetrievePowResponse, _Mapping]] = ...) -> None: ... diff --git a/dcache_py/dcache_pb2_grpc.py b/dcache_py/dcache_pb2_grpc.py index 3ad2977..c6f2df4 100644 --- a/dcache_py/dcache_pb2_grpc.py +++ b/dcache_py/dcache_pb2_grpc.py @@ -39,11 +39,41 @@ class DcacheServiceStub(object): request_serializer=dcache__pb2.CachePowRequest.SerializeToString, response_deserializer=dcache__pb2.RaftReply.FromString, ) + self.RetrievePow = channel.unary_unary( + '/dcache.DcacheService/RetrievePow', + request_serializer=dcache__pb2.RetrievePowRequest.SerializeToString, + response_deserializer=dcache__pb2.OptionalRetrievePoWResponse.FromString, + ) + self.DeletePow = channel.unary_unary( + '/dcache.DcacheService/DeletePow', + request_serializer=dcache__pb2.DeletePowRequest.SerializeToString, + response_deserializer=dcache__pb2.RaftReply.FromString, + ) self.CacheResult = channel.unary_unary( '/dcache.DcacheService/CacheResult', request_serializer=dcache__pb2.CacheResultRequest.SerializeToString, response_deserializer=dcache__pb2.RaftReply.FromString, ) + self.VerifyCaptchaResult = channel.unary_unary( + '/dcache.DcacheService/VerifyCaptchaResult', + request_serializer=dcache__pb2.RetrievePowRequest.SerializeToString, + response_deserializer=dcache__pb2.CaptchaResultVerified.FromString, + ) + self.DeleteCaptchaResult = channel.unary_unary( + '/dcache.DcacheService/DeleteCaptchaResult', + request_serializer=dcache__pb2.DeleteCaptchaResultRequest.SerializeToString, + response_deserializer=dcache__pb2.RaftReply.FromString, + ) + self.CaptchaExists = channel.unary_unary( + '/dcache.DcacheService/CaptchaExists', + request_serializer=dcache__pb2.CaptchaID.SerializeToString, + response_deserializer=dcache__pb2.CaptchaExistsResponse.FromString, + ) + self.GetVisitorCount = channel.unary_unary( + '/dcache.DcacheService/GetVisitorCount', + request_serializer=dcache__pb2.CaptchaID.SerializeToString, + response_deserializer=dcache__pb2.OptionGetVisitorCountResponse.FromString, + ) self.PipelineDcacheOps = channel.unary_unary( '/dcache.DcacheService/PipelineDcacheOps', request_serializer=dcache__pb2.DcacheBatchRequest.SerializeToString, @@ -114,12 +144,48 @@ class DcacheServiceServicer(object): context.set_details('Method not implemented!') raise NotImplementedError('Method not implemented!') + def RetrievePow(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 DeletePow(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 CacheResult(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 VerifyCaptchaResult(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 DeleteCaptchaResult(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 CaptchaExists(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 GetVisitorCount(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 PipelineDcacheOps(self, request, context): """Missing associated documentation comment in .proto file.""" context.set_code(grpc.StatusCode.UNIMPLEMENTED) @@ -193,11 +259,41 @@ def add_DcacheServiceServicer_to_server(servicer, server): request_deserializer=dcache__pb2.CachePowRequest.FromString, response_serializer=dcache__pb2.RaftReply.SerializeToString, ), + 'RetrievePow': grpc.unary_unary_rpc_method_handler( + servicer.RetrievePow, + request_deserializer=dcache__pb2.RetrievePowRequest.FromString, + response_serializer=dcache__pb2.OptionalRetrievePoWResponse.SerializeToString, + ), + 'DeletePow': grpc.unary_unary_rpc_method_handler( + servicer.DeletePow, + request_deserializer=dcache__pb2.DeletePowRequest.FromString, + response_serializer=dcache__pb2.RaftReply.SerializeToString, + ), 'CacheResult': grpc.unary_unary_rpc_method_handler( servicer.CacheResult, request_deserializer=dcache__pb2.CacheResultRequest.FromString, response_serializer=dcache__pb2.RaftReply.SerializeToString, ), + 'VerifyCaptchaResult': grpc.unary_unary_rpc_method_handler( + servicer.VerifyCaptchaResult, + request_deserializer=dcache__pb2.RetrievePowRequest.FromString, + response_serializer=dcache__pb2.CaptchaResultVerified.SerializeToString, + ), + 'DeleteCaptchaResult': grpc.unary_unary_rpc_method_handler( + servicer.DeleteCaptchaResult, + request_deserializer=dcache__pb2.DeleteCaptchaResultRequest.FromString, + response_serializer=dcache__pb2.RaftReply.SerializeToString, + ), + 'CaptchaExists': grpc.unary_unary_rpc_method_handler( + servicer.CaptchaExists, + request_deserializer=dcache__pb2.CaptchaID.FromString, + response_serializer=dcache__pb2.CaptchaExistsResponse.SerializeToString, + ), + 'GetVisitorCount': grpc.unary_unary_rpc_method_handler( + servicer.GetVisitorCount, + request_deserializer=dcache__pb2.CaptchaID.FromString, + response_serializer=dcache__pb2.OptionGetVisitorCountResponse.SerializeToString, + ), 'PipelineDcacheOps': grpc.unary_unary_rpc_method_handler( servicer.PipelineDcacheOps, request_deserializer=dcache__pb2.DcacheBatchRequest.FromString, @@ -328,6 +424,40 @@ class DcacheService(object): options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) + @staticmethod + def RetrievePow(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, '/dcache.DcacheService/RetrievePow', + dcache__pb2.RetrievePowRequest.SerializeToString, + dcache__pb2.OptionalRetrievePoWResponse.FromString, + options, channel_credentials, + insecure, call_credentials, compression, wait_for_ready, timeout, metadata) + + @staticmethod + def DeletePow(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, '/dcache.DcacheService/DeletePow', + dcache__pb2.DeletePowRequest.SerializeToString, + dcache__pb2.RaftReply.FromString, + options, channel_credentials, + insecure, call_credentials, compression, wait_for_ready, timeout, metadata) + @staticmethod def CacheResult(request, target, @@ -345,6 +475,74 @@ class DcacheService(object): options, channel_credentials, insecure, call_credentials, compression, wait_for_ready, timeout, metadata) + @staticmethod + def VerifyCaptchaResult(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, '/dcache.DcacheService/VerifyCaptchaResult', + dcache__pb2.RetrievePowRequest.SerializeToString, + dcache__pb2.CaptchaResultVerified.FromString, + options, channel_credentials, + insecure, call_credentials, compression, wait_for_ready, timeout, metadata) + + @staticmethod + def DeleteCaptchaResult(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, '/dcache.DcacheService/DeleteCaptchaResult', + dcache__pb2.DeleteCaptchaResultRequest.SerializeToString, + dcache__pb2.RaftReply.FromString, + options, channel_credentials, + insecure, call_credentials, compression, wait_for_ready, timeout, metadata) + + @staticmethod + def CaptchaExists(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, '/dcache.DcacheService/CaptchaExists', + dcache__pb2.CaptchaID.SerializeToString, + dcache__pb2.CaptchaExistsResponse.FromString, + options, channel_credentials, + insecure, call_credentials, compression, wait_for_ready, timeout, metadata) + + @staticmethod + def GetVisitorCount(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, '/dcache.DcacheService/GetVisitorCount', + dcache__pb2.CaptchaID.SerializeToString, + dcache__pb2.OptionGetVisitorCountResponse.FromString, + options, channel_credentials, + insecure, call_credentials, compression, wait_for_ready, timeout, metadata) + @staticmethod def PipelineDcacheOps(request, target, diff --git a/proto/dcache/dcache.proto b/proto/dcache/dcache.proto index 0aa1856..1830a1b 100644 --- a/proto/dcache/dcache.proto +++ b/proto/dcache/dcache.proto @@ -77,6 +77,20 @@ message Learner { } +message CaptchaExistsResponse { + bool exists = 1; +} + + +message GetVisitorCountResponse { + uint32 visitors = 1; +} + + +message OptionGetVisitorCountResponse { + optional GetVisitorCountResponse result = 1; +} + message DcacheRequest { oneof DcacheRequest { AddCaptchaRequest addCaptcha = 1; @@ -85,6 +99,8 @@ message DcacheRequest { CaptchaID removeCaptcha = 4; CachePowRequest cachePow = 5; CacheResultRequest cacheResult = 6; + CaptchaID captchaExists = 7; + CaptchaID getVisitorCount = 8; } } @@ -93,6 +109,8 @@ message DcacheResponse { oneof DcacheResponse { OptionAddVisitorResult option_add_visitor_result = 1; RaftReply other = 2; + CaptchaExistsResponse captcha_exists = 3; + OptionGetVisitorCountResponse get_visitor_count = 4; } } @@ -104,13 +122,43 @@ message DcacheBatchResponse { repeated DcacheResponse responses = 1; } +message RetrievePowRequest { + string token = 1; + string key = 2; +} + +message RetrievePowResponse { + uint32 difficulty_factor = 1; + uint64 duration = 2; + string key = 3; +} + + +message CaptchaResultVerified { + bool verified = 1; +} + +message DeletePowRequest { + string string = 1; +} + +message OptionalRetrievePoWResponse { + optional RetrievePowResponse result = 1; +} + service DcacheService { rpc AddCaptcha(AddCaptchaRequest) returns (RaftReply) {} rpc AddVisitor(CaptchaID) returns (OptionAddVisitorResult) {} rpc RenameCaptcha(RenameCaptchaRequest) returns (RaftReply) {} rpc RemoveCaptcha(CaptchaID) returns (RaftReply) {} rpc CachePow(CachePowRequest) returns (RaftReply) {} + rpc RetrievePow(RetrievePowRequest) returns (OptionalRetrievePoWResponse) {} + rpc DeletePow(DeletePowRequest) returns (RaftReply) {} rpc CacheResult(CacheResultRequest) returns (RaftReply) {} + rpc VerifyCaptchaResult(RetrievePowRequest) returns (CaptchaResultVerified) {} + rpc DeleteCaptchaResult(DeleteCaptchaResultRequest) returns (RaftReply) {} + rpc CaptchaExists(CaptchaID) returns (CaptchaExistsResponse) {} + rpc GetVisitorCount(CaptchaID) returns (OptionGetVisitorCountResponse) {} rpc PipelineDcacheOps(DcacheBatchRequest) returns (DcacheBatchResponse) {} diff --git a/src/lib.rs b/src/lib.rs index da2d345..f80ef1d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -119,29 +119,6 @@ pub async fn start_example_raft_node( raft.enable_heartbeat(true); raft.enable_elect(true); - let captcha = serde_json::json!({ - "AddCaptcha": { - "id": "test_1", - "mcaptcha": { - "visitor_threshold": 0, - "defense": { - "levels": [ - {"visitor_threshold": 50, "difficulty_factor": 500}, - {"visitor_threshold": 5000, "difficulty_factor": 50000}, - ], - "current_visitor_threshold": 0, - }, - "duration": 30, - }}}); - #[derive(serde::Serialize)] - struct X { - data: String, - } - let x = X { - data: serde_json::to_string(&captcha).unwrap(), - }; - println!("{}", serde_json::to_string(&x).unwrap()); - // raft.enable_tick(true); // Create an application that will store all the instances created above, this will diff --git a/src/mcaptcha/cache.rs b/src/mcaptcha/cache.rs index cc11cbf..3f42e0a 100644 --- a/src/mcaptcha/cache.rs +++ b/src/mcaptcha/cache.rs @@ -42,8 +42,12 @@ impl HashCache { duration: config.duration, }; - self.difficulty_map.insert(challenge, config); - Ok(()) + if self.difficulty_map.get(&challenge).is_none() { + self.difficulty_map.insert(challenge, config); + Ok(()) + } else { + Err(CaptchaError::InvalidPoW) + } } pub async fn clean_all_after_cold_start(&self, updated: HashCache) { @@ -83,15 +87,12 @@ impl HashCache { tokio::spawn(fut); } - // retrive [PoWConfig] from cache. Deletes config post retrival - pub fn retrive_pow_config( - &self, - msg: VerifyCaptchaResult, - ) -> CaptchaResult> { + // retrieve [PoWConfig] from cache. Deletes config post retrival + pub fn retrieve_pow_config(&self, msg: VerifyCaptchaResult) -> Option { if let Some(difficulty_factor) = self.remove_pow_config(&msg.token) { - Ok(Some(difficulty_factor)) + Some(difficulty_factor) } else { - Ok(None) + None } } @@ -106,15 +107,15 @@ impl HashCache { } // verify captcha result - pub fn verify_captcha_result(&self, challenge: VerifyCaptchaResult) -> CaptchaResult { + pub fn verify_captcha_result(&self, challenge: VerifyCaptchaResult) -> bool { if let Some(captcha_id) = self.remove_cache_result(&challenge.token) { if captcha_id == challenge.key { - Ok(true) + true } else { - Ok(false) + false } } else { - Ok(false) + false } } @@ -209,16 +210,13 @@ mod tests { token: string.clone(), key: KEY.into(), }; - let cache_difficulty_factor = cache.retrive_pow_config(msg.clone()).unwrap(); - let new_cache_difficulty_factor = new_cache.retrive_pow_config(msg.clone()).unwrap(); + let cache_difficulty_factor = cache.retrieve_pow_config(msg.clone()).unwrap(); + let new_cache_difficulty_factor = new_cache.retrieve_pow_config(msg.clone()).unwrap(); + assert_eq!(DIFFICULTY_FACTOR, cache_difficulty_factor.difficulty_factor); assert_eq!( DIFFICULTY_FACTOR, - cache_difficulty_factor.unwrap().difficulty_factor - ); - assert_eq!( - DIFFICULTY_FACTOR, - new_cache_difficulty_factor.unwrap().difficulty_factor + new_cache_difficulty_factor.difficulty_factor ); let verify_msg = VerifyCaptchaResult { @@ -226,23 +224,23 @@ mod tests { token: RES.into(), }; - assert!(new_cache.verify_captcha_result(verify_msg.clone()).unwrap()); - assert!(!new_cache.verify_captcha_result(verify_msg.clone()).unwrap()); + assert!(new_cache.verify_captcha_result(verify_msg.clone())); + assert!(!new_cache.verify_captcha_result(verify_msg.clone())); let duration: Duration = Duration::new(5, 0); //sleep(DURATION + DURATION).await; tokio::time::sleep(duration + duration).await; - let expired_string = cache.retrive_pow_config(msg.clone()).unwrap(); + let expired_string = cache.retrieve_pow_config(msg.clone()); assert_eq!(None, expired_string); - let expired_string = new_cache.retrive_pow_config(msg).unwrap(); + let expired_string = new_cache.retrieve_pow_config(msg); assert_eq!(None, expired_string); cache.cache_result(add_cache); new_cache.clean_all_after_cold_start(cache.clone()).await; tokio::time::sleep(duration + duration).await; - assert!(!new_cache.verify_captcha_result(verify_msg.clone()).unwrap()); - assert!(!cache.verify_captcha_result(verify_msg).unwrap()); + assert!(!new_cache.verify_captcha_result(verify_msg.clone())); + assert!(!cache.verify_captcha_result(verify_msg)); } #[actix_rt::test] @@ -272,18 +270,15 @@ mod tests { token: string.clone(), key: KEY.into(), }; - let cache_difficulty_factor = cache.retrive_pow_config(msg.clone()).unwrap(); + let cache_difficulty_factor = cache.retrieve_pow_config(msg.clone()).unwrap(); - assert_eq!( - DIFFICULTY_FACTOR, - cache_difficulty_factor.unwrap().difficulty_factor - ); + assert_eq!(DIFFICULTY_FACTOR, cache_difficulty_factor.difficulty_factor); let duration: Duration = Duration::new(5, 0); //sleep(DURATION + DURATION).await; tokio::time::sleep(duration + duration).await; - let expired_string = cache.retrive_pow_config(msg).unwrap(); + let expired_string = cache.retrieve_pow_config(msg); assert_eq!(None, expired_string); } @@ -312,16 +307,16 @@ mod tests { token: RES.into(), }; - assert!(cache.verify_captcha_result(verify_msg.clone()).unwrap()); + assert!(cache.verify_captcha_result(verify_msg.clone())); // duplicate - assert!(!cache.verify_captcha_result(verify_msg).unwrap()); + assert!(!cache.verify_captcha_result(verify_msg)); let verify_msg = VerifyCaptchaResult { key: "cz".into(), token: RES.into(), }; - assert!(!cache.verify_captcha_result(verify_msg).unwrap()); + assert!(!cache.verify_captcha_result(verify_msg)); let duration: Duration = Duration::new(5, 0); tokio::time::sleep(duration + duration).await; @@ -330,6 +325,6 @@ mod tests { key: KEY.into(), token: RES.into(), }; - assert!(!cache.verify_captcha_result(verify_msg).unwrap()); + assert!(!cache.verify_captcha_result(verify_msg)); } } diff --git a/src/mcaptcha/mcaptcha.rs b/src/mcaptcha/mcaptcha.rs index 816a481..d67a563 100644 --- a/src/mcaptcha/mcaptcha.rs +++ b/src/mcaptcha/mcaptcha.rs @@ -126,7 +126,7 @@ impl MCaptcha { /// get [Counter]'s current visitor_threshold pub fn get_visitors(&self) -> u32 { - self.visitor_threshold.load(Ordering::Relaxed) + self.visitor_threshold.load(Ordering::SeqCst) } } @@ -152,11 +152,9 @@ impl Manager { } fn gc(captchas: Arc>>) { - println!("running gc"); for captcha in captchas.iter() { let visitor = { captcha.value().get_visitors() }; if visitor == 0 { - println!("Removing captcha"); captchas.remove(captcha.key()); } } @@ -255,7 +253,6 @@ impl Manager { let msg = ManagerMessages::AddVisitor(id); for _ in 0..visitors { self.add_visitor(&msg); - println!("Adding visitor"); } } } @@ -377,7 +374,6 @@ mod tests { mcaptcha_data.get_mut(id).unwrap().add_visitor(); mcaptcha_data.get_mut(id).unwrap().add_visitor(); mcaptcha_data.get_mut(id).unwrap().add_visitor(); - println!("{:?}", mcaptcha_data); // let mcaptcha_data: HashMap = { // let serialized = serde_json::to_string(&mcaptcha_data).unwrap(); // serde_json::from_str(&serialized).unwrap() @@ -386,7 +382,6 @@ mod tests { manager.set_internal_data(mcaptcha_data); let mcaptcha_data = manager.get_internal_data(); - println!("{:?}", mcaptcha_data); assert_eq!( manager.get_captcha(id).unwrap().get_visitors(), mcaptcha_data.get(id).unwrap().get_visitors() diff --git a/src/network/raft_network_impl.rs b/src/network/raft_network_impl.rs index f8820b6..f1b6bc7 100644 --- a/src/network/raft_network_impl.rs +++ b/src/network/raft_network_impl.rs @@ -55,7 +55,6 @@ impl ItemManager for ChannelManager { type Error = tonic::transport::Error; async fn build(&self, addr: &Self::Key) -> Result { - println!("New connection for {addr}"); tonic::transport::Endpoint::new(addr.clone())? .connect() .await diff --git a/src/protobuf.rs b/src/protobuf.rs index 3a93a4f..dd2f9b0 100644 --- a/src/protobuf.rs +++ b/src/protobuf.rs @@ -129,6 +129,36 @@ impl DcacheService for MyDcacheImpl { Ok(Response::new(res.into())) } + async fn retrieve_pow( + &self, + request: tonic::Request, + ) -> std::result::Result, tonic::Status> + { + let req = request.into_inner(); + let sm = self.app.store.state_machine.read().await; + + let res = sm.results.retrieve_pow_config(req.into()); + + Ok(Response::new(dcache::OptionalRetrievePoWResponse { + result: res.map(|x| x.into()), + })) + } + + async fn delete_pow( + &self, + request: tonic::Request, + ) -> std::result::Result, tonic::Status> { + let req = request.into_inner(); + let res = self + .app + .raft + .client_write(DcacheRequest::DeletePoW(CacheMessages::DeletePoW( + req.string, + ))) + .await; + Ok(Response::new(res.into())) + } + async fn cache_result( &self, request: tonic::Request, @@ -142,6 +172,62 @@ impl DcacheService for MyDcacheImpl { Ok(Response::new(res.into())) } + async fn verify_captcha_result( + &self, + request: tonic::Request, + ) -> std::result::Result, tonic::Status> { + let req = request.into_inner(); + let sm = self.app.store.state_machine.read().await; + + let verified = sm.results.verify_captcha_result(req.into()); + + Ok(Response::new(dcache::CaptchaResultVerified { verified })) + } + + async fn delete_captcha_result( + &self, + request: tonic::Request, + ) -> std::result::Result, tonic::Status> { + let req = request.into_inner(); + let res = self + .app + .raft + .client_write(DcacheRequest::DeleteCaptchaResult( + CacheMessages::DeleteCaptchaResult { token: req.token }, + )) + .await; + Ok(Response::new(res.into())) + } + + async fn captcha_exists( + &self, + request: tonic::Request, + ) -> std::result::Result, tonic::Status> { + let req = request.into_inner(); + let sm = self.app.store.state_machine.read().await; + let exists = sm.counter.get_captcha(&req.id).is_some(); + Ok(Response::new(dcache::CaptchaExistsResponse { exists })) + } + + async fn get_visitor_count( + &self, + request: tonic::Request, + ) -> std::result::Result, tonic::Status> + { + let req = request.into_inner(); + let sm = self.app.store.state_machine.read().await; + if let Some(captcha) = sm.counter.get_captcha(&req.id) { + let res = captcha.get_visitors(); + Ok(Response::new(dcache::OptionGetVisitorCountResponse { + result: Some(dcache::GetVisitorCountResponse { visitors: res }), + })) + } else { + Ok(Response::new(dcache::OptionGetVisitorCountResponse { + result: None, + })) + } + } + // type PipelineDcacheOpsStream = // Pin> + Send + 'static>>; @@ -238,6 +324,35 @@ impl DcacheService for MyDcacheImpl { dcache_response: Some(InnerPipelineRes::Other(res.into())), } } + PipelineReq::CaptchaExists(captcha_exists_req) => { + let sm = self.app.store.state_machine.read().await; + let exists = sm.counter.get_captcha(&captcha_exists_req.id).is_some(); + let res = dcache::CaptchaExistsResponse { exists }; + drop(sm); + OuterPipelineRes { + dcache_response: Some(InnerPipelineRes::CaptchaExists(res)), + } + } + + PipelineReq::GetVisitorCount(get_visitor_count_req) => { + let sm = self.app.store.state_machine.read().await; + if let Some(captcha) = sm.counter.get_captcha(&get_visitor_count_req.id) { + let res = captcha.get_visitors(); + OuterPipelineRes { + dcache_response: Some(InnerPipelineRes::GetVisitorCount( + dcache::OptionGetVisitorCountResponse { + result: Some(dcache::GetVisitorCountResponse { visitors: res }), + }, + )), + } + } else { + OuterPipelineRes { + dcache_response: Some(InnerPipelineRes::GetVisitorCount( + dcache::OptionGetVisitorCountResponse { result: None }, + )), + } + } + } }; responses.push(res); } @@ -390,6 +505,27 @@ impl From for CacheMessages::CachePoW { } } +impl From for dcache::CachePowRequest { + fn from(value: CacheMessages::CachePoW) -> Self { + Self { + string: value.string, + difficulty_factor: value.difficulty_factor, + duration: value.duration, + key: value.key, + } + } +} + +impl From for dcache::RetrievePowResponse { + fn from(value: CacheMessages::CachedPoWConfig) -> Self { + Self { + difficulty_factor: value.difficulty_factor, + duration: value.duration, + key: value.key, + } + } +} + impl From for CacheMessages::CacheResult { fn from(value: dcache::CacheResultRequest) -> Self { Self { @@ -399,3 +535,12 @@ impl From for CacheMessages::CacheResult { } } } + +impl From for CacheMessages::VerifyCaptchaResult { + fn from(value: dcache::RetrievePowRequest) -> Self { + Self { + token: value.token, + key: value.key, + } + } +}