77{-# LANGUAGE GeneralizedNewtypeDeriving #-}
88{-# LANGUAGE OverloadedStrings #-}
99{-# LANGUAGE PatternSynonyms #-}
10- {-# LANGUAGE RecordWildCards #-}
1110{-# LANGUAGE ScopedTypeVariables #-}
1211{-# LANGUAGE StandaloneDeriving #-}
1312{-# LANGUAGE TypeApplications #-}
@@ -38,8 +37,9 @@ module Cardano.Ledger.Dijkstra.BlockBody.Internal (
3837 validatePerasCert ,
3938) where
4039
40+ import Cardano.Crypto.Leios (LeiosCert )
4141import Cardano.Ledger.Alonzo.Tx (AlonzoEraTx (.. ), IsValid (.. ))
42- import Cardano.Ledger.BaseTypes (Nonce , ProtVer (.. ), maybeToStrictMaybe )
42+ import Cardano.Ledger.BaseTypes (Nonce , ProtVer (.. ))
4343import Cardano.Ledger.Binary (
4444 Annotator (.. ),
4545 DecCBOR (.. ),
@@ -52,9 +52,11 @@ import Cardano.Ledger.Binary (
5252 decodeSeq ,
5353 encCBOR ,
5454 encodeListLen ,
55+ encodeNullMaybe ,
5556 encodeNullStrictMaybe ,
5657 serialize' ,
5758 )
59+ import Cardano.Ledger.Binary.Crypto (decodeLeiosCert , encodeLeiosCert )
5860import Cardano.Ledger.Core
5961import Cardano.Ledger.Dijkstra.Era
6062import Cardano.Ledger.Dijkstra.Tx (DijkstraTx , Tx (.. ), decodeDijkstraTopTx )
@@ -100,12 +102,16 @@ import NoThunks.Class (NoThunks)
100102
101103data DijkstraBlockBodyRaw era = DijkstraBlockBodyRaw
102104 { dbbrTxs :: ! (StrictSeq (Tx TopTx era ))
105+ , dbbrLeiosCert :: ! (StrictMaybe LeiosCert )
106+ -- ^ Optional Leios certificate
103107 , dbbrPerasCert :: ! (StrictMaybe PerasCert )
104108 -- ^ Optional Peras certificate
105109 }
106110 deriving (Generic )
107111
108- instance (NFData (Tx TopTx era ), NFData PerasCert ) => NFData (DijkstraBlockBodyRaw era )
112+ instance
113+ (NFData (Tx TopTx era ), NFData LeiosCert , NFData PerasCert ) =>
114+ NFData (DijkstraBlockBodyRaw era )
109115
110116type instance MemoHashIndex (DijkstraBlockBodyRaw era ) = EraIndependentBlockBody
111117
@@ -118,15 +124,22 @@ instance EraBlockBody DijkstraEra where
118124 blockBodySize (ProtVer v _) = BS. length . serialize' v . encCBOR
119125
120126mkBasicBlockBodyDijkstra :: forall era . AlonzoEraTx era => DijkstraBlockBody era
121- mkBasicBlockBodyDijkstra = mkMemoized (eraProtVerLow @ era ) $ DijkstraBlockBodyRaw mempty SNothing
127+ mkBasicBlockBodyDijkstra =
128+ mkMemoized (eraProtVerLow @ era ) $
129+ DijkstraBlockBodyRaw mempty SNothing SNothing
122130{-# INLINEABLE mkBasicBlockBodyDijkstra #-}
123131
124132-- | Dijkstra-specific extensions to 'EraBlockBody'
125133class EraBlockBody era => DijkstraEraBlockBody era where
134+ leiosCertBlockBodyL :: Lens' (BlockBody era ) (StrictMaybe LeiosCert )
135+ -- ^ Lens to access the optional Leios certificate in the block body
136+
126137 perasCertBlockBodyL :: Lens' (BlockBody era ) (StrictMaybe PerasCert )
127138 -- ^ Lens to access the optional Peras certificate in the block body
128139
129140instance DijkstraEraBlockBody DijkstraEra where
141+ leiosCertBlockBodyL = lensMemoRawType @ DijkstraEra dbbrLeiosCert (\ bb c -> bb {dbbrLeiosCert = c})
142+
130143 perasCertBlockBodyL = lensMemoRawType @ DijkstraEra dbbrPerasCert (\ bb c -> bb {dbbrPerasCert = c})
131144
132145deriving instance (Typeable era , NoThunks (Tx TopTx era )) => NoThunks (DijkstraBlockBodyRaw era )
@@ -153,13 +166,17 @@ instance Memoized (DijkstraBlockBody era) where
153166pattern DijkstraBlockBody ::
154167 AlonzoEraTx era =>
155168 StrictSeq (Tx TopTx era) ->
169+ StrictMaybe LeiosCert ->
156170 StrictMaybe PerasCert ->
157171 DijkstraBlockBody era
158- pattern DijkstraBlockBody txs perasCert <- (getMemoRawType -> DijkstraBlockBodyRaw txs perasCert)
172+ pattern DijkstraBlockBody txs mbLeiosCert mbPerasCert <-
173+ ( getMemoRawType ->
174+ DijkstraBlockBodyRaw txs mbLeiosCert mbPerasCert
175+ )
159176 where
160- DijkstraBlockBody txs perasCert =
177+ DijkstraBlockBody txs leiosCert perasCert =
161178 mkMemoizedEra @ DijkstraEra $
162- DijkstraBlockBodyRaw txs perasCert
179+ DijkstraBlockBodyRaw txs leiosCert perasCert
163180
164181{-# COMPLETE DijkstraBlockBody #-}
165182
@@ -173,14 +190,15 @@ instance
173190 ) =>
174191 EncCBOR (DijkstraBlockBodyRaw era )
175192 where
176- encCBOR (DijkstraBlockBodyRaw txs perasCert ) =
177- encodeListLen 3
178- <> encodeNullStrictMaybe encCBOR invalidIndices
193+ encCBOR (DijkstraBlockBodyRaw txs mbLeiosCert mbPerasCert ) =
194+ encodeListLen 4
195+ <> encodeNullMaybe encCBOR invalidIndices
179196 <> encCBOR txs
180- <> encodeNullStrictMaybe encCBOR perasCert
197+ <> encodeNullStrictMaybe encodeLeiosCert mbLeiosCert
198+ <> encodeNullStrictMaybe encCBOR mbPerasCert
181199 where
182200 invalidIndices =
183- maybeToStrictMaybe . NonEmptySet. fromFoldable $
201+ NonEmptySet. fromFoldable $
184202 StrictSeq. findIndicesL (\ tx -> tx ^. isValidTxL == IsValid False ) txs
185203
186204instance
@@ -192,16 +210,19 @@ instance
192210 ) =>
193211 DecCBOR (Annotator (DijkstraBlockBodyRaw era ))
194212 where
195- decCBOR = decodeRecordNamed " DijkstraBlockBodyRaw" (const 3 ) $ do
213+ decCBOR = decodeRecordNamed " DijkstraBlockBodyRaw" (const 4 ) $ do
196214 let
197215 decodeInvalidTxs =
198216 decodeNonEmptySetLikeEnforceNoDuplicates
199217 (IntSet. insert . fromIntegral @ Word16 @ Int )
200218 (\ x -> (IntSet. size x, x))
201219 (decCBOR @ Word16 )
220+
202221 invalidTxs :: IntSet <- fold <$> decodeNullMaybe decodeInvalidTxs
203222 txs <- decodeSeq (decodeDijkstraTopTx @ era False )
204- perasCert <- decodeNullStrictMaybe decCBOR
223+ mbLeiosCert <- decodeNullStrictMaybe decodeLeiosCert
224+ mbPerasCert <- decodeNullStrictMaybe decCBOR
225+
205226 let txsLength = Seq. length txs
206227 inRange x = 0 <= x && x < txsLength
207228 forM_ (IntSet. toList invalidTxs) $ \ i ->
@@ -214,7 +235,8 @@ instance
214235 pure $
215236 DijkstraBlockBodyRaw
216237 <$> sequenceA (StrictSeq. forceToStrict txsWithIsValid)
217- <*> pure perasCert
238+ <*> pure mbLeiosCert
239+ <*> pure mbPerasCert
218240
219241deriving via
220242 Mem (DijkstraBlockBodyRaw era)
@@ -228,8 +250,16 @@ deriving via
228250 DecCBOR (Annotator (DijkstraBlockBody era ))
229251
230252instance (AlonzoEraTx era , EncCBOR (Tx TopTx era )) => EncCBORGroup (DijkstraBlockBody era ) where
231- encCBORGroup (DijkstraBlockBody txs perasCert) = do
232- encodeListLen 2 <> encCBOR txs <> encCBOR perasCert
253+ encCBORGroup (DijkstraBlockBody txs mbLeiosCert mbPerasCert) = do
254+ encodeListLen 4
255+ <> encodeNullMaybe encCBOR invalidIndices
256+ <> encCBOR txs
257+ <> encodeNullStrictMaybe encodeLeiosCert mbLeiosCert
258+ <> encodeNullStrictMaybe encCBOR mbPerasCert
259+ where
260+ invalidIndices =
261+ NonEmptySet. fromFoldable $
262+ StrictSeq. findIndicesL (\ tx -> tx ^. isValidTxL == IsValid False ) txs
233263 listLen _ = 1
234264
235265--------------------------------------------------------------------------------
0 commit comments