Skip to content

Commit c97a657

Browse files
committed
Propogate NonZero across all packages with some errors
1 parent 7044044 commit c97a657

File tree

42 files changed

+162
-109
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+162
-109
lines changed

ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ library unstable-byron-testlib
227227
cardano-crypto-class,
228228
cardano-crypto-test,
229229
cardano-crypto-wrapper,
230+
cardano-ledger-core,
230231
cardano-ledger-binary:{cardano-ledger-binary, testlib},
231232
cardano-ledger-byron,
232233
cardano-ledger-byron-test,
@@ -260,6 +261,7 @@ test-suite byron-test
260261
bytestring,
261262
cardano-crypto-class,
262263
cardano-crypto-wrapper,
264+
cardano-ledger-core,
263265
cardano-ledger-binary,
264266
cardano-ledger-byron,
265267
cardano-ledger-byron-test,

ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Conversions.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import qualified Cardano.Chain.Common as CC
1919
import qualified Cardano.Chain.Genesis as Genesis
2020
import qualified Cardano.Chain.Slotting as CC
2121
import qualified Cardano.Chain.Update as CC
22+
import Cardano.Ledger.BaseTypes (nonZeroOr, unNonZero)
2223
import Data.Coerce
2324
import qualified Data.Set as Set
2425
import Numeric.Natural (Natural)
@@ -39,7 +40,7 @@ fromByronBlockNo :: CC.ChainDifficulty -> BlockNo
3940
fromByronBlockNo = coerce
4041

4142
fromByronBlockCount :: CC.BlockCount -> SecurityParam
42-
fromByronBlockCount (CC.BlockCount k) = SecurityParam k
43+
fromByronBlockCount (CC.BlockCount k) = SecurityParam $ nonZeroOr k $ error "Zero found while trying to construct a NonZero"
4344

4445
fromByronEpochSlots :: CC.EpochSlots -> EpochSize
4546
fromByronEpochSlots (CC.EpochSlots n) = EpochSize n
@@ -56,7 +57,7 @@ toByronSlotNo :: SlotNo -> CC.SlotNumber
5657
toByronSlotNo = coerce
5758

5859
toByronBlockCount :: SecurityParam -> CC.BlockCount
59-
toByronBlockCount (SecurityParam k) = CC.BlockCount k
60+
toByronBlockCount (SecurityParam k) = CC.BlockCount $ unNonZero k
6061

6162
toByronSlotLength :: SlotLength -> Natural
6263
toByronSlotLength = (fromIntegral :: Integer -> Natural)

ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Ledger.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import qualified Cardano.Chain.Update.Validation.Endorsement as UPE
5050
import qualified Cardano.Chain.Update.Validation.Interface as UPI
5151
import qualified Cardano.Chain.UTxO as CC
5252
import qualified Cardano.Chain.ValidationMode as CC
53+
import Cardano.Ledger.BaseTypes (unNonZero)
5354
import Cardano.Ledger.Binary (fromByronCBOR, toByronCBOR)
5455
import Cardano.Ledger.Binary.Plain (encodeListLen, enforceSize)
5556
import Codec.CBOR.Decoding (Decoder)
@@ -263,7 +264,7 @@ instance LedgerSupportsProtocol ByronBlock where
263264
, outsideForecastFor = for
264265
}
265266
where
266-
SecurityParam k = genesisSecurityParam cfg
267+
k = unNonZero $ maxRollbacks $ genesisSecurityParam cfg
267268
lastSlot = fromByronSlotNo $ CC.cvsLastSlot st
268269
at = NotOrigin lastSlot
269270

@@ -282,7 +283,7 @@ byronEraParams genesis = HardFork.EraParams {
282283
, eraGenesisWin = GenesisWindow (2 * k)
283284
}
284285
where
285-
SecurityParam k = genesisSecurityParam genesis
286+
k = unNonZero $ maxRollbacks $ genesisSecurityParam genesis
286287

287288
-- | Separate variant of 'byronEraParams' to be used for a Byron-only chain.
288289
byronEraParamsNeverHardForks :: Gen.Config -> HardFork.EraParams

ouroboros-consensus-cardano/src/ouroboros-consensus-cardano/Ouroboros/Consensus/Cardano/Node.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ protocolInfoCardano paramsCardano
561561
praosParams = PraosParams
562562
{ praosSlotsPerKESPeriod = SL.sgSlotsPerKESPeriod genesisShelley,
563563
praosLeaderF = SL.mkActiveSlotCoeff $ SL.sgActiveSlotsCoeff genesisShelley,
564-
praosSecurityParam = SecurityParam $ SL.unNonZero $ SL.sgSecurityParam genesisShelley,
564+
praosSecurityParam = SecurityParam $ SL.sgSecurityParam genesisShelley,
565565
praosMaxKESEvo = SL.sgMaxKESEvolutions genesisShelley,
566566
praosMaxMajorPV = maxMajorProtVer,
567567
praosRandomnessStabilisationWindow =
@@ -592,7 +592,7 @@ protocolInfoCardano paramsCardano
592592
(toTriggerHardFork triggerHardForkAllegra)
593593

594594
kShelley :: SecurityParam
595-
kShelley = SecurityParam $ SL.unNonZero $ sgSecurityParam genesisShelley
595+
kShelley = SecurityParam $ sgSecurityParam genesisShelley
596596

597597
-- Allegra
598598

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/HFEras.hs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ module Ouroboros.Consensus.Shelley.HFEras (
1818
, StandardShelleyBlock
1919
) where
2020

21-
import Cardano.Crypto.DSIGN (Signable)
22-
import Cardano.Crypto.Hash (Hash)
23-
import Cardano.Ledger.Hashes (EraIndependentTxBody, HASH)
24-
import Cardano.Ledger.Keys (DSIGN)
2521
import Ouroboros.Consensus.Protocol.Praos (Praos)
2622
import qualified Ouroboros.Consensus.Protocol.Praos as Praos
2723
import Ouroboros.Consensus.Protocol.TPraos (StandardCrypto, TPraos)

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Node/Common.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Ouroboros.Consensus.Shelley.Node.Common (
2121
) where
2222

2323
import Cardano.Crypto.KES (UnsoundPureSignKeyKES)
24+
import Cardano.Ledger.BaseTypes (unNonZero)
2425
import qualified Cardano.Ledger.Keys as SL
2526
import qualified Cardano.Ledger.Shelley.API as SL
2627
import Cardano.Ledger.Slot
@@ -106,6 +107,7 @@ instance ShelleyCompatible proto era => NodeInitStorage (ShelleyBlock proto era)
106107
simpleChunkInfo
107108
. EpochSize
108109
. (* 10)
110+
. unNonZero
109111
. maxRollbacks
110112
. shelleyStorageConfigSecurityParam
111113

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Node/Praos.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
{-# LANGUAGE ScopedTypeVariables #-}
99
{-# LANGUAGE TypeApplications #-}
1010
{-# LANGUAGE TypeFamilies #-}
11-
{-# LANGUAGE TypeOperators #-}
1211

1312
{-# OPTIONS_GHC -Wno-orphans #-}
1413

@@ -27,8 +26,8 @@ import Ouroboros.Consensus.Block
2726
import Ouroboros.Consensus.Config (configConsensus)
2827
import qualified Ouroboros.Consensus.Ledger.SupportsMempool as Mempool
2928
import qualified Ouroboros.Consensus.Protocol.Ledger.HotKey as HotKey
30-
import Ouroboros.Consensus.Protocol.Praos (Praos, PraosCrypto,
31-
PraosParams (..), praosCheckCanForge)
29+
import Ouroboros.Consensus.Protocol.Praos (Praos, PraosParams (..),
30+
praosCheckCanForge)
3231
import Ouroboros.Consensus.Protocol.Praos.Common
3332
(PraosCanBeLeader (praosCanBeLeaderOpCert))
3433
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock,

ouroboros-consensus-cardano/src/unstable-byron-testlib/Test/Consensus/Byron/Examples.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE DisambiguateRecordFields #-}
23
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE TypeApplications #-}
35

46
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
57

@@ -28,6 +30,7 @@ import qualified Cardano.Chain.Byron.API as CC
2830
import qualified Cardano.Chain.Common as CC
2931
import qualified Cardano.Chain.Update.Validation.Interface as CC.UPI
3032
import qualified Cardano.Chain.UTxO as CC
33+
import Cardano.Ledger.BaseTypes (knownNonZeroBounded)
3134
import Control.Monad.Except (runExcept)
3235
import qualified Data.Map.Strict as Map
3336
import Ouroboros.Consensus.Block
@@ -62,7 +65,7 @@ import Test.Util.Serialisation.SomeResult (SomeResult (..))
6265
-- 'S.WindowSize', because 'decodeByronChainDepState' only takes the
6366
-- 'SecurityParam' and uses it as the basis for the 'S.WindowSize'.
6467
secParam :: SecurityParam
65-
secParam = SecurityParam 2
68+
secParam = SecurityParam $ knownNonZeroBounded @2
6669

6770
windowSize :: S.WindowSize
6871
windowSize = S.WindowSize 2

ouroboros-consensus-cardano/src/unstable-byron-testlib/Test/Consensus/Byron/Generators.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE FlexibleInstances #-}
23
{-# LANGUAGE GADTs #-}
4+
{-# LANGUAGE TypeApplications #-}
35

46
{-# OPTIONS_GHC -Wno-orphans #-}
57

@@ -28,6 +30,7 @@ import qualified Cardano.Chain.Update.Validation.Registration as CC.Reg
2830
import qualified Cardano.Chain.UTxO as CC.UTxO
2931
import Cardano.Crypto (ProtocolMagicId (..))
3032
import Cardano.Crypto.Hashing (Hash)
33+
import Cardano.Ledger.BaseTypes (knownNonZeroBounded)
3134
import Cardano.Ledger.Binary (decCBOR, encCBOR)
3235
import Control.Monad (replicateM)
3336
import Data.Coerce (coerce)
@@ -63,7 +66,7 @@ import Test.Util.Serialisation.SomeResult (SomeResult (..))
6366

6467
-- | Matches that from the 'CC.dummyConfig'
6568
k :: SecurityParam
66-
k = SecurityParam 10
69+
k = SecurityParam $ knownNonZeroBounded @10
6770

6871
-- | Matches that from the 'CC.dummyConfig'
6972
epochSlots :: EpochSlots

ouroboros-consensus-cardano/src/unstable-byron-testlib/Test/ThreadNet/Infra/Byron/Genesis.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import qualified Cardano.Chain.Common as Common
1010
import qualified Cardano.Chain.Genesis as Genesis
1111
import qualified Cardano.Chain.Update as Update
1212
import qualified Cardano.Crypto as Crypto
13+
import Cardano.Ledger.BaseTypes (unNonZero)
1314
import Control.Monad.Except (runExceptT)
1415
import Ouroboros.Consensus.BlockchainTime
1516
import Ouroboros.Consensus.Byron.Ledger.Conversions
@@ -37,7 +38,7 @@ byronPBftParams paramK numCoreNodes = PBftParams
3738
n = fromIntegral x where NumCoreNodes x = numCoreNodes
3839

3940
k :: Num a => a
40-
k = fromIntegral x where SecurityParam x = paramK
41+
k = fromIntegral x where x = unNonZero $ maxRollbacks paramK
4142

4243
-- Instead of using 'Dummy.dummyConfig', which hard codes the number of rich
4344
-- men (= CoreNodes for us) to 4, we generate a dummy config with the given
@@ -62,7 +63,7 @@ generateGenesisConfig slotLen params =
6263
-- The nodes are the richmen
6364
{ Genesis.tboRichmen = fromIntegral numCoreNodes }
6465
}
65-
, Genesis.gsK = Common.BlockCount $ maxRollbacks pbftSecurityParam
66+
, Genesis.gsK = Common.BlockCount $ unNonZero $ maxRollbacks pbftSecurityParam
6667
, Genesis.gsProtocolParameters = gsProtocolParameters
6768
{ Update.ppSlotDuration = toByronSlotLength slotLen
6869
}

ouroboros-consensus-cardano/src/unstable-byron-testlib/Test/ThreadNet/Infra/Byron/TrackUpdates.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import qualified Cardano.Chain.Update.Validation.Registration as Registration
2424
import Cardano.Chain.Update.Vote (AVote)
2525
import qualified Cardano.Chain.Update.Vote as Vote
2626
import qualified Cardano.Crypto as Crypto
27+
import Cardano.Ledger.BaseTypes (unNonZero)
2728
import Cardano.Ledger.Binary (ByteSpan, DecCBOR (..), EncCBOR (..))
2829
import Control.Exception (assert)
2930
import Control.Monad (guard)
@@ -106,7 +107,7 @@ mkUpdateLabels params numSlots genesisConfig nodeJoinPlan topology result
106107
-- a block forged in slot @s@ becomes immutable/stable in slot @s + twoK@
107108
-- according to the Byron Chain Density invariant
108109
twoK :: SlotNo
109-
twoK = SlotNo $ 2 * maxRollbacks pbftSecurityParam
110+
twoK = SlotNo $ 2 * unNonZero (maxRollbacks pbftSecurityParam)
110111

111112
-- the number of slots in an epoch
112113
epochSlots :: SlotNo

ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/ThreadNet/Infra/TwoEras.hs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module Test.ThreadNet.Infra.TwoEras (
3535
import qualified Cardano.Chain.Common as CC.Common
3636
import Cardano.Chain.ProtocolConstants (kEpochSlots)
3737
import Cardano.Chain.Slotting (unEpochSlots)
38+
import Cardano.Ledger.BaseTypes (unNonZero)
3839
import qualified Cardano.Ledger.BaseTypes as SL
3940
import qualified Cardano.Protocol.TPraos.Rules.Overlay as SL
4041
import Cardano.Slotting.EpochInfo
@@ -91,7 +92,7 @@ genTestConfig k (EpochSize epochSize1, EpochSize epochSize2) = do
9192
initSeed <- arbitrary
9293

9394
numSlots <- do
94-
let wiggle = min epochSize1 (2 * maxRollbacks k)
95+
let wiggle = min epochSize1 (2 * unNonZero (maxRollbacks k))
9596

9697
approachSecondEra =
9798
choose (0, wiggle) <&> \t -> epochSize1 + t - wiggle
@@ -140,10 +141,12 @@ genTestConfig k (EpochSize epochSize1, EpochSize epochSize2) = do
140141

141142
-- | Generate 'setupPartition'
142143
genPartition :: NumCoreNodes -> NumSlots -> SecurityParam -> Gen Partition
143-
genPartition (NumCoreNodes n) (NumSlots t) (SecurityParam k) = do
144+
genPartition (NumCoreNodes n) (NumSlots t) (SecurityParam k') = do
144145
let ultimateSlot :: Word64
145146
ultimateSlot = assert (t > 0) $ t - 1
146147

148+
k = unNonZero k'
149+
147150
crop :: Word64 -> Word64
148151
crop s = min ultimateSlot s
149152

@@ -227,7 +230,7 @@ genPartition (NumCoreNodes n) (NumSlots t) (SecurityParam k) = do
227230
, (1, Just $ 4 * k + 1)
228231
, (1, Just $ 4 * k + 1 + quorum)
229232
, (20, assert (numFirstEraEpochs == (1 :: Int)) $
230-
Just $ byronEpochSize (SecurityParam k))
233+
Just $ byronEpochSize (SecurityParam k'))
231234
]
232235

233236
-- Position the partition so that it at least abuts the focus slot.
@@ -448,7 +451,7 @@ mkMessageDelay part = CalcMessageDelay $
448451

449452
byronEpochSize :: SecurityParam -> Word64
450453
byronEpochSize (SecurityParam k) =
451-
unEpochSlots $ kEpochSlots $ CC.Common.BlockCount k
454+
unEpochSlots $ kEpochSlots $ CC.Common.BlockCount $ unNonZero k
452455

453456
shelleyEpochSize :: SecurityParam -> Word64
454457
shelleyEpochSize k = unEpochSize $ Shelley.mkEpochSize k activeSlotCoeff
@@ -463,15 +466,15 @@ isFirstEraBlock = \case
463466
-- PREREQUISITE: The number must not be greater than @k@.
464467
diffK :: SecurityParam -> Word64 -> String
465468
diffK (SecurityParam k) v =
466-
assert (k >= v) $
467-
"k - " <> show (k - v)
469+
assert (unNonZero k >= v) $
470+
"k - " <> show (unNonZero k - v)
468471

469472
-- | Render a number as the nearest tenths of @k@
470473
approxFracK :: SecurityParam -> Word64 -> String
471474
approxFracK (SecurityParam k) v =
472475
"k * " <> show (fromIntegral tenths / 10 :: Double)
473476
where
474-
ratio = toRational v / toRational k
477+
ratio = toRational v / toRational (unNonZero k)
475478
tenths = round (ratio * 10) :: Int
476479

477480
-- | <https://en.wikipedia.org/wiki/Monus>

ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/ThreadNet/Infra/Shelley.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ import Cardano.Crypto.VRF (SignKeyVRF, deriveVerKeyVRF, genKeyVRF,
4747
seedSizeVRF)
4848
import qualified Cardano.Ledger.Allegra.Scripts as SL
4949
import Cardano.Ledger.Alonzo (AlonzoEra)
50-
import Cardano.Ledger.BaseTypes (boundRational)
51-
import Cardano.Ledger.BaseTypes.NonZero (nonZeroOr)
50+
import Cardano.Ledger.BaseTypes (boundRational, unNonZero)
5251
import Cardano.Ledger.Hashes (EraIndependentTxBody,
5352
HashAnnotated (..), SafeHash, hashAnnotated)
5453
import qualified Cardano.Ledger.Keys as LK
@@ -272,7 +271,7 @@ mkEpochSize (SecurityParam k) f =
272271
n = numerator f
273272
d = denominator f
274273

275-
(q, r) = quotRem (10 * k * fromInteger d) (fromInteger n)
274+
(q, r) = quotRem (10 * unNonZero k * fromInteger d) (fromInteger n)
276275

277276
-- | Note: a KES algorithm supports a particular max number of KES evolutions,
278277
-- but we can configure a potentially lower maximum for the ledger, that's why
@@ -297,7 +296,7 @@ mkGenesisConfig pVer k f d maxLovelaceSupply slotLength kesCfg coreNodes =
297296
, sgNetworkMagic = 0
298297
, sgNetworkId = networkId
299298
, sgActiveSlotsCoeff = unsafeBoundRational f
300-
, sgSecurityParam = nonZeroOr (maxRollbacks k) $ error "The security parameter cannot be zero."
299+
, sgSecurityParam = maxRollbacks k
301300
, sgEpochLength = mkEpochSize k f
302301
, sgSlotsPerKESPeriod = slotsPerEvolution kesCfg
303302
, sgMaxKESEvolutions = maxEvolutions kesCfg

0 commit comments

Comments
 (0)