Skip to content

Commit 0eda9fe

Browse files
authored
Merge pull request #4913 from IntersectMBO/lehins/fix-pointer-decoding-leniency
Fix various issues with Ptr decoding
2 parents 6da6559 + 862669a commit 0eda9fe

File tree

7 files changed

+38
-30
lines changed

7 files changed

+38
-30
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import Cardano.Ledger.Address (
4747
compactAddr,
4848
decompactAddr,
4949
fromCborBothAddr,
50-
fromCborRigorousBothAddr,
5150
)
5251
import Cardano.Ledger.Alonzo.Core
5352
import Cardano.Ledger.Alonzo.TxBody (
@@ -535,7 +534,7 @@ instance
535534
peekTokenType >>= \case
536535
TypeBytes -> decodeMemPack
537536
TypeBytesIndef -> decodeMemPack
538-
_ -> decodeBabbageTxOut fromCborRigorousBothAddr
537+
_ -> decCBOR
539538
pure $! internBabbageTxOut (interns credsInterns) txOut
540539
{-# INLINEABLE decShareCBOR #-}
541540

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Cardano.Ledger.Conway.Translation (
2020
import Cardano.Ledger.Address (addrPtrNormalize)
2121
import Cardano.Ledger.Babbage (BabbageEra)
2222
import Cardano.Ledger.Binary (DecoderError)
23-
import Cardano.Ledger.CertState (CommitteeState (..), upgradeCertState)
23+
import Cardano.Ledger.CertState (EraCertState (..))
2424
import Cardano.Ledger.Conway.CertState ()
2525
import Cardano.Ledger.Conway.Core hiding (Tx)
2626
import Cardano.Ledger.Conway.Era (ConwayEra)
@@ -46,12 +46,12 @@ import Cardano.Ledger.Shelley.API (
4646
PState (..),
4747
StrictMaybe (..),
4848
UTxOState (..),
49-
VState (..),
5049
)
5150
import qualified Cardano.Ledger.Shelley.API as API
52-
import Cardano.Ledger.Shelley.CertState (ShelleyCertState)
51+
import Cardano.Ledger.Shelley.CertState (ShelleyCertState (..))
5352
import Cardano.Ledger.Shelley.LedgerState (
5453
epochStateGovStateL,
54+
lsCertStateL,
5555
)
5656
import qualified Cardano.Ledger.UMap as UM
5757
import Data.Default (Default (def))
@@ -147,26 +147,20 @@ instance TranslateEra ConwayEra DState where
147147
, UM.umPtrs = mempty
148148
}
149149

150-
instance TranslateEra ConwayEra CommitteeState where
151-
translateEra _ CommitteeState {..} = pure CommitteeState {..}
152-
153-
instance TranslateEra ConwayEra VState where
154-
translateEra ctx VState {..} = do
155-
committeeState <- translateEra ctx vsCommitteeState
156-
pure VState {vsCommitteeState = committeeState, ..}
157-
158150
instance TranslateEra ConwayEra PState where
159151
translateEra _ PState {..} = pure PState {..}
160152

161-
instance TranslateEra ConwayEra ShelleyCertState where
162-
translateEra ConwayGenesis {} = pure . upgradeCertState
163-
164153
instance TranslateEra ConwayEra API.LedgerState where
165154
translateEra conwayGenesis ls =
166155
pure
167156
API.LedgerState
168157
{ API.lsUTxOState = translateEra' conwayGenesis $ API.lsUTxOState ls
169-
, API.lsCertState = translateEra' conwayGenesis $ API.lsCertState ls
158+
, API.lsCertState =
159+
ShelleyCertState
160+
{ shelleyCertDState = translateEra' conwayGenesis (ls ^. lsCertStateL . certDStateL)
161+
, shelleyCertPState = translateEra' conwayGenesis (ls ^. lsCertStateL . certPStateL)
162+
, shelleyCertVState = def
163+
}
170164
}
171165

172166
translateGovState ::

libs/cardano-ledger-core/CHANGELOG.md

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

33
## 1.17.0.0
44

5+
* Add boolean argument to `fromCborRigorousBothAddr` for lenient `Ptr` decoding
56
* Add `ToCBOR` and `FromCBOR` instances for:
67
* `BoundedRatio`
78
* `PositiveUnitInterval`

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
{-# LANGUAGE GADTs #-}
88
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
99
{-# LANGUAGE InstanceSigs #-}
10-
{-# LANGUAGE LambdaCase #-}
1110
{-# LANGUAGE OverloadedStrings #-}
12-
{-# LANGUAGE PatternSynonyms #-}
1311
{-# LANGUAGE ScopedTypeVariables #-}
1412
{-# LANGUAGE TypeApplications #-}
1513
{-# LANGUAGE TypeSynonymInstances #-}
@@ -427,15 +425,25 @@ fromCborCompactAddr = snd <$> fromCborBothAddr
427425
-- that it was encoded as.
428426
fromCborBothAddr :: Decoder s (Addr, CompactAddr)
429427
fromCborBothAddr = do
430-
ifDecoderVersionAtLeast (natVersion @7) fromCborRigorousBothAddr fromCborBackwardsBothAddr
428+
ifDecoderVersionAtLeast
429+
(natVersion @7)
430+
( ifDecoderVersionAtLeast
431+
(natVersion @9)
432+
(fromCborRigorousBothAddr False)
433+
(fromCborRigorousBothAddr True)
434+
)
435+
fromCborBackwardsBothAddr
431436
{-# INLINE fromCborBothAddr #-}
432437

433438
-- | Starting with Babbage we no longer allow addresses with garbage in them.
434-
fromCborRigorousBothAddr :: Decoder s (Addr, CompactAddr)
435-
fromCborRigorousBothAddr = do
439+
fromCborRigorousBothAddr ::
440+
-- | Should there be a hard failure for garbage pointers (`False`) or should they be normalized instead (`True`)
441+
Bool ->
442+
Decoder s (Addr, CompactAddr)
443+
fromCborRigorousBothAddr isPtrLenient = do
436444
sbs <- decCBOR
437445
flip evalStateT 0 $ do
438-
addr <- decodeAddrStateLenientT False False sbs
446+
addr <- decodeAddrStateLenientT isPtrLenient False sbs
439447
pure (addr, UnsafeCompactAddr sbs)
440448
{-# INLINE fromCborRigorousBothAddr #-}
441449

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,4 @@ instance DecCBORGroup Ptr where
320320
decCBORGroup = do
321321
let decPtrStrict = Ptr <$> decCBOR <*> decCBOR <*> decCBOR
322322
decPtrNormalized = mkPtrNormalized <$> decCBOR <*> decCBOR <*> decCBOR
323-
ifDecoderVersionAtLeast (natVersion @7) decPtrStrict decPtrNormalized
323+
ifDecoderVersionAtLeast (natVersion @9) decPtrStrict decPtrNormalized

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
{-# LANGUAGE OverloadedStrings #-}
1212
{-# LANGUAGE RankNTypes #-}
1313
{-# LANGUAGE ScopedTypeVariables #-}
14-
{-# LANGUAGE StandaloneDeriving #-}
1514
{-# LANGUAGE TypeFamilies #-}
1615
{-# LANGUAGE UndecidableInstances #-}
1716

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ import Cardano.Ledger.BaseTypes (strictMaybe)
121121
import Cardano.Ledger.Binary
122122
import Cardano.Ledger.Coin (Coin (..), CompactForm (CompactCoin), compactCoinOrError)
123123
import Cardano.Ledger.Compactible (Compactible (..))
124-
import Cardano.Ledger.Credential (Credential (..), Ptr)
124+
import Cardano.Ledger.Credential (Credential (..), Ptr, mkPtrNormalized)
125125
import Cardano.Ledger.DRep (DRep)
126126
import Cardano.Ledger.Keys (KeyHash, KeyRole (..))
127127
import Control.DeepSeq (NFData (..))
@@ -469,11 +469,18 @@ instance DecShareCBOR UMap where
469469
decodeRecordNamed "UMap" (const 2) $ do
470470
umElems <- decodeMap (interns a <$> decCBOR) (decShareCBOR (b, c))
471471
let a' = internsFromMap umElems <> a
472+
decodePtrNormalized =
473+
decodeRecordNamed "Ptr" (const 3) $
474+
mkPtrNormalized <$> decCBOR <*> decCBOR <*> decCBOR
475+
decDropPtrMap = do
476+
m <- decodeMap decodePtrNormalized decCBOR
477+
-- ensure that we are dropping the same type as needed for decoding
478+
let idConst :: a -> a -> a
479+
idConst = const
480+
pure $ idConst mempty m
472481
umPtrs <-
473-
ifDecoderVersionAtLeast
474-
(natVersion @9)
475-
(mempty <$ dropCBOR (Proxy @(Map (Credential 'Staking) (Set Ptr))))
476-
$ decodeMap decCBOR (interns a' <$> decCBOR)
482+
ifDecoderVersionAtLeast (natVersion @9) decDropPtrMap $
483+
decodeMap decCBOR (interns a' <$> decCBOR)
477484
pure (UMap {umElems, umPtrs}, (a', b, c))
478485
)
479486

0 commit comments

Comments
 (0)