Skip to content

Commit

Permalink
Genesis rational simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jul 30, 2021
1 parent 144a8d6 commit 6c978ec
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 38 deletions.
1 change: 1 addition & 0 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ library
Cardano.Api.HasTypeProxy
Cardano.Api.IPC
Cardano.Api.IPC.Monad
Cardano.Api.Json
Cardano.Api.Key
Cardano.Api.KeysByron
Cardano.Api.KeysPraos
Expand Down
21 changes: 21 additions & 0 deletions cardano-api/src/Cardano/Api/Json.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Cardano.Api.Json
( toRationalJSON
) where

import Data.Aeson
import Data.Either
import Data.Maybe
import Data.Scientific
import GHC.Real

-- Rationals and JSON are an awkward mix. We cannot convert rationals
-- like @1/3@ to JSON numbers. But _most_ of the numbers we want to use
-- in practice have simple decimal representations. Our solution here is
-- to use simple decimal representations where we can and representation
-- in a @{"numerator": 1, "denominator": 3}@ style otherwise.
--
toRationalJSON :: Rational -> Value
toRationalJSON r =
case fromRationalRepetendLimited 20 r of
Right (s, Nothing) -> toJSON s
_ -> toJSON r
11 changes: 6 additions & 5 deletions cardano-api/src/Cardano/Api/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ module Cardano.Api.Orphans () where

import Prelude

import Data.Aeson (FromJSON (..), ToJSON (..), object, (.=), (.!=), (.:), (.:?))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (FromJSONKey (..), ToJSONKey (..), toJSONKeyText)
import qualified Data.ByteString.Base16 as B16
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.Map.Strict as Map
import Data.Aeson (FromJSON (..), ToJSON (..), object, (.=), (.!=), (.:), (.:?))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (FromJSONKey (..), ToJSONKey (..), toJSONKeyText)

import Control.Applicative
import Control.Iterate.SetAlgebra (BiMap (..), Bimap)

import Cardano.Api.Json
import qualified Cardano.Ledger.BaseTypes as Ledger
import Cardano.Ledger.BaseTypes (StrictMaybe (..), strictMaybeToMaybe)
import Cardano.Ledger.Crypto (StandardCrypto)
Expand Down Expand Up @@ -302,8 +303,8 @@ deriving instance FromJSON Alonzo.ExUnits
instance ToJSON Alonzo.Prices where
toJSON Alonzo.Prices { Alonzo.prSteps, Alonzo.prMem } =
-- We cannot round-trip via NonNegativeInterval, so we go via Rational
object [ "prSteps" .= Ledger.unboundRational prSteps
, "prMem" .= Ledger.unboundRational prMem
object [ "prSteps" .= toRationalJSON (Ledger.unboundRational prSteps)
, "prMem" .= toRationalJSON (Ledger.unboundRational prMem)
]

instance FromJSON Alonzo.Prices where
Expand Down
44 changes: 11 additions & 33 deletions cardano-api/src/Cardano/Api/ProtocolParameters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

-- | The various Cardano protocol parameters, including:
Expand Down Expand Up @@ -58,26 +57,20 @@ module Cardano.Api.ProtocolParameters (

import Prelude

import Control.Applicative
import Control.Monad
import Data.Aeson (FromJSON (..), ToJSON (..), object, withObject,
(.!=), (.:), (.:?), (.=))
import Data.Bifunctor (bimap)
import Data.ByteString (ByteString)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.String (IsString)
import qualified Data.Scientific as Scientific
import Data.Scientific (Scientific)
import Data.Text (Text)
import Data.Maybe (fromMaybe)
import GHC.Generics
import Numeric.Natural

import Control.Monad

import Data.Aeson (FromJSON (..), ToJSON (..), object, withObject,
(.!=), (.:), (.:?), (.=))
import Data.Aeson.Types (Parser)
import qualified Data.Aeson as Aeson
import Data.Bifunctor (bimap)

import Cardano.Api.Json
import qualified Cardano.Binary as CBOR
import qualified Cardano.Crypto.Hash.Class as Crypto
import Cardano.Slotting.Slot (EpochNo)
Expand Down Expand Up @@ -285,16 +278,13 @@ data ProtocolParameters =
}
deriving (Eq, Generic, Show)

parseRationalJSON :: Aeson.Value -> Parser Rational
parseRationalJSON v = parseJSON v <|> (toRational @Scientific <$> parseJSON v)

instance FromJSON ProtocolParameters where
parseJSON =
withObject "ProtocolParameters" $ \o -> do
v <- o .: "protocolVersion"
ProtocolParameters
<$> ((,) <$> v .: "major" <*> v .: "minor")
<*> (o .: "decentralization" >>= parseRationalJSON)
<*> o .: "decentralization"
<*> o .: "extraPraosEntropy"
<*> o .: "maxBlockHeaderSize"
<*> o .: "maxBlockBodySize"
Expand All @@ -307,9 +297,9 @@ instance FromJSON ProtocolParameters where
<*> o .: "minPoolCost"
<*> o .: "poolRetireMaxEpoch"
<*> o .: "stakePoolTargetNum"
<*> (o .: "poolPledgeInfluence" >>= parseRationalJSON)
<*> (o .: "monetaryExpansion" >>= parseRationalJSON)
<*> (o .: "treasuryCut" >>= parseRationalJSON)
<*> o .: "poolPledgeInfluence"
<*> o .: "monetaryExpansion"
<*> o .: "treasuryCut"
<*> o .:? "utxoCostPerWord"
<*> o .:? "costModels" .!= Map.empty
<*> o .:? "executionUnitPrices"
Expand Down Expand Up @@ -351,18 +341,6 @@ instance ToJSON ProtocolParameters where
, "maxCollateralInputs" .= protocolParamMaxCollateralInputs
]

-- Rationals and JSON are an awkward mix. We cannot convert rationals
-- like @1/3@ to JSON numbers. But _most_ of the numbers we want to use
-- in practice have simple decimal representations. Our solution here is
-- to use simple decimal representations where we can and representation
-- in a @{"numerator": 1, "denominator": 3}@ style otherwise.
--
toRationalJSON :: Rational -> Aeson.Value
toRationalJSON r =
case Scientific.fromRationalRepetend (Just 5) r of
Right (s, Nothing) -> toJSON s
_ -> toJSON r


-- ----------------------------------------------------------------------------
-- Updates to the protocol paramaters
Expand Down Expand Up @@ -730,8 +708,8 @@ instance FromJSON ExecutionUnitPrices where
parseJSON =
withObject "ExecutionUnitPrices" $ \o ->
ExecutionUnitPrices
<$> (o .: "priceSteps" >>= parseRationalJSON)
<*> (o .: "priceMemory" >>= parseRationalJSON)
<$> o .: "priceSteps"
<*> o .: "priceMemory"


toAlonzoPrices :: ExecutionUnitPrices -> Maybe Alonzo.Prices
Expand Down

0 comments on commit 6c978ec

Please sign in to comment.