Skip to content

Commit

Permalink
Genesis parameters yielding positive reserves
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Mar 5, 2024
1 parent ff013c5 commit a7f5ace
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ library internal
, cborg
, containers
, contra-tracer
, data-default-class
, deepseq
, directory
, either
Expand Down
22 changes: 17 additions & 5 deletions cardano-api/internal/Cardano/Api/Genesis.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}

module Cardano.Api.Genesis
( ShelleyGenesis(..)
, shelleyGenesisDefaults
, conwayGenesisDefaults

-- ** Configuration
, ByronGenesisConfig
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

15 changes: 14 additions & 1 deletion cardano-api/internal/Cardano/Api/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ module Cardano.Api (

ResolvablePointers(..),

unsafeBoundedRational,
-- ** Supporting modules
module Cardano.Api.Monad.Error,
module Cardano.Api.Pretty
Expand Down
1 change: 1 addition & 0 deletions cardano-api/src/Cardano/Api/Shelley.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Cardano.Api.Shelley
-- * Genesis
ShelleyGenesis(..),
shelleyGenesisDefaults,
conwayGenesisDefaults,

-- * Cryptographic key interface
-- $keys
Expand Down

0 comments on commit a7f5ace

Please sign in to comment.