Skip to content

Commit

Permalink
Merge pull request #646 from SmithSamuelM/misfit
Browse files Browse the repository at this point in the history
removed redundant misfit checks refactored .locallWitnessed method ot…
  • Loading branch information
SmithSamuelM authored Dec 31, 2023
2 parents 1a1bc69 + 2b172d0 commit 7069064
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 58 deletions.
16 changes: 10 additions & 6 deletions src/keri/app/habbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def __init__(self, *, name='test', base="", temp=False,
if self.db.opened and self.ks.opened:
self.setup(**self._inits) # finish setup later


def setup(self, *, seed=None, aeid=None, bran=None, pidx=None, algo=None,
salt=None, tier=None, free=False, temp=None, ):
"""
Expand Down Expand Up @@ -313,6 +314,7 @@ def setup(self, *, seed=None, aeid=None, bran=None, pidx=None, algo=None,
self.loadHabs()
self.inited = True


def loadHabs(self):
"""Load Habs instance from db
Expand Down Expand Up @@ -349,9 +351,9 @@ def loadHabs(self):
rtr=self.rtr, rvy=self.rvy, kvy=self.kvy, psr=self.psr,
name=name, pre=pre, temp=self.temp)

# Rules for acceptance
# if its delegated its accepted into its own local KEL even if the
# delegator has not sealed it
# Rules for acceptance:
# It is accepted into its own local KEL even if it has not been fully
# witnessed and if delegated, its delegator has not yet sealed it
if not hab.accepted and not habord.mid:
raise kering.ConfigurationError(f"Problem loading Hab pre="
f"{pre} name={name} from db.")
Expand Down Expand Up @@ -385,9 +387,9 @@ def loadHabs(self):
rtr=self.rtr, rvy=self.rvy, kvy=self.kvy, psr=self.psr,
name=name, ns=ns, pre=pre, temp=self.temp)

# Rules for acceptance
# if its delegated its accepted into its own local KEL even if the
# delegator has not sealed it
# Rules for acceptance:
# It is accepted into its own local KEL even if it has not been fully
# witnessed and if delegated, its delegator has not yet sealed it
if not hab.accepted and not habord.mid:
raise kering.ConfigurationError(f"Problem loading Hab pre="
f"{pre} name={name} from db.")
Expand All @@ -404,6 +406,7 @@ def loadHabs(self):

self.reconfigure() # post hab load reconfiguration


def makeHab(self, name, ns=None, cf=None, **kwa):
"""Make new Hab with name, pre is generated from **kwa
Expand Down Expand Up @@ -2658,6 +2661,7 @@ def __init__(self, smids, mhab=None, rmids=None, **kwa):

super(GroupHab, self).__init__(**kwa)


def make(self, *, code=coring.MtrDex.Blake3_256, transferable=True, isith=None, nsith=None,
toad=None, wits=None, delpre=None, estOnly=False, DnD=False,
merfers, migers=None, data=None):
Expand Down
93 changes: 42 additions & 51 deletions src/keri/core/eventing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ def locallyMembered(self, pre):
"""Returns True if group hab identifier prefix has as a contributing
member a locally owned prefix
Reads habs database to figure this out.
Use db.prefixes and db.gids to figure this out
Parameters:
pre (str|None): qb64 identifier prefix or None
Expand All @@ -1798,22 +1798,23 @@ def locallyMembered(self, pre):
return True


def locallyWitnessed(self, serder=None):
def locallyWitnessed(self, *, wits=None, serder=None):
"""Returns True if a local controller is a witness of this Kever's KEL
of wits in serder of if None then current wits for this Kever.
i.e. self is witnessd by locally owned (controlled) AID (identifier prefix)
Parameters:
wits (list[str]): qb64 identifier prefixes of witnesses
serder ( SerderKERI | None): SerderKERI instace if any
"""
if serder and serder.pre != self.prefixer.qb64: # not same KEL as self
return False

if not serder:
wits = self.wits
else:
wits, _, _ = self.deriveBacks(serder=serder)
if not wits:
if not serder:
wits = self.wits
else:
if serder.pre != self.prefixer.qb64: # not same KEL as self
return False
wits, _, _ = self.deriveBacks(serder=serder)

return True if (self.prefixes & oset(wits)) else False

Expand Down Expand Up @@ -2297,17 +2298,6 @@ def valSigsWigsDel(self, serder, sigers, verfers, tholder,
If this event is not delegated then saider is ignored
"""
if (not local and
(self.locallyOwned(serder.pre) or
self.locallyWitnessed(serder))):

self.escrowMFEvent(serder=serder, sigers=sigers, wigers=wigers,
seqner=delseqner, saider=delsaider, local=local)
raise MisfitEventSourceError(f"Nonlocal source for locally owned"
f"or locally witnessed event"
f" = {serder.ked}.")


if len(verfers) < tholder.size:
raise ValidationError("Invalid sith = {} for keys = {} for evt = {}."
"".format(tholder.sith,
Expand All @@ -2327,13 +2317,26 @@ def valSigsWigsDel(self, serder, sigers, verfers, tholder,
raise ValidationError("No verified signatures for evt = {}."
"".format(serder.ked))

# Misfit check events that must be locally sourced (protected) get
# escrowed in order to repair the protection when appropriate
if (not local and
(self.locallyOwned(serder.pre) or
self.locallyWitnessed(wits=wits))):

self.escrowMFEvent(serder=serder, sigers=sigers, wigers=wigers,
seqner=delseqner, saider=delsaider, local=local)
raise MisfitEventSourceError(f"Nonlocal source for locally owned"
f"or locally witnessed event"
f" = {serder.ked}.")


werfers = [Verfer(qb64=wit) for wit in wits] # get witnes signatures

# get unique verified wigers and windices lists from wigers list
wigers, windices = verifySigs(raw=serder.raw, sigers=wigers, verfers=werfers)
# each wiger now has added to it a werfer of its wit in its .verfer property

# escrow if not fully signed vs threshold
# escrow if not fully signed vs signing threshold
if not tholder.satisfy(indices): # at least one but not enough
self.escrowPSEvent(serder=serder, sigers=sigers, wigers=wigers, local=local)
if delseqner and delsaider:
Expand All @@ -2342,6 +2345,7 @@ def valSigsWigsDel(self, serder, sigers, verfers, tholder,
f" on sigs for {[siger.qb64 for siger in sigers]}"
f" for evt = {serder.ked}.")

# escrow if not fully signed vs prior next rotation threshold
if serder.ilk in (Ilks.rot, Ilks.drt): # rotation so check prior next threshold
# prior next threshold in .ntholder and digers in .ndigers
ondices = self.exposeds(sigers)
Expand Down Expand Up @@ -2376,17 +2380,11 @@ def valSigsWigsDel(self, serder, sigers, verfers, tholder,

# short circuit witness validation when either locallyOwned or locallyWitnessed
# otherwise must validate fully witnessed
if not (self.locallyOwned(serder.pre) or self.locallyWitnessed(serder=serder)):
#if ((wits and not self.prefixes) or # in promiscuous mode so assume must verify toad
#(wits and self.prefixes and not local and # not promiscuous nonlocal
#not (oset(self.prefixes) & oset(wits))) or # own prefix is not a witness not self.locallyWitnessed(serder)
#(local and self.locallyOwned(delpre))): # local delegator needs to be witnessed
# validate that event is fully witnessed

if wits:
if not (self.locallyOwned(serder.pre) or self.locallyWitnessed(wits=wits)):
if wits: # is witnessed
if toader.num < 1 or toader.num > len(wits): # out of bounds toad
raise ValidationError(f"Invalid toad = {toader.num} for wits = {wits}")
else:
else: # not witnessed
if toader.num != 0: # invalid toad
raise ValidationError(f"Invalid toad = {toader.num} for wits = {wits}")

Expand All @@ -2402,7 +2400,8 @@ def valSigsWigsDel(self, serder, sigers, verfers, tholder,
f"for event={serder.ked}.")

if delpre:
self.validateDelegation(serder, sigers=sigers, wigers=wigers,
self.validateDelegation(serder, sigers=sigers,
wigers=wigers, wits=wits,
local=local, delpre=delpre,
delseqner=delseqner, delsaider=delsaider)

Expand Down Expand Up @@ -2456,10 +2455,13 @@ def exposeds(self, sigers):
return odxs


def validateDelegation(self, serder, sigers, wigers=None, local=True,
def validateDelegation(self, serder, sigers, wigers, wits, local=True,
delpre=None, delseqner=None, delsaider=None):
"""
Returns delegator's qb64 identifier prefix if validation successful.
Assumes that local vs remote source checks have been applied before
this function is called.
Rules:
If event is not a delegated event then not valid delegation
If delegatee's own event (.mine) then valid delegation
Expand All @@ -2473,10 +2475,12 @@ def validateDelegation(self, serder, sigers, wigers=None, local=True,
Parameters:
serder (SerderKERI): instance of delegated event serder
sigers (list): of Siger instances of indexed controller sigs of
sigers (list[Siger]): of Siger instances of indexed controller sigs of
delegated event. Assumes sigers is list of unique verified sigs
wigers (list | None): of optional Siger instance of indexed witness sigs of
wigers (list[Siger]): of optional Siger instance of indexed witness sigs of
delegated event. Assumes wigers is list of unique verified sigs
wits (list[str]): of qb64 non-transferable prefixes of witnesses used to
derive werfers for wigers
local (bool): event source for validation logic
True means event source is local (protected).
False means event source is remote (unprotected).
Expand All @@ -2491,7 +2495,7 @@ def validateDelegation(self, serder, sigers, wigers=None, local=True,
(str | None): qb64 delegator prefix or None if not delegated
Process Logic:
A delegative event is processed differently for each of fourt different
A delegative event is processed differently for each of four different
parties, namely, controller of event, witness to controller of event,
delegator of event , and validator of event that is not controller,
witness or delegator. Events are processed as either local (protected)
Expand Down Expand Up @@ -2633,7 +2637,7 @@ def validateDelegation(self, serder, sigers, wigers=None, local=True,
superseding rotation must be discarded.
"""
if not delpre:
if not delpre: # not delegable so no delegation validation needed
return

# if we are the delegatee, accept the event without requiring the
Expand All @@ -2643,13 +2647,7 @@ def validateDelegation(self, serder, sigers, wigers=None, local=True,
# seal to be anchored in delegator's KEL.
# Witness accepts without waiting for delegation seal to be anchored in
# delegator's KEL. Witness cue in Kevery will then generate receipt

if self.locallyOwned(serder.pre) or self.locallyWitnessed(serder=serder):
if not local: # nonlocal remote event source so misfit
self.escrowMFEvent(serder=serder, sigers=sigers, wigers=wigers, local=local)
raise MisfitEventSourceError(f"Nonlocal source for locally owned"
f"or locally witnessed event"
f" = {serder.ked}.")
if self.locallyOwned(serder.pre) or self.locallyWitnessed(wits=wits):
return


Expand All @@ -2670,15 +2668,8 @@ def validateDelegation(self, serder, sigers, wigers=None, local=True,
# Once fully receipted, cue in Kevery will then trigger cue to approve
# delegation


# during initial delegation we just escrow the delcept event
if delseqner is None and delsaider is None:
if self.locallyOwned(delpre): # local delegator
if not local: # events for local delegator must be sourced locally
self.escrowMFEvent(serder=serder, sigers=sigers, wigers=wigers, local=local)
raise MisfitEventSourceError(f"Nonlocal source for locally"
f" delegated by {delpre} of"
f"event = {serder.ked}.")
if self.locallyOwned(delpre): # local delegator so virtual delegation
# create virtual anchor seal so local delegator can evaluate
# superseding logic with provisional virtual seal
dkever = self.kevers[delpre]
Expand Down
4 changes: 3 additions & 1 deletion src/keri/db/basing.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,8 @@ def __init__(self, headDirPath=None, reopen=False, **kwa):
"""
self.prefixes = oset()
self.prefixes = oset() # should change to hids for hab ids
self.gids = oset() # group hab ids
self._kevers = dbdict()
self._kevers.db = self # assign db for read through cache of kevers

Expand Down Expand Up @@ -1179,6 +1180,7 @@ def reopen(self, **kwa):

return self.env


def reload(self):
"""
Reload stored prefixes and Kevers from .habs
Expand Down

0 comments on commit 7069064

Please sign in to comment.