Skip to content

Commit e75bf4a

Browse files
committed
Fix CostModel json serialization
1 parent 2383f4e commit e75bf4a

File tree

7 files changed

+30
-27
lines changed

7 files changed

+30
-27
lines changed

bench/tx-generator/src/Cardano/TxGenerator/Setup/Plutus.hs

-5
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,5 @@ preExecutePlutusV2 protocolVersion_ (PlutusScript _ (PlutusScriptSerialised scri
170170
, PlutusV2.txInfoRedeemers = PlutusV2.fromList []
171171
}
172172

173-
-- The order of the output list is important, but:
174-
-- * This way of flattening it is not guaranteed to always be correct.
175-
-- * There is no way to ensure that the list remains in the correct order.
176-
-- However, we're bound to the type `Cardano.Api.ProtocolParameters.CostModel` which
177-
-- might be changed from a key-value map to something providing stronger guarantees.
178173
flattenCostModel :: CostModel -> [Integer]
179174
flattenCostModel (CostModel cm) = cm

cardano-api/gen/Test/Gen/Cardano/Api.hs

+5-14
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ module Test.Gen.Cardano.Api
88
, genAlonzoGenesis
99
) where
1010

11-
import Cardano.Api.Shelley as Api
12-
1311
import qualified Data.Map.Strict as Map
1412
import Data.Word (Word64)
1513

@@ -63,9 +61,6 @@ genPrice = do
6361
Nothing -> fail "genPrice: genRational should give us a bounded rational"
6462
Just p -> pure p
6563

66-
genLanguage :: Gen Alonzo.Language
67-
genLanguage = return Alonzo.PlutusV1
68-
6964
genPrices :: Gen Alonzo.Prices
7065
genPrices = do
7166
prMem' <- genPrice
@@ -87,15 +82,11 @@ genExUnits = do
8782

8883
genCostModels :: Gen Alonzo.CostModels
8984
genCostModels = do
90-
CostModel cModel <- genCostModel
91-
lang <- genLanguage
92-
case Alonzo.mkCostModel lang cModel of
93-
Left err -> error $ "genCostModels: " <> show err
94-
Right alonzoCostModel ->
95-
Alonzo.CostModels
96-
<$> (conv <$> Gen.list (Range.linear 1 3) (return alonzoCostModel))
97-
<*> pure mempty
98-
<*> pure mempty
85+
alonzoCostModel <- genCostModel
86+
Alonzo.CostModels
87+
<$> (conv <$> Gen.list (Range.linear 1 3) (return alonzoCostModel))
88+
<*> pure mempty
89+
<*> pure mempty
9990
where
10091
conv :: [Alonzo.CostModel] -> Map.Map Alonzo.Language Alonzo.CostModel
10192
conv [] = mempty

cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -871,14 +871,14 @@ genUpdateProposal =
871871
<*> genProtocolParametersUpdate)
872872
<*> genEpochNo
873873

874-
genCostModel :: Gen CostModel
874+
genCostModel :: Gen Alonzo.CostModel
875875
genCostModel = do
876876
let costModelParams = Alonzo.getCostModelParams Plutus.testingCostModelV1
877877
eCostModel <- Alonzo.mkCostModel <$> genPlutusLanguage
878878
<*> mapM (const $ Gen.integral (Range.linear 0 5000)) costModelParams
879879
case eCostModel of
880880
Left err -> error $ "genCostModel: " <> show err
881-
Right cModel -> return . CostModel $ Alonzo.getCostModelParams cModel
881+
Right cModel -> return cModel
882882

883883
genPlutusLanguage :: Gen Language
884884
genPlutusLanguage = Gen.element [PlutusV1, PlutusV2]
@@ -887,7 +887,7 @@ _genCostModels :: Gen (Map AnyPlutusScriptVersion CostModel)
887887
_genCostModels =
888888
Gen.map (Range.linear 0 (length plutusScriptVersions))
889889
((,) <$> Gen.element plutusScriptVersions
890-
<*> genCostModel)
890+
<*> (Api.fromAlonzoCostModel <$> genCostModel))
891891
where
892892
plutusScriptVersions :: [AnyPlutusScriptVersion]
893893
plutusScriptVersions = [minBound..maxBound]

cardano-api/src/Cardano/Api.hs

+2
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ module Cardano.Api (
446446
ExecutionUnits(..),
447447
ExecutionUnitPrices(..),
448448
CostModel(..),
449+
toAlonzoCostModel,
450+
fromAlonzoCostModel,
449451

450452
-- ** Script addresses
451453
-- | Making addresses from scripts.

cardano-api/src/Cardano/Api/ProtocolParameters.hs

+15-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ import Data.Either.Combinators (maybeToRight)
7979
import Data.Map.Strict (Map)
8080
import qualified Data.Map.Strict as Map
8181
import Data.Maybe (isJust)
82-
import Data.Maybe.Strict (StrictMaybe(..))
82+
import Data.Maybe.Strict (StrictMaybe (..))
8383
import Data.String (IsString)
8484
import GHC.Generics
8585
import Lens.Micro
@@ -323,7 +323,7 @@ instance FromJSON ProtocolParameters where
323323
<*> o .: "monetaryExpansion"
324324
<*> o .: "treasuryCut"
325325
<*> o .:? "utxoCostPerWord"
326-
<*> o .:? "costModels" .!= Map.empty
326+
<*> (fmap unCostModels <$> o .:? "costModels") .!= Map.empty
327327
<*> o .:? "executionUnitPrices"
328328
<*> o .:? "maxTxExecutionUnits"
329329
<*> o .:? "maxBlockExecutionUnits"
@@ -355,7 +355,7 @@ instance ToJSON ProtocolParameters where
355355
, "txFeePerByte" .= protocolParamTxFeePerByte
356356
-- Alonzo era:
357357
, "utxoCostPerWord" .= protocolParamUTxOCostPerWord
358-
, "costModels" .= protocolParamCostModels
358+
, "costModels" .= CostModels protocolParamCostModels
359359
, "executionUnitPrices" .= protocolParamPrices
360360
, "maxTxExecutionUnits" .= protocolParamMaxTxExUnits
361361
, "maxBlockExecutionUnits" .= protocolParamMaxBlockExUnits
@@ -776,9 +776,20 @@ fromAlonzoPrices Alonzo.Prices{Alonzo.prSteps, Alonzo.prMem} =
776776

777777
newtype CostModel = CostModel [Integer]
778778
deriving (Eq, Show)
779-
deriving newtype (ToJSON, FromJSON)
780779
deriving newtype (ToCBOR, FromCBOR)
781780

781+
newtype CostModels = CostModels { unCostModels :: Map AnyPlutusScriptVersion CostModel }
782+
deriving (Eq, Show)
783+
784+
instance FromJSON CostModels where
785+
parseJSON v = CostModels . fromAlonzoCostModels <$> parseJSON v
786+
787+
instance ToJSON CostModels where
788+
toJSON (CostModels costModels) =
789+
case toAlonzoCostModels costModels of
790+
Left err -> error $ "Invalid cost model was constructed: " ++ err
791+
Right ledgerCostModels -> toJSON ledgerCostModels
792+
782793
data InvalidCostModel = InvalidCostModel CostModel Alonzo.CostModelApplyError
783794
deriving Show
784795

cardano-node-chairman/test/Main.hs

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Data.String (IsString (..))
88
import Prelude
99

1010
import qualified System.Environment as E
11+
import System.IO (BufferMode (LineBuffering), hSetBuffering, hSetEncoding, stdout, utf8)
1112
import qualified Test.Tasty as T
1213
import qualified Test.Tasty.Hedgehog as H
1314
import qualified Test.Tasty.Ingredients as T
@@ -35,6 +36,9 @@ ingredients = T.defaultIngredients
3536

3637
main :: IO ()
3738
main = do
39+
hSetBuffering stdout LineBuffering
40+
hSetEncoding stdout utf8
41+
3842
args <- E.getArgs
3943

4044
E.withArgs args $ tests >>= T.defaultMainWithIngredients ingredients

cardano-node/src/Cardano/Node/Queries.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ instance ConvertTxId ByronBlock where
101101

102102
instance ConvertTxId (ShelleyBlock protocol c) where
103103
txIdToRawBytes (ShelleyTxId txId) =
104-
Crypto.hashToBytes . Ledger.extractHash . Ledger._unTxId $ txId
104+
Crypto.hashToBytes . Ledger.extractHash . Ledger.unTxId $ txId
105105

106106
instance All ConvertTxId xs
107107
=> ConvertTxId (HardForkBlock xs) where

0 commit comments

Comments
 (0)