Skip to content

Commit

Permalink
Make the arbitraryScriptOf generator more general and use it more
Browse files Browse the repository at this point in the history
Rather than arbitraryShortScriptOf which was fixed to scripts of exactly
5, we now pick any size between 1 and any max size (but still capped by
the QC size). So this will give more variation in script size.

For gossip scripts, this will give more variation, and we also change
the max size from being 5 to the square root of the QC size, so that
will scale up to 10 for size 100.
  • Loading branch information
dcoutts committed May 4, 2021
1 parent 5251c6f commit d4d1593
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Expand Up @@ -255,7 +255,8 @@ instance Arbitrary PeerGraph where

arbitraryGossipScript :: [PeerAddr] -> Gen GossipScript
arbitraryGossipScript peers =
arbitraryShortScriptOf gossipResult
sized $ \sz ->
arbitraryScriptOf (isqrt sz) gossipResult
where
gossipResult :: Gen (Maybe ([PeerAddr], GossipTime))
gossipResult =
Expand All @@ -268,6 +269,9 @@ arbitraryGossipScript peers =
picked <- vectorOf (length xs) arbitrary
return [ x | (x, True) <- zip xs picked ]

isqrt :: Int -> Int
isqrt = floor . sqrt . (fromIntegral :: Int -> Double)

-- | Remove dangling graph edges and gossip results.
--
prunePeerGraphEdges :: [(PeerAddr, [PeerAddr], PeerInfo)]
Expand Down
Expand Up @@ -10,7 +10,7 @@ module Test.Ouroboros.Network.PeerSelection.Script (
initScript,
stepScript,
stepScriptSTM,
arbitraryShortScriptOf,
arbitraryScriptOf,

-- * Timed scripts
ScriptDelay(..),
Expand Down Expand Up @@ -51,10 +51,11 @@ singletonScript x = (Script (x :| []))
scriptHead :: Script a -> a
scriptHead (Script (x :| _)) = x

arbitraryShortScriptOf :: Gen a -> Gen (Script a)
arbitraryShortScriptOf a =
sized $ \sz ->
(Script . NonEmpty.fromList) <$> vectorOf (min 5 (sz+1)) a
arbitraryScriptOf :: Int -> Gen a -> Gen (Script a)
arbitraryScriptOf maxSz a =
sized $ \sz -> do
n <- choose (1, max 1 (min maxSz sz))
(Script . NonEmpty.fromList) <$> vectorOf n a

initScript :: MonadSTM m => Script a -> m (TVar m (Script a))
initScript = newTVarIO
Expand All @@ -71,7 +72,7 @@ stepScriptSTM scriptVar = do
return x

instance Arbitrary a => Arbitrary (Script a) where
arbitrary = (Script . NonEmpty.fromList) <$> listOf1 arbitrary
arbitrary = sized $ \sz -> arbitraryScriptOf sz arbitrary

shrink (Script (x :| [])) = [ Script (x' :| []) | x' <- shrink x ]
shrink (Script (x :| xs)) =
Expand Down

0 comments on commit d4d1593

Please sign in to comment.