Skip to content

Commit 75f7024

Browse files
committed
refactor eliminate version parameter for supported version since supported versions better determined by
entries in Serder.Fields table.
1 parent cbc9dbf commit 75f7024

File tree

3 files changed

+56
-75
lines changed

3 files changed

+56
-75
lines changed

src/keri/core/serdering.py

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,16 @@ class Serdery:
120120
121121
"""
122122

123-
def __init__(self, *, version=None):
123+
def __init__(self, *pa, **kwa):
124124
"""Init instance
125125
126126
Parameters:
127-
version (Versionage | None): instance supported protocol version
128-
None means do not enforce a supported version
127+
129128
"""
130-
self.version = version # default version
129+
pass
131130

132131

133-
def reap(self, ims, *, version=None):
132+
def reap(self, ims):
134133
"""Extract and return Serder subclass based on protocol type reaped from
135134
version string inside serialized raw of Serder.
136135
@@ -144,14 +143,14 @@ def reap(self, ims, *, version=None):
144143
version (Versionage | None): instance supported protocol version
145144
None means do not enforce a supported version
146145
"""
147-
version = version if version is not None else self.version
146+
#version = version if version is not None else self.version
148147

149-
smellage = smell(ims, version=version)
148+
smellage = smell(ims)
150149

151150
if smellage.protocol == Protos.keri:
152-
return SerderKERI(raw=ims, strip=True, version=version, smellage=smellage)
151+
return SerderKERI(raw=ims, strip=True, smellage=smellage)
153152
elif smellage.protocol == Protos.acdc:
154-
return SerderACDC(raw=ims, strip=True, version=version, smellage=smellage)
153+
return SerderACDC(raw=ims, strip=True, smellage=smellage)
155154
else:
156155
raise ProtocolError(f"Unsupported protocol type = {smellage.proto}.")
157156

@@ -189,8 +188,8 @@ class Serder:
189188
Digests (dict): map of digestive codes. Should be same set of codes as
190189
in coring.DigestCodex coring.DigDex so that .digestive property works.
191190
Use unit tests to ensure codex sets match
192-
Protocol (str): default protocol version type
193-
Proto (str): default CESR protocol genus type
191+
Protocol (str): class specific message protocol
192+
Proto (str): default message protocol
194193
Vrsn (Versionage): default version
195194
Kind (str): default serialization kind one of Serials
196195
Fields (dict): nested dict of field labels keyed by protocol, version,
@@ -270,9 +269,8 @@ class Serder:
270269
}
271270

272271
#override in subclass to enforce specific protocol
273-
Protocol = None # required protocol, None means any in Protos is ok
274-
275-
Proto = Protos.keri # default CESR protocol type for makify on base Serder
272+
Protocol = None # class based message protocol, None means any in Protos is ok
273+
Proto = Protos.keri # default message protocol type for makify on base Serder
276274
Vrsn = Vrsn_1_0 # default protocol version for protocol type
277275
Kind = Serials.json # default serialization kind
278276

@@ -441,7 +439,7 @@ class Serder:
441439
}
442440

443441

444-
def __init__(self, *, raw=b'', sad=None, strip=False, version=Version,
442+
def __init__(self, *, raw=b'', sad=None, strip=False,
445443
smellage=None, verify=True, makify=False,
446444
proto=None, vrsn=None, kind=None, ilk=None, saids=None):
447445
"""Deserialize raw if provided. Update properties from deserialized raw.
@@ -460,8 +458,6 @@ def __init__(self, *, raw=b'', sad=None, strip=False, version=Version,
460458
strip (bool): True means strip (delete) raw from input stream
461459
bytearray after parsing. False means do not strip.
462460
Assumes that raw is bytearray when strip is True.
463-
version (Versionage | None): instance supported protocol version
464-
None means do not enforce a supported version
465461
smellage (Smellage | None): instance of deconstructed and converted
466462
version string elements. If none or empty ignore otherwise assume
467463
that raw already had its version string extracted (reaped) into the
@@ -493,9 +489,7 @@ def __init__(self, *, raw=b'', sad=None, strip=False, version=Version,
493489

494490
if raw: # deserialize raw using property setter
495491
# self._inhale works because it only references class attributes
496-
sad, proto, vrsn, kind, size = self._inhale(raw=raw,
497-
version=version,
498-
smellage=smellage)
492+
sad, proto, vrsn, kind, size = self._inhale(raw=raw, smellage=smellage)
499493
self._raw = bytes(raw[:size]) # crypto ops require bytes not bytearray
500494
self._sad = sad
501495
self._proto = proto
@@ -528,14 +522,13 @@ def __init__(self, *, raw=b'', sad=None, strip=False, version=Version,
528522

529523
elif sad or makify: # serialize sad into raw or make sad
530524
if makify: # recompute properties and said(s) and reset sad
531-
# makify resets sad, raw, proto, version, kind, and size
532-
self.makify(sad=sad, version=version,
533-
proto=proto, vrsn=vrsn, kind=kind, ilk=ilk, saids=saids)
525+
# makify resets sad, raw, proto, vrsn, kind, ilk, and size
526+
self.makify(sad=sad, proto=proto, vrsn=vrsn, kind=kind,
527+
ilk=ilk, saids=saids)
534528

535529
else:
536530
# self._exhale works because it only access class attributes
537-
raw, sad, proto, vrsn, kind, size = self._exhale(sad=sad,
538-
version=version)
531+
raw, sad, proto, vrsn, kind, size = self._exhale(sad=sad)
539532
self._raw = raw
540533
self._sad = sad
541534
self._proto = proto
@@ -680,8 +673,7 @@ def _verify(self):
680673
# verified successfully since no exception
681674

682675

683-
def makify(self, sad, *, version=None,
684-
proto=None, vrsn=None, kind=None, ilk=None, saids=None):
676+
def makify(self, sad, *, proto=None, vrsn=None, kind=None, ilk=None, saids=None):
685677
"""Makify given sad dict makes the versions string and computes the said
686678
field values and sets associated properties:
687679
raw, sad, proto, version, kind, size
@@ -699,8 +691,6 @@ def makify(self, sad, *, version=None,
699691
Parameters:
700692
sad (dict): serializable saidified field map of message.
701693
Ignored if raw provided
702-
version (Versionage): instance supported protocol version
703-
None means do not enforce version
704694
proto (str | None): desired protocol type str value of Protos
705695
If None then its extracted from sad or uses default .Proto
706696
vrsn (Versionage | None): instance desired protocol version
@@ -721,7 +711,7 @@ def makify(self, sad, *, version=None,
721711
sproto = svrsn = skind = silk = None
722712
if sad and 'v' in sad: # attempt to get from vs in sad
723713
try: # extract version string elements as defaults if provided
724-
sproto, svrsn, skind, _ = deversify(sad["v"], version=version)
714+
sproto, svrsn, skind, _ = deversify(sad["v"])
725715
except ValueError as ex:
726716
pass
727717
else:
@@ -750,11 +740,11 @@ def makify(self, sad, *, version=None,
750740
raise SerializeError(f"Expected protocol = {self.Protocol}, got "
751741
f"{proto} instead.")
752742

753-
if version is not None: # compatible version with vrsn
754-
if (vrsn.major > version.major or
755-
(vrsn.major == version.major and vrsn.minor > version.minor)):
756-
raise SerializeError(f"Incompatible {version=}, with "
757-
f"{vrsn=}.")
743+
#if version is not None: # compatible version with vrsn
744+
#if (vrsn.major > version.major or
745+
#(vrsn.major == version.major and vrsn.minor > version.minor)):
746+
#raise SerializeError(f"Incompatible {version=}, with "
747+
#f"{vrsn=}.")
758748

759749

760750
if kind not in Serials:
@@ -895,7 +885,7 @@ def makify(self, sad, *, version=None,
895885

896886

897887
@classmethod
898-
def _inhale(clas, raw, version=Version, smellage=None):
888+
def _inhale(clas, raw, *, smellage=None):
899889
"""Deserializes raw.
900890
Parses serilized event ser of serialization kind and assigns to
901891
instance attributes and returns tuple of associated elements.
@@ -914,7 +904,6 @@ def _inhale(clas, raw, version=Version, smellage=None):
914904
Parameters:
915905
clas (Serder): class reference
916906
raw (bytes): serialized sad message
917-
version (Versionage): instance supported protocol version
918907
smellage (Smellage | None): instance of deconstructed version string
919908
elements. If none or empty ignore otherwise assume that raw
920909
already had its version string extracted (reaped) into the
@@ -928,7 +917,7 @@ def _inhale(clas, raw, version=Version, smellage=None):
928917
if smellage: # passed in so don't need to smell raw again
929918
proto, vrsn, kind, size = smellage # tuple unpack
930919
else: # not passed in so smell raw
931-
proto, vrsn, kind, size = smell(raw, version=version)
920+
proto, vrsn, kind, size = smell(raw)
932921

933922
sad = clas.loads(raw=raw, size=size, kind=kind)
934923

@@ -996,9 +985,8 @@ def _loads(clas, raw, size=None):
996985

997986

998987
@classmethod
999-
def _exhale(clas, sad, version=None):
1000-
"""Serializes sad given kind and version and sets the serialized size
1001-
in the version string.
988+
def _exhale(clas, sad):
989+
"""Serializes sad and sets the serialized size in its version string.
1002990
1003991
As classmethod enables bootstrap of valid sad dict that has correct size
1004992
in version string. This obviates sizeify. This can be called on self as
@@ -1014,16 +1002,14 @@ def _exhale(clas, sad, version=None):
10141002
Parameters:
10151003
clas (Serder): class reference
10161004
sad (dict): serializable attribute dict of saidified data
1017-
version (Versionage | None): supported protocol version for message
1018-
None means do not enforce a supported version
10191005
10201006
10211007
"""
10221008
if "v" not in sad:
10231009
raise SerializeError(f"Missing version string field in {sad}.")
10241010

10251011
# extract elements so can replace size element but keep others
1026-
proto, vrsn, kind, size = deversify(sad["v"], version=version)
1012+
proto, vrsn, kind, size = deversify(sad["v"])
10271013

10281014
raw = clas.dumps(sad, kind)
10291015
size = len(raw)

src/keri/kering.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,15 @@
6666
"""
6767
Smellage = namedtuple("Smellage", "protocol version kind size")
6868

69-
def rematch(match, *, version=None):
69+
def rematch(match):
7070
"""
7171
Returns:
7272
smellage (Smellage): named tuple extracted from version string regex match
7373
(protocol, version, kind, size)
7474
7575
Parameters:
7676
match (re.Match): instance of Match class
77-
version (Versionage | None): supported version. None means do not check
78-
for supported version. namedtuple (major, minor)
77+
7978
8079
"""
8180
full = match.group() # full matched version string
@@ -91,11 +90,11 @@ def rematch(match, *, version=None):
9190
vrsn = Versionage(major=b64ToInt(major), minor=b64ToInt(minor))
9291
if vrsn.major < 2: # version2 vs but major < 2
9392
raise VersionError(f"Incompatible {vrsn=} with version string.")
94-
if version is not None: # compatible version with vrsn
95-
if (vrsn.major > version.major or
96-
(vrsn.major == version.major and vrsn.minor > version.minor)):
97-
raise VersionError(f"Incompatible {version=}, with "
98-
f"{vrsn=}.")
93+
#if version is not None: # compatible version with vrsn
94+
#if (vrsn.major > version.major or
95+
#(vrsn.major == version.major and vrsn.minor > version.minor)):
96+
#raise VersionError(f"Incompatible {version=}, with "
97+
#f"{vrsn=}.")
9998

10099
kind = kind.decode("utf-8")
101100
if kind not in Serials:
@@ -114,11 +113,11 @@ def rematch(match, *, version=None):
114113
vrsn = Versionage(major=int(major, 16), minor=int(minor, 16))
115114
if vrsn.major > 1: # version1 vs but major > 1
116115
raise VersionError(f"Incompatible {vrsn=} with version string.")
117-
if version is not None: # compatible version with vrsn
118-
if (vrsn.major > version.major or
119-
(vrsn.major == version.major and vrsn.minor > version.minor)):
120-
raise VersionError(f"Incompatible {version=}, with "
121-
f"{vrsn=}.")
116+
#if version is not None: # compatible version with vrsn
117+
#if (vrsn.major > version.major or
118+
#(vrsn.major == version.major and vrsn.minor > version.minor)):
119+
#raise VersionError(f"Incompatible {version=}, with "
120+
#f"{vrsn=}.")
122121

123122
kind = kind.decode("utf-8")
124123
if kind not in Serials:
@@ -153,7 +152,7 @@ def versify(protocol=Protos.keri, version=Version, kind=Serials.json, size=0):
153152

154153

155154

156-
def deversify(vs, version=None):
155+
def deversify(vs):
157156
"""
158157
Returns: tuple(proto, kind, version, size) Where:
159158
proto (str): value is protocol type identifier one of Protos (Protocolage)
@@ -166,8 +165,6 @@ def deversify(vs, version=None):
166165
167166
Parameters:
168167
vs (str | bytes): version string to extract from
169-
version (Versionage | None): supported version. None means do not check
170-
for supported version. namedtuple of ints (major, minor)
171168
172169
Uses regex match to extract:
173170
protocol type
@@ -182,12 +179,12 @@ def deversify(vs, version=None):
182179
if not match:
183180
raise VersionError(f"Invalid version string = '{vs}'.")
184181

185-
return rematch(match, version=version)
182+
return rematch(match)
186183

187184

188185

189186

190-
def smell(raw, *, version=None):
187+
def smell(raw):
191188
"""Extract and return instance of Smellage from version string inside
192189
raw serialization.
193190
@@ -198,8 +195,7 @@ def smell(raw, *, version=None):
198195
raw (bytearray) of serialized incoming message stream. Assumes start
199196
of stream is JSON, CBOR, or MGPK field map with first field
200197
is labeled 'v' and value is version string.
201-
version (Versionage | None): instance supported protocol version
202-
None means do not enforce a supported version
198+
203199
"""
204200
if len(raw) < SMELLSIZE:
205201
raise ShortageError(f"Need more raw bytes to smell full version string.")
@@ -209,7 +205,7 @@ def smell(raw, *, version=None):
209205
raise VersionError(f"Invalid version string from smelled raw = "
210206
f"{raw[: SMELLSIZE]}.")
211207

212-
return rematch(match, version=version)
208+
return rematch(match)
213209

214210

215211

tests/core/test_serdering.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,31 +2487,31 @@ def test_serder_v2():
24872487

24882488

24892489
with pytest.raises(ValueError):
2490-
serder = Serder(version=kering.Vrsn_2_0)
2490+
serder = Serder()
24912491

24922492
#Test Serder bare makify bootstrap for ACDC JSON
24932493
serder = Serder(makify=True,
24942494
proto=kering.Protos.acdc,
2495-
vrsn=kering.Vrsn_2_0,
2496-
version=kering.Vrsn_2_0) # make defaults for ACDC
2495+
vrsn=kering.Vrsn_2_0) # make defaults for ACDC
24972496
assert serder.sad == {'v': 'ACDCCAAJSONAABZ.',
24982497
'd': 'EN-uBXL6rsJpJvDSsyOAnttQiI9gka4qLbe3MlIoYwYy',
24992498
'i': '',
25002499
's': ''}
25012500
assert serder.raw == (b'{"v":"ACDCCAAJSONAABZ.","d":"EN-uBXL6rsJpJvDSsyOAnttQiI9gka4qLbe3MlIoYwYy","'
25022501
b'i":"","s":""}')
25032502
assert serder.verify()
2503+
assert serder.vrsn == serder.version == kering.Vrsn_2_0
25042504
sad = serder.sad
25052505
raw = serder.raw
25062506
said = serder.said
25072507
size = serder.size
25082508

2509-
serder = Serder(sad=sad, version=kering.Vrsn_2_0)
2509+
serder = Serder(sad=sad)
25102510
assert serder.raw == raw
25112511
assert isinstance(serder.raw, bytes)
25122512
assert serder.sad == sad
25132513
assert serder.proto == kering.Protos.acdc
2514-
assert serder.vrsn == kering.Vrsn_2_0
2514+
assert serder.vrsn == kering.Vrsn_2_0 == serder.version
25152515
assert serder.size == size
25162516
assert serder.kind == kering.Serials.json
25172517
assert serder.said == said
@@ -2527,12 +2527,12 @@ def test_serder_v2():
25272527
' "s": ""\n'
25282528
'}')
25292529

2530-
serder = Serder(raw=raw, version=kering.Vrsn_2_0)
2530+
serder = Serder(raw=raw)
25312531
assert serder.raw == raw
25322532
assert isinstance(serder.raw, bytes)
25332533
assert serder.sad == sad
25342534
assert serder.proto == kering.Protos.acdc
2535-
assert serder.vrsn == kering.Vrsn_2_0
2535+
assert serder.vrsn == kering.Vrsn_2_0 == serder.version
25362536
assert serder.size == size
25372537
assert serder.kind == kering.Serials.json
25382538
assert serder.said == said
@@ -2541,7 +2541,7 @@ def test_serder_v2():
25412541
assert serder.compare(said=said)
25422542
assert serder.compare(said=said.encode("utf-8"))
25432543

2544-
serder = Serder(sad=sad, makify=True, version=kering.Vrsn_2_0) # test makify with sad
2544+
serder = Serder(sad=sad, makify=True) # test makify with sad
25452545
assert serder.raw == raw
25462546
assert isinstance(serder.raw, bytes)
25472547
assert serder.sad == sad
@@ -2556,8 +2556,7 @@ def test_serder_v2():
25562556

25572557
# test default
25582558
serder = Serder(makify=True,
2559-
vrsn=kering.Vrsn_2_0,
2560-
version=kering.Vrsn_2_0) # make defaults for default proto
2559+
vrsn=kering.Vrsn_2_0) # make defaults for default proto
25612560

25622561

25632562
assert serder.sad == {'v': 'KERICAAJSONAADO.',

0 commit comments

Comments
 (0)