|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE TypeApplications #-} |
| 4 | + |
| 5 | +module Test.Cardano.Rpc.EraSummary where |
| 6 | + |
| 7 | +import Cardano.Api (EpochNo (..), SlotNo (..), SystemStart (..)) |
| 8 | +import Cardano.Api.Consensus qualified as Consensus |
| 9 | +import Cardano.Rpc.Proto.Api.UtxoRpc.Query qualified as U5c |
| 10 | +import Cardano.Rpc.Server.Internal.UtxoRpc.Type (eraSummariesToProto) |
| 11 | + |
| 12 | +import Cardano.Ledger.BaseTypes (knownNonZeroBounded) |
| 13 | +import Cardano.Slotting.Time (RelativeTime (..)) |
| 14 | +import Ouroboros.Consensus.BlockchainTime.WallClock.Types (slotLengthFromSec) |
| 15 | +import Ouroboros.Consensus.Cardano.Block (CardanoEras) |
| 16 | +import Ouroboros.Consensus.HardFork.History qualified as History |
| 17 | + |
| 18 | +import RIO |
| 19 | + |
| 20 | +import Data.SOP.NonEmpty (NonEmpty (..)) |
| 21 | +import Data.Time.Clock.POSIX (posixSecondsToUTCTime) |
| 22 | + |
| 23 | +import Hedgehog as H |
| 24 | +import Hedgehog.Extras qualified as H |
| 25 | + |
| 26 | +-- | Placeholder era parameters: 'eraSummariesToProto' never reads them, but |
| 27 | +-- an 'History.EraSummary' fixture still needs one. |
| 28 | +dummyEraParams :: History.EraParams |
| 29 | +dummyEraParams = |
| 30 | + History.defaultEraParams |
| 31 | + (Consensus.SecurityParam (knownNonZeroBounded @2160)) |
| 32 | + (slotLengthFromSec 1) |
| 33 | + |
| 34 | +mkBound :: SlotNo -> EpochNo -> RelativeTime -> History.Bound |
| 35 | +mkBound slot epoch time = |
| 36 | + History.Bound |
| 37 | + { History.boundTime = time |
| 38 | + , History.boundSlot = slot |
| 39 | + , History.boundEpoch = epoch |
| 40 | + , History.boundPerasRound = History.NoPerasEnabled |
| 41 | + } |
| 42 | + |
| 43 | +mkEraSummary :: History.Bound -> History.EraEnd -> History.EraSummary |
| 44 | +mkEraSummary start end = |
| 45 | + History.EraSummary |
| 46 | + { History.eraStart = start |
| 47 | + , History.eraEnd = end |
| 48 | + , History.eraParams = dummyEraParams |
| 49 | + } |
| 50 | + |
| 51 | +-- | Two eras: the boundary between them uses a fractional-second |
| 52 | +-- 'RelativeTime' to prove the millisecond conversion is exact (rounded to |
| 53 | +-- the nearest millisecond, never routed through 'Double'). The second era is |
| 54 | +-- last and carries a real 'History.EraEnd' bound, but its end must still |
| 55 | +-- come out unset. |
| 56 | +hprop_era_summary_multi_era :: Property |
| 57 | +hprop_era_summary_multi_era = H.propertyOnce $ do |
| 58 | + let systemStart = SystemStart (posixSecondsToUTCTime 0) |
| 59 | + |
| 60 | + byronStart = mkBound (SlotNo 0) (EpochNo 0) (RelativeTime 0) |
| 61 | + -- 172800.6789s proves the ms conversion is exact fixed-point via the |
| 62 | + -- shared 'utcTimeToMs' (nearest-ms rounding): .6789s -> 679ms. A |
| 63 | + -- Double-based path, or a floor instead of a round, would give 678. |
| 64 | + transition = mkBound (SlotNo 21600) (EpochNo 1) (RelativeTime 172800.6789) |
| 65 | + shelleyEnd = mkBound (SlotNo 43200) (EpochNo 2) (RelativeTime 259200) |
| 66 | + |
| 67 | + byronSummary = mkEraSummary byronStart (History.EraEnd transition) |
| 68 | + shelleySummary = mkEraSummary transition (History.EraEnd shelleyEnd) |
| 69 | + |
| 70 | + summary :: History.Summary (CardanoEras Consensus.StandardCrypto) |
| 71 | + summary = History.Summary (NonEmptyCons byronSummary (NonEmptyOne shelleySummary)) |
| 72 | + |
| 73 | + proto = eraSummariesToProto systemStart summary |
| 74 | + entries = proto ^. U5c.summaries |
| 75 | + |
| 76 | + length entries === 2 |
| 77 | + |
| 78 | + byronEntry <- H.nothingFail $ listToMaybe entries |
| 79 | + shelleyEntry <- H.nothingFail . listToMaybe $ drop 1 entries |
| 80 | + |
| 81 | + byronEntry ^. U5c.name === "byron" |
| 82 | + byronEntry ^. U5c.start . U5c.time === 0 |
| 83 | + byronEntry ^. U5c.start . U5c.slot === 0 |
| 84 | + byronEntry ^. U5c.start . U5c.epoch === 0 |
| 85 | + H.assertWith (byronEntry ^. U5c.maybe'end) isJust |
| 86 | + byronEntry ^. U5c.end . U5c.time === 172800679 |
| 87 | + byronEntry ^. U5c.end . U5c.slot === 21600 |
| 88 | + byronEntry ^. U5c.end . U5c.epoch === 1 |
| 89 | + |
| 90 | + shelleyEntry ^. U5c.name === "shelley" |
| 91 | + shelleyEntry ^. U5c.start . U5c.time === 172800679 |
| 92 | + shelleyEntry ^. U5c.start . U5c.slot === 21600 |
| 93 | + shelleyEntry ^. U5c.start . U5c.epoch === 1 |
| 94 | + -- Last era: end must be unset even though the fixture supplies a real bound. |
| 95 | + H.assertWith (shelleyEntry ^. U5c.maybe'end) isNothing |
| 96 | + |
| 97 | +-- | A single-era summary (only Byron) has exactly one entry, and that entry |
| 98 | +-- has no end, whether or not consensus reports the era as unbounded. |
| 99 | +hprop_era_summary_single_era_no_end :: Property |
| 100 | +hprop_era_summary_single_era_no_end = H.propertyOnce $ do |
| 101 | + let systemStart = SystemStart (posixSecondsToUTCTime 0) |
| 102 | + byronStart = mkBound (SlotNo 0) (EpochNo 0) (RelativeTime 0) |
| 103 | + byronSummary = mkEraSummary byronStart History.EraUnbounded |
| 104 | + |
| 105 | + summary :: History.Summary (CardanoEras Consensus.StandardCrypto) |
| 106 | + summary = History.Summary (NonEmptyOne byronSummary) |
| 107 | + |
| 108 | + proto = eraSummariesToProto systemStart summary |
| 109 | + entries = proto ^. U5c.summaries |
| 110 | + |
| 111 | + length entries === 1 |
| 112 | + |
| 113 | + byronEntry <- H.nothingFail $ listToMaybe entries |
| 114 | + byronEntry ^. U5c.name === "byron" |
| 115 | + H.assertWith (byronEntry ^. U5c.maybe'end) isNothing |
0 commit comments