Skip to content

Commit

Permalink
Support for start and end indexes and counts for Notifications. (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeairheller authored Aug 21, 2023
1 parent 41d842d commit a8773f7
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 65 deletions.
1 change: 0 additions & 1 deletion scripts/demo/credentials/single-issuer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ kli oobi resolve --name holder --oobi-alias holder --oobi http://127.0.0.1:7723/
kli vc registry incept --name issuer --alias issuer --registry-name vLEI

kli vc issue --name issuer --alias issuer --registry-name vLEI --schema EBfdlu8R27Fbx-ehrqwImnK-8Cm79sqbAQ4MmvEAYqao --recipient ELjSFdrTdCebJlmvbFNX9-TLhR2PO0_60al1kQp5_e6k --data @${KERI_DEMO_SCRIPT_DIR}/data/credential-data.json
#sleep 2
kli vc accept --name holder --alias holder --poll --auto

kli vc list --name holder --alias holder
Expand Down
2 changes: 0 additions & 2 deletions src/keri/app/kiwiing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3102,8 +3102,6 @@ def setup(hby, rgy, servery, bootConfig, *, controller="", insecure=False, stati
signaler = signaling.Signaler()
notifier = notifying.Notifier(hby=hby, signaler=signaler)
verifier = verifying.Verifier(hby=hby, reger=rgy.reger)
wallet = walleting.Wallet(reger=verifier.reger, name=hby.name)

handlers = []

mbx = storing.Mailboxer(name=hby.name)
Expand Down
80 changes: 38 additions & 42 deletions src/keri/app/notifying.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ def getItemIter(self, keys: Union[str, Iterable] = b""):
for key, val in self.db.getAllItemIter(db=self.sdb, key=self._tokey(keys), split=False):
yield self._tokeys(key), self.klas(raw=bytes(val))

def cntAll(self):
"""
Return count over the all the items in subdb
Returns:
count of all items
"""
return self.db.cnt(db=self.sdb)


class Noter(dbing.LMDBer):
"""
Expand Down Expand Up @@ -305,44 +314,42 @@ def rem(self, rid):
self.ncigs.rem(keys=(rid,))
return self.notes.rem(keys=(dt, rid))

def getNoteIter(self, start="", limit=25):
def getNoteCnt(self):
"""
Returns iterator of tuples (note, cigar) of notices for controller of agent with attached signatures.
Return count over the all Notes
Parameters:
start (Optiona(str,datetime)): date/time to start iteration
limit (int): number of items to return
Returns:
int: count of all items
"""
if hasattr(start, "isoformat"):
start = start.isoformat()
return self.notes.cntAll()

res = 0
for ((_, _), note) in self.notes.getItemIter(keys=(start,)):
cig = self.ncigs.get(keys=(note.rid,))
yield note, cig
res += 1
if res == limit:
break

def getNotes(self, start="", limit=25):
def getNotes(self, start=0, end=25):
"""
Returns list of tuples (note, cigar) of notes for controller of agent
Parameters:
start (Optiona(str,datetime)): date/time to start iteration
limit (int): number of items to return
start (int): number of item to start
end (int): number of last item to return
"""
if hasattr(start, "isoformat"):
start = start.isoformat()

notes = []
it = self.notes.getItemIter(keys=())

# Run off the items before start
for _ in range(start):
try:
next(it)
except StopIteration:
break

for ((_, _), note) in self.notes.getItemIter(keys=(start,)):
for ((_, _), note) in it:
cig = self.ncigs.get(keys=(note.rid,))
notes.append((note, cig))
if len(notes) == limit:
if (not end == -1) and len(notes) == (end - start) + 1:
break

return notes
Expand Down Expand Up @@ -462,39 +469,28 @@ def mar(self, rid):

return False

def getNoteIter(self, start=None, limit=25):
def getNoteCnt(self):
"""
Returns iterator of notices that have verified signatures over the data stored
Return count over the all Notes
Parameters:
start (Optiona(str,datetime)): date/time to start iteration
limit (int): number of items to return
Returns:
int: count of all items
"""
return self.noter.getNoteCnt()

for note, cig in self.noter.getNoteIter(start=start, limit=limit):
if not self.hby.signator.verify(ser=note.raw, cigar=cig):
raise kering.ValidationError("note stored without valid signature")

yield note

def getNotes(self, start="", limit=25):
def getNotes(self, start=0, end=24):
"""
Returns list of notices that have verified signatures over the data stored
Returns list of tuples (note, cigar) of notes for controller of agent
Parameters:
start (Optiona(str,datetime)): date/time to start iteration
limit (int): number of items to return
start (int): number of item to start
end (int): number of last item to return
"""
if hasattr(start, "isoformat"):
start = start.isoformat()

notesigs = self.noter.getNotes(start, end)
notes = []

for note, cig in self.noter.getNoteIter(start=start, limit=limit):
for note, cig in notesigs:
if not self.hby.signator.verify(ser=note.raw, cigar=cig):
raise kering.ValidationError("note stored without valid signature")

Expand Down
6 changes: 3 additions & 3 deletions tests/app/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def createMbxEndRole(hab, cid, eid, url):
hab.db.locs.pin(keys=lockeys, val=httplocer) # overwrite


class TestDoer(doing.DoDoer):
class RunTestDoer(doing.DoDoer):

def __init__(self, wanHby, hby1, hab1, hby2, hab2, hby3, hab3, recp):
self.hab1 = hab1
Expand Down Expand Up @@ -131,7 +131,7 @@ def __init__(self, wanHby, hby1, hab1, hby2, hab2, hby3, hab3, recp):
self.toRemove = list(doers)
doers.extend([doing.doify(self.testDo)])

super(TestDoer, self).__init__(doers=doers)
super(RunTestDoer, self).__init__(doers=doers)

def escrowDo(self, tymth, tock=0.0):
self.wind(tymth)
Expand Down Expand Up @@ -235,7 +235,7 @@ def test_multisig_issue_agent():
habbing.openHby(name="wan", salt=salt, temp=True) as wanHby, \
habbing.openHab(name="recp", transferable=True) as (_, recp):

testDoer = TestDoer(wanHby, hby1, hab1, hby2, hab2, hby3, hab3, recp)
testDoer = RunTestDoer(wanHby, hby1, hab1, hby2, hab2, hby3, hab3, recp)

# Neuter this test for now, it will be moved to KERIA
assert testDoer.done is None
1 change: 0 additions & 1 deletion tests/app/test_delegating.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import time
from hio.base import doing, tyming

import keri.app.oobiing
from keri import kering
from keri.app import habbing, delegating, indirecting, agenting, notifying
from keri.core import eventing, parsing, coring
Expand Down
10 changes: 5 additions & 5 deletions tests/app/test_multisig.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
from falcon import testing
from hio.base import doing
from keri import kering
from keri.app import (habbing, storing, kiwiing, grouping, indirecting,
directing, agenting, booting, notifying)
from keri.app import (habbing, kiwiing, grouping, indirecting,
agenting, booting, notifying)
from keri.core import coring, eventing, parsing
from keri.vdr import credentialing

TEST_DIR = os.path.dirname(os.path.abspath(__file__))


class TestDoer(doing.DoDoer):
class RunTestDoer(doing.DoDoer):

def __init__(self, wanHby, hby1, hab1, hby2, hab2, seeder):
self.hby1 = hby1
Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(self, wanHby, hby1, hab1, hby2, hab2, seeder):
self.toRemove = list(doers)
doers.extend([doing.doify(self.testDo)])

super(TestDoer, self).__init__(doers=doers)
super(RunTestDoer, self).__init__(doers=doers)

def testDo(self, tymth, tock=0.0):
self.wind(tymth)
Expand Down Expand Up @@ -158,7 +158,7 @@ def test_multisig_identifier_ends(seeder):
with habbing.openHab(name="multisig1", temp=True, wits=[wanPre]) as (hby1, hab1), \
habbing.openHab(name="multisig2", temp=True, wits=[wanPre]) as (hby2, hab2), \
habbing.openHby(name="wan", salt=salt, temp=True) as wanHby:
testDoer = TestDoer(wanHby, hby1, hab1, hby2, hab2, seeder)
testDoer = RunTestDoer(wanHby, hby1, hab1, hby2, hab2, seeder)

# Neuter this test for now, it will be moved to KERIA
assert testDoer.done is None
Expand Down
19 changes: 8 additions & 11 deletions tests/app/test_notifying.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ def test_noter():
note = notifying.notice(payload, dt=dt)
assert noter.add(note, cig) is True

notes = noter.getNotes(start="")
notes = noter.getNotes(start=0)
assert len(notes) == 1
note1, cig1 = notes[0]
assert note1.rid == note.rid
assert cig1.qb64 == cig.qb64

notes = noter.getNotes(start=dt)
notes = noter.getNotes(start=0)
assert len(notes) == 1
note2, cig2 = notes[0]
assert note2.rid == note.rid
assert cig2.qb64 == cig.qb64

assert noter.rem("ABC") is False
assert noter.rem(note.rid) is True
notes = noter.getNotes(start="")
notes = noter.getNotes(start=0)
assert len(notes) == 0

dt = datetime.datetime.now()
Expand All @@ -146,7 +146,7 @@ def test_noter():
assert noter.add(note, cig) is True

res = []
for note in noter.getNoteIter(start=dt):
for note in noter.getNotes(start=0):
res.append(note)

assert len(res) == 3
Expand All @@ -160,11 +160,14 @@ def test_noter():
assert noter.add(note, cig) is True

res = []
for note in noter.getNoteIter(limit=5):
for note in noter.getNotes(end=4):
res.append(note)

assert len(res) == 5

cnt = noter.getNoteCnt()
assert cnt == 13


def test_notifier():
with habbing.openHby(name="test") as hby:
Expand Down Expand Up @@ -205,12 +208,6 @@ def test_notifier():
notes = notifier.getNotes()
assert len(notes) == 3

res = []
for note in notifier.getNoteIter(start=dt):
res.append(note)

assert len(res) == 3

payload = dict(a=1, b=2, c=3)
dt = helping.fromIso8601("2022-07-08T15:01:05.453632")
cig = coring.Cigar(qb64="AABr1EJXI1sTuI51TXo4F1JjxIJzwPeCxa-Cfbboi7F4Y4GatPEvK629M7G_5c86_Ssvwg8POZWNMV-WreVqBECw")
Expand Down

0 comments on commit a8773f7

Please sign in to comment.