From a7f5ace0b667b836d1857fae5ff6171925885117 Mon Sep 17 00:00:00 2001 From: Mateusz Galazyn Date: Fri, 12 Jan 2024 21:20:00 +0100 Subject: [PATCH] Genesis parameters yielding positive reserves --- cardano-api/cardano-api.cabal | 1 + cardano-api/internal/Cardano/Api/Genesis.hs | 22 ++++++++++++++++----- cardano-api/internal/Cardano/Api/Utils.hs | 15 +++++++++++++- cardano-api/src/Cardano/Api.hs | 1 + cardano-api/src/Cardano/Api/Shelley.hs | 1 + 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/cardano-api/cardano-api.cabal b/cardano-api/cardano-api.cabal index 7150096d4a..ccc16e6fd1 100644 --- a/cardano-api/cardano-api.cabal +++ b/cardano-api/cardano-api.cabal @@ -178,6 +178,7 @@ library internal , cborg , containers , contra-tracer + , data-default-class , deepseq , directory , either diff --git a/cardano-api/internal/Cardano/Api/Genesis.hs b/cardano-api/internal/Cardano/Api/Genesis.hs index 58e8b4e6c9..28dba71c9e 100644 --- a/cardano-api/internal/Cardano/Api/Genesis.hs +++ b/cardano-api/internal/Cardano/Api/Genesis.hs @@ -1,10 +1,10 @@ {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE GeneralisedNewtypeDeriving #-} -{-# LANGUAGE OverloadedStrings #-} module Cardano.Api.Genesis ( ShelleyGenesis(..) , shelleyGenesisDefaults + , conwayGenesisDefaults -- ** Configuration , ByronGenesisConfig @@ -26,6 +26,7 @@ module Cardano.Api.Genesis ) where import Cardano.Api.IO +import Cardano.Api.Utils (unsafeBoundedRational) import qualified Cardano.Chain.Genesis import qualified Cardano.Crypto.Hash.Blake2b @@ -42,11 +43,14 @@ import qualified Cardano.Ledger.Shelley.Genesis as Ledger import qualified Ouroboros.Consensus.Shelley.Eras as Shelley import Data.ByteString (ByteString) +import qualified Data.Default.Class as DefaultClass import qualified Data.ListMap as ListMap import qualified Data.Map.Strict as Map -import Data.Maybe (fromMaybe) +import Data.Ratio import Data.Text (Text) import qualified Data.Time as Time +import Data.Typeable +import GHC.Stack (HasCallStack) import Lens.Micro data ShelleyConfig = ShelleyConfig @@ -102,9 +106,7 @@ shelleyGenesisDefaults = -- consensus protocol parameters , sgSlotLength = 1.0 :: NominalDiffTimeMicro -- 1s slots - , sgActiveSlotsCoeff = fromMaybe - (error "shelleyGenesisDefaults: impossible") - (Ledger.boundRational (1/20)) -- 20s block times on average + , sgActiveSlotsCoeff = unsafeBR (1 % 20) -- f ; 1/f = 20s block times on average , sgSecurityParam = k , sgEpochLength = Ledger.EpochSize (k * 10 * 20) -- 10k/f , sgSlotsPerKESPeriod = 60 * 60 * 36 -- 1.5 days with 1s slots @@ -121,6 +123,9 @@ shelleyGenesisDefaults = & ppEMaxL .~ EpochInterval 18 & ppMinFeeAL .~ Coin 1 -- The linear factor for the minimum fee calculation & ppMinFeeBL .~ Coin 0 -- The constant factor for the minimum fee calculation + -- pot = tx_fees + ρ * remaining_reserves + & ppRhoL .~ unsafeBR (1 % 10) -- How much of reserves goes into pot + & ppTauL .~ unsafeBR (1 % 10) -- τ * remaining_reserves is sent to treasury every epoch -- genesis keys and initial funds , sgGenDelegs = Map.empty @@ -131,3 +136,10 @@ shelleyGenesisDefaults = where k = 2160 zeroTime = Time.UTCTime (Time.fromGregorian 1970 1 1) 0 -- tradition + unsafeBR :: (HasCallStack, Typeable r, BoundedRational r) => Rational -> r + unsafeBR = unsafeBoundedRational + + +conwayGenesisDefaults :: ConwayGenesis StandardCrypto +conwayGenesisDefaults = DefaultClass.def + diff --git a/cardano-api/internal/Cardano/Api/Utils.hs b/cardano-api/internal/Cardano/Api/Utils.hs index c6124ecba4..d1fb5a871b 100644 --- a/cardano-api/internal/Cardano/Api/Utils.hs +++ b/cardano-api/internal/Cardano/Api/Utils.hs @@ -28,8 +28,10 @@ module Cardano.Api.Utils -- ** CLI option parsing , bounded + , unsafeBoundedRational ) where +import Cardano.Ledger.BaseTypes import Cardano.Ledger.Shelley () import Control.Exception (bracket) @@ -38,10 +40,12 @@ import qualified Data.Aeson.Types as Aeson import qualified Data.ByteString as BS import qualified Data.ByteString.Builder as Builder import qualified Data.ByteString.Lazy as LBS -import Data.Maybe.Strict +import Data.Maybe import Data.Text (Text) import qualified Data.Text as Text +import Data.Typeable import GHC.IO.Handle.FD (openFileBlocking) +import GHC.Stack import Options.Applicative (ReadM) import qualified Options.Applicative as Opt import Options.Applicative.Builder (eitherReader) @@ -129,3 +133,12 @@ modifyWith :: () -> (a -> a) modifyWith = id + + +-- | Convert Rational to a bounded rational. Throw an exception when the rational is out of bounds. +unsafeBoundedRational :: forall r. (HasCallStack, Typeable r, BoundedRational r) + => Rational + -> r +unsafeBoundedRational x = fromMaybe (error errMessage) $ boundRational x + where + errMessage = show (typeRep (Proxy @r)) <> " is out of bounds: " <> show x diff --git a/cardano-api/src/Cardano/Api.hs b/cardano-api/src/Cardano/Api.hs index cec503be7e..8472918386 100644 --- a/cardano-api/src/Cardano/Api.hs +++ b/cardano-api/src/Cardano/Api.hs @@ -988,6 +988,7 @@ module Cardano.Api ( ResolvablePointers(..), + unsafeBoundedRational, -- ** Supporting modules module Cardano.Api.Monad.Error, module Cardano.Api.Pretty diff --git a/cardano-api/src/Cardano/Api/Shelley.hs b/cardano-api/src/Cardano/Api/Shelley.hs index 78c5f238b8..7c9ec1f57d 100644 --- a/cardano-api/src/Cardano/Api/Shelley.hs +++ b/cardano-api/src/Cardano/Api/Shelley.hs @@ -9,6 +9,7 @@ module Cardano.Api.Shelley -- * Genesis ShelleyGenesis(..), shelleyGenesisDefaults, + conwayGenesisDefaults, -- * Cryptographic key interface -- $keys