1111{-# LANGUAGE TypeFamilies #-}
1212{-# LANGUAGE TypeOperators #-}
1313{-# LANGUAGE UndecidableInstances #-}
14+ {-# LANGUAGE UndecidableSuperClasses #-}
1415{-# LANGUAGE ViewPatterns #-}
1516{-# OPTIONS_GHC -Wno-orphans #-}
1617{-# OPTIONS_HADDOCK not-home #-}
@@ -29,10 +30,13 @@ module Cardano.Ledger.Dijkstra.BlockBody.Internal (
2930 alignedValidFlags ,
3031 mkBasicBlockBodyDijkstra ,
3132 txSeqBlockBodyDijkstraL ,
33+ DijkstraEraBlockBody (.. ),
34+ perasCertBlockBodyDijkstraL ,
3235) where
3336
3437import qualified Cardano.Crypto.Hash as Hash
3538import Cardano.Ledger.Alonzo.Tx (AlonzoEraTx (.. ), IsValid (.. ))
39+ import Cardano.Ledger.BaseTypes (PerasCert )
3640import Cardano.Ledger.Binary (
3741 Annotator (.. ),
3842 DecCBOR (.. ),
@@ -53,7 +57,11 @@ import Data.ByteString (ByteString)
5357import Data.ByteString.Builder (Builder , shortByteString , toLazyByteString )
5458import qualified Data.ByteString.Lazy as BSL
5559import Data.Coerce (coerce )
56- import Data.Maybe.Strict (maybeToStrictMaybe , strictMaybeToMaybe )
60+ import Data.Maybe.Strict (
61+ StrictMaybe (.. ),
62+ maybeToStrictMaybe ,
63+ strictMaybeToMaybe ,
64+ )
5765import qualified Data.Sequence as Seq
5866import Data.Sequence.Strict (StrictSeq )
5967import qualified Data.Sequence.Strict as StrictSeq
@@ -74,6 +82,8 @@ import NoThunks.Class (AllowThunksIn (..), NoThunks)
7482
7583data DijkstraBlockBody era = DijkstraBlockBodyInternal
7684 { dbbTxs :: ! (StrictSeq (Tx TopTx era ))
85+ , dbbPerasCert :: ! (StrictMaybe PerasCert )
86+ -- ^ Optional Peras certificate
7787 , dbbHash :: Hash. Hash HASH EraIndependentBlockBody
7888 -- ^ Memoized hash to avoid recomputation. Lazy on purpose.
7989 , dbbTxsBodyBytes :: BSL. ByteString
@@ -102,7 +112,7 @@ mkBasicBlockBodyDijkstra ::
102112 , AlonzoEraTx era
103113 ) =>
104114 BlockBody era
105- mkBasicBlockBodyDijkstra = DijkstraBlockBody mempty
115+ mkBasicBlockBodyDijkstra = DijkstraBlockBody mempty SNothing
106116{-# INLINEABLE mkBasicBlockBodyDijkstra #-}
107117
108118txSeqBlockBodyDijkstraL ::
@@ -111,20 +121,39 @@ txSeqBlockBodyDijkstraL ::
111121 , AlonzoEraTx era
112122 ) =>
113123 Lens' (BlockBody era ) (StrictSeq (Tx TopTx era ))
114- txSeqBlockBodyDijkstraL = lens dbbTxs (\ _ s -> DijkstraBlockBody s)
124+ txSeqBlockBodyDijkstraL = lens dbbTxs (\ _ s -> DijkstraBlockBody s SNothing )
115125{-# INLINEABLE txSeqBlockBodyDijkstraL #-}
116126
127+ -- | Dijkstra-specific extensions to 'EraBlockBody'
128+ class EraBlockBody era => DijkstraEraBlockBody era where
129+ perasCertBlockBodyL :: Lens' (BlockBody era ) (StrictMaybe PerasCert )
130+ -- ^ Lens to access the optional Peras certificate in the block body
131+
132+ perasCertBlockBodyDijkstraL ::
133+ ( SafeToHash (TxWits era )
134+ , BlockBody era ~ DijkstraBlockBody era
135+ , AlonzoEraTx era
136+ ) =>
137+ Lens' (BlockBody era ) (StrictMaybe PerasCert )
138+ perasCertBlockBodyDijkstraL =
139+ lens dbbPerasCert (\ _ s -> DijkstraBlockBody mempty s)
140+ {-# INLINEABLE perasCertBlockBodyDijkstraL #-}
141+
142+ instance DijkstraEraBlockBody DijkstraEra where
143+ perasCertBlockBodyL = perasCertBlockBodyDijkstraL
144+
117145pattern DijkstraBlockBody ::
118146 forall era.
119147 ( AlonzoEraTx era
120148 , SafeToHash (TxWits era)
121149 ) =>
122150 StrictSeq (Tx TopTx era) ->
151+ StrictMaybe PerasCert ->
123152 DijkstraBlockBody era
124- pattern DijkstraBlockBody xs <-
125- DijkstraBlockBodyInternal xs _ _ _ _ _
153+ pattern DijkstraBlockBody xs mbPerasCert <-
154+ DijkstraBlockBodyInternal xs mbPerasCert _ _ _ _ _
126155 where
127- DijkstraBlockBody txns =
156+ DijkstraBlockBody txns mbPerasCert =
128157 let version = eraProtVerLow @ era
129158 serializeFoldablePreEncoded x =
130159 serialize version $
@@ -143,6 +172,7 @@ pattern DijkstraBlockBody xs <-
143172 serialize version $ encCBOR $ nonValidatingIndices txns
144173 in DijkstraBlockBodyInternal
145174 { dbbTxs = txns
175+ , dbbPerasCert = mbPerasCert
146176 , dbbHash = hashDijkstraSegWits txSeqBodies txSeqWits txSeqAuxDatas txSeqIsValids
147177 , dbbTxsBodyBytes = txSeqBodies
148178 , dbbTxsWitsBytes = txSeqWits
@@ -173,7 +203,7 @@ deriving stock instance Eq (Tx TopTx era) => Eq (DijkstraBlockBody era)
173203--------------------------------------------------------------------------------
174204
175205instance Era era => EncCBORGroup (DijkstraBlockBody era ) where
176- encCBORGroup (DijkstraBlockBodyInternal _ _ bodyBytes witsBytes metadataBytes invalidBytes) =
206+ encCBORGroup (DijkstraBlockBodyInternal _ _ _ bodyBytes witsBytes metadataBytes invalidBytes) =
177207 encodePreEncoded $
178208 BSL. toStrict $
179209 bodyBytes <> witsBytes <> metadataBytes <> invalidBytes
@@ -243,9 +273,14 @@ instance
243273 sequenceA $
244274 StrictSeq. forceToStrict $
245275 Seq. zipWith4 dijkstraSegwitTx bodies wits validFlags auxData
276+
277+ let mbPerasCert =
278+ pure SNothing
279+
246280 pure $
247281 DijkstraBlockBodyInternal
248282 <$> txns
283+ <*> mbPerasCert
249284 <*> (hashDijkstraSegWits <$> bodiesAnn <*> witsAnn <*> auxDataAnn <*> isValAnn)
250285 <*> bodiesAnn
251286 <*> witsAnn
@@ -298,6 +333,9 @@ dijkstraSegwitTx txBodyAnn txWitsAnn txIsValid txAuxDataAnn = Annotator $ \bytes
298333 txAuxData <- mapM (`runAnnotator` bytes) txAuxDataAnn
299334 pure $
300335 mkBasicTx txBody
301- & witsTxL .~ txWits
302- & auxDataTxL .~ maybeToStrictMaybe txAuxData
303- & isValidTxL .~ txIsValid
336+ & witsTxL
337+ .~ txWits
338+ & auxDataTxL
339+ .~ maybeToStrictMaybe txAuxData
340+ & isValidTxL
341+ .~ txIsValid
0 commit comments