diff --git a/src/keri/app/cli/commands/escrow.py b/src/keri/app/cli/commands/escrow.py deleted file mode 100644 index 24d78f33..00000000 --- a/src/keri/app/cli/commands/escrow.py +++ /dev/null @@ -1,159 +0,0 @@ -# -*- encoding: utf-8 -*- -""" -KERI -keri.kli.commands module - -""" -import argparse -import json - -from hio import help -from hio.base import doing - -from keri.core import eventing -from keri.app.cli.common import existing -from keri.db import dbing -from keri.kering import ConfigurationError -from keri.vdr import viring - -logger = help.ogler.getLogger() - -parser = argparse.ArgumentParser(description='Initialize a prefix') -parser.set_defaults(handler=lambda args: handler(args), - transferable=True) -parser.add_argument('--name', '-n', help='keystore name and file location of KERI keystore', required=True) -parser.add_argument('--base', '-b', help='additional optional prefix to file location of KERI keystore', - required=False, default="") -parser.add_argument('--passcode', '-p', help='22 character encryption passcode for keystore (is not saved)', - dest="bran", default=None) # passcode => bran - -parser.add_argument("--escrow", "-e", help="show values for one specific escrow", default=None) - - -def handler(args): - """ Command line escrow handler - - """ - kwa = dict(args=args) - return [doing.doify(escrows, **kwa)] - - -def escrows(tymth, tock=0.0, **opts): - _ = (yield tock) - - args = opts["args"] - name = args.name - base = args.base - bran = args.bran - escrow = args.escrow - - try: - with existing.existingHby(name=name, base=base, bran=bran) as hby: - reger = viring.Reger(name=hby.name, db=hby.db, temp=False) - - escrows = dict() - if (not escrow) or escrow == "out-of-order-events": - oots = list() - key = ekey = b'' # both start same. when not same means escrows found - while True: - for ekey, edig in hby.db.getOoeItemsNextIter(key=key): - pre, sn = dbing.splitKeySN(ekey) # get pre and sn from escrow item - - try: - oots.append(eventing.loadEvent(hby.db, pre, edig)) - except ValueError as e: - raise e - - if ekey == key: # still same so no escrows found on last while iteration - break - key = ekey # setup next while iteration, with key after ekey - - escrows["out-of-order-events"] = oots - - if (not escrow) or escrow == "partially-witnessed-events": - pwes = list() - key = ekey = b'' # both start same. when not same means escrows found - while True: # break when done - for ekey, edig in hby.db.getPweItemsNextIter(key=key): - pre, sn = dbing.splitKeySN(ekey) # get pre and sn from escrow item - - try: - pwes.append(eventing.loadEvent(hby.db, pre, edig)) - except ValueError as e: - raise e - - if ekey == key: # still same so no escrows found on last while iteration - break - key = ekey # setup next while iteration, with key after ekey - - escrows["partially-witnessed-events"] = pwes - - if (not escrow) or escrow == "partially-signed-events": - pses = list() - key = ekey = b'' # both start same. when not same means escrows found - while True: # break when done - for ekey, edig in hby.db.getPseItemsNextIter(key=key): - pre, sn = dbing.splitKeySN(ekey) # get pre and sn from escrow item - - try: - pses.append(eventing.loadEvent(hby.db, pre, edig)) - except ValueError as e: - raise e - - if ekey == key: # still same so no escrows found on last while iteration - break - key = ekey # setup next while iteration, with key after ekey - - escrows["partially-signed-events"] = pses - - if (not escrow) or escrow == "likely-duplicitous-events": - ldes = list() - key = ekey = b'' # both start same. when not same means escrows found - while True: # break when done - for ekey, edig in hby.db.getLdeItemsNextIter(key=key): - pre, sn = dbing.splitKeySN(ekey) # get pre and sn from escrow item - - try: - ldes.append(eventing.loadEvent(hby.db, pre, edig)) - except ValueError as e: - raise e - - if ekey == key: # still same so no escrows found on last while iteration - break - key = ekey # setup next while iteration, with key after ekey - - escrows["likely-duplicitous-events"] = ldes - - if (not escrow) or escrow == "missing-registry-escrow": - creds = list() - for (said,), dater in reger.mre.getItemIter(): - creder, *_ = reger.cloneCred(said) - creds.append(creder.sad) - - escrows["missing-registry-escrow"] = creds - - if (not escrow) or escrow == "broken-chain-escrow": - creds = list() - for (said,), dater in reger.mce.getItemIter(): - creder, *_ = reger.cloneCred(said) - creds.append(creder.sad) - - escrows["broken-chain-escrow"] = creds - - if (not escrow) or escrow == "missing-schema-escrow": - creds = list() - for (said,), dater in reger.mse.getItemIter(): - creder, *_ = reger.cloneCred(said) - creds.append(creder.sad) - - escrows["missing-schema-escrow"] = creds - - print(json.dumps(escrows, indent=2)) - - if not(escrow) or escrow == 'tel-partial-witness-escrow': - for (regk, snq), (prefixer, seqner, saider) in reger.tpwe.getItemIter(): - pass - - except ConfigurationError as e: - print(f"identifier prefix for {name} does not exist, incept must be run first", ) - return -1 diff --git a/src/keri/app/cli/commands/escrow/list.py b/src/keri/app/cli/commands/escrow/list.py index 06fe7661..9e317856 100644 --- a/src/keri/app/cli/commands/escrow/list.py +++ b/src/keri/app/cli/commands/escrow/list.py @@ -63,7 +63,7 @@ def escrows(tymth, tock=0.0, **opts): pses = list() key = ekey = b'' # both start same. when not same means escrows found while True: # break when done - for ekey, edig in hby.db.getPseItemIter(key=key): + for ekey, edig in hby.db.getPseItemsNextIter(key=key): pre, sn = dbing.splitSnKey(ekey) # get pre and sn from escrow item try: diff --git a/src/keri/db/basing.py b/src/keri/db/basing.py index 74c1951e..eddb9304 100644 --- a/src/keri/db/basing.py +++ b/src/keri/db/basing.py @@ -1173,7 +1173,7 @@ def clearEscrows(self): logger.info(f"KEL: Cleared {count} verified receipt escrows") count = 0 - for (k, _) in self.getPseItemIter(): + for (k, _) in self.getPseItemsNextIter(): count += 1 self.delPses(key=k) logger.info(f"KEL: Cleared {count} partially signed escrows") @@ -1203,14 +1203,12 @@ def clearEscrows(self): logger.info(f"KEL: Cleared {count} likely duplicitous escrows") count = 0 - for ekey, edig in self.getQnfItemsNextIter(): - count += 1 - pre, _ = splitKey(ekey) - self.delQnf(dgKey(pre, edig), edig) + for k, _ in self.getQnfItemsNextIter(): + self.delQnfs(key=k) logger.info(f"KEL: Cleared {count} query not found escrows") count = 0 - for (key, on, val) in self.getPdesItemsNextIter(): + for (key, _) in self.getPdeItemsNextIter(): count += 1 self.delPde(key=key) logger.info(f"KEL: Cleared {count} partially delegated key event escrows") @@ -1224,8 +1222,11 @@ def clearEscrows(self): ('gpse', self.gpse, 'group partial signature escrow'), ('epse', self.epse, 'exchange partial signature escrow'), ('dune', self.dune, 'delegated unanchored escrow')]: + count = 0 + for (k, _) in escrow.getItemIter(): + count += 1 escrow.trim() - logger.info(f"KEL: Cleared escrow ({name.ljust(5)}): {desc}") + logger.info(f"KEL: Cleared {count} escrows from ({name.ljust(5)}): {desc}") logger.info("Cleared KEL escrows") @property @@ -2637,18 +2638,6 @@ def getPseLast(self, key): """ return self.getIoValLast(self.pses, key) - def getPseItemIter(self, key=b''): - """ - Use sgKey() - Return iterator of partial signed escrowed event dig items at next key after key. - Items is (key, val) where proem has already been stripped from val - If key is b'' empty then returns dup items at first key. - If skip is False and key is not b'' empty then returns dup items at key - Raises StopIteration Error when empty - Duplicates are retrieved in insertion order. - """ - return self.getTopIoDupItemIter(self.pses, key) - def getPseItemsNext(self, key=b'', skip=True): """ Use snKey() @@ -2728,6 +2717,24 @@ def getPde(self, key): """ return self.getVal(self.pdes, key) + def getPdes(self, key): + """ + Use dgKey() + Return list of out of order escrow event dig vals at key + Returns empty list if no entry at key + Duplicates are retrieved in insertion order. + """ + return self.getIoVals(self.pdes, key) + + def getPdeItemsNextIter(self, key=b'', skip=True): + """ + Use dgKey() + Return list of witnessed signed escrowed event dig vals at key + Returns empty list if no entry at key + Duplicates are retrieved in insertion order. + """ + return self.getIoItemsNextIter(self.pdes, key, skip) + def delPde(self, key): """ Use dgKey() @@ -3134,9 +3141,6 @@ def getQnfItemsNextIter(self, key=b'', skip=True): """ return self.getIoItemsNextIter(self.qnfs, key, skip) - def getPdesItemsNextIter(self, key=b'', skip=True): - return self.getOnIoDupItemIter(self.pdes, key, skip) - def cntQnfs(self, key): """ Use snKey() diff --git a/tests/db/test_basing.py b/tests/db/test_basing.py index b067985d..df04d734 100644 --- a/tests/db/test_basing.py +++ b/tests/db/test_basing.py @@ -11,6 +11,7 @@ import pytest from hio.base import doing +from keri.core.serdering import Serder from tests.app import openMultiSig from keri.kering import Versionage from keri.app import habbing @@ -21,7 +22,7 @@ from keri.core.eventing import incept, rotate, interact, Kever from keri.db import basing from keri.db import dbing -from keri.db.basing import openDB, Baser, KeyStateRecord +from keri.db.basing import openDB, Baser, KeyStateRecord, OobiRecord from keri.db.dbing import (dgKey, onKey, snKey) from keri.db.dbing import openLMDB from keri.help.helping import datify, dictify @@ -2274,6 +2275,76 @@ def test_group_members(): """End Test""" +def test_clear_escrows(): + with openDB() as db: + key = b'A.a' + vals = [b"z", b"m", b"x", b"a"] + + db.putUres(key, vals) + db.putVres(key, vals) + db.putPses(key, vals) + db.putPwes(key, vals) + db.putUwes(key, vals) + db.putOoes(key, vals) + db.putLdes(key, vals) + db.putQnfs(key, vals) + + preb = 'DAzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhcc'.encode("utf-8") + digb = 'EGAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4'.encode("utf-8") + key = dgKey(preb, digb) + ssnu1 = b'0AAAAAAAAAAAAAAAAAAAAAAB' + sdig1 = b'EALkveIFUPvt38xhtgYYJRCCpAGO7WjjHVR37Pawv67E' + val1 = ssnu1 + sdig1 + + db.putPde(key, val1) + + pre = 'k' + saider = coring.Saider(qb64b='EGAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4') + db.rpes.put(keys=('route',), vals=[saider]) + assert db.rpes.cnt(keys=('route',)) == 1 + + db.eoobi.pin(keys=('url',), val=OobiRecord()) + assert db.eoobi.cntAll() == 1 + + db.gpwe.add(keys=(pre,), val=(coring.Seqner(qb64b=b'0AAAAAAAAAAAAAAAAAAAAAAB'), saider)) + assert db.gpwe.cnt(keys=(pre,)) == 1 + + db.gdee.add(keys=(pre,), val=(coring.Seqner(qb64b=b'0AAAAAAAAAAAAAAAAAAAAAAB'), saider)) + assert db.gdee.cnt(keys=(pre,)) == 1 + + serder = Serder(raw=b'{"v":"KERI10JSON0000cb_","t":"ixn","d":"EG8WAmM29ZBdoXbnb87yiPxQw4Y7gcQjqZS74vBAKsRm","i":"DApYGFaqnrALTyejaJaGAVhNpSCtqyerPqWVK9ZBNZk0","s":"4","p":"EAskHI462CuIMS_gNkcl_QewzrRSKH2p9zHQIO132Z30","a":[]}') + db.dpwe.pin(keys=(pre, 'said'), val=serder) + assert db.dpwe.get(keys=(pre, 'said')) is not None + + db.gpse.add(keys=('qb64',), val=(coring.Seqner(qb64b=b'0AAAAAAAAAAAAAAAAAAAAAAB'), saider)) + assert db.gpse.cnt(keys=('qb64',)) == 1 + + db.epse.put(keys=('dig',), val=serder) + assert db.epse.get(keys=('dig',)) is not None + + db.dune.pin(keys=(pre, 'said'), val=serder) + assert db.dune.get(keys=(pre, 'said')) is not None + + db.clearEscrows() + + assert db.getUres(key) == [] + assert db.getVres(key) == [] + assert db.getPses(key) == [] + assert db.getPwes(key) == [] + assert db.getUwes(key) == [] + assert db.getOoes(key) == [] + assert db.getLdes(key) == [] + assert db.getQnfs(key) == [] + assert db.getPdes(key) == [] + assert db.rpes.cnt(keys=('route',)) == 0 + assert db.eoobi.cntAll() == 0 + assert db.gpwe.cnt(keys=(pre,)) == 0 + assert db.gdee.cnt(keys=(pre,)) == 0 + assert db.dpwe.get(keys=(pre, 'said')) is None + assert db.gpse.cnt(keys=('qb64',)) == 0 + assert db.epse.get(keys=('dig',)) is None + assert db.dune.get(keys=(pre, 'said')) is None + if __name__ == "__main__": test_baser() test_clean_baser()