Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FromJSON instance to AlonzoGenesis
This instances was copied from node. But it should really live in ledger spec. Same as ShelleyGenesis does. This should be addressed in the near future.
- Loading branch information
1 parent
e74388a
commit a4b4778
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
ouroboros-consensus-cardano/tools/db-analyser/Block/Alonzo.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
|
||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
module Block.Alonzo () where | ||
|
||
import Control.Applicative | ||
import qualified Data.Map.Strict as Map | ||
import Data.Text (Text) | ||
import Prelude | ||
|
||
import Data.Aeson (FromJSON (..), (.:), (.:?)) | ||
import qualified Data.Aeson as Aeson | ||
import Data.Aeson.Types (FromJSONKey (..)) | ||
|
||
import qualified Cardano.Ledger.Alonzo.Genesis as Alonzo | ||
import qualified Cardano.Ledger.Alonzo.Language as Alonzo | ||
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo | ||
import qualified Cardano.Ledger.BaseTypes as Ledger | ||
|
||
import Plutus.V1.Ledger.Api (defaultCostModelParams) | ||
|
||
instance FromJSON Alonzo.AlonzoGenesis where | ||
parseJSON = Aeson.withObject "Alonzo Genesis" $ \o -> do | ||
coinsPerUTxOWord <- o .: "lovelacePerUTxOWord" | ||
<|> o .: "adaPerUTxOWord" | ||
cModels <- o .:? "costModels" | ||
prices <- o .: "executionPrices" | ||
maxTxExUnits <- o .: "maxTxExUnits" | ||
maxBlockExUnits <- o .: "maxBlockExUnits" | ||
maxValSize <- o .: "maxValueSize" | ||
collateralPercentage <- o .: "collateralPercentage" | ||
maxCollateralInputs <- o .: "maxCollateralInputs" | ||
case cModels of | ||
Nothing -> case Alonzo.CostModel <$> defaultCostModelParams of | ||
Just m -> return Alonzo.AlonzoGenesis | ||
{ Alonzo.coinsPerUTxOWord | ||
, Alonzo.costmdls = Map.singleton Alonzo.PlutusV1 m | ||
, Alonzo.prices | ||
, Alonzo.maxTxExUnits | ||
, Alonzo.maxBlockExUnits | ||
, Alonzo.maxValSize | ||
, Alonzo.collateralPercentage | ||
, Alonzo.maxCollateralInputs | ||
} | ||
Nothing -> fail "Failed to extract the cost model params from defaultCostModel" | ||
Just costmdls -> return Alonzo.AlonzoGenesis | ||
{ Alonzo.coinsPerUTxOWord | ||
, Alonzo.costmdls | ||
, Alonzo.prices | ||
, Alonzo.maxTxExUnits | ||
, Alonzo.maxBlockExUnits | ||
, Alonzo.maxValSize | ||
, Alonzo.collateralPercentage | ||
, Alonzo.maxCollateralInputs | ||
} | ||
|
||
deriving instance FromJSON Alonzo.ExUnits | ||
|
||
instance FromJSON Alonzo.Language where | ||
parseJSON = Aeson.withText "Language" languageFromText | ||
|
||
instance FromJSONKey Alonzo.Language where | ||
fromJSONKey = Aeson.FromJSONKeyTextParser languageFromText | ||
|
||
instance FromJSON Alonzo.Prices where | ||
parseJSON = | ||
Aeson.withObject "prices" $ \o -> do | ||
steps <- o .: "prSteps" | ||
mem <- o .: "prMem" | ||
prSteps <- checkBoundedRational steps | ||
prMem <- checkBoundedRational mem | ||
return Alonzo.Prices { Alonzo.prSteps, Alonzo.prMem } | ||
where | ||
-- We cannot round-trip via NonNegativeInterval, so we go via Rational | ||
checkBoundedRational r = | ||
case Ledger.boundRational r of | ||
Nothing -> fail ("too much precision for bounded rational: " ++ show r) | ||
Just s -> return s | ||
|
||
deriving newtype instance FromJSON Alonzo.CostModel | ||
|
||
languageFromText :: MonadFail m => Text -> m Alonzo.Language | ||
languageFromText "PlutusV1" = pure Alonzo.PlutusV1 | ||
languageFromText lang = fail $ "Error decoding Language: " ++ show lang |