Skip to content

Commit 28ee28e

Browse files
committed
Add optional Peras certificate to Dijkstra block body
This commit adds an optional PerasCert to the Dijkstra block body. In addition, it defines and instantiates a DijkstraEraBlockBody type class to expose this certificate via the perasCertBlockBodyL lens. At this point, serialization does not yet account for certificates, and will be implemented and tested in a separate commit.
1 parent bc74483 commit 28ee28e

File tree

2 files changed

+61
-10
lines changed
  • eras/dijkstra/impl/src/Cardano/Ledger/Dijkstra/BlockBody
  • libs/cardano-ledger-core/src/Cardano/Ledger

2 files changed

+61
-10
lines changed

eras/dijkstra/impl/src/Cardano/Ledger/Dijkstra/BlockBody/Internal.hs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
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

3437
import qualified Cardano.Crypto.Hash as Hash
3538
import Cardano.Ledger.Alonzo.Tx (AlonzoEraTx (..), IsValid (..))
39+
import Cardano.Ledger.BaseTypes (PerasCert)
3640
import Cardano.Ledger.Binary (
3741
Annotator (..),
3842
DecCBOR (..),
@@ -53,7 +57,11 @@ import Data.ByteString (ByteString)
5357
import Data.ByteString.Builder (Builder, shortByteString, toLazyByteString)
5458
import qualified Data.ByteString.Lazy as BSL
5559
import Data.Coerce (coerce)
56-
import Data.Maybe.Strict (maybeToStrictMaybe, strictMaybeToMaybe)
60+
import Data.Maybe.Strict (
61+
StrictMaybe (..),
62+
maybeToStrictMaybe,
63+
strictMaybeToMaybe,
64+
)
5765
import qualified Data.Sequence as Seq
5866
import Data.Sequence.Strict (StrictSeq)
5967
import qualified Data.Sequence.Strict as StrictSeq
@@ -74,6 +82,8 @@ import NoThunks.Class (AllowThunksIn (..), NoThunks)
7482

7583
data 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

108118
txSeqBlockBodyDijkstraL ::
@@ -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+
117145
pattern 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

175205
instance 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

libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ module Cardano.Ledger.BaseTypes (
8787
-- * Aeson helpers
8888
KeyValuePairs (..),
8989
ToKeyValuePairs (..),
90+
91+
-- * Peras-specific types
92+
PerasCert,
9093
) where
9194

9295
import Cardano.Crypto.Hash
@@ -978,3 +981,13 @@ newtype KeyValuePairs a = KeyValuePairs {unKeyValuePairs :: a}
978981
instance ToKeyValuePairs a => ToJSON (KeyValuePairs a) where
979982
toJSON = object . toKeyValuePairs . unKeyValuePairs
980983
toEncoding = pairs . mconcat . toKeyValuePairs . unKeyValuePairs
984+
985+
--------------------------------------------------------------------------------
986+
-- Peras-related types
987+
--------------------------------------------------------------------------------
988+
989+
-- | Placeholder for Peras certificates
990+
--
991+
-- NOTE: The real type will be brought from 'cardano-base' once it's ready.
992+
data PerasCert = PerasCert
993+
deriving (Eq, Show, Generic, NoThunks)

0 commit comments

Comments
 (0)