|
| 1 | +{-# LANGUAGE FlexibleContexts #-} |
| 2 | +{-# LANGUAGE FlexibleInstances #-} |
| 3 | +{-# LANGUAGE MultiParamTypeClasses #-} |
| 4 | +{-# LANGUAGE NamedFieldPuns #-} |
| 5 | +{-# LANGUAGE TypeApplications #-} |
| 6 | +{-# LANGUAGE UndecidableInstances #-} |
| 7 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 8 | +{-# OPTIONS_GHC -Wno-unused-top-binds #-} |
| 9 | + |
| 10 | +module Test.Consensus.MiniProtocol.ObjectDiffusion.Cert.Smoke (tests) where |
| 11 | + |
| 12 | +import Control.Monad.IOSim (runSimStrictShutdown) |
| 13 | +import Control.Tracer (Tracer (..), contramap, nullTracer, traceWith) |
| 14 | +import Data.Functor.Identity (Identity (..)) |
| 15 | +import qualified Data.List.NonEmpty as NE |
| 16 | +import Debug.Trace (traceM) |
| 17 | +import Network.TypedProtocol.Channel (createConnectedChannels) |
| 18 | +import Network.TypedProtocol.Driver.Simple (runPeer, runPipelinedPeer) |
| 19 | +import Ouroboros.Consensus.Block.SupportsPeras |
| 20 | +import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.Inbound |
| 21 | +import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.Cert |
| 22 | +import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.Outbound |
| 23 | +import Ouroboros.Consensus.Node.NetworkProtocolVersion (NodeToNodeVersion (NodeToNodeV_14)) |
| 24 | +import Ouroboros.Consensus.Storage.PerasCertDB.API (AddPerasCertResult (..), PerasCertDB) |
| 25 | +import qualified Ouroboros.Consensus.Storage.PerasCertDB.API as PerasCertDB |
| 26 | +import qualified Ouroboros.Consensus.Storage.PerasCertDB.Impl as PerasCertDB |
| 27 | +import Ouroboros.Consensus.Util.IOLike |
| 28 | +import Ouroboros.Network.Block (Point (..), SlotNo (SlotNo), StandardHash) |
| 29 | +import Ouroboros.Network.ControlMessage (ControlMessage (..)) |
| 30 | +import Ouroboros.Network.Diffusion.Configuration |
| 31 | +import Ouroboros.Network.Point (Block (Block), WithOrigin (..)) |
| 32 | +import Ouroboros.Network.Protocol.ObjectDiffusion.Codec |
| 33 | +import Ouroboros.Network.Protocol.ObjectDiffusion.Inbound |
| 34 | + ( objectDiffusionNonInitInboundPeerPipelined |
| 35 | + ) |
| 36 | +import Ouroboros.Network.Protocol.ObjectDiffusion.Outbound (objectDiffusionInitOutboundPeer) |
| 37 | +import Test.Consensus.MiniProtocol.ObjectDiffusion.Smoke (ListWithUniqueIds (..), WithId, getId) |
| 38 | +import Test.QuickCheck |
| 39 | +import Test.Tasty |
| 40 | +import Test.Tasty.QuickCheck (testProperty) |
| 41 | +import Test.Util.TestBlock |
| 42 | + |
| 43 | +-- When wanting to debug a part of the test: |
| 44 | +debugTracer :: Monad m => Tracer m String |
| 45 | +debugTracer = Tracer traceM |
| 46 | + |
| 47 | +tests :: TestTree |
| 48 | +tests = |
| 49 | + testGroup |
| 50 | + "ObjectDiffusion.Cert.Smoke" |
| 51 | + [ testProperty "smoke" prop_smoke |
| 52 | + ] |
| 53 | + |
| 54 | +instance Arbitrary (Point TestBlock) where |
| 55 | + arbitrary = |
| 56 | + -- Sometimes pick the genesis point |
| 57 | + frequency |
| 58 | + [ (1, pure $ Point Origin) |
| 59 | + , |
| 60 | + ( 4 |
| 61 | + , do |
| 62 | + slotNo <- SlotNo <$> arbitrary |
| 63 | + hash <- TestHash . NE.fromList . getNonEmpty <$> arbitrary |
| 64 | + pure $ Point (At (Block slotNo hash)) |
| 65 | + ) |
| 66 | + ] |
| 67 | + |
| 68 | +instance Arbitrary (Point blk) => Arbitrary (PerasCert blk) where |
| 69 | + arbitrary = do |
| 70 | + pcCertRound <- PerasRoundNo <$> arbitrary |
| 71 | + pcCertBoostedBlock <- arbitrary |
| 72 | + pure $ PerasCert{pcCertRound, pcCertBoostedBlock} |
| 73 | + |
| 74 | +instance WithId (PerasCert blk) PerasRoundNo where |
| 75 | + getId = pcCertRound |
| 76 | + |
| 77 | +newCertDB :: (IOLike m, StandardHash blk) => [PerasCert blk] -> m (PerasCertDB m blk) |
| 78 | +newCertDB certs = do |
| 79 | + db <- PerasCertDB.openDB (PerasCertDB.PerasCertDbArgs @Identity nullTracer) |
| 80 | + mapM_ |
| 81 | + ( \cert -> do |
| 82 | + result <- PerasCertDB.addCert db cert |
| 83 | + case result of |
| 84 | + AddedPerasCertToDB -> pure () |
| 85 | + PerasCertAlreadyInDB -> throwIO (userError "Expected AddedPerasCertToDB, but cert was already in DB") |
| 86 | + ) |
| 87 | + certs |
| 88 | + pure db |
| 89 | + |
| 90 | +prop_smoke :: ListWithUniqueIds (PerasCert TestBlock) PerasRoundNo -> Property |
| 91 | +prop_smoke (ListWithUniqueIds certs) = |
| 92 | + case simulationResult of |
| 93 | + Right actualCerts -> actualCerts === certs |
| 94 | + Left msg -> counterexample (show msg) $ property False |
| 95 | + where |
| 96 | + simulationResult = runSimStrictShutdown $ do |
| 97 | + traceWith nullTracer "========== [ Starting test ] ==========" |
| 98 | + traceWith nullTracer (show certs) |
| 99 | + |
| 100 | + inboundPool <- newCertDB [] |
| 101 | + outboundPool <- newCertDB certs |
| 102 | + |
| 103 | + controlMessage <- uncheckedNewTVarM Continue |
| 104 | + |
| 105 | + let |
| 106 | + server = |
| 107 | + objectDiffusionInbound |
| 108 | + nullTracer |
| 109 | + numObjectIdsToAck |
| 110 | + (makeCertPoolReaderFromCertDB inboundPool) |
| 111 | + (makeCertPoolWriterFromCertDB inboundPool) |
| 112 | + nodeToNodeVersion |
| 113 | + |
| 114 | + client = |
| 115 | + objectDiffusionOutbound |
| 116 | + nullTracer |
| 117 | + numObjectIdsToAck |
| 118 | + (makeCertPoolReaderFromCertDB outboundPool) |
| 119 | + nodeToNodeVersion |
| 120 | + (readTVar controlMessage) |
| 121 | + |
| 122 | + (clientChannel, serverChannel) <- createConnectedChannels |
| 123 | + clientAsync <- |
| 124 | + async $ |
| 125 | + ( () |
| 126 | + <$ runPeer |
| 127 | + ((\x -> "Client: " ++ show x) `contramap` nullTracer) |
| 128 | + codecObjectDiffusionId |
| 129 | + clientChannel |
| 130 | + (objectDiffusionInitOutboundPeer client) |
| 131 | + ) |
| 132 | + |
| 133 | + serverAsync <- |
| 134 | + async $ |
| 135 | + ( () |
| 136 | + <$ runPipelinedPeer |
| 137 | + ((\x -> "Server: " ++ show x) `contramap` nullTracer) |
| 138 | + codecObjectDiffusionId |
| 139 | + serverChannel |
| 140 | + (objectDiffusionNonInitInboundPeerPipelined server) |
| 141 | + ) |
| 142 | + |
| 143 | + controlMessageAsync <- async $ do |
| 144 | + threadDelay 1000 -- give a head start to the other threads |
| 145 | + atomically $ writeTVar controlMessage Terminate |
| 146 | + threadDelay 1000 -- wait for the other threads to finish |
| 147 | + _ <- waitAnyCancel [clientAsync, serverAsync, controlMessageAsync] |
| 148 | + |
| 149 | + actualCerts <- certsFrom inboundPool |
| 150 | + traceWith nullTracer ("Certs received by server: " ++ show actualCerts) |
| 151 | + |
| 152 | + traceWith nullTracer "========== [ Test finished ] ==========" |
| 153 | + |
| 154 | + pure actualCerts |
| 155 | + |
| 156 | + certsFrom db = do |
| 157 | + certSnapshot <- atomically $ PerasCertDB.getCertSnapshot db |
| 158 | + let certsTickets = PerasCertDB.getCertsAfter certSnapshot PerasCertDB.zeroPerasCertTicketNo |
| 159 | + pure $ map fst certsTickets |
| 160 | + |
| 161 | + numObjectIdsToAck = certDiffusionMaxUnacked defaultMiniProtocolParameters |
| 162 | + nodeToNodeVersion = NodeToNodeV_14 |
0 commit comments