Skip to content

Commit

Permalink
Document throwable vs structural PredicateFailures
Browse files Browse the repository at this point in the history
Add comments on `PredicateFailure` data declarations.

Rename a few constructors, adding a `S_` prefix to indicate their
structural-ness.

Document the convention in the class definition.
  • Loading branch information
mhuesch committed Aug 20, 2019
1 parent 7e77840 commit 8be14ba
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ instance STS BBODY where

type Signal BBODY = Block

-- | These `PredicateFailure`s are all throwable.
data PredicateFailure BBODY
= InvalidBlockSize
| InvalidUtxoHash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ instance STS SIGCNT where

type Signal SIGCNT = VKey

-- | These `PredicateFailure`s are all throwable.
data PredicateFailure SIGCNT
= TooManyIssuedBlocks VKeyGenesis -- The given genesis key issued too many blocks.
| NotADelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ instance STS UTXO where
type Environment UTXO = UTxOEnv
type State UTXO = UTxOState
type Signal UTXO = Tx

-- | These `PredicateFailure`s are all "throwable". The disjunction of the
-- rules' preconditions is not `True` - the `PredicateFailure`s represent
-- `False` cases.
data PredicateFailure UTXO
= EmptyTxInputs
| EmptyTxOutputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ instance STS UTXOW where
type Environment UTXOW = UTxOEnv
type State UTXOW = UTxOState
type Signal UTXOW = TxWits

-- | These `PredicateFailure`s are all throwable.
data PredicateFailure UTXOW
= UtxoFailure (PredicateFailure UTXO)
| InsufficientWitnesses
Expand Down
24 changes: 14 additions & 10 deletions byron/ledger/executable-spec/src/Ledger/Delegation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ instance STS SDELEG where
type Signal SDELEG = DCert
type Environment SDELEG = DSEnv

-- | These `PredicateFailure`s are all "throwable". The disjunction of the
-- rules' preconditions is not `True` - the `PredicateFailure`s represent
-- `False` cases.
data PredicateFailure SDELEG
= IsNotGenesisKey
| EpochInThePast EpochDiff
Expand Down Expand Up @@ -325,14 +328,15 @@ instance STS ADELEG where
type Signal ADELEG = (Slot, (VKeyGenesis, VKey))
type Environment ADELEG = Set VKeyGenesis

-- | None of these `PredicateFailure`s are actually "throwable". The
-- disjuction of the rules' preconditions is `True`, which means that one of
-- them will pass. The `PredicateFailure` just act as switches to direct
-- control flow to the successful one.
data PredicateFailure ADELEG
= BeforeExistingDelegation
-- | Not actually a failure; this should just trigger the other rule.
| NoLastDelegation
-- | Not a failure; this should just pass the other rule
| AfterExistingDelegation
-- | The given key is a delegate of the given genesis key.
| AlreadyADelegateOf VKey VKeyGenesis
= S_BeforeExistingDelegation
| S_NoLastDelegation
| S_AfterExistingDelegation
| S_AlreadyADelegateOf VKey VKeyGenesis
deriving (Eq, Show, Data, Typeable)

initialRules = [
Expand All @@ -354,11 +358,11 @@ instance STS ADELEG where
}
, (s, (vks, vkd))
) <- judgmentContext
vkd range dms ?! AlreadyADelegateOf vkd (dms !> vkd)
vkd range dms ?! S_AlreadyADelegateOf vkd (dms !> vkd)
case Map.lookup vks dws of
Nothing -> pure () -- If vks hasn't delegated, then we proceed and
-- update the @ADELEG@ state.
Just sp -> sp < s ?! BeforeExistingDelegation
Just sp -> sp < s ?! S_BeforeExistingDelegation
return $!
DState { _dStateDelegationMap = dms [(vks, vkd)]
, _dStateLastDelegation = dws [(vks, s)]
Expand All @@ -374,7 +378,7 @@ instance STS ADELEG where
then return st
else do
case Map.lookup vks dws of
Just sp -> sp >= s ?! AfterExistingDelegation
Just sp -> sp >= s ?! S_AfterExistingDelegation
Nothing -> error $ "This can't happen since otherwise "
++ "the previous rule would have been triggered."
return st
Expand Down
30 changes: 23 additions & 7 deletions byron/ledger/executable-spec/src/Ledger/Update.hs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ instance STS UPSVV where
type State UPSVV = Map UpId (ApName, ApVer, Metadata)
type Signal UPSVV = UProp

-- | These `PredicateFailure`s are all "throwable". The disjunction of the
-- rules' preconditions is not `True` - the `PredicateFailure`s represent
-- `False` cases.
data PredicateFailure UPSVV
= AlreadyProposedSv
| CannotFollowSv
Expand Down Expand Up @@ -332,6 +335,9 @@ instance STS UPPVV where
type State UPPVV = Map UpId (ProtVer, PParams)
type Signal UPPVV = UProp

-- | These `PredicateFailure`s are all "throwable". The disjunction of the
-- rules' preconditions is not `True` - the `PredicateFailure`s represent
-- `False` cases.
data PredicateFailure UPPVV
= CannotFollowPv
| CannotUpdatePv [UpdateConstraintViolation]
Expand Down Expand Up @@ -369,6 +375,7 @@ instance STS UPV where

type Signal UPV = UProp

-- | These `PredicateFailure`s are all throwable.
data PredicateFailure UPV
= UPPVVFailure (PredicateFailure UPPVV)
| UPSVVFailure (PredicateFailure UPSVV)
Expand Down Expand Up @@ -430,6 +437,7 @@ instance STS UPREG where
)
type Signal UPREG = UProp

-- | These `PredicateFailure`s are all throwable.
data PredicateFailure UPREG
= UPVFailure (PredicateFailure UPV)
| NotGenesisDelegate
Expand Down Expand Up @@ -483,6 +491,7 @@ instance STS ADDVOTE where
type State ADDVOTE = Set (UpId, Core.VKeyGenesis)
type Signal ADDVOTE = Vote

-- | These `PredicateFailure`s are all throwable.
data PredicateFailure ADDVOTE
= AVSigDoesNotVerify
| NoUpdateProposal UpId
Expand Down Expand Up @@ -520,10 +529,14 @@ instance STS UPVOTE where
, Set (UpId, Core.VKeyGenesis)
)
type Signal UPVOTE = Vote

-- | `S_CfmThdNotReached` is a structural `PredicateFailure`, used to fail
-- from one transition rule to the other. The other `PredicateFailure`s are
-- all throwable.
data PredicateFailure UPVOTE
= ADDVOTEFailure (PredicateFailure ADDVOTE)
| HigherThanThdAndNotAlreadyConfirmed
| CfmThdNotReached
| S_CfmThdNotReached
| AlreadyConfirmed
deriving (Eq, Show)

Expand All @@ -547,7 +560,7 @@ instance STS UPVOTE where
) <- judgmentContext
vts' <- trans @ADDVOTE $ TRC ((rups, dms), vts, vote)
let pid = vote ^. vPropId
t <= size ([pid] vts') ?! CfmThdNotReached
t <= size ([pid] vts') ?! S_CfmThdNotReached
pid dom cps ?! AlreadyConfirmed
pure $! ( cps [(pid, sn)]
, vts'
Expand Down Expand Up @@ -613,9 +626,12 @@ instance STS UPEND where
)
type Signal UPEND = (ProtVer, Core.VKey)

-- | `S_TryNextRule` is a structural `PredicateFailure`, used to fail from
-- one transition rule to the other. The other `PredicateFailure`s are all
-- throwable.
data PredicateFailure UPEND
= ProtVerUnknown ProtVer
| TryNextRule
| S_TryNextRule
| CanAdopt ProtVer
| CannotAdopt ProtVer
| NotADelegate VKey
Expand All @@ -634,7 +650,7 @@ instance STS UPEND where
Just (pid, _) -> do
-- If we found the proposal id that corresponds to 'bv' then we
-- have to check that it isn't confirmed for this rule to succeed.
pid dom (cps ▷<= sn -. 2 *. k) ?! TryNextRule
pid dom (cps ▷<= sn -. 2 *. k) ?! S_TryNextRule
pure $! (fads, bvs)
Nothing ->
-- If we didn't find the proposal id that corresponds to 'bv' then
Expand All @@ -654,17 +670,17 @@ instance STS UPEND where
) <- judgmentContext
case lookupR vk dms of
Nothing -> do
False ?! TryNextRule
False ?! S_TryNextRule
pure $! (fads, bvs)
Just vks -> do
let bvs' = bvs singleton bv vks
size ([bv] bvs') < t ?! CanAdopt bv
case findKey ((== bv) . fst) rpus of
Just (pid, _) -> do
pid dom (cps ▷<= sn -. 2 *. k) ?! TryNextRule
pid dom (cps ▷<= sn -. 2 *. k) ?! S_TryNextRule
pure $! (fads, bvs')
Nothing -> do
False ?! TryNextRule
False ?! S_TryNextRule
pure $! (fads, bvs')

, do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class ( Eq (PredicateFailure a)

-- | Descriptive type for the possible failures which might cause a transition
-- to fail.
--
-- As a convention, `PredicateFailure`s which are "structural" (meaning that
-- they are not "throwable" in practice, and are used to pass control from
-- one transition rule to another) are prefixed with `S_`.
data PredicateFailure a :: *

-- | Rules governing transition under this system.
Expand Down

0 comments on commit 8be14ba

Please sign in to comment.