From 9e06c8f2e19928062f3b0c37812e37980c9e5102 Mon Sep 17 00:00:00 2001 From: Samuel M Smith Date: Mon, 9 Sep 2024 20:06:34 -0600 Subject: [PATCH] refactore db.pdes to use new suber methods to make more convenient added unit tests --- src/keri/core/eventing.py | 6 +++--- src/keri/db/dbing.py | 2 +- src/keri/db/subing.py | 22 ++++++++++++++++++++++ tests/core/test_escrow.py | 6 +++--- tests/db/test_subing.py | 4 ++++ 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/keri/core/eventing.py b/src/keri/core/eventing.py index a7bd6619..b244af68 100644 --- a/src/keri/core/eventing.py +++ b/src/keri/core/eventing.py @@ -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): @@ -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]) @@ -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) diff --git a/src/keri/db/dbing.py b/src/keri/db/dbing.py index 4bd1e67c..667edb2b 100644 --- a/src/keri/db/dbing.py +++ b/src/keri/db/dbing.py @@ -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 diff --git a/src/keri/db/subing.py b/src/keri/db/subing.py index 4e6abf31..521c1788 100644 --- a/src/keri/db/subing.py +++ b/src/keri/db/subing.py @@ -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 = ''): """ diff --git a/tests/core/test_escrow.py b/tests/core/test_escrow.py index 7808bdc1..b6d9b4df 100644 --- a/tests/core/test_escrow.py +++ b/tests/core/test_escrow.py @@ -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 @@ -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 diff --git a/tests/db/test_subing.py b/tests/db/test_subing.py index 8ac6d5e5..58c72ccd 100644 --- a/tests/db/test_subing.py +++ b/tests/db/test_subing.py @@ -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