Skip to content

Commit 9129bb0

Browse files
authored
More precise type for Index (#1435)
Taken from [here](https://github.com/IntersectMBO/ouroboros-consensus/pull/1399/files#r1961871210) we also make the payload strict by using the strict `NS` (#1331).
2 parents af0e6ae + 8376343 commit 9129bb0

12 files changed

Lines changed: 71 additions & 51 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- empty -->

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ import Data.Functor.These (These1 (..))
6969
import qualified Data.Map.Strict as Map
7070
import Data.SOP.BasicFunctors
7171
import Data.SOP.Counting
72-
import Data.SOP.Index (Index (..))
72+
import Data.SOP.Index
7373
import Data.SOP.OptNP (NonEmptyOptNP, OptNP (OptSkip))
7474
import qualified Data.SOP.OptNP as OptNP
7575
import Data.SOP.Strict
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Non-Breaking
2+
3+
- Define some functions in terms of SOP combinators leveraging the recent
4+
simplification of `Index` in `sop-extras` package.

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Abstract/SingleEraBlock.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ eraIndexEmpty (EraIndex ns) = case ns of {}
159159
eraIndexFromNS :: SListI xs => NS f xs -> EraIndex xs
160160
eraIndexFromNS = EraIndex . hmap (const (K ()))
161161

162-
eraIndexFromIndex :: Index xs blk -> EraIndex xs
162+
eraIndexFromIndex :: SListI xs => Index xs blk -> EraIndex xs
163163
eraIndexFromIndex index = EraIndex $ injectNS index (K ())
164164

165165
eraIndexZero :: EraIndex (x ': xs)

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Block.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ distribAnnTip AnnTip{..} =
205205
distrib (WrapTipInfo info) =
206206
AnnTip annTipSlotNo annTipBlockNo info
207207

208-
undistribAnnTip :: SListI xs => NS AnnTip xs -> AnnTip (HardForkBlock xs)
208+
undistribAnnTip :: forall xs. SListI xs => NS AnnTip xs -> AnnTip (HardForkBlock xs)
209209
undistribAnnTip = hcollapse . himap undistrib
210210
where
211211
undistrib :: Index xs blk

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Embed/Nary.hs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ module Ouroboros.Consensus.HardFork.Combinator.Embed.Nary (
2525
import Data.Bifunctor (first)
2626
import Data.Coerce (Coercible, coerce)
2727
import Data.SOP.BasicFunctors
28+
import Data.SOP.Constraint
2829
import Data.SOP.Counting (Exactly (..))
2930
import Data.SOP.Dict (Dict (..))
3031
import Data.SOP.Index
3132
import qualified Data.SOP.InPairs as InPairs
3233
import Data.SOP.Strict
34+
import qualified Data.SOP.Telescope as Telescope
3335
import Ouroboros.Consensus.Block
3436
import Ouroboros.Consensus.Config
3537
import Ouroboros.Consensus.HardFork.Combinator
@@ -75,11 +77,9 @@ newtype InjectionIndex xs x =
7577

7678
-- | Many instances of 'Inject' do not need the 'History.Bound's, eg those that
7779
-- do not construct 'HardForkState's
78-
forgetInjectionIndex :: InjectionIndex xs x -> Index xs x
79-
forgetInjectionIndex (InjectionIndex tele) = case tele of
80-
TZ (State.Current _k Refl) -> IZ
81-
TS _k tele' ->
82-
IS (forgetInjectionIndex (InjectionIndex tele'))
80+
forgetInjectionIndex :: SListI xs => InjectionIndex xs x -> Index xs x
81+
forgetInjectionIndex (InjectionIndex tele) =
82+
Index $ hmap State.currentState $ Telescope.tip tele
8383

8484
-- | Build an 'InjectionIndex' from oracular 'History.Bound's and an 'Index'
8585
--
@@ -89,18 +89,17 @@ forgetInjectionIndex (InjectionIndex tele) = case tele of
8989
-- INVARIANT: the result is completely independent of the 'history.Bound's for
9090
-- eras /after/ the given 'Index'.
9191
oracularInjectionIndex ::
92-
Exactly xs History.Bound
92+
SListI xs
93+
=> Exactly xs History.Bound
9394
-> Index xs x
9495
-> InjectionIndex xs x
95-
oracularInjectionIndex (Exactly np) idx = case (idx, np) of
96-
(IZ , K start :* _ ) ->
97-
InjectionIndex
98-
$ TZ State.Current { currentStart = start, currentState = Refl }
99-
(IS idx', kstart :* kstarts) ->
100-
let InjectionIndex iidx =
101-
oracularInjectionIndex (Exactly kstarts) idx'
102-
in
103-
InjectionIndex (TS kstart iidx)
96+
oracularInjectionIndex (Exactly np) (Index idx) =
97+
InjectionIndex
98+
$ Telescope.bihzipWith
99+
(\x (K ()) -> x)
100+
(\(K start) Refl -> State.Current { currentStart = start, currentState = Refl })
101+
np
102+
$ Telescope.fromTip idx
104103

105104
-- | NOT EXPORTED
106105
--

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Ledger.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ instance CanHardFork xs => IsLedger (LedgerState (HardForkBlock xs)) where
157157
extended :: HardForkState LedgerState xs
158158
extended = State.extendToSlot cfg slot st
159159

160-
tickOne :: SingleEraBlock blk
160+
tickOne :: (SListI xs, SingleEraBlock blk)
161161
=> EpochInfo (Except PastHorizonException)
162162
-> SlotNo
163163
-> ComputeLedgerEvents
@@ -207,7 +207,7 @@ instance CanHardFork xs
207207
error "reapplyBlockLedgerResult: can't be from other era"
208208
)
209209

210-
apply :: SingleEraBlock blk
210+
apply :: (SListI xs, SingleEraBlock blk)
211211
=> STS.ValidationPolicy
212212
-> ComputeLedgerEvents
213213
-> Index xs blk
@@ -728,14 +728,14 @@ ledgerViewInfo :: forall blk f. SingleEraBlock blk
728728
=> f blk -> LedgerEraInfo blk
729729
ledgerViewInfo _ = LedgerEraInfo $ singleEraInfo (Proxy @blk)
730730

731-
injectLedgerError :: Index xs blk -> LedgerError blk -> HardForkLedgerError xs
731+
injectLedgerError :: SListI xs => Index xs blk -> LedgerError blk -> HardForkLedgerError xs
732732
injectLedgerError index =
733733
HardForkLedgerErrorFromEra
734734
. OneEraLedgerError
735735
. injectNS index
736736
. WrapLedgerErr
737737

738-
injectLedgerEvent :: Index xs blk -> AuxLedgerEvent (LedgerState blk) -> OneEraLedgerEvent xs
738+
injectLedgerEvent :: SListI xs => Index xs blk -> AuxLedgerEvent (LedgerState blk) -> OneEraLedgerEvent xs
739739
injectLedgerEvent index =
740740
OneEraLedgerEvent
741741
. injectNS index

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Mempool.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,14 @@ ledgerInfo :: forall blk. SingleEraBlock blk
354354
=> State.Current (Ticked :.: LedgerState) blk -> LedgerEraInfo blk
355355
ledgerInfo _ = LedgerEraInfo $ singleEraInfo (Proxy @blk)
356356

357-
injectApplyTxErr :: Index xs blk -> ApplyTxErr blk -> HardForkApplyTxErr xs
357+
injectApplyTxErr :: SListI xs => Index xs blk -> ApplyTxErr blk -> HardForkApplyTxErr xs
358358
injectApplyTxErr index =
359359
HardForkApplyTxErrFromEra
360360
. OneEraApplyTxErr
361361
. injectNS index
362362
. WrapApplyTxErr
363363

364-
injectValidatedGenTx :: Index xs blk -> Validated (GenTx blk) -> Validated (GenTx (HardForkBlock xs))
364+
injectValidatedGenTx :: SListI xs => Index xs blk -> Validated (GenTx blk) -> Validated (GenTx (HardForkBlock xs))
365365
injectValidatedGenTx index =
366366
HardForkValidatedGenTx
367367
. OneEraValidatedGenTx

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Protocol.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Ouroboros.Consensus.HardFork.Combinator.Protocol (
3131
import Control.Monad.Except
3232
import Data.Functor.Product
3333
import Data.SOP.BasicFunctors
34+
import Data.SOP.Constraint
3435
import Data.SOP.Index
3536
import Data.SOP.InPairs (InPairs (..))
3637
import qualified Data.SOP.InPairs as InPairs
@@ -304,7 +305,7 @@ update HardForkConsensusConfig{..}
304305
where
305306
cfgs = getPerEraConsensusConfig hardForkConsensusConfigPerEra
306307

307-
updateEra :: forall xs blk. SingleEraBlock blk
308+
updateEra :: forall xs blk. (SListI xs, SingleEraBlock blk)
308309
=> EpochInfo (Except PastHorizonException)
309310
-> SlotNo
310311
-> Index xs blk
@@ -378,7 +379,8 @@ translateConsensus ei HardForkConsensusConfig{..} =
378379
pcfgs = getPerEraConsensusConfig hardForkConsensusConfigPerEra
379380
cfgs = hcmap proxySingle (completeConsensusConfig'' ei) pcfgs
380381

381-
injectValidationErr :: Index xs blk
382+
injectValidationErr :: SListI xs
383+
=> Index xs blk
382384
-> ValidationErr (BlockProtocol blk)
383385
-> HardForkValidationErr xs
384386
injectValidationErr index =

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Serialisation/Common.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ encodeNS es ns = mconcat [
446446
, hcollapse $ hzipWith apFn es ns
447447
]
448448

449-
decodeNS :: SListI xs => NP (Decoder s :.: f) xs -> Decoder s (NS f xs)
449+
decodeNS :: forall xs f s. SListI xs => NP (Decoder s :.: f) xs -> Decoder s (NS f xs)
450450
decodeNS ds = do
451451
enforceSize "decodeNS" 2
452452
i <- Dec.decodeWord8
@@ -460,7 +460,7 @@ decodeNS ds = do
460460
-> K (Decoder s (NS f xs)) blk
461461
aux index (Comp dec) (K ()) = K $ injectNS index <$> dec
462462

463-
decodeAnnNS :: SListI xs
463+
decodeAnnNS :: forall xs f. SListI xs
464464
=> NP (AnnDecoder f) xs
465465
-> forall s. Decoder s (Lazy.ByteString -> NS f xs)
466466
decodeAnnNS ds = do

0 commit comments

Comments
 (0)