Skip to content

Commit 25723a5

Browse files
authored
Merge pull request #4911 from IntersectMBO/lehins/improve-decoding-error-message
Make `invalidKey` report the type that it is decoding
2 parents f5b3569 + f23546d commit 25723a5

File tree

19 files changed

+95
-77
lines changed

19 files changed

+95
-77
lines changed

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

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{-# LANGUAGE GADTs #-}
99
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1010
{-# LANGUAGE InstanceSigs #-}
11+
{-# LANGUAGE LambdaCase #-}
1112
{-# LANGUAGE MultiParamTypeClasses #-}
1213
{-# LANGUAGE NamedFieldPuns #-}
1314
{-# LANGUAGE OverloadedStrings #-}
@@ -97,6 +98,7 @@ import Cardano.Ledger.Binary.Coders (
9798
decode,
9899
encode,
99100
field,
101+
invalidField,
100102
(!>),
101103
)
102104
import Cardano.Ledger.Coin (Coin (..))
@@ -663,31 +665,32 @@ instance Era era => EncCBOR (AlonzoPParams StrictMaybe era) where
663665
encCBOR ppup = encode (encodePParamsUpdate ppup)
664666

665667
updateField :: Word -> Field (AlonzoPParams StrictMaybe era)
666-
updateField 0 = field (\x up -> up {appMinFeeA = SJust x}) From
667-
updateField 1 = field (\x up -> up {appMinFeeB = SJust x}) From
668-
updateField 2 = field (\x up -> up {appMaxBBSize = SJust x}) From
669-
updateField 3 = field (\x up -> up {appMaxTxSize = SJust x}) From
670-
updateField 4 = field (\x up -> up {appMaxBHSize = SJust x}) From
671-
updateField 5 = field (\x up -> up {appKeyDeposit = SJust x}) From
672-
updateField 6 = field (\x up -> up {appPoolDeposit = SJust x}) From
673-
updateField 7 = field (\x up -> up {appEMax = SJust x}) From
674-
updateField 8 = field (\x up -> up {appNOpt = SJust x}) From
675-
updateField 9 = field (\x up -> up {appA0 = SJust x}) From
676-
updateField 10 = field (\x up -> up {appRho = SJust x}) From
677-
updateField 11 = field (\x up -> up {appTau = SJust x}) From
678-
updateField 12 = field (\x up -> up {appD = SJust x}) From
679-
updateField 13 = field (\x up -> up {appExtraEntropy = SJust x}) From
680-
updateField 14 = field (\x up -> up {appProtocolVersion = SJust x}) From
681-
updateField 16 = field (\x up -> up {appMinPoolCost = SJust x}) From
682-
updateField 17 = field (\x up -> up {appCoinsPerUTxOWord = SJust x}) From
683-
updateField 18 = field (\x up -> up {appCostModels = SJust x}) From
684-
updateField 19 = field (\x up -> up {appPrices = SJust x}) From
685-
updateField 20 = field (\x up -> up {appMaxTxExUnits = SJust x}) From
686-
updateField 21 = field (\x up -> up {appMaxBlockExUnits = SJust x}) From
687-
updateField 22 = field (\x up -> up {appMaxValSize = SJust x}) From
688-
updateField 23 = field (\x up -> up {appCollateralPercentage = SJust x}) From
689-
updateField 24 = field (\x up -> up {appMaxCollateralInputs = SJust x}) From
690-
updateField k = field (\_x up -> up) (Invalid k)
668+
updateField = \case
669+
0 -> field (\x up -> up {appMinFeeA = SJust x}) From
670+
1 -> field (\x up -> up {appMinFeeB = SJust x}) From
671+
2 -> field (\x up -> up {appMaxBBSize = SJust x}) From
672+
3 -> field (\x up -> up {appMaxTxSize = SJust x}) From
673+
4 -> field (\x up -> up {appMaxBHSize = SJust x}) From
674+
5 -> field (\x up -> up {appKeyDeposit = SJust x}) From
675+
6 -> field (\x up -> up {appPoolDeposit = SJust x}) From
676+
7 -> field (\x up -> up {appEMax = SJust x}) From
677+
8 -> field (\x up -> up {appNOpt = SJust x}) From
678+
9 -> field (\x up -> up {appA0 = SJust x}) From
679+
10 -> field (\x up -> up {appRho = SJust x}) From
680+
11 -> field (\x up -> up {appTau = SJust x}) From
681+
12 -> field (\x up -> up {appD = SJust x}) From
682+
13 -> field (\x up -> up {appExtraEntropy = SJust x}) From
683+
14 -> field (\x up -> up {appProtocolVersion = SJust x}) From
684+
16 -> field (\x up -> up {appMinPoolCost = SJust x}) From
685+
17 -> field (\x up -> up {appCoinsPerUTxOWord = SJust x}) From
686+
18 -> field (\x up -> up {appCostModels = SJust x}) From
687+
19 -> field (\x up -> up {appPrices = SJust x}) From
688+
20 -> field (\x up -> up {appMaxTxExUnits = SJust x}) From
689+
21 -> field (\x up -> up {appMaxBlockExUnits = SJust x}) From
690+
22 -> field (\x up -> up {appMaxValSize = SJust x}) From
691+
23 -> field (\x up -> up {appCollateralPercentage = SJust x}) From
692+
24 -> field (\x up -> up {appMaxCollateralInputs = SJust x}) From
693+
k -> invalidField k
691694

692695
instance Era era => DecCBOR (AlonzoPParams StrictMaybe era) where
693696
decCBOR =

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Rules/Utxo.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ encFail NoCollateralInputs =
665665
Sum NoCollateralInputs 20
666666

667667
decFail ::
668-
( DecCBOR (TxOut era)
668+
( Era era
669+
, DecCBOR (TxOut era)
669670
, DecCBOR (Value era)
670671
, DecCBOR (PredicateFailure (EraRule "UTXOS" era))
671672
) =>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ instance
432432

433433
instance
434434
( Typeable era
435+
, Typeable (TxBody era)
436+
, Typeable (TxWits era)
437+
, Typeable (TxAuxData era)
435438
, DecCBOR (Annotator (TxBody era))
436439
, DecCBOR (Annotator (TxWits era))
437440
, DecCBOR (Annotator (TxAuxData era))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ instance Era era => DecCBOR (Annotator (AlonzoTxAuxDataRaw era)) where
218218
auxDataField 2 = fieldA (addPlutusScripts PlutusV1) (D (guardPlutus PlutusV1 >> decCBOR))
219219
auxDataField 3 = fieldA (addPlutusScripts PlutusV2) (D (guardPlutus PlutusV2 >> decCBOR))
220220
auxDataField 4 = fieldA (addPlutusScripts PlutusV3) (D (guardPlutus PlutusV3 >> decCBOR))
221-
auxDataField n = field (\_ t -> t) (Invalid n)
221+
auxDataField n = invalidField n
222222

223223
instance Era era => DecCBOR (AlonzoTxAuxDataRaw era) where
224224
decCBOR =
@@ -244,7 +244,7 @@ instance Era era => DecCBOR (AlonzoTxAuxDataRaw era) where
244244
auxDataField 2 = field (addPlutusScripts PlutusV1) (D (guardPlutus PlutusV1 >> decCBOR))
245245
auxDataField 3 = field (addPlutusScripts PlutusV2) (D (guardPlutus PlutusV2 >> decCBOR))
246246
auxDataField 4 = field (addPlutusScripts PlutusV3) (D (guardPlutus PlutusV3 >> decCBOR))
247-
auxDataField n = field (\_ t -> t) (Invalid n)
247+
auxDataField n = invalidField n
248248

249249
decodeTxAuxDataByTokenType :: forall t s. Decoder s t -> Decoder s t -> Decoder s t -> Decoder s t
250250
decodeTxAuxDataByTokenType decodeShelley decodeAllegra decodeAlonzo =

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxBody/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ instance
622622
bodyFields 11 = ofield (\x tx -> tx {atbrScriptIntegrityHash = x}) From
623623
bodyFields 14 = field (\x tx -> tx {atbrReqSignerHashes = x}) From
624624
bodyFields 15 = ofield (\x tx -> tx {atbrTxNetworkId = x}) From
625-
bodyFields n = field (\_ t -> t) (Invalid n)
625+
bodyFields n = invalidField n
626626
requiredFields =
627627
[ (0, "inputs")
628628
, (1, "outputs")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ instance
707707
txWitnessField 5 = fieldAA (\x wits -> wits {atwrRdmrsTxWits = x}) From
708708
txWitnessField 6 = fieldA addScripts (decodePlutus SPlutusV2)
709709
txWitnessField 7 = fieldA addScripts (decodePlutus SPlutusV3)
710-
txWitnessField n = field (\_ t -> t) (Invalid n)
710+
txWitnessField n = invalidField n
711711
{-# INLINE txWitnessField #-}
712712

713713
nativeScriptsDecoder :: Decoder s (Annotator (Map ScriptHash (Script era)))
@@ -766,7 +766,7 @@ instance
766766
txWitnessField 5 = field (\x wits -> wits {atwrRdmrsTxWits = x}) From
767767
txWitnessField 6 = field addScripts (decodePlutus SPlutusV2)
768768
txWitnessField 7 = field addScripts (decodePlutus SPlutusV3)
769-
txWitnessField n = field (\_ t -> t) (Invalid n)
769+
txWitnessField n = invalidField n
770770

771771
nativeScriptsDecoder :: Decoder s (Map ScriptHash (Script era))
772772
nativeScriptsDecoder =

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Translation/TranslationInstance.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ instance
148148

149149
instance
150150
( Typeable era
151+
, Typeable (Tx era)
151152
, DecCBOR (PParams era)
152153
, DecCBOR (UTxO era)
153154
, DecCBOR (Annotator (Core.Tx era))
@@ -183,6 +184,7 @@ instance
183184
deserializeTranslationInstances ::
184185
forall era.
185186
( Era era
187+
, Typeable (Tx era)
186188
, DecCBOR (PParams era)
187189
, DecCBOR (UTxO era)
188190
, DecCBOR (Annotator (Core.Tx era))

eras/babbage/impl/src/Cardano/Ledger/Babbage/PParams.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import Cardano.Ledger.Binary.Coders (
8888
decode,
8989
encode,
9090
field,
91+
invalidField,
9192
(!>),
9293
)
9394
import Cardano.Ledger.Coin (Coin (..))
@@ -495,7 +496,7 @@ updateField 21 = field (\x up -> up {bppMaxBlockExUnits = SJust x}) From
495496
updateField 22 = field (\x up -> up {bppMaxValSize = SJust x}) From
496497
updateField 23 = field (\x up -> up {bppCollateralPercentage = SJust x}) From
497498
updateField 24 = field (\x up -> up {bppMaxCollateralInputs = SJust x}) From
498-
updateField k = field (\_x up -> up) (Invalid k)
499+
updateField k = invalidField k
499500

500501
instance Era era => DecCBOR (BabbagePParams StrictMaybe era) where
501502
decCBOR =

eras/babbage/impl/src/Cardano/Ledger/Babbage/TxBody/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ instance
839839
bodyFields 11 = ofield (\x tx -> tx {btbrScriptIntegrityHash = x}) From
840840
bodyFields 14 = field (\x tx -> tx {btbrReqSignerHashes = x}) From
841841
bodyFields 15 = ofield (\x tx -> tx {btbrTxNetworkId = x}) From
842-
bodyFields n = field (\_ t -> t) (Invalid n)
842+
bodyFields n = invalidField n
843843
{-# INLINE bodyFields #-}
844844
requiredFields :: [(Word, String)]
845845
requiredFields =

eras/babbage/impl/src/Cardano/Ledger/Babbage/TxOut.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ decodeTxOut decAddr = do
646646
ofield
647647
(\x txo -> txo {decodingTxOutScript = x})
648648
(D $ decodeCIC "Script")
649-
bodyFields n = field (\_ t -> t) (Invalid n)
649+
bodyFields n = invalidField n
650650
{-# INLINE bodyFields #-}
651651
requiredFields =
652652
[ (0, "addr")

0 commit comments

Comments
 (0)