Skip to content

Commit 2f1000c

Browse files
authored
Merge pull request #5807 from IntersectMBO/aniketd/coders
Stop using Coders in some places
2 parents 84c0a5e + 0fd042e commit 2f1000c

7 files changed

Lines changed: 99 additions & 36 deletions

File tree

eras/allegra/impl/src/Cardano/Ledger/Allegra/Scripts.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ import Cardano.Ledger.Binary (
6767
DecCBOR (decCBOR),
6868
EncCBOR (encCBOR),
6969
ToCBOR (..),
70+
decodeRecordNamed,
71+
ifDecoderVersionAtLeast,
72+
natVersion,
7073
)
7174
import Cardano.Ledger.Binary.Coders (
7275
Decode (..),
@@ -139,7 +142,12 @@ decodeVI :: Decode (Closed Dense) ValidityInterval
139142
decodeVI = RecD ValidityInterval <! From <! From
140143

141144
instance DecCBOR ValidityInterval where
142-
decCBOR = decode decodeVI
145+
decCBOR =
146+
ifDecoderVersionAtLeast (natVersion @12) decodeValidityInterval (decode decodeVI)
147+
where
148+
decodeValidityInterval =
149+
decodeRecordNamed "ValidityInterval" (const 2) $
150+
ValidityInterval <$> decCBOR <*> decCBOR
143151

144152
instance ToJSON ValidityInterval where
145153
toJSON vi =

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ import Cardano.Ledger.Binary (
6868
ToCBOR,
6969
TokenType (..),
7070
decodeStrictSeq,
71+
ifDecoderVersionAtLeast,
72+
natVersion,
7173
peekTokenType,
7274
)
7375
import Cardano.Ledger.Binary.Coders
@@ -199,7 +201,7 @@ instance
199201
decCBOR =
200202
decodeTxAuxDataByTokenType @(Annotator (AlonzoTxAuxDataRaw era))
201203
decodeShelley
202-
decodeAllegra
204+
(ifDecoderVersionAtLeast (natVersion @12) decodeDijkstra decodeAllegra)
203205
decodeAlonzo
204206
where
205207
decodeShelley =
@@ -217,6 +219,11 @@ instance
217219
(sequence <$> decodeStrictSeq decCBOR)
218220
<*! Ann (Emit Map.empty)
219221
)
222+
decodeDijkstra =
223+
decodeRecordNamed "AlonzoTxAuxDataRaw" (const 2) $ do
224+
metadata <- decCBOR
225+
annScripts <- sequence <$> decodeStrictSeq decCBOR
226+
pure $ AlonzoTxAuxDataRaw <$> pure metadata <*> annScripts <*> pure Map.empty
220227
decodeAlonzo =
221228
decode $
222229
TagD 259 $

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ import Cardano.Ledger.Binary (
110110
encodeListLen,
111111
encodeNullStrictMaybe,
112112
encodeWord8,
113+
ifDecoderVersionAtLeast,
113114
internsFromMap,
114115
invalidKey,
116+
natVersion,
115117
)
116118
import Cardano.Ledger.Binary.Coders (
117119
Decode (..),
@@ -179,10 +181,13 @@ data GovActionId = GovActionId
179181

180182
instance DecCBOR GovActionId where
181183
decCBOR =
182-
decode $
183-
RecD GovActionId
184-
<! From
185-
<! From
184+
ifDecoderVersionAtLeast (natVersion @12) decodeGovActionId $
185+
decode $
186+
RecD GovActionId <! From <! From
187+
where
188+
decodeGovActionId =
189+
decodeRecordNamed "GovActionId" (const 2) $
190+
GovActionId <$> decCBOR <*> decCBOR
186191

187192
instance EncCBOR GovActionId where
188193
encCBOR GovActionId {..} =
@@ -450,10 +455,15 @@ instance NFData (VotingProcedure era)
450455

451456
instance Era era => DecCBOR (VotingProcedure era) where
452457
decCBOR =
453-
decode $
454-
RecD VotingProcedure
455-
<! From
456-
<! D (decodeNullStrictMaybe decCBOR)
458+
ifDecoderVersionAtLeast (natVersion @12) decodeVotingProcedure $
459+
decode $
460+
RecD VotingProcedure
461+
<! From
462+
<! D (decodeNullStrictMaybe decCBOR)
463+
where
464+
decodeVotingProcedure =
465+
decodeRecordNamed "VotingProcedure" (const 2) $
466+
VotingProcedure <$> decCBOR <*> decodeNullStrictMaybe decCBOR
457467
{-# INLINE decCBOR #-}
458468

459469
instance Era era => EncCBOR (VotingProcedure era) where
@@ -511,12 +521,17 @@ instance EraPParams era => NFData (ProposalProcedure era)
511521

512522
instance EraPParams era => DecCBOR (ProposalProcedure era) where
513523
decCBOR =
514-
decode $
515-
RecD ProposalProcedure
516-
<! From
517-
<! From
518-
<! From
519-
<! From
524+
ifDecoderVersionAtLeast (natVersion @12) decodeProposalProcedure $
525+
decode $
526+
RecD ProposalProcedure
527+
<! From
528+
<! From
529+
<! From
530+
<! From
531+
where
532+
decodeProposalProcedure =
533+
decodeRecordNamed "ProposalProcedure" (const 4) $
534+
ProposalProcedure <$> decCBOR <*> decCBOR <*> decCBOR <*> decCBOR
520535
{-# INLINE decCBOR #-}
521536

522537
instance EraPParams era => EncCBOR (ProposalProcedure era) where
@@ -932,10 +947,15 @@ instance Era era => Default (Constitution era) where
932947

933948
instance Era era => DecCBOR (Constitution era) where
934949
decCBOR =
935-
decode $
936-
RecD Constitution
937-
<! From
938-
<! D (decodeNullStrictMaybe decCBOR)
950+
ifDecoderVersionAtLeast (natVersion @12) decodeConstitution $
951+
decode $
952+
RecD Constitution
953+
<! From
954+
<! D (decodeNullStrictMaybe decCBOR)
955+
where
956+
decodeConstitution =
957+
decodeRecordNamed "Constitution" (const 2) $
958+
Constitution <$> decCBOR <*> decodeNullStrictMaybe decCBOR
939959

940960
instance Era era => EncCBOR (Constitution era) where
941961
encCBOR Constitution {..} =

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import Cardano.Ledger.Binary (
4949
peekTokenType,
5050
serialize,
5151
)
52-
import Cardano.Ledger.Binary.Coders (Decode (..), Encode (..), decode, encode, (!>), (<*!))
52+
import Cardano.Ledger.Binary.Coders (Encode (..), encode, (!>))
5353
import Cardano.Ledger.Conway.Tx (AlonzoEraTx (..), Tx (..), getConwayMinFeeTx)
5454
import Cardano.Ledger.Core
5555
import Cardano.Ledger.Dijkstra.Era (DijkstraEra)
@@ -158,11 +158,11 @@ instance (EraTx era, Typeable l) => DecCBOR (Annotator (DijkstraTx l era)) where
158158
decCBOR = withSTxBothLevels @l $ \case
159159
STopTx -> decodeDijkstraTopTx True
160160
SSubTx ->
161-
decode $
162-
Ann (RecD DijkstraSubTx)
163-
<*! From
164-
<*! From
165-
<*! D (sequence <$> decodeNullStrictMaybe decCBOR)
161+
decodeRecordNamed "DijkstraSubTx" (const 3) $ do
162+
body <- decCBOR
163+
wits <- decCBOR
164+
aux <- sequence <$> decodeNullStrictMaybe decCBOR
165+
pure $ DijkstraSubTx <$> body <*> wits <*> aux
166166

167167
instance HasEraTxLevel DijkstraTx DijkstraEra where
168168
toSTxLevel DijkstraTx {} = STopTx

eras/mary/impl/src/Cardano/Ledger/Mary/Value.hs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import Cardano.Ledger.Binary (
4646
cborError,
4747
decodeInteger,
4848
decodeMap,
49+
decodeRecordNamed,
4950
decodeWord64,
5051
ifDecoderVersionAtLeast,
5152
peekTokenType,
@@ -296,10 +297,17 @@ decodeMaryValue = do
296297

297298
decodeValuePair :: (forall t. Decoder t Integer) -> Decoder s MaryValue
298299
decodeValuePair decodeMultiAssetAmount =
299-
decode $
300-
RecD MaryValue
301-
<! D (Coin . toInteger <$> decodeWord64)
302-
<! D (decodeMultiAsset decodeMultiAssetAmount)
300+
ifDecoderVersionAtLeast (natVersion @12) decodeMaryValue' $
301+
decode $
302+
RecD MaryValue
303+
<! From
304+
<! D (decodeMultiAsset decodeMultiAssetAmount)
305+
where
306+
decodeMaryValue' =
307+
decodeRecordNamed "MaryValue" (const 2) $
308+
MaryValue
309+
<$> decCBOR
310+
<*> decodeMultiAsset decodeMultiAssetAmount
303311

304312
-- | `MultiAsset` can be used in two different circumstances:
305313
--

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import Cardano.Ledger.Binary (
114114
ToCBOR,
115115
cborError,
116116
decodeIntegralRational,
117+
decodeRecordNamed,
117118
encodeListLen,
118119
fromPlainDecoder,
119120
ifDecoderVersionAtLeast,
@@ -987,10 +988,13 @@ instance NFData Anchor where
987988

988989
instance DecCBOR Anchor where
989990
decCBOR =
990-
decode $
991-
RecD Anchor
992-
<! From
993-
<! From
991+
ifDecoderVersionAtLeast (natVersion @12) decodeAnchor $
992+
decode $
993+
RecD Anchor <! From <! From
994+
where
995+
decodeAnchor =
996+
decodeRecordNamed "Anchor" (const 2) $
997+
Anchor <$> decCBOR <*> decCBOR
994998

995999
instance EncCBOR Anchor where
9961000
encCBOR Anchor {..} =

libs/cardano-ledger-core/src/Cardano/Ledger/Plutus/ExUnits.hs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ import Cardano.Ledger.Binary (
3535
DecoderError (..),
3636
EncCBOR (encCBOR),
3737
cborError,
38+
decodeRecordNamed,
39+
ifDecoderVersionAtLeast,
40+
natVersion,
3841
)
3942
import Cardano.Ledger.Binary.Coders (
4043
Decode (D, From, RecD),
@@ -190,8 +193,14 @@ instance EncCBOR ExUnits where
190193
encCBOR (ExUnits m s) = encode $ Rec ExUnits !> To m !> To s
191194

192195
instance DecCBOR ExUnits where
193-
decCBOR = decode $ RecD ExUnits <! D decNat <! D decNat
196+
decCBOR =
197+
ifDecoderVersionAtLeast (natVersion @12) decodeExUnits $
198+
decode $
199+
RecD ExUnits <! D decNat <! D decNat
194200
where
201+
decodeExUnits =
202+
decodeRecordNamed "ExUnits" (const 2) $
203+
ExUnits <$> decNat <*> decNat
195204
decNat :: Decoder s Natural
196205
decNat = do
197206
x <- decCBOR
@@ -211,5 +220,12 @@ instance EncCBOR Prices where
211220
encCBOR (Prices m s) = encode $ Rec Prices !> To m !> To s
212221

213222
instance DecCBOR Prices where
214-
decCBOR = decode $ RecD Prices <! From <! From
223+
decCBOR =
224+
ifDecoderVersionAtLeast (natVersion @12) decodePrices $
225+
decode $
226+
RecD Prices <! From <! From
227+
where
228+
decodePrices =
229+
decodeRecordNamed "Prices" (const 2) $
230+
Prices <$> decCBOR <*> decCBOR
215231
{-# INLINE decCBOR #-}

0 commit comments

Comments
 (0)