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