Skip to content

Commit 8a64ca0

Browse files
committed
Define DecCBOR, FromCBOR instances in EraPParams based on pparams
1 parent cdff0b9 commit 8a64ca0

File tree

1 file changed

+42
-16
lines changed
  • libs/cardano-ledger-core/src/Cardano/Ledger/Core

1 file changed

+42
-16
lines changed

libs/cardano-ledger-core/src/Cardano/Ledger/Core/PParams.hs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,28 @@ import Cardano.Ledger.BaseTypes (
9191
maybeToStrictMaybe,
9292
)
9393
import Cardano.Ledger.Binary
94+
import Cardano.Ledger.Binary.Coders
9495
import Cardano.Ledger.Coin (Coin (..))
95-
import Cardano.Ledger.Core.Era (Era (..), PreviousEra, ProtVerAtMost, toEraCBOR)
96+
import Cardano.Ledger.Core.Era (Era (..), PreviousEra, ProtVerAtMost, fromEraCBOR, toEraCBOR)
9697
import Cardano.Ledger.HKD (HKD, HKDApplicative, HKDFunctor (..), NoUpdate (..))
9798
import Cardano.Ledger.Plutus.ToPlutusData (ToPlutusData (..))
9899
import Control.DeepSeq (NFData)
99100
import Control.Monad.Identity (Identity)
100101
import Data.Aeson (FromJSON, ToJSON)
101-
import Data.Data (Typeable)
102102
import Data.Default (Default (..))
103-
import qualified Data.Foldable as F (foldMap', foldl')
103+
import qualified Data.Foldable as F (foldMap', foldl', foldlM)
104+
import Data.IntMap (IntMap)
105+
import qualified Data.IntMap as IntMap
104106
import Data.Kind (Type)
105107
import Data.Map.Strict (Map)
106108
import qualified Data.Map.Strict as Map
109+
import Data.Proxy (Proxy (..))
107110
import Data.Text (Text)
111+
import qualified Data.Text as T
112+
import Data.Typeable (typeRep)
108113
import Data.Word (Word16, Word32)
109114
import GHC.Generics (Generic (..), K1 (..), M1 (..), U1, V1, type (:*:) (..))
110-
import Lens.Micro (Lens', SimpleGetter, lens, (^.))
115+
import Lens.Micro (Lens', SimpleGetter, lens, set, (^.))
111116
import NoThunks.Class (NoThunks)
112117

113118
-- | Protocol parameters
@@ -144,14 +149,21 @@ instance EraPParams era => EncCBOR (PParams era) where
144149
where
145150
toEnc PParam' {ppLens} = encCBOR $ pp ^. ppLens
146151

147-
deriving newtype instance
148-
(Typeable era, DecCBOR (PParamsHKD Identity era)) => DecCBOR (PParams era)
152+
instance EraPParams era => DecCBOR (PParams era) where
153+
decCBOR =
154+
decodeRecordNamed
155+
(T.pack . show . typeRep $ Proxy @(PParams era))
156+
(const (fromIntegral (length (eraPParams @era))))
157+
$ F.foldlM accum (emptyPParams @era) (eraPParams @era)
158+
where
159+
accum acc PParam' {ppLens} =
160+
set ppLens <$> decCBOR <*> pure acc
149161

150162
instance EraPParams era => ToCBOR (PParams era) where
151163
toCBOR = toEraCBOR @era
152164

153-
deriving newtype instance
154-
(Typeable era, FromCBOR (PParamsHKD Identity era)) => FromCBOR (PParams era)
165+
instance EraPParams era => FromCBOR (PParams era) where
166+
fromCBOR = fromEraCBOR @era
155167

156168
deriving instance Generic (PParams era)
157169

@@ -189,14 +201,32 @@ instance EraPParams era => EncCBOR (PParamsUpdate era) where
189201
SJust y -> (n + 1, acc <> y)
190202
SNothing -> (n, acc)
191203

192-
deriving newtype instance
193-
(Typeable era, DecCBOR (PParamsHKD StrictMaybe era)) => DecCBOR (PParamsUpdate era)
204+
instance EraPParams era => DecCBOR (PParamsUpdate era) where
205+
decCBOR =
206+
decode $
207+
SparseKeyed
208+
(show . typeRep $ Proxy @(PParamsUpdate era))
209+
emptyPParamsUpdate
210+
updateField
211+
[]
212+
where
213+
updateField k =
214+
IntMap.findWithDefault
215+
(invalidField k)
216+
(fromIntegral k)
217+
updateFieldMap
218+
updateFieldMap :: IntMap (Field (PParamsUpdate era))
219+
updateFieldMap =
220+
IntMap.fromList
221+
[ (fromIntegral ppuTag, field (set ppuLens . SJust) From)
222+
| PParam' {ppUpdate = Just PParamUpdate {ppuTag, ppuLens}} <- eraPParams @era
223+
]
194224

195225
instance EraPParams era => ToCBOR (PParamsUpdate era) where
196226
toCBOR = toEraCBOR @era
197227

198-
deriving newtype instance
199-
(Typeable era, FromCBOR (PParamsHKD StrictMaybe era)) => FromCBOR (PParamsUpdate era)
228+
instance EraPParams era => FromCBOR (PParamsUpdate era) where
229+
fromCBOR = fromEraCBOR @era
200230

201231
deriving newtype instance
202232
ToJSON (PParamsHKD StrictMaybe era) => ToJSON (PParamsUpdate era)
@@ -252,17 +282,13 @@ class
252282
, Ord (PParamsHKD Identity era)
253283
, Show (PParamsHKD Identity era)
254284
, NFData (PParamsHKD Identity era)
255-
, DecCBOR (PParamsHKD Identity era)
256-
, FromCBOR (PParamsHKD Identity era)
257285
, NoThunks (PParamsHKD Identity era)
258286
, ToJSON (PParamsHKD Identity era)
259287
, FromJSON (PParamsHKD Identity era)
260288
, Eq (PParamsHKD StrictMaybe era)
261289
, Ord (PParamsHKD StrictMaybe era)
262290
, Show (PParamsHKD StrictMaybe era)
263291
, NFData (PParamsHKD StrictMaybe era)
264-
, DecCBOR (PParamsHKD StrictMaybe era)
265-
, FromCBOR (PParamsHKD StrictMaybe era)
266292
, NoThunks (PParamsHKD StrictMaybe era)
267293
, ToJSON (PParamsHKD StrictMaybe era)
268294
) =>

0 commit comments

Comments
 (0)