Skip to content

Commit

Permalink
Add verifySelection function to verify the correctness of a selection.
Browse files Browse the repository at this point in the history
We also add a property `prop_performSelection_onSuccess_isCorrect`.

We will use this property to replace the other correctness properties,
step by step, in later commits.
  • Loading branch information
jonathanknowles committed Oct 12, 2021
1 parent 6bca834 commit 9e75591
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
@@ -1,5 +1,6 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE EmptyDataDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NamedFieldPuns #-}
Expand Down Expand Up @@ -38,6 +39,10 @@ module Cardano.Wallet.Primitive.CoinSelection
, SelectionOutputSizeExceedsLimitError (..)
, SelectionOutputTokenQuantityExceedsLimitError (..)

-- * Selection correctness
, SelectionCorrectness (..)
, verifySelection

-- * Selection deltas
, SelectionDelta (..)
, selectionDelta
Expand Down Expand Up @@ -338,6 +343,42 @@ toBalanceResult selection = Balance.SelectionResult
, extraCoinSink = view #extraCoinSink selection
}

--------------------------------------------------------------------------------
-- Selection correctness
--------------------------------------------------------------------------------

-- | Indicates whether or not a selection is correct.
--
data SelectionCorrectness
= SelectionCorrect
| SelectionIncorrect SelectionCorrectnessError
deriving (Eq, Show)

-- | Indicates that a selection is incorrect.
--
data SelectionCorrectnessError
deriving (Eq, Show)

-- | Verifies a selection for correctness.
--
-- This function is provided primarily as a convenience for testing. As such,
-- it's not usually necessary to call this function from ordinary application
-- code, unless you suspect that a selection is incorrect in some way.
--
verifySelection
:: SelectionConstraints
-> SelectionParams
-> Selection
-> SelectionCorrectness
verifySelection cs ps selection =
either SelectionIncorrect (const SelectionCorrect) verifyAll
where
verifyAll :: Either SelectionCorrectnessError ()
verifyAll = Right ()

failWith :: Maybe e1 -> (e1 -> e2) -> Either e2 ()
onError `failWith` thisError = maybe (Right ()) (Left . thisError) onError

--------------------------------------------------------------------------------
-- Selection deltas
--------------------------------------------------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelectionSpec.hs
Expand Up @@ -17,6 +17,7 @@ import Cardano.Wallet.Primitive.CoinSelection
, Selection
, SelectionCollateralRequirement (..)
, SelectionConstraints (..)
, SelectionCorrectness (..)
, SelectionError (..)
, SelectionParams (..)
, computeMinimumCollateral
Expand All @@ -30,6 +31,7 @@ import Cardano.Wallet.Primitive.CoinSelection
, selectionMinimumCollateral
, selectionMinimumCost
, toBalanceConstraintsParams
, verifySelection
)
import Cardano.Wallet.Primitive.CoinSelection.Balance
( SelectionLimit, SelectionLimitOf (..), SelectionSkeleton )
Expand Down Expand Up @@ -132,6 +134,9 @@ spec = describe "Cardano.Wallet.Primitive.CoinSelectionSpec" $ do

parallel $ describe "Performing selections" $ do

it "prop_performSelection_onSuccess_isCorrect" $
prop_performSelection_onSuccess
prop_performSelection_onSuccess_isCorrect
it "prop_performSelection_onSuccess_hasValidSurplus" $
prop_performSelection_onSuccess
prop_performSelection_onSuccess_hasValidSurplus
Expand Down Expand Up @@ -243,6 +248,11 @@ prop_performSelection_onSuccess onSuccess = property $
prop_performSelection_with $ \constraints params ->
either (const $ property True) (onSuccess constraints params)

prop_performSelection_onSuccess_isCorrect
:: PerformSelectionPropertyOnSuccess
prop_performSelection_onSuccess_isCorrect cs ps selection =
Pretty (verifySelection cs ps selection) === Pretty SelectionCorrect

prop_performSelection_onSuccess_hasValidSurplus
:: PerformSelectionPropertyOnSuccess
prop_performSelection_onSuccess_hasValidSurplus cs ps selection =
Expand Down

0 comments on commit 9e75591

Please sign in to comment.