Skip to content

Commit

Permalink
refactore db.pdes to use new suber methods to make more convenient
Browse files Browse the repository at this point in the history
added unit tests
  • Loading branch information
SmithSamuelM committed Sep 10, 2024
1 parent cadfa44 commit 9e06c8f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/keri/core/eventing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3380,7 +3380,7 @@ def escrowPDEvent(self, serder, *, sigers=None, wigers=None,

logger.debug(f"Kever state: Escrowed partially delegated event=\n"
f"{serder.ked}\n.")
return self.db.pdes.add(keys=snKey(serder.preb, serder.sn), val=serder.saidb)
return self.db.pdes.addOn(keys=serder.pre, on=serder.sn, val=serder.said)


def state(self):
Expand Down Expand Up @@ -5976,7 +5976,7 @@ def processEscrowPartialDels(self):
except Exception as ex: # log diagnostics errors etc
# error other than waiting on sigs or seal so remove from escrow
# removes one event escrow at key val
self.db.pdes.rem(keys=snKey(epre, esn), val=edig) # event idx escrow
self.db.pdes.remOn(keys=epre, on=esn, val=edig) # event idx escrow
self.db.udes.rem(keys=dgkey) # remove source seal escrow if any
if logger.isEnabledFor(logging.DEBUG):
logger.exception("Kevery unescrowed: %s", ex.args[0])
Expand All @@ -5988,7 +5988,7 @@ def processEscrowPartialDels(self):
# duplicitous so we process remaining escrows in spite of found
# valid event escrow.
# removes one event escrow at key val
self.db.pdes.rem(keys=snKey(epre, esn), val=edig) # event idx escrow
self.db.pdes.remOn(keys=epre, on=esn, val=edig) # event idx escrow
self.db.udes.rem(keys=dgkey) # remove source seal escrow if any
logger.info("Kevery unescrow succeeded in valid event: "
"event=%s", eserder.said)
Expand Down
2 changes: 1 addition & 1 deletion src/keri/db/dbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,7 @@ def delOnIoDupVal(self, db, key, on=0, val=b'', sep=b'.'):
# used in OnIoDupSuber
def getOnIoDupValIter(self, db, key=b'', on=0, *, sep=b'.'):
"""
Returns iterator of triples (key, on, val), at each key over all ordinal
Returns iterator of val at each key over all ordinal
numbered keys with same key + sep + on in db. Values are sorted by
onKey(key, on) where on is ordinal number int and key is prefix sans on.
Values duplicates are sorted internally by hidden prefixed insertion order
Expand Down
22 changes: 22 additions & 0 deletions src/keri/db/subing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,28 @@ def appendOn(self, keys: str | bytes | memoryview,
val=self._ser(val),
sep=self.sep.encode()))


def getOn(self, keys: str | bytes | memoryview | Iterable, on: int = 0):
"""
Gets dup vals list at key made from keys
Parameters:
keys (str | bytes | memoryview | Iterable): of key strs to be
combined in order to form key
on (int): ordinal number used with onKey(pre,on) to form key.
Returns:
vals (list): each item in list is str
empty list if no entry at keys
"""
return [self._des(val) for val in
self.db.getOnIoDupValIter(db=self.sdb,
key=self._tokey(keys),
on=on,
sep=self.sep.encode())]


def remOn(self, keys: str | bytes | memoryview | Iterable, on: int=0,
val: str | bytes | memoryview = ''):
"""
Expand Down
6 changes: 3 additions & 3 deletions tests/core/test_escrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def test_missing_delegator_escrow():
psr.parse(ims=bytearray(delIcpMsg), kvy=watKvy, local=False)
assert not bobPre in watKvy.kevers
assert not delPre in watKvy.kevers
escrows = watKvy.db.pdes.get(dbing.snKey(delPre, delSrdr.sn))
escrows = watKvy.db.pdes.getOn(keys=delPre, on=delSrdr.sn)
assert len(escrows) == 1
assert escrows[0] == delSrdr.said # escrow entry for event

Expand All @@ -538,14 +538,14 @@ def test_missing_delegator_escrow():
assert watBobK.sn == 0
watKvy.processEscrows()
assert not delPre in watKvy.kevers
escrows = watKvy.db.pdes.get(dbing.snKey(delPre, delSrdr.sn))
escrows = watKvy.db.pdes.getOn(keys=delPre, on=delSrdr.sn)
assert len(escrows) == 1
assert escrows[0] == delSrdr.said # escrow entry for event

# Now apply Bob's ixn to wat's kvy and process escrow
psr.parse(ims=bytearray(bobIxnMsg1), kvy=watKvy, local=False)
watKvy.processEscrows()
escrows = watKvy.db.pdes.get(dbing.snKey(delPre, delSrdr.sn))
escrows = watKvy.db.pdes.getOn(keys=delPre, on=delSrdr.sn)
assert len(escrows) == 0
assert watBobK.sn == 1

Expand Down
4 changes: 4 additions & 0 deletions tests/db/test_subing.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,13 @@ def test_on_iodup_suber():

# test addOn remOn
assert onsuber.addOn(keys="z", on=0, val=w)
assert onsuber.getOn(keys="z", on=0) == [w]
assert onsuber.addOn(keys="z", on=0, val=x)
assert onsuber.getOn(keys="z", on=0) == [w, x]
assert onsuber.addOn(keys="z", on=1, val=y)
assert onsuber.getOn(keys="z", on=1) == [y]
assert onsuber.addOn(keys="z", on=1, val=z)
assert onsuber.getOn(keys="z", on=1) == [y, z]

assert onsuber.cntOn(keys=("z",)) == 4

Expand Down

0 comments on commit 9e06c8f

Please sign in to comment.