From c83904d62b908e5f030bbf7f957d397cd259035f Mon Sep 17 00:00:00 2001 From: Rubel Hassan Date: Thu, 11 Jul 2024 10:48:16 -0500 Subject: [PATCH] Make agent doers' tocks configurable from habitat config file (#265) * Make agent doers' tocks configurable from habitat config file * Fix typo of naming in test_load_tocks_config --------- Co-authored-by: Rubel Hassan Mollik Co-authored-by: Philip Feairheller --- src/keria/app/agenting.py | 96 ++++++++++++++++----------- tests/app/test_agenting.py | 48 ++++++++++++++ tests/app/test_delegating.py | 7 +- tests/scripts/keri/cf/main/keria.json | 4 ++ 4 files changed, 114 insertions(+), 41 deletions(-) diff --git a/src/keria/app/agenting.py b/src/keria/app/agenting.py index bb02f4df..da140056 100644 --- a/src/keria/app/agenting.py +++ b/src/keria/app/agenting.py @@ -8,6 +8,7 @@ import os from dataclasses import asdict from urllib.parse import urlparse, urljoin +from types import MappingProxyType import falcon from falcon import media @@ -289,6 +290,8 @@ def __init__(self, hby, rgy, agentHab, agency, caid, **opts): self.agentHab = agentHab self.agency = agency self.caid = caid + self.cfd = MappingProxyType(dict(self.hby.cf.get()) if self.hby.cf is not None else dict()) + self.tocks = MappingProxyType(self.cfd.get("tocks", {})) self.swain = delegating.Anchorer(hby=hby, proxy=agentHab) self.counselor = Counselor(hby=hby, swain=self.swain, proxy=agentHab) @@ -364,19 +367,25 @@ def __init__(self, hby, rgy, agentHab, agency, caid, **opts): local=True) # disable misfit escrow until we can add another parser for remote. doers.extend([ - Initer(agentHab=agentHab, caid=caid), - Querier(hby=hby, agentHab=agentHab, kvy=self.kvy, queries=self.queries), + Initer(agentHab=agentHab, caid=caid, tock=self.tocks.get("initer", 0.0)), + Querier(hby=hby, agentHab=agentHab, kvy=self.kvy, queries=self.queries, + tock=self.tocks.get("querier", 0.0)), Escrower(kvy=self.kvy, rgy=self.rgy, rvy=self.rvy, tvy=self.tvy, exc=self.exc, vry=self.verifier, - registrar=self.registrar, credentialer=self.credentialer), - ParserDoer(kvy=self.kvy, parser=self.parser), - Witnesser(receiptor=receiptor, witners=self.witners), - Delegator(agentHab=agentHab, swain=self.swain, anchors=self.anchors), - ExchangeSender(hby=hby, agentHab=agentHab, exc=self.exc, exchanges=self.exchanges), - Granter(hby=hby, rgy=rgy, agentHab=agentHab, exc=self.exc, grants=self.grants), - Admitter(hby=hby, witq=self.witq, psr=self.parser, agentHab=agentHab, exc=self.exc, admits=self.admits), - GroupRequester(hby=hby, agentHab=agentHab, counselor=self.counselor, groups=self.groups), - SeekerDoer(seeker=self.seeker, cues=self.verifier.cues), - ExchangeCueDoer(seeker=self.exnseeker, cues=self.exc.cues, queries=self.queries) + registrar=self.registrar, credentialer=self.credentialer, tock=self.tocks.get("escrower", 0.0)), + ParserDoer(kvy=self.kvy, parser=self.parser, tock=self.tocks.get("parser", 0.0)), + Witnesser(receiptor=receiptor, witners=self.witners, tock=self.tocks.get("witnesser", 0.0)), + Delegator(agentHab=agentHab, swain=self.swain, anchors=self.anchors, tock=self.tocks.get("delegator", 0.0)), + ExchangeSender(hby=hby, agentHab=agentHab, exc=self.exc, exchanges=self.exchanges, + tock=self.tocks.get("exchangeSender", 0.0)), + Granter(hby=hby, rgy=rgy, agentHab=agentHab, exc=self.exc, grants=self.grants, + tock=self.tocks.get("granter", 0.0)), + Admitter(hby=hby, witq=self.witq, psr=self.parser, agentHab=agentHab, exc=self.exc, admits=self.admits, + tock=self.tocks.get("admitter", 0.0)), + GroupRequester(hby=hby, agentHab=agentHab, counselor=self.counselor, groups=self.groups, + tock=self.tocks.get("groupRequester", 0.0)), + SeekerDoer(seeker=self.seeker, cues=self.verifier.cues, tock=self.tocks.get("seeker", 0.0)), + ExchangeCueDoer(seeker=self.exnseeker, cues=self.exc.cues, queries=self.queries, + tock=self.tocks.get("exchangecue", 0.0)), ]) super(Agent, self).__init__(doers=doers, always=True, **opts) @@ -412,10 +421,11 @@ def inceptExtern(self, pre, verfers, digers, **kwargs): class ParserDoer(doing.Doer): - def __init__(self, kvy, parser): + def __init__(self, kvy, parser, tock=0.0): self.kvy = kvy self.parser = parser - super(ParserDoer, self).__init__() + self.tock = tock + super(ParserDoer, self).__init__(tock=self.tock) def recur(self, tyme=None): if self.parser.ims: @@ -426,10 +436,11 @@ def recur(self, tyme=None): class Witnesser(doing.Doer): - def __init__(self, receiptor, witners): + def __init__(self, receiptor, witners, tock=0.0): self.receiptor = receiptor self.witners = witners - super(Witnesser, self).__init__() + self.tock = tock + super(Witnesser, self).__init__(tock=self.tock) def recur(self, tyme=None): while True: @@ -450,11 +461,12 @@ def recur(self, tyme=None): class Delegator(doing.Doer): - def __init__(self, agentHab, swain, anchors): + def __init__(self, agentHab, swain, anchors, tock=0.0): self.agentHab = agentHab self.swain = swain self.anchors = anchors - super(Delegator, self).__init__() + self.tock = tock + super(Delegator, self).__init__(tock=self.tock) def recur(self, tyme=None): if self.anchors: @@ -467,12 +479,13 @@ def recur(self, tyme=None): class ExchangeSender(doing.DoDoer): - def __init__(self, hby, agentHab, exc, exchanges): + def __init__(self, hby, agentHab, exc, exchanges, tock=0.0): self.hby = hby self.agentHab = agentHab self.exc = exc self.exchanges = exchanges - super(ExchangeSender, self).__init__(always=True) + self.tock = tock + super(ExchangeSender, self).__init__(always=True, tock=self.tock) def recur(self, tyme, deeds=None): if self.exchanges: @@ -507,13 +520,14 @@ def recur(self, tyme, deeds=None): class Granter(doing.DoDoer): - def __init__(self, hby, rgy, agentHab, exc, grants): + def __init__(self, hby, rgy, agentHab, exc, grants, tock=0.0): self.hby = hby self.rgy = rgy self.agentHab = agentHab self.exc = exc self.grants = grants - super(Granter, self).__init__(always=True) + self.tock = tock + super(Granter, self).__init__(always=True, tock=self.tock) def recur(self, tyme, deeds=None): if self.grants: @@ -553,14 +567,15 @@ def recur(self, tyme, deeds=None): class Admitter(doing.Doer): - def __init__(self, hby, witq, psr, agentHab, exc, admits): + def __init__(self, hby, witq, psr, agentHab, exc, admits, tock=0.0): self.hby = hby self.agentHab = agentHab self.witq = witq self.psr = psr self.exc = exc self.admits = admits - super(Admitter, self).__init__() + self.tock = tock + super(Admitter, self).__init__(tock=self.tock) def recur(self, tyme): if self.admits: @@ -600,11 +615,11 @@ def recur(self, tyme): class SeekerDoer(doing.Doer): - def __init__(self, seeker, cues): + def __init__(self, seeker, cues, tock=0.0): self.seeker = seeker self.cues = cues - - super(SeekerDoer, self).__init__() + self.tock = tock + super(SeekerDoer, self).__init__(tock=self.tock) def recur(self, tyme=None): if self.cues: @@ -623,12 +638,12 @@ def recur(self, tyme=None): class ExchangeCueDoer(doing.Doer): - def __init__(self, seeker, cues, queries): + def __init__(self, seeker, cues, queries, tock=0.0): self.seeker = seeker self.cues = cues self.queries = queries - - super(ExchangeCueDoer, self).__init__() + self.tock = tock + super(ExchangeCueDoer, self).__init__(tock=self.tock) def recur(self, tyme=None): if self.cues: @@ -649,10 +664,11 @@ def recur(self, tyme=None): class Initer(doing.Doer): - def __init__(self, agentHab, caid): + def __init__(self, agentHab, caid, tock=0.0): self.agentHab = agentHab self.caid = caid - super(Initer, self).__init__() + self.tock = tock + super(Initer, self).__init__(tock=self.tock) def recur(self, tyme): """ Prints Agent name and prefix """ @@ -665,13 +681,13 @@ def recur(self, tyme): class GroupRequester(doing.Doer): - def __init__(self, hby, agentHab, counselor, groups): + def __init__(self, hby, agentHab, counselor, groups, tock=0.0): self.hby = hby self.agentHab = agentHab self.counselor = counselor self.groups = groups - - super(GroupRequester, self).__init__() + self.tock = tock + super(GroupRequester, self).__init__(tock=self.tock) def recur(self, tyme): """ Checks cue for group proceccing requests and processes any with Counselor """ @@ -691,13 +707,13 @@ def recur(self, tyme): class Querier(doing.DoDoer): - def __init__(self, hby, agentHab, queries, kvy): + def __init__(self, hby, agentHab, queries, kvy, tock=0.0): self.hby = hby self.agentHab = agentHab self.queries = queries self.kvy = kvy - - super(Querier, self).__init__(always=True) + self.tock = tock + super(Querier, self).__init__(always=True, tock=self.tock) def recur(self, tyme, deeds=None): """ Processes query reqests submitting any on the cue""" @@ -724,7 +740,7 @@ def recur(self, tyme, deeds=None): class Escrower(doing.Doer): - def __init__(self, kvy, rgy, rvy, tvy, exc, vry, registrar, credentialer): + def __init__(self, kvy, rgy, rvy, tvy, exc, vry, registrar, credentialer, tock=0.0): """ Recuring process or escrows for all components in an Agent Parameters: @@ -745,7 +761,7 @@ def __init__(self, kvy, rgy, rvy, tvy, exc, vry, registrar, credentialer): self.vry = vry self.registrar = registrar self.credentialer = credentialer - self.tock = 1.0 + self.tock = tock super(Escrower, self).__init__(tock=self.tock) diff --git a/tests/app/test_agenting.py b/tests/app/test_agenting.py index c93acdb9..af4d9f55 100644 --- a/tests/app/test_agenting.py +++ b/tests/app/test_agenting.py @@ -9,6 +9,8 @@ import os import shutil +import pytest + import falcon import hio from falcon import testing @@ -62,6 +64,52 @@ def test_load_ends(helpers): assert isinstance(end, agenting.QueryCollectionEnd) +def test_load_tocks_config(helpers): + with helpers.openKeria() as (agency, agent, app, client): + agenting.loadEnds(app=app) + assert app._router is not None + + assert agent.cfd == { + "dt": "2022-01-20T12:57:59.823350+00:00", + "keria": { + "dt": "2022-01-20T12:57:59.823350+00:00", + "curls": ["http://127.0.0.1:3902/"] + }, + "EK35JRNdfVkO4JwhXaSTdV4qzB_ibk_tGJmSVcY4pZqx": { + "dt": "2022-01-20T12:57:59.823350+00:00", + "curls": ["http://127.0.0.1:3902/"] + }, + "EI7AkI40M11MS7lkTCb10JC9-nDt-tXwQh44OHAFlv_9": { + "dt": "2022-01-20T12:57:59.823350+00:00", + "curls": ["http://127.0.0.1:3902/"] + }, + "tocks": { + "initer": 0.0, + "escrower": 1.0 + } + } + + assert agent.tocks == { + "initer": 0.0, + "escrower": 1.0 + } + + escrower_doer = next((doer for doer in agent.doers if isinstance(doer, agenting.Escrower)), None) + assert escrower_doer is not None + assert escrower_doer.tock == 1.0 + + initer_doer = next((doer for doer in agent.doers if isinstance(doer, agenting.Initer)), None) + assert initer_doer is not None + assert initer_doer.tock == 0.0 + + querier_doer = next((doer for doer in agent.doers if isinstance(doer, agenting.Querier)), None) + assert querier_doer is not None + assert querier_doer.tock == 0.0 + + with pytest.raises(TypeError): + agent.tocks["initer"] = 1.0 # agent.tocks is read-only + + def test_agency(): salt = b'0123456789abcdef' salter = core.Salter(raw=salt) diff --git a/tests/app/test_delegating.py b/tests/app/test_delegating.py index b5b261b8..b7c9a866 100644 --- a/tests/app/test_delegating.py +++ b/tests/app/test_delegating.py @@ -17,6 +17,7 @@ from keria.app import aiding, delegating from keria.core import longrunning from keria.end import ending +from keria.app import agenting def test_sealer(): with habbing.openHby(name="p1", temp=True) as hby: @@ -69,6 +70,10 @@ def test_delegator_end(helpers): # Create Anchorer to test anchorer = delegating.Anchorer(hby=hby) + + escrower = next((doer for doer in toragent.doers if isinstance(doer, agenting.Escrower)), None) + assert escrower is not None + dipEvtProcDelay = escrower.tock * 60 + 10 # 60 seconds * escrower.tock + 10 seconds #setup agency endpoints ending.loadEnds(app=torapp, agency=toragency) @@ -151,7 +156,7 @@ def test_delegator_end(helpers): assert res.status_code == 200 op = res.json count += 1 - if count > 60: + if count > dipEvtProcDelay: raise Exception("Delegator never processed the delegatee dip event") # Delegator escrows completed and now aknowledges the delegatee dip event diff --git a/tests/scripts/keri/cf/main/keria.json b/tests/scripts/keri/cf/main/keria.json index 93faf9c9..9ccd32fe 100755 --- a/tests/scripts/keri/cf/main/keria.json +++ b/tests/scripts/keri/cf/main/keria.json @@ -11,5 +11,9 @@ "EI7AkI40M11MS7lkTCb10JC9-nDt-tXwQh44OHAFlv_9": { "dt": "2022-01-20T12:57:59.823350+00:00", "curls": ["http://127.0.0.1:3902/"] + }, + "tocks": { + "initer": 0.0, + "escrower": 1.0 } } \ No newline at end of file