Skip to content

Commit c7f8ec1

Browse files
committed
Remove default implementation for the DecCBOR class
1 parent 9fe71f4 commit c7f8ec1

File tree

20 files changed

+275
-171
lines changed

20 files changed

+275
-171
lines changed

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Genesis.hs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE DeriveGeneric #-}
23
{-# LANGUAGE DerivingVia #-}
34
{-# LANGUAGE FlexibleContexts #-}
@@ -90,16 +91,18 @@ newtype AlonzoExtraConfig = AlonzoExtraConfig
9091
deriving (Eq)
9192
deriving newtype (NFData, NoThunks, Show)
9293

93-
instance DecCBOR AlonzoExtraConfig
94+
alonzoExtraConfigDecoder :: Decode (Closed Dense) AlonzoExtraConfig
95+
alonzoExtraConfigDecoder = RecD AlonzoExtraConfig <! D (decodeNullMaybe decodeCostModelsLenient)
96+
97+
instance DecCBOR AlonzoExtraConfig where
98+
decCBOR = decode alonzoExtraConfigDecoder
99+
{-# INLINE decCBOR #-}
94100

95101
instance EncCBOR AlonzoExtraConfig
96102

97103
instance FromCBOR AlonzoExtraConfig where
98-
fromCBOR =
99-
eraDecoder @AlonzoEra $
100-
decode $
101-
RecD AlonzoExtraConfig
102-
<! D (decodeNullMaybe decodeCostModelsLenient)
104+
fromCBOR = eraDecoder @AlonzoEra $ decode alonzoExtraConfigDecoder
105+
{-# INLINE fromCBOR #-}
103106

104107
instance ToCBOR AlonzoExtraConfig where
105108
toCBOR x@(AlonzoExtraConfig _) =
@@ -183,25 +186,29 @@ pattern AlonzoGenesis
183186
instance EraGenesis AlonzoEra where
184187
type Genesis AlonzoEra = AlonzoGenesis
185188

189+
alonzoGenesisDecoder :: Decode (Closed Dense) AlonzoGenesis
190+
alonzoGenesisDecoder =
191+
RecD AlonzoGenesis
192+
<! From
193+
<! D (decodeCostModel PlutusV1)
194+
<! From
195+
<! From
196+
<! From
197+
<! From
198+
<! From
199+
<! From
200+
<! From
201+
186202
-- | Genesis types are always encoded with the version of era they are defined in.
187-
instance DecCBOR AlonzoGenesis
203+
instance DecCBOR AlonzoGenesis where
204+
decCBOR = decode alonzoGenesisDecoder
205+
{-# INLINE decCBOR #-}
188206

189207
instance EncCBOR AlonzoGenesis
190208

191209
instance FromCBOR AlonzoGenesis where
192-
fromCBOR =
193-
eraDecoder @AlonzoEra $
194-
decode $
195-
RecD AlonzoGenesis
196-
<! From
197-
<! D (decodeCostModel PlutusV1)
198-
<! From
199-
<! From
200-
<! From
201-
<! From
202-
<! From
203-
<! From
204-
<! From
210+
fromCBOR = eraDecoder @AlonzoEra $ decode alonzoGenesisDecoder
211+
{-# INLINE fromCBOR #-}
205212

206213
instance ToCBOR AlonzoGenesis where
207214
toCBOR x@(AlonzoGenesis _ _ _ _ _ _ _ _ _) =

eras/byron/crypto/src/Cardano/Crypto/Signing/Redeem/SigningKey.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Cardano.Crypto.Signing.Redeem.VerificationKey (
1414
redeemVKB64F,
1515
)
1616
import Cardano.Ledger.Binary (
17-
DecCBOR,
17+
DecCBOR (..),
1818
EncCBOR,
1919
FromCBOR (..),
2020
ToCBOR (..),
@@ -34,7 +34,9 @@ newtype RedeemSigningKey
3434

3535
instance EncCBOR RedeemSigningKey
3636

37-
instance DecCBOR RedeemSigningKey
37+
instance DecCBOR RedeemSigningKey where
38+
decCBOR = RedeemSigningKey <$> decCBOR
39+
{-# INLINE decCBOR #-}
3840

3941
-- Note that there is deliberately no Ord instance. The crypto libraries
4042
-- encourage using key /hashes/ not keys for things like sets, map etc.

eras/conway/impl/src/Cardano/Ledger/Conway/Genesis.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ instance NoThunks ConwayGenesis
6868

6969
instance NFData ConwayGenesis
7070

71+
conwayGenesisDecoder :: Decode (Closed Dense) ConwayGenesis
72+
conwayGenesisDecoder = RecD ConwayGenesis <! From <! From <! From <! From <! From
73+
7174
-- | Genesis are always encoded with the version of era they are defined in.
7275
instance FromCBOR ConwayGenesis where
73-
fromCBOR =
74-
eraDecoder @ConwayEra $
75-
decode $
76-
RecD ConwayGenesis <! From <! From <! From <! From <! From
76+
fromCBOR = eraDecoder @ConwayEra $ decode conwayGenesisDecoder
77+
{-# INLINE fromCBOR #-}
7778

7879
instance ToCBOR ConwayGenesis where
7980
toCBOR x@(ConwayGenesis _ _ _ _ _) =
@@ -86,7 +87,9 @@ instance ToCBOR ConwayGenesis where
8687
!> To cgDelegs
8788
!> To cgInitialDReps
8889

89-
instance DecCBOR ConwayGenesis
90+
instance DecCBOR ConwayGenesis where
91+
decCBOR = decode conwayGenesisDecoder
92+
{-# INLINE decCBOR #-}
9093

9194
instance EncCBOR ConwayGenesis
9295

eras/conway/impl/src/Cardano/Ledger/Conway/Governance.hs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,15 @@ import Cardano.Ledger.Binary (
195195
ToCBOR (..),
196196
decNoShareCBOR,
197197
decodeRecordNamedT,
198+
decodeWord8,
198199
)
199200
import Cardano.Ledger.Binary.Coders (
200201
Encode (..),
201202
encode,
202203
(!>),
203204
)
204-
import Cardano.Ledger.Binary.Plain (
205-
decodeWord8,
206-
encodeWord8,
207-
)
205+
import Cardano.Ledger.Binary.Plain (encodeWord8)
206+
import qualified Cardano.Ledger.Binary.Plain as Plain
208207
import Cardano.Ledger.Coin (Coin (..))
209208
import Cardano.Ledger.Compactible (CompactForm)
210209
import Cardano.Ledger.Conway.Era (ConwayEra)
@@ -539,7 +538,7 @@ data DefaultVote
539538

540539
instance FromCBOR DefaultVote where
541540
fromCBOR = do
542-
tag <- decodeWord8
541+
tag <- Plain.decodeWord8
543542
case tag of
544543
0 -> pure DefaultNo
545544
1 -> pure DefaultAbstain
@@ -553,7 +552,15 @@ instance ToCBOR DefaultVote where
553552

554553
instance EncCBOR DefaultVote
555554

556-
instance DecCBOR DefaultVote
555+
instance DecCBOR DefaultVote where
556+
decCBOR = do
557+
tag <- decodeWord8
558+
case tag of
559+
0 -> pure DefaultNo
560+
1 -> pure DefaultAbstain
561+
2 -> pure DefaultNoConfidence
562+
_ -> fail $ "Invalid DefaultVote tag " ++ show tag
563+
{-# INLINE decCBOR #-}
557564

558565
defaultStakePoolVote ::
559566
ConwayEraAccounts era =>

eras/dijkstra/impl/src/Cardano/Ledger/Dijkstra/Genesis.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE DeriveGeneric #-}
23
{-# LANGUAGE DerivingVia #-}
34
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
@@ -36,12 +37,12 @@ newtype DijkstraGenesis = DijkstraGenesis
3637
instance EraGenesis DijkstraEra where
3738
type Genesis DijkstraEra = DijkstraGenesis
3839

40+
dijkstraGenesisDecoder :: Decode (Closed Dense) DijkstraGenesis
41+
dijkstraGenesisDecoder = RecD DijkstraGenesis <! From
42+
3943
instance FromCBOR DijkstraGenesis where
40-
fromCBOR =
41-
eraDecoder @DijkstraEra $
42-
decode $
43-
RecD DijkstraGenesis
44-
<! From
44+
fromCBOR = eraDecoder @DijkstraEra $ decode dijkstraGenesisDecoder
45+
{-# INLINE fromCBOR #-}
4546

4647
instance ToCBOR DijkstraGenesis where
4748
toCBOR dg@(DijkstraGenesis _) =
@@ -50,6 +51,8 @@ instance ToCBOR DijkstraGenesis where
5051
Rec DijkstraGenesis
5152
!> To dgUpgradePParams
5253

53-
instance DecCBOR DijkstraGenesis
54+
instance DecCBOR DijkstraGenesis where
55+
decCBOR = decode dijkstraGenesisDecoder
56+
{-# INLINE decCBOR #-}
5457

5558
instance EncCBOR DijkstraGenesis

eras/shelley/impl/src/Cardano/Ledger/Shelley/Genesis.hs

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,46 @@ instance FromJSON ShelleyGenesisStaking where
426426
<$> (forceElemsToWHNF <$> obj .: "pools")
427427
<*> (forceElemsToWHNF <$> obj .: "stake")
428428

429+
shelleyGenesisDecoder :: Decoder s ShelleyGenesis
430+
shelleyGenesisDecoder =
431+
decodeRecordNamed "ShelleyGenesis" (const 15) $ do
432+
sgSystemStart <- decCBOR
433+
sgNetworkMagic <- decCBOR
434+
sgNetworkId <- decCBOR
435+
sgActiveSlotsCoeff <- activeSlotsCoeffDecCBOR
436+
sgSecurityParam <- decCBOR
437+
sgEpochLength <- decCBOR
438+
sgSlotsPerKESPeriod <- decCBOR
439+
sgMaxKESEvolutions <- decCBOR
440+
sgSlotLength <- decCBOR
441+
sgUpdateQuorum <- decCBOR
442+
sgMaxLovelaceSupply <- decCBOR
443+
sgProtocolParams <- decCBOR
444+
sgGenDelegs <- decCBOR
445+
sgInitialFunds <- decCBOR
446+
sgStaking <- decCBOR
447+
pure $
448+
ShelleyGenesis
449+
sgSystemStart
450+
sgNetworkMagic
451+
sgNetworkId
452+
sgActiveSlotsCoeff
453+
sgSecurityParam
454+
(EpochSize sgEpochLength)
455+
sgSlotsPerKESPeriod
456+
sgMaxKESEvolutions
457+
sgSlotLength
458+
sgUpdateQuorum
459+
sgMaxLovelaceSupply
460+
sgProtocolParams
461+
sgGenDelegs
462+
sgInitialFunds
463+
sgStaking
464+
429465
-- | Genesis are always encoded with the version of era they are defined in.
430-
instance DecCBOR ShelleyGenesis
466+
instance DecCBOR ShelleyGenesis where
467+
decCBOR = shelleyGenesisDecoder
468+
{-# INLINE decCBOR #-}
431469

432470
instance EncCBOR ShelleyGenesis
433471

@@ -469,40 +507,8 @@ instance ToCBOR ShelleyGenesis where
469507
<> encCBOR sgStaking
470508

471509
instance FromCBOR ShelleyGenesis where
472-
fromCBOR = toPlainDecoder Nothing shelleyProtVer $ do
473-
decodeRecordNamed "ShelleyGenesis" (const 15) $ do
474-
sgSystemStart <- decCBOR
475-
sgNetworkMagic <- decCBOR
476-
sgNetworkId <- decCBOR
477-
sgActiveSlotsCoeff <- activeSlotsCoeffDecCBOR
478-
sgSecurityParam <- decCBOR
479-
sgEpochLength <- decCBOR
480-
sgSlotsPerKESPeriod <- decCBOR
481-
sgMaxKESEvolutions <- decCBOR
482-
sgSlotLength <- decCBOR
483-
sgUpdateQuorum <- decCBOR
484-
sgMaxLovelaceSupply <- decCBOR
485-
sgProtocolParams <- decCBOR
486-
sgGenDelegs <- decCBOR
487-
sgInitialFunds <- decCBOR
488-
sgStaking <- decCBOR
489-
pure $
490-
ShelleyGenesis
491-
sgSystemStart
492-
sgNetworkMagic
493-
sgNetworkId
494-
sgActiveSlotsCoeff
495-
sgSecurityParam
496-
(EpochSize sgEpochLength)
497-
sgSlotsPerKESPeriod
498-
sgMaxKESEvolutions
499-
sgSlotLength
500-
sgUpdateQuorum
501-
sgMaxLovelaceSupply
502-
sgProtocolParams
503-
sgGenDelegs
504-
sgInitialFunds
505-
sgStaking
510+
fromCBOR = toPlainDecoder Nothing shelleyProtVer shelleyGenesisDecoder
511+
{-# INLINE fromCBOR #-}
506512

507513
-- | Serialize `PositiveUnitInterval` type in the same way `Rational` is serialized,
508514
-- however ensure there is no usage of tag 30 by enforcing Shelley protocol version.

eras/shelley/impl/src/Cardano/Ledger/Shelley/Translation.hs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@ import Cardano.Ledger.Binary (
2323
toPlainDecoder,
2424
toPlainEncoding,
2525
)
26-
import Cardano.Ledger.Binary.Coders (Decode (..), Encode (..), decode, encode, (!>), (<!))
26+
import Cardano.Ledger.Binary.Coders (
27+
Decode (..),
28+
Density (..),
29+
Encode (..),
30+
Wrapped (..),
31+
decode,
32+
encode,
33+
(!>),
34+
(<!),
35+
)
2736
import Cardano.Ledger.Core (PParams, TranslationContext, emptyPParams)
2837
import Cardano.Ledger.Keys
2938
import Cardano.Ledger.Shelley.Era (ShelleyEra)
@@ -54,18 +63,22 @@ instance ToCBOR FromByronTranslationContext where
5463
!> To fbtcProtocolParams
5564
!> To fbtcMaxLovelaceSupply
5665

66+
fromByronTranslationContextDecoder :: Decode (Closed Dense) FromByronTranslationContext
67+
fromByronTranslationContextDecoder =
68+
RecD FromByronTranslationContext
69+
<! From
70+
<! From
71+
<! From
72+
5773
instance FromCBOR FromByronTranslationContext where
58-
fromCBOR =
59-
toPlainDecoder Nothing shelleyProtVer $
60-
decode $
61-
RecD FromByronTranslationContext
62-
<! From
63-
<! From
64-
<! From
74+
fromCBOR = toPlainDecoder Nothing shelleyProtVer $ decode fromByronTranslationContextDecoder
75+
{-# INLINE fromCBOR #-}
6576

6677
instance EncCBOR FromByronTranslationContext
6778

68-
instance DecCBOR FromByronTranslationContext
79+
instance DecCBOR FromByronTranslationContext where
80+
decCBOR = decode fromByronTranslationContextDecoder
81+
{-# INLINE decCBOR #-}
6982

7083
instance FromJSON FromByronTranslationContext where
7184
parseJSON = withObject "FromByronTranslationContext" $ \o -> do

libs/cardano-ledger-binary/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.8.0.0
44

5+
* Remove default implementation for `DecCBOR` class
56
* Change `Version` from `Word64` to `Word32`
67
- Add `mkVersion32` and `getVersion32`
78
* Remove `listLenBound` from `EncCBORGroup`

0 commit comments

Comments
 (0)