Skip to content

Commit

Permalink
Add a further check for index set consistency.
Browse files Browse the repository at this point in the history
In response to review feedback:

#3217 (comment)
  • Loading branch information
jonathanknowles committed Apr 5, 2022
1 parent c568314 commit 8c6b725
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/core/src/Cardano/Wallet/Primitive/Types/UTxOIndex/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ data InvariantStatus
-- ^ Indicates that the 'index' is missing one or more entries.
| InvariantIndexNonMinimal
-- ^ Indicates that the 'index' has one or more unnecessary entries.
| InvariantIndexInconsistent
-- ^ Indicates that the index sets are not consistent.
| InvariantAssetsInconsistent
-- ^ Indicates that the 'index' and the cached 'balance' value disagree
-- about which assets are included.
Expand All @@ -625,6 +627,8 @@ checkInvariant i
InvariantIndexIncomplete
| not (indexIsMinimal i) =
InvariantIndexNonMinimal
| not (indexIsConsistent i) =
InvariantIndexInconsistent
| not (assetsConsistent i) =
InvariantAssetsInconsistent
| otherwise =
Expand Down Expand Up @@ -729,6 +733,38 @@ indexIsMinimal i = F.and
entryMatches :: (TokenBundle -> Bool) -> u -> Bool
entryMatches test u = maybe False test $ Map.lookup u $ universe i

-- | Checks that index set relationships are correct.
--
indexIsConsistent :: Ord u => UTxOIndex u -> Bool
indexIsConsistent i = F.and
[ indexSingletons i
`isDisjointTo` indexPairs i
, indexSingletons i
`isSubmapOf` indexAll i
, indexPairs i
`isSubmapOf` indexAll i
]
where
isDisjointTo
:: Ord u
=> Map a (NonEmptySet u)
-> Map a (NonEmptySet u)
-> Bool
isDisjointTo m1 m2 = s1 `Set.disjoint` s2
where
s1 = F.foldMap NonEmptySet.toSet m1
s2 = F.foldMap NonEmptySet.toSet m2

isSubmapOf
:: (Ord a, Ord u)
=> Map a (NonEmptySet u)
-> Map a (NonEmptySet u)
-> Bool
isSubmapOf m1 m2 = Map.isSubmapOfBy isNonEmptySubsetOf m1 m2
where
isNonEmptySubsetOf s1 s2 =
NonEmptySet.toSet s1 `Set.isSubsetOf` NonEmptySet.toSet s2

-- | Checks that the asset sets are consistent.
--
-- In particular, the set of assets in the cached 'balance' must be:
Expand Down

0 comments on commit 8c6b725

Please sign in to comment.