Skip to content

Commit

Permalink
refactor change tags in counting to Codens simplified. Removed tag in…
Browse files Browse the repository at this point in the history
…it parameter

now code init parameter is polymorphic so can take code or code name (coden)
started migrating coring.Counter to counting.Counter with gvrsn=Vrsn_1_0
  • Loading branch information
SmithSamuelM committed Jul 21, 2024
1 parent 3c6e973 commit b3dd43b
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 313 deletions.
6 changes: 4 additions & 2 deletions src/keri/app/agenting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from . import httping, forwarding
from .. import help
from .. import kering
from .. import core
from ..core import eventing, parsing, coring, serdering, indexing
from ..core.coring import CtrDex
from ..db import dbing
Expand Down Expand Up @@ -104,7 +105,7 @@ def receipt(self, pre, sn=None, auths=None):
del rct[:rserder.size]

# pull off the count code
coring.Counter(qb64b=rct, strip=True)
core.Counter(qb64b=rct, strip=True, gvrsn=kering.Vrsn_1_0)
rcts[wit] = rct
else:
print(f"invalid response {rep.status} from witnesses {wit}")
Expand All @@ -124,7 +125,8 @@ def receipt(self, pre, sn=None, auths=None):
sn=sn,
said=ser.said)
msg.extend(rserder.raw)
msg.extend(coring.Counter(code=CtrDex.NonTransReceiptCouples, count=len(wigs)).qb64b)
msg.extend(core.Counter(core.Codens.NonTransReceiptCouples,
count=len(wigs), gvrsn=kering.Vrsn_1_0).qb64b)
for wig in wigs:
msg.extend(wig)

Expand Down
2 changes: 1 addition & 1 deletion src/keri/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
from .coring import Tholder
from .indexing import Indexer, Siger, IdrDex, IdxSigDex
from .signing import Signer, Salter, Cipher, Encrypter, Decrypter
from .counting import Counter
from .counting import Counter, Codens
98 changes: 48 additions & 50 deletions src/keri/core/counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Provides versioning support for Counter classes and codes
"""
import copy

from dataclasses import dataclass, astuple, asdict
from collections import namedtuple
Expand Down Expand Up @@ -147,18 +148,15 @@ def __iter__(self):

CtrDex_2_0 = CounterCodex_2_0()

# keys and values as strings of keys
Codict1 = asdict(CtrDex_1_0)
Tagage_1_0 = namedtuple("Tagage_1_0", list(Codict1), defaults=list(Codict1))
Tags_1_0 = Tagage_1_0() # uses defaults

Codict2 = asdict(CtrDex_2_0)
Tagage_2_0 = namedtuple("Tagage_2_0", list(Codict2), defaults=list(Codict2))
Tags_2_0 = Tagage_2_0() # uses defaults

CodictAll = Codict2 | Codict1
AllTagage = namedtuple("AllTagage", list(CodictAll), defaults=list(CodictAll))
AllTags = AllTagage() # uses defaults
# CodeNames is tuple of codes names given by attributes of union of codices
CodeNames = tuple(asdict(CtrDex_2_0) | asdict(CtrDex_1_0))
# Codens is namedtuple of CodeNames where its names are the code names
# Codens enables using the attributes of the named tuple to specify a code by
# name (indirection) so that changes in the code itself do not break the
# creation of a counter. Enables specifying a counter by the code name not the
# code itself. The code may change between versions but the code name does not.
Codenage = namedtuple("Codenage", CodeNames, defaults=CodeNames)
Codens = Codenage()


@dataclass(frozen=True)
Expand Down Expand Up @@ -200,8 +198,9 @@ class Counter:
Includes the following attributes and properties:
Class Attributes:
Codes (dict): of codexes keyed by version
Tags (dict): of tagages keyed by version
Codes (dict): nested of codexes keyed by major and minor version
Names (dict): nested of map of code names to codes keyed by
major and minor version
Hards (dict): of hard code sizes keyed by text domain selector
Bards (dict): of hard code sizes keyed by binary domain selector
Sizes (dict): of size tables keyed by version. Size table is dict
Expand Down Expand Up @@ -310,7 +309,15 @@ class Counter:
},
}

Tags = {Vrsn_1_0: Tags_1_0, Vrsn_2_0: Tags_2_0}

# invert dataclass codenames: codes to dict codes: codenames
Names = copy.deepcopy(Codes) # make deep nested copy so can invert nested values
for minor in Names.values():
for key in minor:
minor[key] = {val: key for key, val in asdict(minor[key]).items()}




# Hards table maps from bytes Base64 first two code chars to int of
# hard size, hs,(stable) of code. The soft size, ss, (unstable) for Counter
Expand Down Expand Up @@ -415,17 +422,16 @@ class Counter:
}


def __init__(self, tag=None, *, code = None, count=None, countB64=None,
def __init__(self, code=None, *, count=None, countB64=None,
qb64b=None, qb64=None, qb2=None, strip=False, gvrsn=Vrsn_2_0):
"""
Validate as fully qualified
Parameters:
tag (str | None): label of stable (hard) part of derivation code
to lookup in codex so it can depend on version.
takes precedence over code.
code (str | None): stable (hard) part of derivation code
if tag provided lookup code from tag
else if tag is None and code provided use code.
code (str | None): either stable (hard) part of derivation code or
code name. When code name then look up code from
._codes. This allows versioning to change code
but keep stable code name.
count (int | None): count of framed material in quadlets/triplets
for composition. Count does not include code.
When both count and countB64 are None then count
Expand Down Expand Up @@ -466,14 +472,15 @@ def __init__(self, tag=None, *, code = None, count=None, countB64=None,
self._version = gvrsn # provided version may be earlier than supported version


if tag:
if not hasattr(self._codes, tag):
raise kering.InvalidCodeError(f"Unsupported {tag=}.")
code = self._codes[tag]

if code is not None: # code (hard) provided
if code: # code (hard) provided
# assumes ._sizes ._codes coherent
if code not in self._sizes or len(code) < 2:
raise kering.InvalidCodeError(f"Unsupported {code=}.")
try:
code = self._codes[code] # code is code name so look up code
if code not in self._sizes or len(code) < 2:
raise kering.InvalidCodeError(f"Unsupported {code=}.")
except Exception as ex:
raise kering.InvalidCodeError(f"Unsupported {code=}.") from ex

hs, ss, fs, ls = self._sizes[code] # get sizes for code
cs = hs + ss # both hard + soft code size
Expand Down Expand Up @@ -520,8 +527,7 @@ def __init__(self, tag=None, *, code = None, count=None, countB64=None,
"(code and count) or qb64b or "
"qb64 or qb2.")

codenames = { val: key for key, val in asdict(self.codes).items()} # map codes to code names
self._tag = codenames[self.code]
self._name = self.Names[gvrsn.major][latest][self.code]

@property
def version(self):
Expand All @@ -547,13 +553,13 @@ def codes(self):
"""
return self._codes

@property
def tags(self):
"""
Returns tags for current .version
Makes .tags read only
"""
return self.Tags[self.version] # use own version
#@property
#def tags(self):
#"""
#Returns tags for current .version
#Makes .tags read only
#"""
#return self.Tags[self.version] # use own version

@property
def sizes(self):
Expand All @@ -574,25 +580,17 @@ def code(self):
"""
return self._code

@property
def tag(self):
"""
Returns:
tag (str): code name for self.code
Getter for ._tag. Makes .tag read only
"""
return self._tag

@property
def name(self):
"""
Returns:
name (str): code name for self.code alias of .tag. Match interface
name (str): code name for self.code. Match interface
for annotation for primitives like Matter
Getter for ._name. Makes .name read only
"""
return self.tag
return self._name


@property
Expand Down
12 changes: 6 additions & 6 deletions src/keri/core/serdering.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from .coring import (Matter, Saider, Verfer, Diger, Number, Tholder, Tagger,
Ilker, Traitor, Verser, )

from .counting import GenDex, AllTags, Counter, SealDex_2_0
from .counting import GenDex, Counter, Codens, SealDex_2_0

from .structing import Sealer, SClanDom

Expand Down Expand Up @@ -1298,7 +1298,7 @@ def _dumps(self, sad=None, proto=None, vrsn=None):
for e in v: # list
frame.extend(e.encode("utf-8"))

val = bytearray(Counter(tag=AllTags.GenericListGroup,
val = bytearray(Counter(Codens.GenericListGroup,
count=len(frame) // 4).qb64b)
val.extend(frame)

Expand All @@ -1307,7 +1307,7 @@ def _dumps(self, sad=None, proto=None, vrsn=None):
for e in v: # list
frame.extend(Traitor(trait=e).qb64b)

val = bytearray(Counter(tag=AllTags.GenericListGroup,
val = bytearray(Counter(Codens.GenericListGroup,
count=len(frame) // 4).qb64b)
val.extend(frame)

Expand Down Expand Up @@ -1342,7 +1342,7 @@ def _dumps(self, sad=None, proto=None, vrsn=None):
#generic seal no count type (v, Mapping):
#for l, e in v.items():
#pass
#val = bytearray(Counter(tag=AllTags.GenericMapGroup,
#val = bytearray(Counter(tag=""GenericMapGroup"",
# count=len(frame) // 4).qb64b)
#val.extend(mapframe)

Expand All @@ -1352,7 +1352,7 @@ def _dumps(self, sad=None, proto=None, vrsn=None):
gframe = bytearray()
gcode = None

val = bytearray(Counter(tag=AllTags.GenericListGroup,
val = bytearray(Counter(Codens.GenericListGroup,
count=len(frame) // 4).qb64b)
val.extend(frame)

Expand All @@ -1378,7 +1378,7 @@ def _dumps(self, sad=None, proto=None, vrsn=None):
# prepend count code for message
if fixed:

raw = bytearray(Counter(tag=AllTags.FixedMessageBodyGroup,
raw = bytearray(Counter(Codens.FixedMessageBodyGroup,
count=len(bdy) // 4).qb64b)
raw.extend(bdy)
else:
Expand Down
28 changes: 14 additions & 14 deletions src/keri/db/basing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1556,55 +1556,55 @@ def cloneEvtMsg(self, pre, fn, dig):
# add indexed signatures to attachments
if not (sigs := self.getSigs(key=dgkey)):
raise kering.MissingEntryError("Missing sigs for dig={}.".format(dig))
atc.extend(coring.Counter(code=coring.CtrDex.ControllerIdxSigs,
count=len(sigs)).qb64b)
atc.extend(core.Counter(code=core.Codens.ControllerIdxSigs,
count=len(sigs), gvrsn=kering.Vrsn_1_0).qb64b)
for sig in sigs:
atc.extend(sig)

# add indexed witness signatures to attachments
if wigs := self.getWigs(key=dgkey):
atc.extend(coring.Counter(code=coring.CtrDex.WitnessIdxSigs,
count=len(wigs)).qb64b)
atc.extend(core.Counter(code=core.Codens.WitnessIdxSigs,
count=len(wigs), gvrsn=kering.Vrsn_1_0).qb64b)
for wig in wigs:
atc.extend(wig)

# add authorizer (delegator/issuer) source seal event couple to attachments
couple = self.getAes(dgkey)
if couple is not None:
atc.extend(coring.Counter(code=coring.CtrDex.SealSourceCouples,
count=1).qb64b)
atc.extend(core.Counter(code=core.Codens.SealSourceCouples,
count=1, gvrsn=kering.Vrsn_1_0).qb64b)
atc.extend(couple)

# add trans endorsement quadruples to attachments not controller
# may have been originally key event attachments or receipted endorsements
if quads := self.getVrcs(key=dgkey):
atc.extend(coring.Counter(code=coring.CtrDex.TransReceiptQuadruples,
count=len(quads)).qb64b)
atc.extend(core.Counter(code=core.Codens.TransReceiptQuadruples,
count=len(quads), gvrsn=kering.Vrsn_1_0).qb64b)
for quad in quads:
atc.extend(quad)

# add nontrans endorsement couples to attachments not witnesses
# may have been originally key event attachments or receipted endorsements
if coups := self.getRcts(key=dgkey):
atc.extend(coring.Counter(code=coring.CtrDex.NonTransReceiptCouples,
count=len(coups)).qb64b)
atc.extend(core.Counter(code=core.Codens.NonTransReceiptCouples,
count=len(coups), gvrsn=kering.Vrsn_1_0).qb64b)
for coup in coups:
atc.extend(coup)

# add first seen replay couple to attachments
if not (dts := self.getDts(key=dgkey)):
raise kering.MissingEntryError("Missing datetime for dig={}.".format(dig))
atc.extend(coring.Counter(code=coring.CtrDex.FirstSeenReplayCouples,
count=1).qb64b)
atc.extend(core.Counter(code=core.Codens.FirstSeenReplayCouples,
count=1, gvrsn=kering.Vrsn_1_0).qb64b)
atc.extend(core.Number(num=fn, code=core.NumDex.Huge).qb64b) # may not need to be Huge
atc.extend(coring.Dater(dts=bytes(dts)).qb64b)

# prepend pipelining counter to attachments
if len(atc) % 4:
raise ValueError("Invalid attachments size={}, nonintegral"
" quadlets.".format(len(atc)))
pcnt = coring.Counter(code=coring.CtrDex.AttachmentGroup,
count=(len(atc) // 4)).qb64b
pcnt = core.Counter(code=core.Codens.AttachmentGroup,
count=(len(atc) // 4), gvrsn=kering.Vrsn_1_0).qb64b
msg.extend(pcnt)
msg.extend(atc)
return msg
Expand Down
4 changes: 3 additions & 1 deletion src/keri/vdr/credentialing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .. import kering, help
from ..app import agenting
from ..app.habbing import GroupHab
from .. import kering, core
from ..core import parsing, coring, scheming, serdering
from ..core.coring import Seqner, MtrDex
from ..core.eventing import TraitDex
Expand Down Expand Up @@ -931,7 +932,8 @@ def sendCredential(hby, hab, reger, postman, creder, recp):
postman.send(serder=source, attachment=atc)

serder, prefixer, seqner, saider = reger.cloneCred(creder.said)
atc = bytearray(coring.Counter(coring.CtrDex.SealSourceTriples, count=1).qb64b)
atc = bytearray(core.Counter(core.Codens.SealSourceTriples,
count=1, gvrsn=kering.Vrsn_1_0).qb64b)
atc.extend(prefixer.qb64b)
atc.extend(seqner.qb64b)
atc.extend(saider.qb64b)
Expand Down
5 changes: 3 additions & 2 deletions tests/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from contextlib import contextmanager

from keri.app import habbing
from keri import kering, core
from keri.core import coring, eventing, parsing
from keri.db import dbing

Expand Down Expand Up @@ -54,8 +55,8 @@ def openMultiSig(prefix="test", salt=b'0123456789abcdef', temp=True, **kwa):
sigs.extend(bytes(hab3.db.getSigs(dgkey)[0]))

evt = bytearray(eraw)
evt.extend(coring.Counter(code=coring.CtrDex.ControllerIdxSigs,
count=3).qb64b) # attach cnt
evt.extend(core.Counter(code=core.Codens.ControllerIdxSigs,
count=3, gvrsn=kering.Vrsn_1_0).qb64b) # attach cnt
evt.extend(sigs)

parsing.Parser().parse(ims=bytearray(evt), kvy=kev3, local=True)
Expand Down
8 changes: 3 additions & 5 deletions tests/app/test_agenting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

from hio.base import doing, tyming

from keri import kering

from keri import core

from keri import kering, core
from keri.core import coring, serdering
from keri.core.coring import Counter, CtrDex, Seqner
from keri.help import nowIso8601
Expand Down Expand Up @@ -158,7 +155,8 @@ def testDo(self, tymth, tock=0.0):
serder = eventing.issue(vcdig=regser.pre,
regk="EbA1o_bItVC9i6YB3hr2C3I_Gtqvz02vCmavJNoBA3Jg")
msg = bytearray(serder.raw)
msg.extend(Counter(CtrDex.SealSourceCouples, count=1).qb64b)
msg.extend(core.Counter(core.Codens.SealSourceCouples, count=1,
gvrsn=kering.Vrsn_1_0).qb64b)
msg.extend(Seqner(sn=self.palHab.kever.sn).qb64b)
msg.extend(self.palHab.kever.serder.saidb)

Expand Down
Loading

0 comments on commit b3dd43b

Please sign in to comment.