|
| 1 | +{-# LANGUAGE GADTs #-} |
| 2 | +{-# LANGUAGE StandaloneDeriving #-} |
| 3 | + |
| 4 | +-- | Instantiate 'ObjectPoolReader' and 'ObjectPoolWriter' using Peras |
| 5 | +-- certificates from the 'PerasCertDB' (or the 'ChainDB' which is wrapping the |
| 6 | +-- 'PerasCertDB'). |
| 7 | +module Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.PerasCert |
| 8 | + ( makePerasCertPoolReaderFromCertDB |
| 9 | + , makePerasCertPoolWriterFromCertDB |
| 10 | + , makePerasCertPoolReaderFromChainDB |
| 11 | + , makePerasCertPoolWriterFromChainDB |
| 12 | + ) where |
| 13 | + |
| 14 | +import GHC.Exception (throw) |
| 15 | +import Ouroboros.Consensus.Block |
| 16 | +import Ouroboros.Consensus.MiniProtocol.ObjectDiffusion.ObjectPool.API |
| 17 | +import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB) |
| 18 | +import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB |
| 19 | +import Ouroboros.Consensus.Storage.PerasCertDB.API |
| 20 | + ( PerasCertDB |
| 21 | + , PerasCertSnapshot |
| 22 | + , PerasCertTicketNo |
| 23 | + ) |
| 24 | +import qualified Ouroboros.Consensus.Storage.PerasCertDB.API as PerasCertDB |
| 25 | +import Ouroboros.Consensus.Util.IOLike |
| 26 | + |
| 27 | +makePerasCertPoolReaderFromSnapshot :: |
| 28 | + (IOLike m, StandardHash blk) => |
| 29 | + STM m (PerasCertSnapshot blk) -> |
| 30 | + ObjectPoolReader PerasRoundNo (PerasCert blk) PerasCertTicketNo m |
| 31 | +makePerasCertPoolReaderFromSnapshot getCertSnapshot = |
| 32 | + ObjectPoolReader |
| 33 | + { oprObjectId = getPerasCertRound |
| 34 | + , oprZeroTicketNo = PerasCertDB.zeroPerasCertTicketNo |
| 35 | + , oprObjectsAfter = \lastKnown limit -> do |
| 36 | + certSnapshot <- getCertSnapshot |
| 37 | + pure $ |
| 38 | + take (fromIntegral limit) $ |
| 39 | + [ (ticketNo, getPerasCertRound cert, pure (getPerasCert cert)) |
| 40 | + | (cert, ticketNo) <- PerasCertDB.getCertsAfter certSnapshot lastKnown |
| 41 | + ] |
| 42 | + } |
| 43 | + |
| 44 | +makePerasCertPoolReaderFromCertDB :: |
| 45 | + (IOLike m, StandardHash blk) => |
| 46 | + PerasCertDB m blk -> ObjectPoolReader PerasRoundNo (PerasCert blk) PerasCertTicketNo m |
| 47 | +makePerasCertPoolReaderFromCertDB perasCertDB = |
| 48 | + makePerasCertPoolReaderFromSnapshot (PerasCertDB.getCertSnapshot perasCertDB) |
| 49 | + |
| 50 | +makePerasCertPoolWriterFromCertDB :: |
| 51 | + (StandardHash blk, IOLike m) => |
| 52 | + PerasCertDB m blk -> ObjectPoolWriter PerasRoundNo (PerasCert blk) m |
| 53 | +makePerasCertPoolWriterFromCertDB perasCertDB = |
| 54 | + ObjectPoolWriter |
| 55 | + { opwObjectId = getPerasCertRound |
| 56 | + , opwAddObjects = \certs -> do |
| 57 | + validatePerasCerts certs |
| 58 | + >>= mapM_ (PerasCertDB.addCert perasCertDB) |
| 59 | + , opwHasObject = do |
| 60 | + certSnapshot <- PerasCertDB.getCertSnapshot perasCertDB |
| 61 | + pure $ PerasCertDB.containsCert certSnapshot |
| 62 | + } |
| 63 | + |
| 64 | +makePerasCertPoolReaderFromChainDB :: |
| 65 | + (IOLike m, StandardHash blk) => |
| 66 | + ChainDB m blk -> ObjectPoolReader PerasRoundNo (PerasCert blk) PerasCertTicketNo m |
| 67 | +makePerasCertPoolReaderFromChainDB chainDB = |
| 68 | + makePerasCertPoolReaderFromSnapshot (ChainDB.getPerasCertSnapshot chainDB) |
| 69 | + |
| 70 | +makePerasCertPoolWriterFromChainDB :: |
| 71 | + (StandardHash blk, IOLike m) => |
| 72 | + ChainDB m blk -> ObjectPoolWriter PerasRoundNo (PerasCert blk) m |
| 73 | +makePerasCertPoolWriterFromChainDB chainDB = |
| 74 | + ObjectPoolWriter |
| 75 | + { opwObjectId = getPerasCertRound |
| 76 | + , opwAddObjects = \certs -> do |
| 77 | + validatePerasCerts certs |
| 78 | + >>= mapM_ (ChainDB.addPerasCertAsync chainDB) |
| 79 | + , opwHasObject = do |
| 80 | + certSnapshot <- ChainDB.getPerasCertSnapshot chainDB |
| 81 | + pure $ PerasCertDB.containsCert certSnapshot |
| 82 | + } |
| 83 | + |
| 84 | +data PerasCertInboundException |
| 85 | + = forall blk. PerasCertValidationError (PerasValidationErr blk) |
| 86 | + |
| 87 | +deriving instance Show PerasCertInboundException |
| 88 | + |
| 89 | +instance Exception PerasCertInboundException |
| 90 | + |
| 91 | +-- | Validate a list of 'PerasCert's, throwing a 'PerasCertInboundException' if |
| 92 | +-- any of them are invalid. |
| 93 | +validatePerasCerts :: |
| 94 | + (StandardHash blk, MonadThrow m) => |
| 95 | + [PerasCert blk] -> |
| 96 | + m [ValidatedPerasCert blk] |
| 97 | +validatePerasCerts certs = do |
| 98 | + let perasCfg = makePerasCfg Nothing |
| 99 | + -- TODO replace the mocked-up Nothing with a real |
| 100 | + -- 'BlockConfig' when all the plumbing is in place |
| 101 | + case traverse (validatePerasCert perasCfg) certs of |
| 102 | + Left validationErr -> throw (PerasCertValidationError validationErr) |
| 103 | + Right validatedCerts -> return validatedCerts |
0 commit comments