Skip to content

Commit

Permalink
Make agent doers' tocks configurable from habitat config file (WebOfT…
Browse files Browse the repository at this point in the history
…rust#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 <[email protected]>
Co-authored-by: Philip Feairheller <[email protected]>
  • Loading branch information
3 people authored and rodolfomiranda committed Jul 11, 2024
1 parent ee1c714 commit a076dc4
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 41 deletions.
96 changes: 56 additions & 40 deletions src/keria/app/agenting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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 """
Expand All @@ -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 """
Expand All @@ -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"""
Expand All @@ -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:
Expand All @@ -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)

Expand Down
48 changes: 48 additions & 0 deletions tests/app/test_agenting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os
import shutil

import pytest

import falcon
import hio
from falcon import testing
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion tests/app/test_delegating.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/scripts/keri/cf/main/keria.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit a076dc4

Please sign in to comment.