Skip to content

Commit 331ee56

Browse files
committed
Define ToPlutusData instance in ConwayEraPParams based on pparams
and substitute it for the existing one
1 parent 11fb035 commit 331ee56

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{-# LANGUAGE InstanceSigs #-}
99
{-# LANGUAGE LambdaCase #-}
1010
{-# LANGUAGE MultiParamTypeClasses #-}
11+
{-# LANGUAGE NamedFieldPuns #-}
1112
{-# LANGUAGE OverloadedStrings #-}
1213
{-# LANGUAGE RankNTypes #-}
1314
{-# LANGUAGE RecordWildCards #-}
@@ -99,6 +100,7 @@ import Cardano.Ledger.BaseTypes (
99100
ProtVer (ProtVer),
100101
UnitInterval,
101102
integralToBounded,
103+
strictMaybeToMaybe,
102104
)
103105
import Cardano.Ledger.Binary (
104106
DecCBOR (..),
@@ -131,8 +133,11 @@ import Control.DeepSeq (NFData (..), rwhnf)
131133
import Data.Aeson hiding (Encoding, Value, decode, encode)
132134
import qualified Data.Aeson as Aeson
133135
import Data.Default (Default (def))
136+
import Data.Foldable (foldlM)
134137
import Data.Functor.Identity (Identity)
138+
import qualified Data.IntMap as IntMap
135139
import qualified Data.Map.Strict as Map
140+
import Data.Maybe (mapMaybe)
136141
import Data.Maybe.Strict (StrictMaybe (..))
137142
import Data.Proxy
138143
import Data.Set (Set)
@@ -161,6 +166,30 @@ class BabbageEraPParams era => ConwayEraPParams era where
161166
hkdMinFeeRefScriptCostPerByteL ::
162167
HKDFunctor f => Lens' (PParamsHKD f era) (HKD f NonNegativeInterval)
163168

169+
instance ConwayEraPParams era => ToPlutusData (PParamsUpdate era) where
170+
toPlutusData ppu = P.Map $ mapMaybe ppToData (eraPParams @era)
171+
where
172+
ppToData PParam' {ppUpdate} = do
173+
PParamUpdate {ppuTag, ppuLens} <- ppUpdate
174+
t <- strictMaybeToMaybe $ ppu ^. ppuLens
175+
pure (P.I (toInteger @Word ppuTag), toPlutusData t)
176+
177+
fromPlutusData (P.Map dataPairs) = foldlM accum emptyPParamsUpdate dataPairs
178+
where
179+
accum acc (dataKey, dataVal) = do
180+
tag <- fromPlutusData @Word dataKey
181+
PParam' {ppUpdate} <-
182+
IntMap.lookup (fromIntegral tag) ppMap
183+
PParamUpdate {ppuLens} <- ppUpdate
184+
plutusData <- fromPlutusData dataVal
185+
pure $ set ppuLens (SJust plutusData) acc
186+
ppMap =
187+
IntMap.fromList
188+
[ (fromIntegral ppuTag, pp)
189+
| pp@PParam' {ppUpdate = Just PParamUpdate {ppuTag}} <- eraPParams @era
190+
]
191+
fromPlutusData _ = Nothing
192+
164193
ppPoolVotingThresholdsL ::
165194
forall era. ConwayEraPParams era => Lens' (PParams era) PoolVotingThresholds
166195
ppPoolVotingThresholdsL = ppLensHKD . hkdPoolVotingThresholdsL @era @Identity

eras/conway/impl/src/Cardano/Ledger/Conway/Plutus/Context.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ conwayPParam =
145145
-- given a ToPlutusData instance for PParamsUpdate
146146

147147
class
148-
( ToPlutusData (PParamsUpdate era)
149-
, EraPlutusTxInfo l era
150-
) =>
148+
EraPlutusTxInfo l era =>
151149
ConwayEraPlutusTxInfo (l :: Language) era
152150
where
153151
toPlutusChangedParameters :: proxy l -> PParamsUpdate era -> PV3.ChangedParameters

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ import Cardano.Ledger.Conway.Governance (
8181
)
8282
import Cardano.Ledger.Conway.Plutus.Context (
8383
ConwayEraPlutusTxInfo (toPlutusChangedParameters),
84-
conwayPParamMap,
85-
pparamUpdateFromData,
86-
pparamUpdateToData,
8784
)
8885
import Cardano.Ledger.Conway.Scripts (ConwayPlutusPurpose (..), PlutusScript (..))
8986
import Cardano.Ledger.Conway.Tx ()
@@ -730,12 +727,5 @@ scriptPurposeToScriptInfo sp maybeSpendingData =
730727
PV3.Voting voter -> PV3.VotingScript voter
731728
PV3.Proposing ix proposal -> PV3.ProposingScript ix proposal
732729

733-
-- ==========================
734-
-- Instances
735-
736-
instance ToPlutusData (PParamsUpdate ConwayEra) where
737-
toPlutusData = pparamUpdateToData conwayPParamMap
738-
fromPlutusData = pparamUpdateFromData conwayPParamMap
739-
740730
instance ConwayEraPlutusTxInfo 'PlutusV3 ConwayEra where
741731
toPlutusChangedParameters _ x = PV3.ChangedParameters (PV3.dataToBuiltinData (toPlutusData x))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ makePParamMap xs = Map.fromList [(n, p) | p@(PParam n _) <- xs]
630630
-- | Represents a single protocol parameter and the data required to serialize it.
631631
data PParam' era where
632632
PParam' ::
633-
(DecCBOR t, EncCBOR t, FromJSON t, ToJSON t) =>
633+
(DecCBOR t, EncCBOR t, FromJSON t, ToJSON t, ToPlutusData t) =>
634634
{ ppName :: Text
635635
-- ^ Used as JSON key
636636
, ppLens :: Lens' (PParams era) t

0 commit comments

Comments
 (0)