|
| 1 | +{-# LANGUAGE FlexibleContexts #-} |
| 2 | +{-# LANGUAGE FlexibleInstances #-} |
| 3 | +{-# LANGUAGE MultiParamTypeClasses #-} |
| 4 | +{-# LANGUAGE NamedFieldPuns #-} |
| 5 | +{-# LANGUAGE RankNTypes #-} |
| 6 | +{-# LANGUAGE TypeApplications #-} |
| 7 | +{-# LANGUAGE UndecidableInstances #-} |
| 8 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 9 | +{-# OPTIONS_GHC -Wno-unused-top-binds #-} |
| 10 | + |
| 11 | +module Test.Consensus.MiniProtocol.ObjectDiffusion.PerasCert.Smoke (tests) where |
| 12 | + |
| 13 | +import Control.Monad.IOSim (IOSim) |
| 14 | +import Control.Tracer (Tracer (..), contramap, nullTracer) |
| 15 | +import Data.Functor.Identity (Identity (..)) |
| 16 | +import qualified Data.List.NonEmpty as NE |
| 17 | +import Debug.Trace (traceM) |
| 18 | +import Network.TypedProtocol.Driver.Simple (runPeer, runPipelinedPeer) |
| 19 | +import Ouroboros.Consensus.Block.SupportsPeras |
| 20 | +import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.API |
| 21 | +import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.PerasCert |
| 22 | +import Ouroboros.Consensus.Storage.PerasCertDB.API |
| 23 | + ( AddPerasCertResult (..) |
| 24 | + , PerasCertDB |
| 25 | + , PerasCertTicketNo |
| 26 | + ) |
| 27 | +import qualified Ouroboros.Consensus.Storage.PerasCertDB.API as PerasCertDB |
| 28 | +import qualified Ouroboros.Consensus.Storage.PerasCertDB.Impl as PerasCertDB |
| 29 | +import Ouroboros.Consensus.Util.IOLike |
| 30 | +import Ouroboros.Network.Block (Point (..), SlotNo (SlotNo), StandardHash) |
| 31 | +import Ouroboros.Network.Point (Block (Block), WithOrigin (..)) |
| 32 | +import Ouroboros.Network.Protocol.ObjectDiffusion.Codec |
| 33 | +import Ouroboros.Network.Protocol.ObjectDiffusion.Inbound |
| 34 | + ( objectDiffusionInboundServerPeerPipelined |
| 35 | + ) |
| 36 | +import Ouroboros.Network.Protocol.ObjectDiffusion.Outbound (objectDiffusionOutboundClientPeer) |
| 37 | +import Test.Consensus.MiniProtocol.ObjectDiffusion.Smoke |
| 38 | + ( ListWithUniqueIds (..) |
| 39 | + , WithId |
| 40 | + , getId |
| 41 | + , prop_smoke_object_diffusion |
| 42 | + ) |
| 43 | +import Test.QuickCheck |
| 44 | +import Test.Tasty |
| 45 | +import Test.Tasty.QuickCheck (testProperty) |
| 46 | +import Test.Util.TestBlock |
| 47 | + |
| 48 | +-- When wanting to debug a part of the test: |
| 49 | +debugTracer :: Monad m => Tracer m String |
| 50 | +debugTracer = Tracer traceM |
| 51 | + |
| 52 | +tests :: TestTree |
| 53 | +tests = |
| 54 | + testGroup |
| 55 | + "ObjectDiffusion.PerasCert.Smoke" |
| 56 | + [ testProperty "PerasCertDiffusion smoke test" prop_smoke |
| 57 | + ] |
| 58 | + |
| 59 | +instance Arbitrary (Point TestBlock) where |
| 60 | + arbitrary = |
| 61 | + -- Sometimes pick the genesis point |
| 62 | + frequency |
| 63 | + [ (1, pure $ Point Origin) |
| 64 | + , |
| 65 | + ( 4 |
| 66 | + , do |
| 67 | + slotNo <- SlotNo <$> arbitrary |
| 68 | + hash <- TestHash . NE.fromList . getNonEmpty <$> arbitrary |
| 69 | + pure $ Point (At (Block slotNo hash)) |
| 70 | + ) |
| 71 | + ] |
| 72 | + |
| 73 | +instance Arbitrary (Point blk) => Arbitrary (PerasCert blk) where |
| 74 | + arbitrary = do |
| 75 | + pcCertRound <- PerasRoundNo <$> arbitrary |
| 76 | + pcCertBoostedBlock <- arbitrary |
| 77 | + pure $ PerasCert{pcCertRound, pcCertBoostedBlock} |
| 78 | + |
| 79 | +instance WithId (PerasCert blk) PerasRoundNo where |
| 80 | + getId = pcCertRound |
| 81 | + |
| 82 | +newCertDB :: (IOLike m, StandardHash blk) => [PerasCert blk] -> m (PerasCertDB m blk) |
| 83 | +newCertDB certs = do |
| 84 | + db <- PerasCertDB.openDB (PerasCertDB.PerasCertDbArgs @Identity nullTracer) |
| 85 | + mapM_ |
| 86 | + ( \cert -> do |
| 87 | + result <- PerasCertDB.addCert db cert |
| 88 | + case result of |
| 89 | + AddedPerasCertToDB -> pure () |
| 90 | + PerasCertAlreadyInDB -> throwIO (userError "Expected AddedPerasCertToDB, but cert was already in DB") |
| 91 | + ) |
| 92 | + certs |
| 93 | + pure db |
| 94 | + |
| 95 | +prop_smoke :: ListWithUniqueIds (PerasCert TestBlock) PerasRoundNo -> Property |
| 96 | +prop_smoke (ListWithUniqueIds certs) = |
| 97 | + prop_smoke_object_diffusion certs mkOutboundAsync mkInboundAsync mkPoolInterfaces |
| 98 | + where |
| 99 | + mkOutboundAsync outbound outboundChannel tracer = |
| 100 | + async $ |
| 101 | + ( () |
| 102 | + <$ runPeer |
| 103 | + ((\x -> "Outbound (Client): " ++ show x) `contramap` tracer) |
| 104 | + codecObjectDiffusionId |
| 105 | + outboundChannel |
| 106 | + (objectDiffusionOutboundClientPeer outbound) |
| 107 | + ) |
| 108 | + mkInboundAsync inbound inboundChannel tracer = |
| 109 | + async $ |
| 110 | + ( () |
| 111 | + <$ runPipelinedPeer |
| 112 | + ((\x -> "Inbound (Server): " ++ show x) `contramap` tracer) |
| 113 | + codecObjectDiffusionId |
| 114 | + inboundChannel |
| 115 | + (objectDiffusionInboundServerPeerPipelined inbound) |
| 116 | + ) |
| 117 | + mkPoolInterfaces :: |
| 118 | + forall s. |
| 119 | + IOSim |
| 120 | + s |
| 121 | + ( ObjectPoolReader PerasRoundNo (PerasCert TestBlock) PerasCertTicketNo (IOSim s) |
| 122 | + , ObjectPoolReader PerasRoundNo (PerasCert TestBlock) PerasCertTicketNo (IOSim s) |
| 123 | + , ObjectPoolWriter PerasRoundNo (PerasCert TestBlock) (IOSim s) |
| 124 | + ) |
| 125 | + mkPoolInterfaces = do |
| 126 | + outboundPool <- newCertDB certs |
| 127 | + inboundPool <- newCertDB [] |
| 128 | + |
| 129 | + let outboundPoolReader = makePerasCertPoolReaderFromCertDB outboundPool |
| 130 | + inboundPoolReader = makePerasCertPoolReaderFromCertDB inboundPool |
| 131 | + inboundPoolWriter = makePerasCertPoolWriterFromCertDB inboundPool |
| 132 | + |
| 133 | + return (outboundPoolReader, inboundPoolReader, inboundPoolWriter) |
0 commit comments