Skip to content

Commit

Permalink
Add type SinglePoolCertificateSequence.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Aug 13, 2020
1 parent 80be998 commit ce3e651
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion lib/core/test/unit/Cardano/Pool/DB/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.Pool.DB.Arbitrary
( StakePoolsFixture (..)
( SinglePoolCertificateSequence (..)
, StakePoolsFixture (..)
, genStakePoolMetadata
, isValidSinglePoolCertificateSequence
) where

import Prelude
Expand All @@ -34,11 +36,15 @@ import Cardano.Wallet.Primitive.Types
, StakePoolMetadataHash (..)
, StakePoolMetadataUrl (..)
, StakePoolTicker (..)
, getPoolCertificatePoolId
, setPoolCertificatePoolId
)
import Control.Arrow
( second )
import Control.Monad
( foldM )
import Data.Function
( (&) )
import Data.Generics.Internal.VL.Lens
( (^.) )
import Data.Ord
Expand All @@ -58,6 +64,7 @@ import Test.QuickCheck
, arbitrarySizedBoundedIntegral
, choose
, elements
, frequency
, genericShrink
, listOf
, oneof
Expand Down Expand Up @@ -169,6 +176,53 @@ instance Arbitrary PoolCertificate where
]
shrink = const []

-- | Represents a valid sequence of registration and retirement certificates
-- for a single pool.
--
data SinglePoolCertificateSequence = SinglePoolCertificateSequence
{ getSinglePoolId :: PoolId
, getSinglePoolCertificateSequence :: [PoolCertificate]
}
deriving (Eq, Show)

isValidSinglePoolCertificateSequence :: SinglePoolCertificateSequence -> Bool
isValidSinglePoolCertificateSequence
(SinglePoolCertificateSequence sharedPoolId certificates) =
allCertificatesReferToSamePool &&
firstCertificateIsRegistration
where
allCertificatesReferToSamePool =
all (== sharedPoolId) (getPoolCertificatePoolId <$> certificates)
firstCertificateIsRegistration = case certificates of
Registration _ : _ -> True
Retirement _ : _ -> False
[] -> True

instance Arbitrary SinglePoolCertificateSequence where

arbitrary = do
sharedPoolId <- arbitrary
frequency
[ (1, genEmptySequence sharedPoolId)
, (9, genNonEmptySequence sharedPoolId)
]
where
genEmptySequence sharedPoolId =
pure $ SinglePoolCertificateSequence sharedPoolId []
genNonEmptySequence sharedPoolId = do
-- We must start with a registration certificate:
certificates <- (:)
<$> (Registration <$> arbitrary)
<*> arbitrary
pure $ SinglePoolCertificateSequence sharedPoolId $
setPoolCertificatePoolId sharedPoolId <$> certificates

shrink (SinglePoolCertificateSequence sharedPoolId certificates) =
genericShrink certificates
& fmap (fmap (setPoolCertificatePoolId sharedPoolId))
& fmap (SinglePoolCertificateSequence sharedPoolId)
& filter isValidSinglePoolCertificateSequence

instance Arbitrary StakePoolMetadataHash where
arbitrary = fmap (StakePoolMetadataHash . BS.pack) (vector 32)

Expand Down

0 comments on commit ce3e651

Please sign in to comment.